##// END OF EJS Templates
almost woking typings
almost woking typings

File last commit:

r9:988f0f6aab67 default
r9:988f0f6aab67 default
Show More
LifetimeManager.ts
217 lines | 6.0 KiB | video/mp2t | TypeScriptLexer
/ src / main / ts / LifetimeManager.ts
cin
almost woking typings
r9 import { IActivationContext, ILifetime, ILifetimeManager } from "./interfaces";
cin
initial commit
r0 import { ActivationContext } from "./ActivationContext";
cin
working on fluent configuration
r1 import { argumentNotNull, isDestroyable } from "./traits";
cin
initial commit
r0
cin
working on fluent configuration
r1 const safeCall = (item: () => void) => {
cin
initial commit
r0 try {
item();
} catch {
// silence!
}
cin
working on fluent configuration
r1 };
cin
initial commit
r0
cin
almost woking typings
r9 const _emptyLifetime = Object.freeze({
cin
initial commit
r0 has() {
return false;
},
initialize() {
},
get() {
throw new Error("The specified item isn't registered with this lifetime manager");
},
store() {
// does nothing
},
toString() {
return `[object EmptyLifetime]`;
}
});
cin
almost woking typings
r9 const _unknownLifetime = Object.freeze({
cin
initial commit
r0 has() {
return false;
},
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");
},
cin
almost woking typings
r9
cin
initial commit
r0 toString() {
return `[object UnknownLifetime]`;
}
});
let nextId = 0;
cin
almost woking typings
r9 const singletons: { [K: string]: unknown } = {};
cin
initial commit
r0
cin
almost woking typings
r9 export class LifetimeManager implements ILifetimeManager {
cin
initial commit
r0 private _cleanup: (() => void)[] = [];
cin
almost woking typings
r9 private readonly _cache: { [K: string]: unknown } = {};
cin
initial commit
r0 private _destroyed = false;
cin
almost woking typings
r9 private readonly _pending: { [K: string]: unknown } = {};
cin
initial commit
r0
cin
almost woking typings
r9 create<T>(): ILifetime<T> & { remove(): void; } {
cin
initial commit
r0 const id = ++nextId;
return {
cin
working on fluent configuration
r1 has: () => id in this._cache,
get: () => {
cin
almost woking typings
r9 const t = this._cache[id] as T;
if (t === undefined || t === null)
cin
working on fluent configuration
r1 throw new Error(`The item with with the key ${id} isn't found`);
cin
almost woking typings
r9 return t;
cin
initial commit
r0 },
cin
working on fluent configuration
r1 initialize: () => {
if (this._pending[id])
throw Error(`Cyclic reference detected: the item with the key ${id} is already activating.`);
this._pending[id] = true;
cin
initial commit
r0 },
cin
almost woking typings
r9 store: (item: NonNullable<T>, cleanup?: (item: NonNullable<T>) => void) => {
cin
working on fluent configuration
r1 if (id in this._cache)
cin
initial commit
r0 throw new Error(`The item with with the key ${id} already registered with this lifetime manager`);
cin
almost woking typings
r9 if (this._destroyed)
throw new Error("Lifetime manager is destroyed");
cin
working on fluent configuration
r1 delete this._pending[id];
cin
initial commit
r0
cin
working on fluent configuration
r1 this._cache[id] = item;
cin
initial commit
r0
if (cleanup) {
cin
working on fluent configuration
r1 this._cleanup.push(() => cleanup(item));
cin
initial commit
r0 } else if (isDestroyable(item)) {
cin
working on fluent configuration
r1 this._cleanup.push(() => item.destroy());
cin
initial commit
r0 }
cin
almost woking typings
r9 },
remove: () => {
if (this._pending[id])
throw new Error(`The item '${id}' can't be removed before it has been stored`);
delete this._cache[id];
cin
initial commit
r0 }
};
}
destroy() {
if (!this._destroyed) {
this._destroyed = true;
this._cleanup.forEach(safeCall);
this._cleanup.length = 0;
}
}
cin
almost woking typings
r9 }
export const emptyLifetime = <T>(): ILifetime<T> => {
return _emptyLifetime;
};
cin
initial commit
r0
cin
almost woking typings
r9 export const hierarchyLifetime = <T>(): ILifetime<T> => {
let _lifetime: ILifetime<T> = _unknownLifetime;
return {
initialize(context: IActivationContext<object>) {
if (_lifetime !== _unknownLifetime)
throw new Error("Cyclic reference activation detected");
cin
initial commit
r0
cin
almost woking typings
r9 _lifetime = context.createContainerLifetime<T>();
},
get() {
return _lifetime.get();
},
has() {
return _lifetime.has();
},
store(item: NonNullable<T>, cleanup?: (item: NonNullable<T>) => void) {
return _lifetime.store(item, cleanup);
},
toString() {
return `[object HierarchyLifetime, has=${String(this.has())}]`;
}
};
};
cin
initial commit
r0
cin
almost woking typings
r9 export const contextLifetime = <T>(): ILifetime<T> => {
let _lifetime: ILifetime<T> = _unknownLifetime;
return {
initialize(context: ActivationContext<object>) {
if (_lifetime !== _unknownLifetime)
throw new Error("Cyclic reference detected");
_lifetime = context.createLifetime();
},
get() {
return _lifetime.get();
},
has() {
return _lifetime.has();
},
store(item: NonNullable<T>) {
_lifetime.store(item);
},
toString() {
return `[object ContextLifetime, has=${String(this.has())}]`;
}
};
};
cin
initial commit
r0
cin
almost woking typings
r9 export const singletonLifetime = <T>(typeId: string): ILifetime<T> => {
argumentNotNull(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] as NonNullable<T>;
},
initialize() {
if (pending)
throw new Error("Cyclic reference detected");
pending = true;
},
store(item: NonNullable<T>) {
singletons[typeId] = item;
pending = false;
},
toString() {
return `[object SingletonLifetime, has=${String(this.has())}, typeId=${typeId}]`;
}
};
};
export const containerLifetime = <T>(container: { createLifetime<X>(): ILifetime<X> }) => {
let _lifetime: ILifetime<T> = _unknownLifetime;
return {
initialize() {
if (_lifetime !== _unknownLifetime)
throw new Error("Cyclic reference detected");
_lifetime = container.createLifetime();
},
get() {
return _lifetime.get();
},
has() {
return _lifetime.has();
},
store(item: NonNullable<T>) {
_lifetime.store(item);
},
toString() {
return `[object ContainerLifetime, has=${String(_lifetime.has())}]`;
}
};
};