assemblerjs 0.5.2 → 0.5.4

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.8%25-brightgreen.svg?style=flat) ![Branches](https://img.shields.io/badge/branches-82.4%25-yellow.svg?style=flat) ![Functions](https://img.shields.io/badge/functions-86.48%25-yellow.svg?style=flat) ![Lines](https://img.shields.io/badge/lines-90.41%25-brightgreen.svg?style=flat)
5
+ ![Statements](https://img.shields.io/badge/statements-90.9%25-brightgreen.svg?style=flat) ![Branches](https://img.shields.io/badge/branches-82.27%25-yellow.svg?style=flat) ![Functions](https://img.shields.io/badge/functions-86.55%25-yellow.svg?style=flat) ![Lines](https://img.shields.io/badge/lines-90.53%25-brightgreen.svg?style=flat)
6
6
 
7
7
  ---
8
8
 
package/dist/index.d.ts CHANGED
@@ -220,6 +220,7 @@ export declare class Assembler extends EventManager implements AbstractAssembler
220
220
  has: AbstractAssembler['has'];
221
221
  require: AbstractAssembler['require'];
222
222
  tagged: AbstractAssembler['tagged'];
223
+ dispose: AssemblerDispose;
223
224
  on: AbstractAssembler['on'];
224
225
  once: AbstractAssembler['once'];
225
226
  off: AbstractAssembler['off'];
@@ -239,7 +240,6 @@ export declare class Assembler extends EventManager implements AbstractAssembler
239
240
  declare interface AssemblerPrivateContext extends AssemblerContext {
240
241
  register: AbstractAssembler['register'];
241
242
  use: AbstractAssembler['use'];
242
- dispose: AssemblerDispose;
243
243
  prepareInitHook: AbstractAssembler['prepareInitHook'];
244
244
  emit: AbstractAssembler['emit'];
245
245
  addChannels: AbstractAssembler['addChannels'];
@@ -311,6 +311,8 @@ export declare class Assembler extends EventManager implements AbstractAssembler
311
311
  */
312
312
  export declare const Context: () => ParameterDecorator;
313
313
 
314
+ export declare const decorateAssemblage: <T>(target: Concrete<T>, definition?: AssemblageDefinition) => Concrete<T>;
315
+
314
316
  /**
315
317
  * Injects the assemblage's definition object.
316
318
  */
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)},$=D(E.Context),S=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)}}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()}}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),tagged:this.tagged.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),dispose:this.dispose.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=S,exports.Context=$,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.getDecoratedParametersIndexes=_,exports.isAssemblage=b;
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()}}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),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;
package/dist/index.mjs CHANGED
@@ -257,14 +257,20 @@ const resolveInstanceInjectionTuple = (e)=>{
257
257
  };
258
258
  };
259
259
 
260
- const Assemblage = (m)=>{
261
- const n = m ? validateDefinition(m) : validateDefinition({});
262
- return (t)=>{
263
- defineCustomMetadata(ReflectFlags.IsAssemblage, true, t);
264
- defineCustomMetadata(ReflectValue.AssemblageDefinition, n, t);
265
- return t;
260
+ const Assemblage = (r)=>{
261
+ const n = r ? validateDefinition(r) : validateDefinition({});
262
+ return (o)=>{
263
+ defineCustomMetadata(ReflectFlags.IsAssemblage, true, o);
264
+ defineCustomMetadata(ReflectValue.AssemblageDefinition, n, o);
265
+ return o;
266
266
  };
267
267
  };
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;
273
+ };
268
274
 
269
275
  function e$4(e, t, n) {
270
276
  if (t in e) {
@@ -757,7 +763,7 @@ class Injectable {
757
763
  this.dependenciesIds = resolveDependencies(this.concrete);
758
764
  if (e.instance) {
759
765
  this.singletonInstance = e.instance;
760
- }
766
+ } else if (this.isSingleton) ;
761
767
  }
762
768
  }
763
769
 
@@ -877,6 +883,7 @@ class Assembler extends EventManager {
877
883
  has: this.has.bind(this),
878
884
  require: this.require.bind(this),
879
885
  tagged: this.tagged.bind(this),
886
+ dispose: this.dispose.bind(this),
880
887
  on: this.on.bind(this),
881
888
  once: this.once.bind(this),
882
889
  off: this.off.bind(this),
@@ -889,8 +896,7 @@ class Assembler extends EventManager {
889
896
  prepareInitHook: this.prepareInitHook.bind(this),
890
897
  emit: this.emit.bind(this),
891
898
  addChannels: this.addChannels.bind(this),
892
- removeChannels: this.removeChannels.bind(this),
893
- dispose: this.dispose.bind(this)
899
+ removeChannels: this.removeChannels.bind(this)
894
900
  };
895
901
  }
896
902
  }
@@ -898,4 +904,4 @@ class Assembler extends EventManager {
898
904
  class AbstractAssembler extends AbstractEventManager {
899
905
  }
900
906
 
901
- 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, getDecoratedParametersIndexes, isAssemblage };
907
+ 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 };
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.2",
4
+ "version": "0.5.4",
5
5
  "author": "Benoît LAHOZ <info@benoitlahoz.io>",
6
6
  "bugs": "https://github.com/benoitlahoz/assemblerjs/issues",
7
7
  "devDependencies": {