Skip to content

Instantly share code, notes, and snippets.

@mmick66
Created June 1, 2021 10:56
Show Gist options
  • Save mmick66/5ea937af06ccd751fbbe29aabecd898a to your computer and use it in GitHub Desktop.
Save mmick66/5ea937af06ccd751fbbe29aabecd898a to your computer and use it in GitHub Desktop.
import 'reflect-metadata';
const RUNNING = Symbol("RUNNING");
export function NoOverlap(): MethodDecorator {
return function (target, key?, descriptor?:TypedPropertyDescriptor<any>) {
const origFn = descriptor.value;
if (!Reflect.hasMetadata(RUNNING, target, key)) {
Reflect.defineMetadata(RUNNING, false, target, key);
}
descriptor.value = async function(...args) {
const isRunning = Reflect.getMetadata(RUNNING, target, key);
if (isRunning) {
return;
}
Reflect.defineMetadata(RUNNING, true, target, key);
let err;
let result;
try {
result = await origFn.bind(this)(...args);
} catch (e) {
err = e;
}
Reflect.defineMetadata(RUNNING, false, target, key);
if (err) {
throw err;
}
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment