##// END OF EJS Templates
sync
sync

File last commit:

r15:3985e8405319 tip default
r15:3985e8405319 tip default
Show More
LifetimeManager.ts
97 lines | 2.9 KiB | video/mp2t | TypeScriptLexer
/ src / main / ts / LifetimeManager.ts
cin
sync
r15 import { ILifetime, ILifetimeContext, ILifetimeManager, ILifetimeSlot } from "../typings/interfaces";
import { LifetimeSlot } from "./LifetimeSlot";
import { argumentNotNull } from "./traits";
cin
initial commit
r0
cin
WIP lifetime services
r12 const noop = () => { };
cin
WIP lifetime services, change target to ES2018
r11
cin
WIP lifetime services
r12 const fail = (message: string) => (): never => {
cin
WIP lifetime services, change target to ES2018
r11 throw new Error(message);
};
cin
intoruced initial work on ILifetimeSlot
r10 const _emptySlot = Object.freeze({
has: () => false,
cin
initial commit
r0
cin
sync
r15 initialize: () => false,
cin
WIP lifetime services, change target to ES2018
r11
get: fail("The specified item isn't registered with a lifetime manager"),
cin
initial commit
r0
cin
WIP lifetime services, change target to ES2018
r11 store: noop,
cin
initial commit
r0
cin
WIP lifetime services
r12 remove: noop,
cin
intoruced initial work on ILifetimeSlot
r10
cin
WIP lifetime services
r12 cleanup: noop,
cin
initial commit
r0 });
cin
almost woking typings
r9 export class LifetimeManager implements ILifetimeManager {
cin
initial commit
r0 private _destroyed = false;
cin
intoruced initial work on ILifetimeSlot
r10 private readonly _slots: Record<string, ILifetimeSlot<unknown>> = {};
cin
working on fluent configuration
r1
cin
sync
r15 slot<T>(slotId: string | number): ILifetimeSlot<T> {
cin
intoruced initial work on ILifetimeSlot
r10 if (this._destroyed)
throw new Error("The lifetime manager is destroyed");
cin
sync
r15 if (slotId in this._slots)
return this._slots[slotId] as ILifetimeSlot<T>;
return this._slots[slotId] = new LifetimeSlot<T>(() => delete this._slots[slotId]);
cin
intoruced initial work on ILifetimeSlot
r10 }
cin
sync
r15
cin
initial commit
r0 destroy() {
if (!this._destroyed) {
this._destroyed = true;
cin
sync
r15 Object.values(this._slots).forEach(slot => {
try {
slot.cleanup();
} catch {
// ignore
}
});
cin
initial commit
r0 }
}
cin
almost woking typings
r9 }
cin
sync
r15 export const emptySlot = <T>() => _emptySlot as ILifetimeSlot<T>;
cin
initial commit
r0
cin
sync
r15 export const emptyLifetime = <T>() => emptySlot as ILifetime<T>;
cin
initial commit
r0
cin
sync
r15 export const scopeLifetime = <T>(level: number, slotId: string | number) =>
(context: ILifetimeContext) =>
context.scopeSlot<T>(level, slotId);
cin
WIP service descriptors
r13
cin
sync
r15 export const hierarchyLifetime = <T>(slotId: string | number) =>
(context: ILifetimeContext) =>
context.hierarchySlot<T>(slotId);
cin
initial commit
r0
cin
intoruced initial work on ILifetimeSlot
r10 /**
* Creates a lifetime instance bound to the current activation context. This
* lifetime will store the service instance per activation context. Every
* top level service resolution will create a new activation context. This
* context is propagated to subsequent service resolution thus all services
* with context lifetime will be shared among their consumers.
*
* @returns The instance of the lifetime.
*/
cin
sync
r15 export const contextLifetime = <T>(slotId: string | number) =>
(context: ILifetimeContext) =>
cin
WIP lifetime services
r12 context.contextSlot<T>(slotId);
cin
initial commit
r0
cin
WIP lifetime services
r12 const singletons = new LifetimeManager();
cin
intoruced initial work on ILifetimeSlot
r10 /**
* Creates the lifetime for the service which will allow existence only one
* instance with the specified {@linkcode typeId}. If there will be created
* several lifetime instances with same `typeId` in the runtime, they will
* share the same service instance.
*
* @param typeId The identified for the global instance, usually this is a
* fully qualified class name
* @returns The lifetime instance
*/
cin
WIP lifetime services
r12 export const singletonLifetime = <T>(typeId: string) => {
cin
almost woking typings
r9 argumentNotNull(typeId, "typeId");
cin
WIP lifetime services
r12
return () => singletons.slot<T>(typeId);
cin
almost woking typings
r9 };