##// END OF EJS Templates
working on fluent configuration
working on fluent configuration

File last commit:

r133:09ea4b9e3735 ioc ts support
r133:09ea4b9e3735 ioc ts support
Show More
LifetimeManager.ts
132 lines | 3.5 KiB | video/mp2t | TypeScriptLexer
/ src / main / ts / di / LifetimeManager.ts
cin
working on lifetime management
r129 import { IDestroyable, MapOf } from "../interfaces";
import { argumentNotNull, isDestroyable } from "../safe";
cin
working on services lifetime
r130 import { ILifetimeManager, ILifetime } from "./interfaces";
import { ActivationContext } from "./ActivationContext";
cin
working on lifetime management
r129
function safeCall(item: () => void) {
try {
item();
} catch {
cin
Refactoring, working on services lifetime
r131 // silence!
cin
working on lifetime management
r129 }
}
cin
Refactoring, working on services lifetime
r131 const emptyLifetime: ILifetime = {
has() {
return false;
},
enter() {
},
get() {
throw new Error("The specified item isn't registered with this lifetime manager");
},
store() {
// does nothing
}
};
cin
working on fluent configuration
r133 let nextId = 0;
cin
working on lifetime management
r129 export class LifetimeManager implements IDestroyable, ILifetimeManager {
private _cleanup: (() => void)[] = [];
private _cache: MapOf<any> = {};
private _destroyed = false;
cin
working on fluent configuration
r132 private _pending: MapOf<boolean> = {};
cin
working on fluent configuration
r133 initialize(): ILifetime {
cin
working on services lifetime
r130 const self = this;
cin
working on fluent configuration
r133 const id = ++nextId;
cin
working on services lifetime
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;
},
cin
working on lifetime management
r129
cin
working on services lifetime
r130 enter() {
cin
working on fluent configuration
r132 if (self._pending[id])
cin
working on services lifetime
r130 throw Error(`Cyclic reference detected: the item with the key ${id} is already activating.`);
cin
working on fluent configuration
r132 self._pending[id] = true;
cin
working on services lifetime
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`);
cin
working on fluent configuration
r132 delete self._pending[id];
cin
working on services lifetime
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());
}
}
};
cin
working on lifetime management
r129 }
destroy() {
if (!this._destroyed) {
this._destroyed = true;
this._cleanup.forEach(safeCall);
this._cleanup.length = 0;
}
}
static readonly empty: ILifetimeManager = {
cin
Refactoring, working on services lifetime
r131 initialize(): ILifetime {
return emptyLifetime;
}
};
static readonly hierarchyLifetime: ILifetimeManager = {
cin
working on fluent configuration
r133 initialize(context: ActivationContext<any>): ILifetime {
return context.getContainer().getLifetimeManager().initialize(context);
cin
Refactoring, working on services lifetime
r131 }
};
cin
working on lifetime management
r129
cin
Refactoring, working on services lifetime
r131 static readonly contextLifetime: ILifetimeManager = {
cin
working on fluent configuration
r133 initialize(context: ActivationContext<any>): ILifetime {
const id = String(++nextId);
cin
Refactoring, working on services lifetime
r131 return {
enter() {
if (context.visit(id))
throw new Error("Cyclic reference detected");
},
get() {
return context.get(id);
},
has() {
return context.has(id);
},
store(item: any) {
context.store(id, item);
}
};
cin
working on lifetime management
r129 }
};
cin
working on fluent configuration
r133
static singletonLifetime(typeId: string): ILifetimeManager {
return {
initialize() {
return emptyLifetime;
}
};
}
cin
working on lifetime management
r129 }