assemblerjs 0.5.5 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  A general purpose and zero-dependency [Dependency Injection](https://en.wikipedia.org/wiki/Dependency_injection) library for node and browser.
4
4
 
5
- ![Statements](https://img.shields.io/badge/statements-90.39%25-brightgreen.svg?style=flat) ![Branches](https://img.shields.io/badge/branches-81.53%25-yellow.svg?style=flat) ![Functions](https://img.shields.io/badge/functions-86.09%25-yellow.svg?style=flat) ![Lines](https://img.shields.io/badge/lines-90%25-brightgreen.svg?style=flat)
5
+ ![Statements](https://img.shields.io/badge/statements-91.28%25-brightgreen.svg?style=flat) ![Branches](https://img.shields.io/badge/branches-80.57%25-yellow.svg?style=flat) ![Functions](https://img.shields.io/badge/functions-87.62%25-yellow.svg?style=flat) ![Lines](https://img.shields.io/badge/lines-90.89%25-brightgreen.svg?style=flat)
6
6
 
7
7
  ---
8
8
 
package/dist/index.d.ts CHANGED
@@ -14,14 +14,16 @@ export declare abstract class AbstractAssemblage {
14
14
  * Called on concrete class registration by assembler.
15
15
  *
16
16
  * @param { AssemblerContext } context The assembler's context.
17
+ * @param { Reord<string, any> } configuration The configuration object.
17
18
  */
18
- static onRegister(context: AssemblerContext): void;
19
+ static onRegister(context: AssemblerContext, configuration?: Record<string, any>): void;
19
20
  /**
20
21
  * Called on instantiated class after the dependency tree is fully resolved.
21
22
  *
22
23
  * @param { AssemblerContext } context The assembler's context.
24
+ * @param { Reord<string, any> } configuration The configuration object.
23
25
  */
24
- abstract onInit?(context: AssemblerContext): void | Promise<void>;
26
+ abstract onInit?(context: AssemblerContext, configuration?: Record<string, any>): void | Promise<void>;
25
27
  /**
26
28
  * Called when instance of class is disposed.
27
29
  *
@@ -47,7 +49,7 @@ export declare abstract class AbstractAssembler extends AbstractEventManager {
47
49
  abstract size: number;
48
50
  abstract register<T>(injection: Injection<T>, instance?: boolean): Injectable<T>;
49
51
  abstract use<T>(identifier: string | Symbol, object: T): T;
50
- abstract prepareInitHook<T = AbstractAssemblage>(instance: T): unknown[];
52
+ abstract prepareInitHook<T = AbstractAssemblage>(instance: T, configuration?: Record<string, any>): unknown[];
51
53
  abstract has<T>(identifier: Identifier<T>): boolean;
52
54
  abstract require<T>(identifier: Identifier<T> | string | Symbol): T;
53
55
  abstract concrete<T>(identifier: Identifier<T>): Concrete<T> | undefined;
@@ -101,7 +103,7 @@ declare type ArrayLengthMutationKeys = 'splice' | 'push' | 'pop' | 'shift' | 'un
101
103
  * @param { AssemblageDefinition } definition Definition of the assemblage that provides injections, etc.
102
104
  * @returns { ClassDecorator } The decorated class.
103
105
  */
104
- export declare const Assemblage: (definition?: AssemblageDefinition) => ClassDecorator;
106
+ export declare const Assemblage: <T>(definition?: AssemblageDefinition) => (target: Concrete<T>) => Concrete<T>;
105
107
 
106
108
  declare interface AssemblageDefinition {
107
109
  singleton?: false;
@@ -112,6 +114,8 @@ declare interface AssemblageDefinition {
112
114
  metadata?: Record<string, any>;
113
115
  }
114
116
 
117
+ export declare const AssemblageWrappedDecorator: (fn?: () => void) => <T extends new (...args: any[]) => {}>(Base: T) => Concrete< {}>;
118
+
115
119
  /**
116
120
  * `assembler.js` dependency injection container and handler.
117
121
  */
@@ -181,9 +185,10 @@ export declare class Assembler extends EventManager implements AbstractAssembler
181
185
  * when the dependency tree will be fully resolved.
182
186
  *
183
187
  * @param { T = AbstractAssemblage } instance The built instance.
188
+ * @param { Record<string, any> | undefined } configuration The configuration object.
184
189
  * @returns { unknown[] } The instances to be inited as this point.
185
190
  */
186
- prepareInitHook<T = AbstractAssemblage>(instance: T): unknown[];
191
+ prepareInitHook<T = AbstractAssemblage>(instance: T, configuration?: Record<string, any>): unknown[];
187
192
  /**
188
193
  * Check if `Assembler` has given identifier registered.
189
194
  *
@@ -320,8 +325,23 @@ export declare class Assembler extends EventManager implements AbstractAssembler
320
325
  */
321
326
  export declare const Context: () => ParameterDecorator;
322
327
 
328
+ /**
329
+ * Create a custom decorator to wrap `Assemblage`.
330
+ * Note that it must be placed before the `Assemnblage` decorator.
331
+ *
332
+ * @param { function(): void | undefined } fn A function to execute after `super`.
333
+ * Do not use arrow function here if access to `this` is required.
334
+ * @returns A new decorator.
335
+ */
336
+ export declare const createWrappedDecorator: (fn?: () => void) => () => <T extends new (...args: any[]) => {}>(Base: T) => Concrete< {}>;
337
+
323
338
  export declare const decorateAssemblage: <T>(target: Concrete<T>, definition?: AssemblageDefinition) => Concrete<T>;
324
339
 
340
+ /**
341
+ * Decorator as a wrapper function.
342
+ */
343
+ export declare const decorateUse: (identifier: string | Symbol, target: any, index: number) => void;
344
+
325
345
  /**
326
346
  * Injects the assemblage's definition object.
327
347
  */
@@ -348,13 +368,7 @@ export declare class Assembler extends EventManager implements AbstractAssembler
348
368
  private cleanChannel;
349
369
  }
350
370
 
351
- export declare const getDecoratedParametersIndexes: <T>(target: Concrete<T>) => {
352
- context: number[];
353
- definition: number[];
354
- configuration: number[];
355
- dispose: number[];
356
- use: number[];
357
- };
371
+ export declare const getDecoratedParametersIndexes: <T>(target: Concrete<T>) => ParametersDecoratorsIndexes;
358
372
 
359
373
  /**
360
374
  * An identifier can be an abstract or a concrete class.
@@ -439,6 +453,14 @@ export declare class Assembler extends EventManager implements AbstractAssembler
439
453
  */
440
454
  declare type Listener = (...args: any[]) => void | Promise<void>;
441
455
 
456
+ declare interface ParametersDecoratorsIndexes {
457
+ Context: number[];
458
+ Definition: number[];
459
+ Configuration: number[];
460
+ Dispose: number[];
461
+ Use: number[];
462
+ }
463
+
442
464
  /**
443
465
  * Internal `Reflect` parameters decorators' indexes in constructor.
444
466
  */
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var e=function(e){return e.IsAssemblage="is_assemblage",e}({}),t=function(e){return e.AssemblageDefinition="assemblage:definition.value",e}({});const n=(...e)=>{},s=(...e)=>t=>{if(e.includes(typeof t))return typeof t},i=e=>!(e=>void 0===e)(e)&&!(e=>null===e)(e),r=e=>e&&"function"==typeof e&&void 0!==e.constructor,o=e=>"function"==typeof e&&"AsyncFunction"===e.constructor.name,c=(e,...t)=>{t.map(((e,n)=>{const s=/\S/.test(e);return s&&(t[n]=t[n].trim()),s?-1:n})).filter((e=>e>=0)).every((e=>{t[e]=" ",((e,t,n)=>{const s=e[t];e.splice(t,1),e.splice(n,0,s)})(t,e,t.length-1)})),t=(e=>Array.from(new Set(e)))(t);const n=new RegExp(`[^A-Za-zÀ-ÖØ-öø-ÿ0-9${t.join("")}]`,"gi");return e.replace(n,"")},a=(...e)=>t=>e.reduce(((e,t)=>t(e)),t),h=e=>t=>e.if(t)?e.then(t):e.else?e.else(t):void 0,l=e=>t=>{const s=Array.isArray(e)?e=>parseInt(e):n;for(const[n,i]of Object.entries(e))t(i,s(n))},u=(e,t)=>{const n=new Proxy(e,{get:function(n,s){return s===Symbol.iterator?n[Symbol.iterator].bind(n):((e,t)=>[...Object.getOwnPropertyNames(t.prototype),...Object.getOwnPropertyNames(Object.getPrototypeOf(e)),...Object.getOwnPropertyNames(e)])(e,t).includes(s)?n[s]:n.collection[s]},set:function(e,t,n){return Reflect.set(e,t,n)}});return n},f=(e,t)=>{const n=e;for(const s of((e,t)=>[...Object.getOwnPropertyNames(t.prototype),...Object.getOwnPropertyNames(Object.getPrototypeOf(e)),...Object.getOwnPropertyNames(e)])(e,t))delete n[s]},d=(e,t,n)=>{Reflect.defineMetadata(`__${e}__`,t,n)},p=(e,t)=>Reflect.getOwnMetadata(`__${e}__`,t),g=e=>Reflect.getMetadata("design:paramtypes",e)||[],b=t=>p(e.IsAssemblage,t)||!1,m={singleton:{test:e=>"boolean"==typeof e||void 0===e,throw:()=>{throw new Error("'singleton' property must be of type 'boolean' or 'undefined'.")},transform:e=>void 0===e||!!e},events:{test:e=>void 0===e||Array.isArray(e)&&e.every((e=>"string"==typeof e)),throw:()=>{throw new Error("'events' property must be an array of strings or 'undefined'.")},transform:e=>e},inject:{test:e=>void 0===e||Array.isArray(e)&&e.every((e=>Array.isArray(e)&&e.length>=1&&e.length<=3)),throw:()=>{throw new Error("'inject' property must be an array of tuples of length 1, 2 or 3.")},transform:e=>e},use:{test:e=>void 0===e||Array.isArray(e)&&e.every((e=>Array.isArray(e)&&2==e.length)),throw:()=>{throw new Error("'use' property must be an array of tuples of length 2.")},transform:e=>e},tags:{test:e=>void 0===e||"string"==typeof e||Array.isArray(e)&&e.every((e=>"string"==typeof e)),throw:()=>{throw new Error("'tags' property must be a string or an array of strings.")},transform:e=>"string"==typeof e?[e]:e},metadata:{test:e=>("object"==typeof e||void 0===e)&&!Array.isArray(e),throw:()=>{throw new Error("'metadata' property must be of type 'object' or 'undefined'.")},transform:e=>e}},y=e=>{const t={...e};for(const n in t)if(!Object.keys(m).includes(n))throw new Error(`Property '${n}' is not a valid assemblage definition property.`);for(const n in m){const e=m[n].test,s=m[n].throw,i=m[n].transform;e(t[n])||s(),t[n]=i(t[n])}return t},v=e=>{if(!b(e))throw new Error(`Class '${e.name}' is not an assemblage.`);return p(t.AssemblageDefinition,e)},w=(e,t)=>v(t)[e],x=e=>{const t=()=>r(e[0])&&(e=>"object"==typeof e&&!Array.isArray(e)&&!r(e))(e[1]);return a(h({if:()=>r(e[0])&&r(e[1]),then:()=>({identifier:e[0],concrete:e[1],configuration:{}})}),h({if:()=>t(),then:()=>({identifier:e[0],concrete:e[0],configuration:e[1]}),else:e=>e}))()};let C=Symbol.iterator;class j{dispose(){f(this,j)}add(...e){const t=e=>this.collection[e.channel].push(e.listener),n=h({if:()=>2===e.length,then:()=>({channel:e[0],listener:e[1]}),else:()=>{const t=e[0];return{channel:t[0],listener:t[1]}}}),s=h({if:e=>!i(this.collection[e.channel]),then:e=>{this.collection[e.channel]=[],t(e)},else:e=>{t(e)}});return a(n,s)(),this}remove(e,t){const n=t=>this.collection[e].splice(t,1),s=h({if:()=>this.collection[e]&&0===this.collection[e].length,then:()=>delete this.collection[e]}),r=h({if:()=>i(t),then:()=>n(this.collection[e].indexOf(t)),else:()=>delete this.collection[e]}),o=h({if:e=>this.has(e),then:e=>this.collection[e]});return a(o,r,s)(),this}has(...e){return s("string")(e[0])?Object.keys(this.collection).includes(e[0]):!!s("function")(e[0])&&Object.values(this.collection).flat().includes(e[0])}get(...e){return s("string")(e[0])?this.collection[e[0]]:s("function")(e[0])?Object.values(this.collection).flat().filter((t=>t===e[0])):[]}clear(){const e=(e=>t=>{const n=Array.isArray(e)?e=>parseInt(e):e=>e;for(const s in e)t(n(s))})(this.collection),t=e=>l(this.collection[e])((t=>this.remove(e,t)));return e((e=>t(e))),this}get listeners(){return Object.values(this.collection).flat()}get channels(){return Object.keys(this.collection)}get length(){return Object.values(this.collection).flat().length}[C](){let e=-1;const t=this.collection?Object.keys(this.collection):[];return{next:()=>({value:t[++e],done:!(e in t)})}}constructor(){!function(e,t,n){t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n}(this,"collection",{});return u(this,j)}}class A{}function I(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}class O{dispose(){this.listeners.dispose(),this.channels.clear(),f(this,O)}addChannels(...e){return l(e)((e=>{const t=this.cleanChannel(e);if(this.channels.has(t))throw new Error(`Channel '${t}' already exists.`);this.channels.add(t)})),this}removeChannels(...e){return l(e)((e=>{const t=this.cleanChannel(e);"*"!==t&&this.channels.has(t)&&(this.channels.delete(t),this.listeners.remove(t),this.onceListeners.remove(t))})),this}on(e,t){const n=this.cleanChannel(e);if(!this.channels.has(n))throw new Error(`Channel '${n}' was not registered.`);return this.listeners.add(n,t),this}once(e,t){const n=this.cleanChannel(e);if(!this.channels.has(n))throw new Error(`Channel '${n}' was not registered.`);return this.onceListeners.add(n,t),this}off(e,t){const n=this.cleanChannel(e);return this.listeners.remove(n,t),this}emit(e,...t){const n=this.cleanChannel(e);if(this.channels.has(n)){const e=this.onceListeners.get("*")||[],s=this.listeners.get("*")||[],i=this.onceListeners.get(n)||[],r=this.listeners.get(n)||[],o=l(e),c=l(s),a=l(i),h=l(r);o((e=>{this.run(e,...t),this.onceListeners.remove("*",e)})),c((e=>{this.run(e,...t)})),a((e=>{this.run(e,...t),this.onceListeners.remove(n,e)})),h((e=>{this.run(e,...t)}))}return this}run(e,...t){if(o(e)){return e(...t).then((()=>Promise.resolve()))}e(...t)}cleanChannel(e){return c(e,"*")}constructor(...e){I(this,"listeners",new j),I(this,"onceListeners",new j),I(this,"channels",new Set(["*"])),this.addChannels(...e)}}var P=function(e){return e.UseIdentifier="assemblage:use.param.value",e}({}),E=function(e){return e.Context="assembler:context.param.index",e.Dispose="assembler:dispose.param.index",e.Definition="assemblage:definition.param.index",e.Configuration="assemblage:configuration.param.index",e.Use="assemblage:use.param.index",e}({});const D=e=>()=>(t,n,s)=>{const i=p(e,t)||[];i.push(s),d(e,i,t)},S=D(E.Context),$=D(E.Configuration),R=D(E.Definition),U=D(E.Dispose),_=e=>{const t=(e=>p(E.Context,e)||[])(e),n=(e=>p(E.Definition,e)||[])(e),s=(e=>p(E.Configuration,e)||[])(e),i=(e=>p(E.Dispose,e)||[])(e),r=(e=>p(E.Use,e)||[])(e);return{context:t,definition:n,configuration:s,dispose:i,use:r}};function k(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}class M{static of(e,t,n){return new M(e,t,n)}dispose(){this.singletonInstance&&(((e,t)=>{if(e.concrete.prototype instanceof O){const n=t;for(const t of e.events)n.off(t);n.removeChannels(...e.events),e.privateContext.removeChannels(...e.events)}})(this,this.singletonInstance),L(this.singletonInstance,"onDispose",this.publicContext),f(this.singletonInstance,this.concrete)),f(this,M)}build(){if(this.singletonInstance)return this.singletonInstance;const e=(e=>{const t=[],n=g(e.concrete),s=_(e.concrete);let i=0;for(const r of n)if(s.context.includes(i))t.push(e.publicContext),i++;else if(s.configuration.includes(i))t.push(e.configuration),i++;else if(s.definition.includes(i))t.push(e.definition),i++;else if(s.dispose.includes(i))t.push(e.privateContext.dispose),i++;else if(s.use.includes(i)){const n=p(P.UseIdentifier,e.concrete)[i];t.push(e.privateContext.require(n)),i++}else t.push(e.privateContext.require(r)),i++;return t})(this),t=new this.concrete(...e);return((e,t)=>{if(e.concrete.prototype instanceof O){const n=t,s=n.channels;for(const t of e.events)s.has(t)||n.addChannels(t),e.privateContext.events.has(t)||e.privateContext.addChannels(t);for(const i of e.events)t.on(i,((...t)=>{e.privateContext.emit(i,...t)}))}})(this,t),this.isSingleton?(this.singletonInstance=t,this.privateContext.prepareInitHook(t),this.singletonInstance):(L(t,"onInit",this.publicContext),t)}get dependencies(){return this.dependenciesIds}get definition(){return v(this.concrete)||{}}get isSingleton(){return w("singleton",this.concrete)}get singleton(){return this.singletonInstance}get injections(){return w("inject",this.concrete)||[]}get objects(){return w("use",this.concrete)||[]}get tags(){return w("tags",this.concrete)||[]}get events(){return w("events",this.concrete)||[]}constructor(e,t,n){if(k(this,"privateContext",void 0),k(this,"publicContext",void 0),k(this,"identifier",void 0),k(this,"concrete",void 0),k(this,"configuration",void 0),k(this,"dependenciesIds",void 0),k(this,"singletonInstance",void 0),this.privateContext=t,this.publicContext=n,this.dependenciesIds=[],this.identifier=e.identifier,this.concrete=e.concrete,this.configuration=e.configuration,!b(this.concrete))throw new Error(`Class '${this.concrete.name}' is not an Assemblage.`);const s=l(this.injections),i=l(this.objects);s((e=>this.privateContext.register(e))),i((e=>{"string"==typeof e[0]||"symbol"==typeof e[0]?this.privateContext.use(e[0],e[1]):this.privateContext.register(e,!0)})),this.dependenciesIds=(e=>{const t=[],n=g(e),s=_(e);let i=0;for(const r of n)s.context.includes(i)||s.configuration.includes(i)||s.definition.includes(i)||s.dispose.includes(i)||s.use.includes(i)||t.push(r),i++;return t})(this.concrete),e.instance?this.singletonInstance=e.instance:this.isSingleton}}const L=(e,t,n)=>new Promise((s=>{const i=e[t];if(i){if(o(i))return void i.bind(e)(n).then((()=>{s()}));s(i.bind(e)(n))}}));function q(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}class N extends O{static build(e){const n=new N;((e,n,s)=>{const i=v(s);i[e]=n;const r=y(i);d(t.AssemblageDefinition,r,s)})("singleton",!0,e);const s=n.register([e]),i=n.require(s.identifier),r=n.initCache.indexOf(i);n.initCache.splice(r,1);for(const t of n.initCache)L(t,"onInit",n.publicContext);return L(i,"onInit",n.publicContext),n.initCache.length=0,i}dispose(){for(const[e,t]of this.injectables)t.dispose();f(this,N)}register(e,t=!1){const n=!0===t?(e=>({identifier:e[0],concrete:e[0],instance:e[1],configuration:{}}))(e):(e=>((e,t)=>(n,...s)=>e[n]?e[n](...s):t?t(n,...s):void 0)({1:()=>(e=>({identifier:e[0],concrete:e[0],configuration:{}}))(e),2:()=>x(e),3:()=>(e=>({identifier:e[0],concrete:e[1],configuration:e[2]}))(e)},(()=>{throw new Error("Injection tuple must be of length 1, 2 or 3.")}))(e.length))(e);if(this.has(n.identifier))throw new Error(`An assemblage is already registered with identifier '${n.identifier.name}'.`);const s=M.of(n,this.privateContext,this.publicContext);return this.injectables.set(s.identifier,s),L(s.concrete,"onRegister",this.publicContext),s}use(e,t){if(this.has(e))throw new Error(`A value is already registered with identifier '${String(e)}'.`);return this.objects.set(e,t),t}prepareInitHook(e){return this.initCache.push(e),this.initCache}has(e){return"string"==typeof e||"symbol"==typeof e?this.objects.has(e):this.injectables.has(e)}require(e){switch(typeof e){case"string":case"symbol":if(!this.objects.has(e))throw new Error(`Injected object with identifier '${String(e)}' has not been registered.`);return this.objects.get(e);default:if(!this.injectables.has(e))throw new Error(`Class with identifier '${e.name}' has not been registered or is a circular dependency.`);return this.injectables.get(e).build()}}concrete(e){const t=this.injectables.get(e);if(t)return t.concrete}tagged(...e){const t=[];for(const n of e)for(const[e,s]of this.injectables)s.tags.includes(n)&&t.push(s.build());return t}get size(){return this.injectables.size}constructor(){super(),q(this,"injectables",new Map),q(this,"objects",new Map),q(this,"initCache",[]),q(this,"privateContext",void 0),q(this,"publicContext",void 0),this.publicContext={has:this.has.bind(this),require:this.require.bind(this),concrete:this.concrete.bind(this),tagged:this.tagged.bind(this),dispose:this.dispose.bind(this),on:this.on.bind(this),once:this.once.bind(this),off:this.off.bind(this),events:this.channels},this.privateContext={...this.publicContext,register:this.register.bind(this),use:this.use.bind(this),prepareInitHook:this.prepareInitHook.bind(this),emit:this.emit.bind(this),addChannels:this.addChannels.bind(this),removeChannels:this.removeChannels.bind(this)}}}exports.AbstractAssemblage=class{static onRegister(e){}},exports.AbstractAssembler=class extends A{},exports.AbstractEventManager=A,exports.Assemblage=n=>{const s=y(n||{});return n=>(d(e.IsAssemblage,!0,n),d(t.AssemblageDefinition,s,n),n)},exports.Assembler=N,exports.Await=(e,t=25)=>(n,s,i)=>{const r=i.value;i.value=async function(){return new Promise((n=>{if(this[e])r.apply(this),n();else{const s=setInterval((()=>{this[e]&&(clearInterval(s),r.apply(this),n())}),t)}}))}},exports.Configuration=$,exports.Context=S,exports.Definition=R,exports.Dispose=U,exports.EventManager=O,exports.ReflectParamIndex=E,exports.ReflectParamValue=P,exports.Use=e=>(t,n,s)=>{const i=p(E.Use,t)||[];i.push(s),d(E.Use,i,t);const r=p(P.UseIdentifier,t)||{};r[s]=e,d(P.UseIdentifier,r,t)},exports.decorateAssemblage=(n,s)=>{const i=y(s||{});return d(e.IsAssemblage,!0,n),d(t.AssemblageDefinition,i,n),n},exports.getDecoratedParametersIndexes=_,exports.isAssemblage=b;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e="design:paramtypes";var t=function(e){return e.IsAssemblage="is_assemblage",e}({}),n=function(e){return e.AssemblageDefinition="assemblage:definition.value",e}({});const s=(...e)=>{},i=(...e)=>t=>{if(e.includes(typeof t))return typeof t},r=e=>!(e=>void 0===e)(e)&&!(e=>null===e)(e),o=e=>e&&"function"==typeof e&&void 0!==e.constructor,c=e=>"function"==typeof e&&"AsyncFunction"===e.constructor.name,a=(e,...t)=>{t.map(((e,n)=>{const s=/\S/.test(e);return s&&(t[n]=t[n].trim()),s?-1:n})).filter((e=>e>=0)).every((e=>{t[e]=" ",((e,t,n)=>{const s=e[t];e.splice(t,1),e.splice(n,0,s)})(t,e,t.length-1)})),t=(e=>Array.from(new Set(e)))(t);const n=new RegExp(`[^A-Za-zÀ-ÖØ-öø-ÿ0-9${t.join("")}]`,"gi");return e.replace(n,"")},l=(...e)=>t=>e.reduce(((e,t)=>t(e)),t),h=e=>t=>e.if(t)?e.then(t):e.else?e.else(t):void 0,u=e=>t=>{const n=Array.isArray(e)?e=>parseInt(e):s;for(const[s,i]of Object.entries(e))t(i,n(s))},f=(e,t)=>{const n=new Proxy(e,{get:function(n,s){return s===Symbol.iterator?n[Symbol.iterator].bind(n):((e,t)=>[...Object.getOwnPropertyNames(t.prototype),...Object.getOwnPropertyNames(Object.getPrototypeOf(e)),...Object.getOwnPropertyNames(e)])(e,t).includes(s)?n[s]:n.collection[s]},set:function(e,t,n){return Reflect.set(e,t,n)}});return n},d=(e,t)=>{const n=e;for(const s of((e,t)=>[...Object.getOwnPropertyNames(t.prototype),...Object.getOwnPropertyNames(Object.getPrototypeOf(e)),...Object.getOwnPropertyNames(e)])(e,t))delete n[s]},p=(e,t,n)=>{Reflect.defineMetadata(`__${e}__`,t,n)},g=(e,t)=>Reflect.getOwnMetadata(`__${e}__`,t),b=t=>Reflect.getMetadata(e,t)||[],m=e=>g(t.IsAssemblage,e)||!1,y={singleton:{test:e=>"boolean"==typeof e||void 0===e,throw:()=>{throw new Error("'singleton' property must be of type 'boolean' or 'undefined'.")},transform:e=>void 0===e||!!e},events:{test:e=>void 0===e||Array.isArray(e)&&e.every((e=>"string"==typeof e)),throw:()=>{throw new Error("'events' property must be an array of strings or 'undefined'.")},transform:e=>e},inject:{test:e=>void 0===e||Array.isArray(e)&&e.every((e=>Array.isArray(e)&&e.length>=1&&e.length<=3)),throw:()=>{throw new Error("'inject' property must be an array of tuples of length 1, 2 or 3.")},transform:e=>e},use:{test:e=>void 0===e||Array.isArray(e)&&e.every((e=>Array.isArray(e)&&2==e.length)),throw:()=>{throw new Error("'use' property must be an array of tuples of length 2.")},transform:e=>e},tags:{test:e=>void 0===e||"string"==typeof e||Array.isArray(e)&&e.every((e=>"string"==typeof e)),throw:()=>{throw new Error("'tags' property must be a string or an array of strings.")},transform:e=>"string"==typeof e?[e]:e},metadata:{test:e=>("object"==typeof e||void 0===e)&&!Array.isArray(e),throw:()=>{throw new Error("'metadata' property must be of type 'object' or 'undefined'.")},transform:e=>e}},v=e=>{const t={...e};for(const n in t)if(!Object.keys(y).includes(n))throw new Error(`Property '${n}' is not a valid assemblage definition property.`);for(const n in y){const e=y[n].test,s=y[n].throw,i=y[n].transform;e(t[n])||s(),t[n]=i(t[n])}return t},C=e=>{if(!m(e))throw new Error(`Class '${e.name}' is not an assemblage.`);return g(n.AssemblageDefinition,e)},w=(e,t)=>C(t)[e],x=e=>{const t=()=>o(e[0])&&(e=>"object"==typeof e&&!Array.isArray(e)&&!o(e))(e[1]);return l(h({if:()=>o(e[0])&&o(e[1]),then:()=>({identifier:e[0],concrete:e[1],configuration:{}})}),h({if:()=>t(),then:()=>({identifier:e[0],concrete:e[0],configuration:e[1]}),else:e=>e}))()},j=(e,s)=>{const i=v(s||{});return p(t.IsAssemblage,!0,e),p(n.AssemblageDefinition,i,e),e};let A=Symbol.iterator;class I{dispose(){d(this,I)}add(...e){const t=e=>this.collection[e.channel].push(e.listener),n=h({if:()=>2===e.length,then:()=>({channel:e[0],listener:e[1]}),else:()=>{const t=e[0];return{channel:t[0],listener:t[1]}}}),s=h({if:e=>!r(this.collection[e.channel]),then:e=>{this.collection[e.channel]=[],t(e)},else:e=>{t(e)}});return l(n,s)(),this}remove(e,t){const n=t=>this.collection[e].splice(t,1),s=h({if:()=>this.collection[e]&&0===this.collection[e].length,then:()=>delete this.collection[e]}),i=h({if:()=>r(t),then:()=>n(this.collection[e].indexOf(t)),else:()=>delete this.collection[e]}),o=h({if:e=>this.has(e),then:e=>this.collection[e]});return l(o,i,s)(),this}has(...e){return i("string")(e[0])?Object.keys(this.collection).includes(e[0]):!!i("function")(e[0])&&Object.values(this.collection).flat().includes(e[0])}get(...e){return i("string")(e[0])?this.collection[e[0]]:i("function")(e[0])?Object.values(this.collection).flat().filter((t=>t===e[0])):[]}clear(){const e=(e=>t=>{const n=Array.isArray(e)?e=>parseInt(e):e=>e;for(const s in e)t(n(s))})(this.collection),t=e=>u(this.collection[e])((t=>this.remove(e,t)));return e((e=>t(e))),this}get listeners(){return Object.values(this.collection).flat()}get channels(){return Object.keys(this.collection)}get length(){return Object.values(this.collection).flat().length}[A](){let e=-1;const t=this.collection?Object.keys(this.collection):[];return{next:()=>({value:t[++e],done:!(e in t)})}}constructor(){!function(e,t,n){t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n}(this,"collection",{});return f(this,I)}}class O{}function D(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}class P{dispose(){this.listeners.dispose(),this.channels.clear(),d(this,P)}addChannels(...e){return u(e)((e=>{const t=this.cleanChannel(e);if(this.channels.has(t))throw new Error(`Channel '${t}' already exists.`);this.channels.add(t)})),this}removeChannels(...e){return u(e)((e=>{const t=this.cleanChannel(e);"*"!==t&&this.channels.has(t)&&(this.channels.delete(t),this.listeners.remove(t),this.onceListeners.remove(t))})),this}on(e,t){const n=this.cleanChannel(e);if(!this.channels.has(n))throw new Error(`Channel '${n}' was not registered.`);return this.listeners.add(n,t),this}once(e,t){const n=this.cleanChannel(e);if(!this.channels.has(n))throw new Error(`Channel '${n}' was not registered.`);return this.onceListeners.add(n,t),this}off(e,t){const n=this.cleanChannel(e);return this.listeners.remove(n,t),this}emit(e,...t){const n=this.cleanChannel(e);if(this.channels.has(n)){const e=this.onceListeners.get("*")||[],s=this.listeners.get("*")||[],i=this.onceListeners.get(n)||[],r=this.listeners.get(n)||[],o=u(e),c=u(s),a=u(i),l=u(r);o((e=>{this.run(e,...t),this.onceListeners.remove("*",e)})),c((e=>{this.run(e,...t)})),a((e=>{this.run(e,...t),this.onceListeners.remove(n,e)})),l((e=>{this.run(e,...t)}))}return this}run(e,...t){if(c(e)){return e(...t).then((()=>Promise.resolve()))}e(...t)}cleanChannel(e){return a(e,"*")}constructor(...e){D(this,"listeners",new I),D(this,"onceListeners",new I),D(this,"channels",new Set(["*"])),this.addChannels(...e)}}var E=function(e){return e.UseIdentifier="assemblage:use.param.value",e}({}),U=function(e){return e.Context="assembler:context.param.index",e.Dispose="assembler:dispose.param.index",e.Definition="assemblage:definition.param.index",e.Configuration="assemblage:configuration.param.index",e.Use="assemblage:use.param.index",e}({});const S=e=>()=>(t,n,s)=>{const i=g(e,t)||[];i.push(s),p(e,i,t)},$=S(U.Context),R=S(U.Configuration),M=S(U.Definition),_=S(U.Dispose),k=(e,t,n)=>{const s=g(U.Use,t)||[];s.push(n),p(U.Use,s,t);const i=g(E.UseIdentifier,t)||{};i[n]=e,p(E.UseIdentifier,i,t)},L=e=>{const t=(e=>g(U.Context,e)||[])(e),n=(e=>g(U.Definition,e)||[])(e),s=(e=>g(U.Configuration,e)||[])(e),i=(e=>g(U.Dispose,e)||[])(e),r=(e=>g(U.Use,e)||[])(e);return{Context:t,Definition:n,Configuration:s,Dispose:i,Use:r}},q=t=>s=>{const i=class extends s{constructor(...e){super(...e),t&&t.call(this)}},r=Reflect.getOwnMetadata(e,s),o=L(s),c=[];for(let e=0;e<r.length;e++)if(o.Context.includes(e)){const t=g(U.Context,s)||[];t.push(e),p(U.Context,t,i)}else if(o.Definition.includes(e)){const t=g(U.Definition,s)||[];t.push(e),p(U.Definition,t,i)}else if(o.Configuration.includes(e)){const t=g(U.Configuration,s)||[];t.push(e),p(U.Configuration,t,i)}else if(o.Dispose.includes(e)){const t=g(U.Dispose,s)||[];t.push(e),p(U.Dispose,t,i),c.push(r[e])}else if(o.Use.includes(e)){const t=g(E.UseIdentifier,s);k(t[e],i,e)}else;return Object.defineProperty(i,"name",{value:s.name}),j(i,g(n.AssemblageDefinition,s))};function N(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}class H{static of(e,t,n){return new H(e,t,n)}dispose(){this.singletonInstance&&(((e,t)=>{if(e.concrete.prototype instanceof P){const n=t;for(const t of e.events)n.off(t);n.removeChannels(...e.events),e.privateContext.removeChannels(...e.events)}})(this,this.singletonInstance),z(this.singletonInstance,"onDispose",this.publicContext,this.configuration),d(this.singletonInstance,this.concrete)),d(this,H)}build(){if(this.singletonInstance)return this.singletonInstance;const e=(e=>{const t=[],n=b(e.concrete),s=L(e.concrete);let i=0;for(const r of n)if(s.Context.includes(i))t.push(e.publicContext),i++;else if(s.Configuration.includes(i))t.push(e.configuration),i++;else if(s.Definition.includes(i))t.push(e.definition),i++;else if(s.Dispose.includes(i))t.push(e.privateContext.dispose),i++;else if(s.Use.includes(i)){const n=g(E.UseIdentifier,e.concrete)[i];t.push(e.privateContext.require(n)),i++}else t.push(e.privateContext.require(r)),i++;return t})(this),t=new this.concrete(...e);return((e,t)=>{if(e.concrete.prototype instanceof P){const n=t,s=n.channels;for(const t of e.events)s.has(t)||n.addChannels(t),e.privateContext.events.has(t)||e.privateContext.addChannels(t);for(const i of e.events)t.on(i,((...t)=>{e.privateContext.emit(i,...t)}))}})(this,t),this.isSingleton?(this.singletonInstance=t,this.privateContext.prepareInitHook(t,this.configuration),this.singletonInstance):(z(t,"onInit",this.publicContext),t)}get dependencies(){return this.dependenciesIds}get definition(){return C(this.concrete)||{}}get isSingleton(){return w("singleton",this.concrete)}get singleton(){return this.singletonInstance}get injections(){return w("inject",this.concrete)||[]}get objects(){return w("use",this.concrete)||[]}get tags(){return w("tags",this.concrete)||[]}get events(){return w("events",this.concrete)||[]}constructor(e,t,n){if(N(this,"privateContext",void 0),N(this,"publicContext",void 0),N(this,"identifier",void 0),N(this,"concrete",void 0),N(this,"configuration",void 0),N(this,"dependenciesIds",void 0),N(this,"singletonInstance",void 0),this.privateContext=t,this.publicContext=n,this.dependenciesIds=[],this.identifier=e.identifier,this.concrete=e.concrete,this.configuration=e.configuration,!m(this.concrete))throw new Error(`Class '${this.concrete.name}' is not an Assemblage.`);const s=u(this.injections),i=u(this.objects);s((e=>this.privateContext.register(e))),i((e=>{"string"==typeof e[0]||"symbol"==typeof e[0]?this.privateContext.use(e[0],e[1]):this.privateContext.register(e,!0)})),this.dependenciesIds=(e=>{const t=[],n=b(e),s=L(e);let i=0;for(const r of n)s.Context.includes(i)||s.Configuration.includes(i)||s.Definition.includes(i)||s.Dispose.includes(i)||s.Use.includes(i)||t.push(r),i++;return t})(this.concrete),e.instance?this.singletonInstance=e.instance:this.isSingleton}}const z=(e,t,n,s)=>new Promise((i=>{const r=e[t];if(r){if(c(r))return void r.bind(e)(n,s).then((()=>{i()}));i(r.bind(e)(n,s))}}));function W(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}class F extends P{static build(e){const t=new F;((e,t,s)=>{const i=C(s);i[e]=t;const r=v(i);p(n.AssemblageDefinition,r,s)})("singleton",!0,e);const s=t.register([e]),i=t.require(s.identifier),r=t.initCache.find((e=>e.instance===i));if(!r)throw new Error("Root instance not found in assemblages cache.");const o=t.initCache.indexOf(r);t.initCache.splice(o,1);for(const n of t.initCache)z(n.instance,"onInit",t.publicContext,n.configuration);return z(i,"onInit",t.publicContext,s.configuration),t.initCache.length=0,i}dispose(){for(const[e,t]of this.injectables)t.dispose();d(this,F)}register(e,t=!1){const n=!0===t?(e=>({identifier:e[0],concrete:e[0],instance:e[1],configuration:{}}))(e):(e=>((e,t)=>(n,...s)=>e[n]?e[n](...s):t?t(n,...s):void 0)({1:()=>(e=>({identifier:e[0],concrete:e[0],configuration:{}}))(e),2:()=>x(e),3:()=>(e=>({identifier:e[0],concrete:e[1],configuration:e[2]}))(e)},(()=>{throw new Error("Injection tuple must be of length 1, 2 or 3.")}))(e.length))(e);if(this.has(n.identifier))throw new Error(`An assemblage is already registered with identifier '${n.identifier.name}'.`);const s=H.of(n,this.privateContext,this.publicContext);return this.injectables.set(s.identifier,s),z(s.concrete,"onRegister",this.publicContext,s.configuration),s}use(e,t){if(this.has(e))throw new Error(`A value is already registered with identifier '${String(e)}'.`);return this.objects.set(e,t),t}prepareInitHook(e,t){return this.initCache.push({instance:e,configuration:t}),this.initCache}has(e){return"string"==typeof e||"symbol"==typeof e?this.objects.has(e):this.injectables.has(e)}require(e){switch(typeof e){case"string":case"symbol":if(!this.objects.has(e))throw new Error(`Injected object with identifier '${String(e)}' has not been registered.`);return this.objects.get(e);default:if(!this.injectables.has(e))throw new Error(`Class with identifier '${e.name}' has not been registered or is a circular dependency.`);return this.injectables.get(e).build()}}concrete(e){const t=this.injectables.get(e);if(t)return t.concrete}tagged(...e){const t=[];for(const n of e)for(const[e,s]of this.injectables)s.tags.includes(n)&&t.push(s.build());return t}get size(){return this.injectables.size}constructor(){super(),W(this,"injectables",new Map),W(this,"objects",new Map),W(this,"initCache",[]),W(this,"privateContext",void 0),W(this,"publicContext",void 0),this.publicContext={has:this.has.bind(this),require:this.require.bind(this),concrete:this.concrete.bind(this),tagged:this.tagged.bind(this),dispose:this.dispose.bind(this),on:this.on.bind(this),once:this.once.bind(this),off:this.off.bind(this),events:this.channels},this.privateContext={...this.publicContext,register:this.register.bind(this),use:this.use.bind(this),prepareInitHook:this.prepareInitHook.bind(this),emit:this.emit.bind(this),addChannels:this.addChannels.bind(this),removeChannels:this.removeChannels.bind(this)}}}exports.AbstractAssemblage=class{static onRegister(e,t){}},exports.AbstractAssembler=class extends O{},exports.AbstractEventManager=O,exports.Assemblage=e=>t=>j(t,e),exports.AssemblageWrappedDecorator=q,exports.Assembler=F,exports.Await=(e,t=25)=>(n,s,i)=>{const r=i.value;i.value=async function(){return new Promise((n=>{if(this[e])r.apply(this),n();else{const s=setInterval((()=>{this[e]&&(clearInterval(s),r.apply(this),n())}),t)}}))}},exports.Configuration=R,exports.Context=$,exports.Definition=M,exports.Dispose=_,exports.EventManager=P,exports.ReflectParamIndex=U,exports.ReflectParamValue=E,exports.Use=e=>(t,n,s)=>{k(e,t,s)},exports.createWrappedDecorator=e=>()=>q(e),exports.decorateAssemblage=j,exports.decorateUse=k,exports.getDecoratedParametersIndexes=L,exports.isAssemblage=m;
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  class AbstractAssemblage {
2
- static onRegister(s) {}
2
+ static onRegister(s, t) {}
3
3
  }
4
4
 
5
5
  const ReflectParamTypes = 'design:paramtypes';
@@ -257,19 +257,16 @@ const resolveInstanceInjectionTuple = (e)=>{
257
257
  };
258
258
  };
259
259
 
260
- const Assemblage = (r)=>{
261
- const n = r ? validateDefinition(r) : validateDefinition({});
260
+ const Assemblage = (e)=>{
262
261
  return (o)=>{
263
- defineCustomMetadata(ReflectFlags.IsAssemblage, true, o);
264
- defineCustomMetadata(ReflectValue.AssemblageDefinition, n, o);
265
- return o;
262
+ return decorateAssemblage(o, e);
266
263
  };
267
264
  };
268
- const decorateAssemblage = (r, n)=>{
269
- const m = n ? validateDefinition(n) : validateDefinition({});
270
- defineCustomMetadata(ReflectFlags.IsAssemblage, true, r);
271
- defineCustomMetadata(ReflectValue.AssemblageDefinition, m, r);
272
- return r;
265
+ const decorateAssemblage = (s, m)=>{
266
+ const n = m ? validateDefinition(m) : validateDefinition({});
267
+ defineCustomMetadata(ReflectFlags.IsAssemblage, true, s);
268
+ defineCustomMetadata(ReflectValue.AssemblageDefinition, n, s);
269
+ return s;
273
270
  };
274
271
 
275
272
  function e$4(e, t, n) {
@@ -576,16 +573,19 @@ const r$1 = i(ReflectParamIndex.Configuration);
576
573
  const c$1 = i(ReflectParamIndex.Definition);
577
574
  const e$2 = i(ReflectParamIndex.Dispose);
578
575
 
579
- const Use = (n)=>{
580
- return (r, i, m)=>{
581
- const c = getOwnCustomMetadata(ReflectParamIndex.Use, r) || [];
582
- c.push(m);
583
- defineCustomMetadata(ReflectParamIndex.Use, c, r);
584
- const U = getOwnCustomMetadata(ReflectParamValue.UseIdentifier, r) || {};
585
- U[m] = n;
586
- defineCustomMetadata(ReflectParamValue.UseIdentifier, U, r);
576
+ const Use = (e)=>{
577
+ return (o, t, s)=>{
578
+ decorateUse(e, o, s);
587
579
  };
588
580
  };
581
+ const decorateUse = (r, n, c)=>{
582
+ const U = getOwnCustomMetadata(ReflectParamIndex.Use, n) || [];
583
+ U.push(c);
584
+ defineCustomMetadata(ReflectParamIndex.Use, U, n);
585
+ const i = getOwnCustomMetadata(ReflectParamValue.UseIdentifier, n) || {};
586
+ i[c] = r;
587
+ defineCustomMetadata(ReflectParamValue.UseIdentifier, i, n);
588
+ };
589
589
 
590
590
  const o = (o)=>{
591
591
  return getOwnCustomMetadata(ReflectParamIndex.Context, o) || [];
@@ -609,64 +609,115 @@ const getDecoratedParametersIndexes = (t)=>{
609
609
  const u = s(t);
610
610
  const a = c(t);
611
611
  return {
612
- context: n,
613
- definition: i,
614
- configuration: m,
615
- dispose: u,
616
- use: a
612
+ Context: n,
613
+ Definition: i,
614
+ Configuration: m,
615
+ Dispose: u,
616
+ Use: a
617
617
  };
618
618
  };
619
619
 
620
+ const createWrappedDecorator = (e)=>{
621
+ return ()=>AssemblageWrappedDecorator(e);
622
+ };
623
+ const AssemblageWrappedDecorator = (p)=>(u)=>{
624
+ const f = class extends u {
625
+ constructor(...e){
626
+ super(...e);
627
+ if (p) p.call(this);
628
+ }
629
+ };
630
+ const m = Reflect.getOwnMetadata(ReflectParamTypes, u);
631
+ const l = getDecoratedParametersIndexes(u);
632
+ const d = [];
633
+ for(let t = 0; t < m.length; t++){
634
+ if (l.Context.includes(t)) {
635
+ const n = getOwnCustomMetadata(ReflectParamIndex.Context, u) || [];
636
+ n.push(t);
637
+ defineCustomMetadata(ReflectParamIndex.Context, n, f);
638
+ continue;
639
+ }
640
+ if (l.Definition.includes(t)) {
641
+ const n = getOwnCustomMetadata(ReflectParamIndex.Definition, u) || [];
642
+ n.push(t);
643
+ defineCustomMetadata(ReflectParamIndex.Definition, n, f);
644
+ continue;
645
+ }
646
+ if (l.Configuration.includes(t)) {
647
+ const n = getOwnCustomMetadata(ReflectParamIndex.Configuration, u) || [];
648
+ n.push(t);
649
+ defineCustomMetadata(ReflectParamIndex.Configuration, n, f);
650
+ continue;
651
+ }
652
+ if (l.Dispose.includes(t)) {
653
+ const n = getOwnCustomMetadata(ReflectParamIndex.Dispose, u) || [];
654
+ n.push(t);
655
+ defineCustomMetadata(ReflectParamIndex.Dispose, n, f);
656
+ d.push(m[t]);
657
+ continue;
658
+ }
659
+ if (l.Use.includes(t)) {
660
+ const e = getOwnCustomMetadata(ReflectParamValue.UseIdentifier, u);
661
+ decorateUse(e[t], f, t);
662
+ continue;
663
+ }
664
+ }
665
+ Object.defineProperty(f, 'name', {
666
+ value: u.name
667
+ });
668
+ return decorateAssemblage(f, getOwnCustomMetadata(ReflectValue.AssemblageDefinition, u));
669
+ };
670
+
620
671
  const resolveInjectableParameters = (i)=>{
621
- const c = [];
622
- const s = getParamTypes(i.concrete);
623
- const u = getDecoratedParametersIndexes(i.concrete);
624
- let r = 0;
625
- for (const n of s){
626
- if (u.context.includes(r)) {
627
- c.push(i.publicContext);
628
- r++;
672
+ const s = [];
673
+ const c = getParamTypes(i.concrete);
674
+ const r = getDecoratedParametersIndexes(i.concrete);
675
+ let u = 0;
676
+ for (const n of c){
677
+ if (r.Context.includes(u)) {
678
+ s.push(i.publicContext);
679
+ u++;
629
680
  continue;
630
681
  }
631
- if (u.configuration.includes(r)) {
632
- c.push(i.configuration);
633
- r++;
682
+ if (r.Configuration.includes(u)) {
683
+ s.push(i.configuration);
684
+ u++;
634
685
  continue;
635
686
  }
636
- if (u.definition.includes(r)) {
637
- c.push(i.definition);
638
- r++;
687
+ if (r.Definition.includes(u)) {
688
+ s.push(i.definition);
689
+ u++;
639
690
  continue;
640
691
  }
641
- if (u.dispose.includes(r)) {
642
- c.push(i.privateContext.dispose);
643
- r++;
692
+ if (r.Dispose.includes(u)) {
693
+ s.push(i.privateContext.dispose);
694
+ u++;
644
695
  continue;
645
696
  }
646
- if (u.use.includes(r)) {
697
+ if (r.Use.includes(u)) {
647
698
  const n = getOwnCustomMetadata(ReflectParamValue.UseIdentifier, i.concrete);
648
- const t = n[r];
649
- c.push(i.privateContext.require(t));
650
- r++;
699
+ const t = n[u];
700
+ s.push(i.privateContext.require(t));
701
+ u++;
651
702
  continue;
652
703
  }
653
- c.push(i.privateContext.require(n));
654
- r++;
704
+ s.push(i.privateContext.require(n));
705
+ u++;
655
706
  }
656
- return c;
707
+ return s;
657
708
  };
658
709
  const resolveDependencies = (e)=>{
659
710
  const o = [];
660
711
  const i = getParamTypes(e);
661
- const c = getDecoratedParametersIndexes(e);
662
- let s = 0;
712
+ const s = getDecoratedParametersIndexes(e);
713
+ let c = 0;
663
714
  for (const e of i){
664
- if (c.context.includes(s) || c.configuration.includes(s) || c.definition.includes(s) || c.dispose.includes(s) || c.use.includes(s)) {
665
- s++;
715
+ if (s.Context.includes(c) || s.Configuration.includes(c) || s.Definition.includes(c) || s.Dispose.includes(c) || s.Use.includes(c)) {
716
+ c++;
666
717
  continue;
667
718
  }
668
719
  o.push(e);
669
- s++;
720
+ c++;
670
721
  }
671
722
  return o;
672
723
  };
@@ -691,7 +742,7 @@ class Injectable {
691
742
  dispose() {
692
743
  if (this.singletonInstance) {
693
744
  unregisterEvents(this, this.singletonInstance);
694
- callHook(this.singletonInstance, 'onDispose', this.publicContext);
745
+ callHook(this.singletonInstance, 'onDispose', this.publicContext, this.configuration);
695
746
  clearInstance(this.singletonInstance, this.concrete);
696
747
  }
697
748
  clearInstance(this, Injectable);
@@ -703,7 +754,7 @@ class Injectable {
703
754
  registerEvents(this, e);
704
755
  if (this.isSingleton) {
705
756
  this.singletonInstance = e;
706
- this.privateContext.prepareInitHook(e);
757
+ this.privateContext.prepareInitHook(e, this.configuration);
707
758
  return this.singletonInstance;
708
759
  }
709
760
  callHook(e, 'onInit', this.publicContext);
@@ -767,17 +818,17 @@ class Injectable {
767
818
  }
768
819
  }
769
820
 
770
- const callHook = (n, r, t)=>{
771
- return new Promise((e)=>{
772
- const i = n[r];
773
- if (i) {
774
- if (isAsync(i)) {
775
- i.bind(n)(t).then(()=>{
776
- e();
821
+ const callHook = (n, r, t, e)=>{
822
+ return new Promise((i)=>{
823
+ const m = n[r];
824
+ if (m) {
825
+ if (isAsync(m)) {
826
+ m.bind(n)(t, e).then(()=>{
827
+ i();
777
828
  });
778
829
  return;
779
830
  }
780
- e(i.bind(n)(t));
831
+ i(m.bind(n)(t, e));
781
832
  }
782
833
  });
783
834
  };
@@ -803,12 +854,16 @@ class Assembler extends EventManager {
803
854
  e
804
855
  ]);
805
856
  const n = t.require(s.identifier);
806
- const r = t.initCache.indexOf(n);
807
- t.initCache.splice(r, 1);
857
+ const r = t.initCache.find((e)=>e.instance === n);
858
+ if (!r) {
859
+ throw new Error('Root instance not found in assemblages cache.');
860
+ }
861
+ const o = t.initCache.indexOf(r);
862
+ t.initCache.splice(o, 1);
808
863
  for (const e of t.initCache){
809
- callHook(e, 'onInit', t.publicContext);
864
+ callHook(e.instance, 'onInit', t.publicContext, e.configuration);
810
865
  }
811
- callHook(n, 'onInit', t.publicContext);
866
+ callHook(n, 'onInit', t.publicContext, s.configuration);
812
867
  t.initCache.length = 0;
813
868
  return n;
814
869
  }
@@ -825,7 +880,7 @@ class Assembler extends EventManager {
825
880
  }
826
881
  const o = Injectable.of(i, this.privateContext, this.publicContext);
827
882
  this.injectables.set(o.identifier, o);
828
- callHook(o.concrete, 'onRegister', this.publicContext);
883
+ callHook(o.concrete, 'onRegister', this.publicContext, o.configuration);
829
884
  return o;
830
885
  }
831
886
  use(e, t) {
@@ -835,8 +890,11 @@ class Assembler extends EventManager {
835
890
  this.objects.set(e, t);
836
891
  return t;
837
892
  }
838
- prepareInitHook(e) {
839
- this.initCache.push(e);
893
+ prepareInitHook(e, t) {
894
+ this.initCache.push({
895
+ instance: e,
896
+ configuration: t
897
+ });
840
898
  return this.initCache;
841
899
  }
842
900
  has(e) {
@@ -910,4 +968,4 @@ class Assembler extends EventManager {
910
968
  class AbstractAssembler extends AbstractEventManager {
911
969
  }
912
970
 
913
- export { AbstractAssemblage, AbstractAssembler, AbstractEventManager, Assemblage, Assembler, Await, r$1 as Configuration, s$1 as Context, c$1 as Definition, e$2 as Dispose, EventManager, ReflectParamIndex, ReflectParamValue, Use, decorateAssemblage, getDecoratedParametersIndexes, isAssemblage };
971
+ export { AbstractAssemblage, AbstractAssembler, AbstractEventManager, Assemblage, AssemblageWrappedDecorator, Assembler, Await, r$1 as Configuration, s$1 as Context, c$1 as Definition, e$2 as Dispose, EventManager, ReflectParamIndex, ReflectParamValue, Use, createWrappedDecorator, decorateAssemblage, decorateUse, getDecoratedParametersIndexes, isAssemblage };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "assemblerjs",
3
3
  "description": "A general purpose and zero-dependency Dependency Injection library for node and browser.",
4
- "version": "0.5.5",
4
+ "version": "0.7.0",
5
5
  "author": "Benoît LAHOZ <info@benoitlahoz.io>",
6
6
  "bugs": "https://github.com/benoitlahoz/assemblerjs/issues",
7
7
  "devDependencies": {