LifetimeManager.ts
218 lines
| 6.1 KiB
| video/mp2t
|
TypeScriptLexer
|
|
r129 | import { IDestroyable, MapOf } from "../interfaces"; | ||
|
|
r154 | import { argumentNotNull, isDestroyable, argumentNotEmptyString, isRemovable } from "../safe"; | ||
|
|
r144 | import { ILifetime, ServiceContainer } from "./interfaces"; | ||
|
|
r130 | import { ActivationContext } from "./ActivationContext"; | ||
|
|
r129 | |||
| function safeCall(item: () => void) { | ||||
| try { | ||||
| item(); | ||||
| } catch { | ||||
|
|
r131 | // silence! | ||
|
|
r129 | } | ||
| } | ||||
|
|
r135 | const emptyLifetime: ILifetime = Object.freeze({ | ||
|
|
r131 | has() { | ||
| return false; | ||||
| }, | ||||
|
|
r134 | initialize() { | ||
|
|
r131 | |||
| }, | ||||
| get() { | ||||
| throw new Error("The specified item isn't registered with this lifetime manager"); | ||||
| }, | ||||
| store() { | ||||
| // does nothing | ||||
|
|
r158 | }, | ||
| toString() { | ||||
| return `[object EmptyLifetime]`; | ||||
|
|
r131 | } | ||
|
|
r135 | }); | ||
|
|
r131 | |||
|
|
r135 | const unknownLifetime: ILifetime = Object.freeze({ | ||
|
|
r134 | has() { | ||
|
|
r135 | return false; | ||
|
|
r134 | }, | ||
| initialize() { | ||||
| throw new Error("Can't call initialize on the unknown lifetime object"); | ||||
| }, | ||||
| get() { | ||||
| throw new Error("The lifetime object isn't initialized"); | ||||
| }, | ||||
| store() { | ||||
| throw new Error("Can't store a value in the unknown lifetime object"); | ||||
|
|
r158 | }, | ||
| toString() { | ||||
| return `[object UnknownLifetime]`; | ||||
|
|
r134 | } | ||
|
|
r135 | }); | ||
|
|
r134 | |||
|
|
r133 | let nextId = 0; | ||
|
|
r144 | const singletons: any = {}; | ||
|
|
r135 | |||
| export class LifetimeManager implements IDestroyable { | ||||
|
|
r129 | private _cleanup: (() => void)[] = []; | ||
| private _cache: MapOf<any> = {}; | ||||
| private _destroyed = false; | ||||
|
|
r132 | private _pending: MapOf<boolean> = {}; | ||
|
|
r134 | create(): ILifetime { | ||
|
|
r130 | const self = this; | ||
|
|
r133 | const id = ++nextId; | ||
|
|
r130 | return { | ||
| has() { | ||||
| return (id in self._cache); | ||||
| }, | ||||
| get() { | ||||
| const t = self._cache[id]; | ||||
| if (t === undefined) | ||||
| throw new Error(`The item with with the key ${id} isn't found`); | ||||
| return t; | ||||
| }, | ||||
|
|
r129 | |||
|
|
r134 | initialize() { | ||
|
|
r132 | if (self._pending[id]) | ||
|
|
r130 | throw Error(`Cyclic reference detected: the item with the key ${id} is already activating.`); | ||
|
|
r132 | self._pending[id] = true; | ||
|
|
r130 | }, | ||
| store(item: any, cleanup?: (item: any) => void) { | ||||
| argumentNotNull(id, "id"); | ||||
| argumentNotNull(item, "item"); | ||||
| if (this.has()) | ||||
| throw new Error(`The item with with the key ${id} already registered with this lifetime manager`); | ||||
|
|
r132 | delete self._pending[id]; | ||
|
|
r130 | |||
| self._cache[id] = item; | ||||
| if (self._destroyed) | ||||
| throw new Error("Lifetime manager is destroyed"); | ||||
| if (cleanup) { | ||||
| self._cleanup.push(() => cleanup(item)); | ||||
| } else if (isDestroyable(item)) { | ||||
| self._cleanup.push(() => item.destroy()); | ||||
|
|
r154 | } else if (isRemovable(item)) { | ||
| self._cleanup.push(() => item.remove()); | ||||
|
|
r130 | } | ||
| } | ||||
| }; | ||||
|
|
r129 | } | ||
| destroy() { | ||||
| if (!this._destroyed) { | ||||
| this._destroyed = true; | ||||
| this._cleanup.forEach(safeCall); | ||||
| this._cleanup.length = 0; | ||||
| } | ||||
| } | ||||
|
|
r134 | static empty(): ILifetime { | ||
| return emptyLifetime; | ||||
| } | ||||
|
|
r158 | static hierarchyLifetime() { | ||
|
|
r134 | let _lifetime = unknownLifetime; | ||
| return { | ||||
| initialize(context: ActivationContext<any>) { | ||||
| if (_lifetime !== unknownLifetime) | ||||
| throw new Error("Cyclic reference activation detected"); | ||||
|
|
r131 | |||
|
|
r135 | _lifetime = context.getContainer().getLifetimeManager().create(); | ||
|
|
r134 | }, | ||
| get() { | ||||
| return _lifetime.get(); | ||||
| }, | ||||
| has() { | ||||
| return _lifetime.has(); | ||||
| }, | ||||
| store(item: any, cleanup?: (item: any) => void) { | ||||
| return _lifetime.store(item, cleanup); | ||||
|
|
r158 | }, | ||
| toString() { | ||||
| return `[object HierarchyLifetime, has=${this.has()}]`; | ||||
|
|
r134 | } | ||
| }; | ||||
| } | ||||
|
|
r129 | |||
|
|
r158 | static contextLifetime() { | ||
|
|
r134 | let _lifetime = unknownLifetime; | ||
| return { | ||||
| initialize(context: ActivationContext<any>) { | ||||
| if (_lifetime !== unknownLifetime) | ||||
| throw new Error("Cyclic reference detected"); | ||||
| _lifetime = context.createLifetime(); | ||||
| }, | ||||
| get() { | ||||
| return _lifetime.get(); | ||||
| }, | ||||
| has() { | ||||
| return _lifetime.has(); | ||||
| }, | ||||
| store(item: any) { | ||||
| _lifetime.store(item); | ||||
|
|
r158 | }, | ||
| toString() { | ||||
| return `[object ContextLifetime, has=${this.has()}]`; | ||||
|
|
r134 | } | ||
| }; | ||||
| } | ||||
|
|
r133 | |||
|
|
r158 | static singletonLifetime(typeId: string) { | ||
|
|
r135 | argumentNotEmptyString(typeId, "typeId"); | ||
| let pending = false; | ||||
| return { | ||||
| has() { | ||||
| return typeId in singletons; | ||||
| }, | ||||
| get() { | ||||
| if (!this.has()) | ||||
| throw new Error(`The instance ${typeId} doesn't exists`); | ||||
| return singletons[typeId]; | ||||
| }, | ||||
| initialize() { | ||||
| if (pending) | ||||
| throw new Error("Cyclic reference detected"); | ||||
| pending = true; | ||||
| }, | ||||
| store(item: any) { | ||||
| singletons[typeId] = item; | ||||
| pending = false; | ||||
|
|
r158 | }, | ||
| toString() { | ||||
| return `[object SingletonLifetime, has=${this.has()}, typeId=${typeId}]`; | ||||
|
|
r135 | } | ||
| }; | ||||
|
|
r134 | } | ||
|
|
r144 | static containerLifetime(container: ServiceContainer<any>) { | ||
|
|
r134 | let _lifetime = unknownLifetime; | ||
|
|
r133 | return { | ||
|
|
r134 | initialize(context: ActivationContext<any>) { | ||
| if (_lifetime !== unknownLifetime) | ||||
| throw new Error("Cyclic reference detected"); | ||||
|
|
r135 | _lifetime = container.getLifetimeManager().create(); | ||
|
|
r134 | }, | ||
| get() { | ||||
| return _lifetime.get(); | ||||
| }, | ||||
| has() { | ||||
| return _lifetime.has(); | ||||
| }, | ||||
| store(item: any) { | ||||
| _lifetime.store(item); | ||||
|
|
r158 | }, | ||
| toString() { | ||||
| return `[object ContainerLifetime, has=${_lifetime.has()}]` | ||||
|
|
r133 | } | ||
| }; | ||||
| } | ||||
|
|
r129 | } | ||
