##// END OF EJS Templates
file rename
file rename

File last commit:

r94:131e369d1143 default
r98:12074cb11473 v1.3
Show More
Scope.ts
39 lines | 1.0 KiB | video/mp2t | TypeScriptLexer
import { IDestroyable, IRemovable } from "@implab/core-amd/interfaces";
import { isDestroyable, isRemovable } from "@implab/core-amd/safe";
export interface IScope {
own(target: (() => void) | IDestroyable | IRemovable): void;
}
export class Scope implements IDestroyable, IScope {
private readonly _cleanup: (() => void)[] = [];
static readonly dummy: IScope = { own() { } };
own(target: (() => void) | IDestroyable | IRemovable) {
if (target instanceof Function) {
this._cleanup.push(target);
} else if (isDestroyable(target)) {
this._cleanup.push(() => target.destroy());
} else if (isRemovable(target)) {
this._cleanup.push(() => target.remove());
}
}
clean() {
const guard = (cb: () => void) => {
try {
cb();
} catch {
// guard
}
}
this._cleanup.forEach(guard);
this._cleanup.length = 0;
}
destroy() {
this.clean();
}
}