undecorated-di
Version:
Type-safe, straightforward dependency injection for TypeScript without annotations or reflect-metadata.
2 lines • 5.52 kB
JavaScript
var k=Object.defineProperty,D=Object.defineProperties;var U=Object.getOwnPropertyDescriptors;var j=Object.getOwnPropertySymbols;var V=Object.prototype.hasOwnProperty,I=Object.prototype.propertyIsEnumerable;var S=(r,e,t)=>e in r?k(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t,m=(r,e)=>{for(var t in e||(e={}))V.call(e,t)&&S(r,t,e[t]);if(j)for(var t of j(e))I.call(e,t)&&S(r,t,e[t]);return r},f=(r,e)=>D(r,U(e));var g=class{constructor(e,t){this.key=e,this.bindable=t,this.dependencies=t.__dependencies__}resolve(e){return this.key.asType(this.bindable.bind(this.bindable,...e))}};var c=class{constructor(e){this.value=e}resolve(){return this.value}};var p=class{constructor(e,t,n){this.keyName=e,this.isSingleton=t,this.parent=n}};var i=class{constructor(e,t,n){this.key=e,this.injectable=t,this.dependencies=t.__dependencies__,this.isSingleton=n}resolve(e){return this.key.asType(new this.injectable(...e))}};var y=class extends Error{constructor(t){super(`A circular dependency was found when resolving "${t}." All members of a dependency cycle must be singletons.`);this.name="CircularDependencyError"}};var o=class extends Error{constructor(t){super(`"${t}" is already assigned.`);this.name="DuplicateKeyError"}};var d=class r extends Error{constructor(t){super(r.getMessage(t));this.name="MissingDependencyError"}static getMessage(t){return`Service with key ${t.keyName} not found. Dependency graph is ${this.buildDependencyGraph(t)}.`}static buildDependencyGraph(t){let n=[];for(n.push(t.keyName);t.parent;)t=t.parent,n.push(t.keyName);return n.reverse(),n.join("-->")}};var u=class extends Error{constructor(t){super(`Injectable with key name "${t}" not found. Was it registered?`);this.name="MissingInjectableError"}};var s=class extends Error{constructor(){super("A singleton that is part of a dependency cycle accessed a property of another member of that cycle in its constructor before the cycle could be resolved.");this.name="UninitializedPropertyAccessed"}};var h=class{constructor(){let e=this;this.proxy=new Proxy({},{get(t,n){if(!e.instance)throw new s;return Reflect.get(e.instance,n)},set(t,n,l){if(!e.instance)throw new s;return Reflect.set(e.instance,n,l)},getPrototypeOf(t){if(!e.instance)throw new s;return Reflect.getPrototypeOf(e.instance)},setPrototypeOf(t,n){if(!e.instance)throw new s;return Reflect.setPrototypeOf(e.instance,n)},defineProperty(t,n,l){if(!e.instance)throw new s;return Reflect.defineProperty(e.instance,n,l)},deleteProperty(t,n){if(!e.instance)throw new s;return Reflect.deleteProperty(e.instance,n)},getOwnPropertyDescriptor(t,n){if(!e.instance)throw new s;return Reflect.getOwnPropertyDescriptor(e.instance,n)},has(t,n){if(!e.instance)throw new s;return Reflect.has(e.instance,n)},ownKeys(t){if(!e.instance)throw new s;return Reflect.ownKeys(e.instance)},isExtensible(t){if(!e.instance)throw new s;return Reflect.isExtensible(e.instance)||Reflect.preventExtensions(t),Reflect.isExtensible(t)},preventExtensions(t){if(!e.instance)throw new s;return Reflect.preventExtensions(e.instance),Reflect.preventExtensions(t)}})}};var b=class{constructor(e){this.injectables=e;this.resolvedInjectables=new Set;this.singletons={};this.singletonPlaceholders={}}get(e){let t=new p(e.name,this.isSingleton(e.name));return this.resolveWithDependencyGraph(t)}resolveWithDependencyGraph(e){let t=this.singletons[e.keyName];if(t)return t;let n=this.injectables[e.keyName];if(!n)throw e.parent?new d(e):new u(e.keyName);if(n instanceof c)return n.resolve();if(!this.wasPreviouslyResolved(e.keyName)&&this.completesSingletonOnlyDependencyCycle(e)){e.keyName in this.singletonPlaceholders||(this.singletonPlaceholders[e.keyName]=[]);let a=new h;return this.singletonPlaceholders[e.keyName].push(a),a.proxy}let l=n.dependencies.map(a=>{let w=new p(a.name,this.isSingleton(a.name),e);return this.resolveWithDependencyGraph(w)}),v=this.injectables[e.keyName].resolve(l);return e.isSingleton&&(this.singletons[e.keyName]=v,e.keyName in this.singletonPlaceholders&&(this.singletonPlaceholders[e.keyName].forEach(w=>{w.instance=v}),delete this.singletonPlaceholders[e.keyName])),this.flagResolved(e.keyName),v}isSingleton(e){let t=this.injectables[e];return t instanceof i&&t.isSingleton}wasPreviouslyResolved(e){return this.resolvedInjectables.has(e)}completesSingletonOnlyDependencyCycle(e){let{keyName:t}=e,n=e.isSingleton;for(;e.parent;)if(e=e.parent,e.isSingleton||(n=!1),e.keyName===t){if(!n)throw new y(t);return!0}return!1}flagResolved(e){this.resolvedInjectables.add(e)}};var T=class r{constructor(e){this.injectables=e}static createBuilder(){return new r({})}registerClass(e,t){let n=new i(e,t,!1);return this.register(e.name,n)}registerSingleton(e,t){let n=new i(e,t,!0);return this.register(e.name,n)}registerFunction(e,t){let n=new g(e,t);return this.register(e.name,n)}registerConstant(e,t){let n=new c(t);return this.register(e.name,n)}build(){return new b(this.injectables)}register(e,t){if(e in this.injectables)throw new o(e);let n=f(m({},this.injectables),{[e]:t});return new r(n)}};var x=class{constructor(e){this.name=e}asType(e){return e}};var K=class r{constructor(e){this.keys=e}static createKeys(){return new r({})}addKey(e){if(e in this.keys)throw new o(e);return{forType:()=>{let t=f(m({},this.keys),{[e]:new x(e)});return new r(t)}}}};function P(r,e){let t=(...n)=>r(...n);return t.__dependencies__=e,t}function R(r,e){let t=r;return t.__dependencies__=e,t}export{T as ContainerBuilder,K as Keys,P as bind,R as inject};
//# sourceMappingURL=index.js.map