##// END OF EJS Templates
Added tag v1.9.0-rc5 for changeset 894b8239b953
Added tag v1.9.0-rc5 for changeset 894b8239b953

File last commit:

r131:c7d9ad82b374 v1.8.1 default
r143:62b21ca84dce default
Show More
Scope.ts
54 lines | 1.4 KiB | video/mp2t | TypeScriptLexer
import { IDestroyable, IRemovable } from "@implab/core-amd/interfaces";
import { isDestroyable, isRemovable } from "@implab/core-amd/safe";
import { isUnsubscribable, Unsubscribable } from "../observable";
export interface IScope {
own(target: (() => void) | IDestroyable | IRemovable | Unsubscribable): void;
}
const guard = (cb: () => void) => {
try {
cb();
} catch {
// guard
}
};
export class Scope implements IDestroyable, IScope {
private readonly _pending: (() => void)[] = [];
private _disposed = false;
static readonly dummy: IScope = { own() { } };
own(target: (() => void) | IDestroyable | IRemovable | Unsubscribable) {
if (target instanceof Function) {
this._own(target);
} else if (isDestroyable(target)) {
this._own(() => target.destroy());
} else if (isRemovable(target)) {
this._own(() => target.remove());
} else if (isUnsubscribable(target)) {
this._own(() => target.unsubscribe());
}
}
private _own(target: () => void) {
if (this._disposed)
guard(target);
else
this._pending.push(target);
}
clean() {
this._pending.forEach(guard);
this._pending.length = 0;
}
destroy() {
if (!this._disposed) {
this._disposed = true;
this.clean();
}
}
}