assemblerjs 0.9.2 → 0.9.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/dist/index.d.ts +36 -3
- package/dist/index.js +1 -1
- package/dist/index.mjs +72 -17
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -56,10 +56,12 @@ export declare abstract class AbstractAssembler extends AbstractEventManager {
|
|
|
56
56
|
abstract register<T>(injection: Injection<T>, instance?: boolean): Injectable<T>;
|
|
57
57
|
abstract use<T>(identifier: string | symbol, object: T): T;
|
|
58
58
|
abstract prepareInitHook<T = AbstractAssemblage>(instance: T, configuration?: Record<string, any>): unknown[];
|
|
59
|
+
abstract addGlobal(key: string, value: any): void;
|
|
59
60
|
abstract has<T>(identifier: Identifier<T>): boolean;
|
|
60
61
|
abstract require<T>(identifier: Identifier<T> | string | symbol, configuration?: Record<string, any>): T;
|
|
61
62
|
abstract concrete<T>(identifier: Identifier<T>): Concrete<T> | undefined;
|
|
62
63
|
abstract tagged(...tags: string[]): any[];
|
|
64
|
+
abstract global(key: string): any | undefined;
|
|
63
65
|
abstract dispose(): void;
|
|
64
66
|
}
|
|
65
67
|
|
|
@@ -123,7 +125,7 @@ export declare interface AssemblageDefinition {
|
|
|
123
125
|
use?: InstanceInjection<unknown>[];
|
|
124
126
|
tags?: string | string[];
|
|
125
127
|
metadata?: Record<string, any>;
|
|
126
|
-
|
|
128
|
+
global?: Record<string, any>;
|
|
127
129
|
}
|
|
128
130
|
|
|
129
131
|
/**
|
|
@@ -139,6 +141,7 @@ export declare class Assembler extends EventManager implements AbstractAssembler
|
|
|
139
141
|
static build<T>(entry: Concrete<T>): T;
|
|
140
142
|
protected injectables: Map<Identifier<unknown>, Injectable<unknown>>;
|
|
141
143
|
protected objects: Map<string | symbol, unknown>;
|
|
144
|
+
protected globals: Map<string, any>;
|
|
142
145
|
private initCache;
|
|
143
146
|
/**
|
|
144
147
|
* Context passed to internal classes.
|
|
@@ -231,6 +234,14 @@ export declare class Assembler extends EventManager implements AbstractAssembler
|
|
|
231
234
|
* identifier is not marked as 'singleton', will resolve in a new instance.
|
|
232
235
|
*/
|
|
233
236
|
tagged(...tags: string[]): unknown[];
|
|
237
|
+
addGlobal(key: string, value: any): void;
|
|
238
|
+
/**
|
|
239
|
+
* Get a global value by key.
|
|
240
|
+
*
|
|
241
|
+
* @param { string } key The key to get global value.
|
|
242
|
+
* @returns { any | undefined } The global value or `undefined` if not set.
|
|
243
|
+
*/
|
|
244
|
+
global(key: string): any | undefined;
|
|
234
245
|
/**
|
|
235
246
|
* Size of the assembler: number of registered dependencies.
|
|
236
247
|
*/
|
|
@@ -246,6 +257,7 @@ export declare class Assembler extends EventManager implements AbstractAssembler
|
|
|
246
257
|
require: AbstractAssembler['require'];
|
|
247
258
|
concrete: AbstractAssembler['concrete'];
|
|
248
259
|
tagged: AbstractAssembler['tagged'];
|
|
260
|
+
global: AbstractAssembler['global'];
|
|
249
261
|
dispose: AssemblerDispose;
|
|
250
262
|
on: AbstractAssembler['on'];
|
|
251
263
|
once: AbstractAssembler['once'];
|
|
@@ -267,6 +279,7 @@ export declare class Assembler extends EventManager implements AbstractAssembler
|
|
|
267
279
|
register: AbstractAssembler['register'];
|
|
268
280
|
use: AbstractAssembler['use'];
|
|
269
281
|
prepareInitHook: AbstractAssembler['prepareInitHook'];
|
|
282
|
+
addGlobal: AbstractAssembler['addGlobal'];
|
|
270
283
|
emit: AbstractAssembler['emit'];
|
|
271
284
|
addChannels: AbstractAssembler['addChannels'];
|
|
272
285
|
removeChannels: AbstractAssembler['removeChannels'];
|
|
@@ -366,6 +379,11 @@ export declare class Assembler extends EventManager implements AbstractAssembler
|
|
|
366
379
|
*/
|
|
367
380
|
export declare const decorateAssemblage: <T>(target: Concrete<T>, definition?: AssemblageDefinition) => Concrete<T>;
|
|
368
381
|
|
|
382
|
+
/**
|
|
383
|
+
* Decorator as a wrapper function.
|
|
384
|
+
*/
|
|
385
|
+
export declare const decorateGlobal: (identifier: string | symbol, target: any, index: number) => void;
|
|
386
|
+
|
|
369
387
|
/**
|
|
370
388
|
* Decorator as a wrapper function.
|
|
371
389
|
*/
|
|
@@ -411,6 +429,12 @@ export declare class Assembler extends EventManager implements AbstractAssembler
|
|
|
411
429
|
|
|
412
430
|
export declare const getDecoratedParametersIndexes: <T>(target: Concrete<T>) => ParametersDecoratorsIndexes;
|
|
413
431
|
|
|
432
|
+
/**
|
|
433
|
+
* Injects an object passed with `string` or `symbol` identifier.
|
|
434
|
+
*/
|
|
435
|
+
declare const Global_2: (identifier: string | symbol) => ParameterDecorator;
|
|
436
|
+
export { Global_2 as Global }
|
|
437
|
+
|
|
414
438
|
/**
|
|
415
439
|
* An identifier can be an abstract or a concrete class.
|
|
416
440
|
*/
|
|
@@ -469,6 +493,12 @@ export declare class Assembler extends EventManager implements AbstractAssembler
|
|
|
469
493
|
* Tags passed in assemblage's definition.
|
|
470
494
|
*/
|
|
471
495
|
get tags(): string[];
|
|
496
|
+
/**
|
|
497
|
+
* Global injections passed in assemblage's definition.
|
|
498
|
+
* These injections are available in all assemblages and can be used
|
|
499
|
+
* to provide global services or utilities.
|
|
500
|
+
*/
|
|
501
|
+
get globals(): Record<string, any> | undefined;
|
|
472
502
|
/**
|
|
473
503
|
* Event channels passed in assemblage's definition.
|
|
474
504
|
*/
|
|
@@ -597,6 +627,7 @@ export declare class Assembler extends EventManager implements AbstractAssembler
|
|
|
597
627
|
Configuration: number[];
|
|
598
628
|
Dispose: number[];
|
|
599
629
|
Use: number[];
|
|
630
|
+
Global: number[];
|
|
600
631
|
}
|
|
601
632
|
|
|
602
633
|
/**
|
|
@@ -607,14 +638,16 @@ export declare class Assembler extends EventManager implements AbstractAssembler
|
|
|
607
638
|
Dispose = "assembler:dispose.param.index",
|
|
608
639
|
Definition = "assemblage:definition.param.index",
|
|
609
640
|
Configuration = "assemblage:configuration.param.index",
|
|
610
|
-
Use = "assemblage:use.param.index"
|
|
641
|
+
Use = "assemblage:use.param.index",
|
|
642
|
+
Global = "assemblage:global.param.index"
|
|
611
643
|
}
|
|
612
644
|
|
|
613
645
|
/**
|
|
614
646
|
* Internal `Reflect` parameters decorators' values.
|
|
615
647
|
*/
|
|
616
648
|
export declare enum ReflectParamValue {
|
|
617
|
-
UseIdentifier = "assemblage:use.param.value"
|
|
649
|
+
UseIdentifier = "assemblage:use.param.value",
|
|
650
|
+
GlobalIdentifier = "assemblage:global.param.value"
|
|
618
651
|
}
|
|
619
652
|
|
|
620
653
|
/**
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("@assemblerjs/core");const t="design:paramtypes";var n,s;(n||(n={})).IsAssemblage="is_assemblage",(s||(s={})).AssemblageDefinition="assemblage:definition.value";const i=(e,t,n)=>{Reflect.defineMetadata(`__${e}__`,t,n)},r=(e,t)=>Reflect.getOwnMetadata(`__${e}__`,t),o=e=>Reflect.getMetadata(t,e)||[],a=e=>r(n.IsAssemblage,e)||!1,c={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}},l=e=>{const t={...e};for(const n in t)if(!Object.keys(c).includes(n))throw new Error(`Property '${n}' is not a valid assemblage definition property.`);for(const n in c){const e=c[n].test,s=c[n].throw,i=c[n].transform;e(t[n])||s(),t[n]=i(t[n])}return t},h=e=>{if(!a(e))throw new Error(`Class '${e.name}' is not an assemblage.`);return r(s.AssemblageDefinition,e)},f=(e,t)=>h(t)[e],u=t=>e.pipe(e.conditionally({if:()=>e.isClass(t[0])&&e.isClass(t[1]),then:()=>({identifier:t[0],concrete:t[1],configuration:{}})}),e.conditionally({if:()=>e.isClass(t[0])&&e.isObject(t[1]),then:()=>({identifier:t[0],concrete:t[0],configuration:t[1]}),else:e=>e}))(),d=(e,t)=>{const r=l(t||{});return i(n.IsAssemblage,!0,e),i(s.AssemblageDefinition,r,e),e};class p{dispose(){e.clearInstance(this,p)}add(...t){const n=e=>this.collection[e.channel].push(e.listener),s=e.conditionally({if:()=>2===t.length,then:()=>({channel:t[0],listener:t[1]}),else:()=>{const e=t[0];return{channel:e[0],listener:e[1]}}}),i=e.conditionally({if:t=>!e.isDefined(this.collection[t.channel]),then:e=>{this.collection[e.channel]=[],n(e)},else:e=>{n(e)}});return e.pipe(s,i)(),this}remove(t,n){const s=e=>this.collection[t].splice(e,1),i=e.conditionally({if:()=>this.collection[t]&&0===this.collection[t].length,then:()=>delete this.collection[t]}),r=e.conditionally({if:()=>e.isDefined(n),then:()=>s(this.collection[t].indexOf(n)),else:()=>delete this.collection[t]}),o=e.conditionally({if:e=>this.has(e),then:e=>this.collection[e]});return e.pipe(o,r,i)(),this}has(...t){return e.isOfType("string")(t[0])?Object.keys(this.collection).includes(t[0]):!!e.isOfType("function")(t[0])&&Object.values(this.collection).flat().includes(t[0])}get(...t){return e.isOfType("string")(t[0])?this.collection[t[0]]:e.isOfType("function")(t[0])?Object.values(this.collection).flat().filter((e=>e===t[0])):[]}clear(){const t=e.forIn(this.collection),n=t=>e.forOf(this.collection[t])((e=>this.remove(t,e)));return t((e=>n(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}[Symbol.iterator](){let e=-1;const t=this.collection?Object.keys(this.collection):[];return{next:()=>({value:t[++e],done:!(e in t)})}}constructor(){this.collection={};return e.proxifyIterable(this,p)}}class g{}class x{dispose(){this.listeners.dispose(),this.channels.clear(),e.clearInstance(this,x)}addChannels(...t){return e.forOf(t)((e=>{const t=this.cleanChannel(e);if(this.channels.has(t))throw new Error(`Channel '${t}' already exists.`);this.channels.add(t)})),this}removeChannels(...t){return e.forOf(t)((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);return this.listeners.add(n,t),this}once(e,t){const n=this.cleanChannel(e);return this.onceListeners.add(n,t),this}off(e,t){const n=this.cleanChannel(e);return this.listeners.remove(n,t),this}emit(t,...n){const s=this.cleanChannel(t);if(this.channels.has(s)){const t=this.onceListeners.get("*")||[],i=this.listeners.get("*")||[],r=this.onceListeners.get(s)||[],o=this.listeners.get(s)||[],a=e.forOf(t),c=e.forOf(i),l=e.forOf(r),h=e.forOf(o);a((e=>{this.run(e,...n),this.onceListeners.remove("*",e)})),c((e=>{this.run(e,...n)})),l((e=>{this.run(e,...n),this.onceListeners.remove(s,e)})),h((e=>{this.run(e,...n)}))}return this}run(t,...n){if(e.isAsync(t)){return t(...n).then((()=>Promise.resolve()))}t(...n)}cleanChannel(t){return e.onlyAlphanumeric(t,"*",":",".","-","_")}constructor(...e){this.listeners=new p,this.onceListeners=new p,this.channels=new Set(["*"]),this.addChannels(...e)}}exports.ReflectParamValue=void 0,(exports.ReflectParamValue||(exports.ReflectParamValue={})).UseIdentifier="assemblage:use.param.value",exports.ReflectParamIndex=void 0,function(e){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"}(exports.ReflectParamIndex||(exports.ReflectParamIndex={}));const m=e=>()=>(t,n,s)=>{const o=r(e,t)||[];o.push(s),i(e,o,t)},b=m(exports.ReflectParamIndex.Context),C=m(exports.ReflectParamIndex.Configuration),v=m(exports.ReflectParamIndex.Definition),y=m(exports.ReflectParamIndex.Dispose),I=(e,t,n)=>{const s=r(exports.ReflectParamIndex.Use,t)||[];s.push(n),i(exports.ReflectParamIndex.Use,s,t);const o=r(exports.ReflectParamValue.UseIdentifier,t)||{};o[n]=e,i(exports.ReflectParamValue.UseIdentifier,o,t)},w=e=>{const t=(e=>r(exports.ReflectParamIndex.Context,e)||[])(e)||[],n=(e=>r(exports.ReflectParamIndex.Definition,e)||[])(e)||[],s=(e=>r(exports.ReflectParamIndex.Configuration,e)||[])(e)||[],i=(e=>r(exports.ReflectParamIndex.Dispose,e)||[])(e)||[],o=(e=>r(exports.ReflectParamIndex.Use,e)||[])(e)||[];return{Context:t,Definition:n,Configuration:s,Dispose:i,Use:o}},A=(e,n)=>o=>{const a=class extends o{constructor(...t){super(...t),e&&e.call(this,n)}};Object.defineProperty(a,"name",{value:o.name});const c=Reflect.getOwnMetadata(t,o)||[],l=w(o),h=[];for(let e=0;e<c.length;e++)if(l.Context.includes(e)){const t=r(exports.ReflectParamIndex.Context,o)||[];t.push(e),i(exports.ReflectParamIndex.Context,t,a)}else if(l.Definition.includes(e)){const t=r(exports.ReflectParamIndex.Definition,o)||[];t.push(e),i(exports.ReflectParamIndex.Definition,t,a)}else if(l.Configuration.includes(e)){const t=r(exports.ReflectParamIndex.Configuration,o)||[];t.push(e),i(exports.ReflectParamIndex.Configuration,t,a)}else if(l.Dispose.includes(e)){const t=r(exports.ReflectParamIndex.Dispose,o)||[];t.push(e),i(exports.ReflectParamIndex.Dispose,t,a),h.push(c[e])}else if(l.Use.includes(e)){const t=r(exports.ReflectParamValue.UseIdentifier,o);I(t[e],a,e)}else;return d(a,r(s.AssemblageDefinition,o))};class j{static of(e,t,n){return new j(e,t,n)}dispose(){this.singletonInstance&&(((e,t)=>{if(e.concrete.prototype instanceof x){const n=t;for(const t of e.events)n.off(t);n.removeChannels(...e.events),e.privateContext.removeChannels(...e.events)}else for(const n of e.events)e.privateContext.events.has(n)&&e.privateContext.removeChannels(n)})(this,this.singletonInstance),P(this.singletonInstance,"onDispose",this.publicContext,this.configuration),e.clearInstance(this.singletonInstance,this.concrete)),e.clearInstance(this,j)}build(e){if(this.singletonInstance)return this.singletonInstance;const t=(e=>{const t=[],n=o(e.concrete),s=w(e.concrete);let i=0;for(const o 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=r(exports.ReflectParamValue.UseIdentifier,e.concrete)[i];t.push(e.privateContext.require(n)),i++}else t.push(e.privateContext.require(o)),i++;return t})(this),n=new this.concrete(...t);if(((e,t)=>{if(e.concrete.prototype instanceof x){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)}))}else for(const n of e.events)e.privateContext.events.has(n)||e.privateContext.addChannels(n)})(this,n),this.isSingleton)return this.singletonInstance=n,this.privateContext.prepareInitHook(n,this.configuration),this.singletonInstance;let s={};return this.configuration&&(s=this.configuration),e&&(s={...s,...e}),P(n,"onInit",this.publicContext,s),n}get dependencies(){return this.dependenciesIds}get definition(){return h(this.concrete)||{}}get isSingleton(){return f("singleton",this.concrete)}get singleton(){return this.singletonInstance}get injections(){return f("inject",this.concrete)||[]}get objects(){return f("use",this.concrete)||[]}get tags(){return f("tags",this.concrete)||[]}get events(){return f("events",this.concrete)||[]}constructor(t,n,s){if(this.privateContext=n,this.publicContext=s,this.dependenciesIds=[],this.identifier=t.identifier,this.concrete=t.concrete,this.configuration=t.configuration,!a(this.concrete))throw new Error(`Class '${this.concrete.name}' is not an Assemblage.`);const i=e.forOf(this.injections),r=e.forOf(this.objects);i((e=>this.privateContext.register(e))),r((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=o(e),s=w(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),t.instance?this.singletonInstance=t.instance:this.isSingleton}}const P=(t,n,s,i)=>new Promise((r=>{const o=t[n];if(o){if(e.isAsync(o))return void o.bind(t)(s,i).then((()=>{r()}));r(o.bind(t)(s,i))}}));class R extends x{static build(e){const t=new R;((e,t,n)=>{const r=h(n);r[e]=t;const o=l(r);i(s.AssemblageDefinition,o,n)})("singleton",!0,e);const n=t.register([e]),r=t.require(n.identifier),o=t.initCache.find((e=>e.instance===r));if(!o)throw new Error("Root instance not found in assemblages cache.");const a=t.initCache.indexOf(o);t.initCache.splice(a,1);for(const s of t.initCache)P(s.instance,"onInit",t.publicContext,s.configuration);P(r,"onInit",t.publicContext,n.configuration);for(const s of t.initCache.reverse())P(s.instance,"onInited",t.publicContext,s.configuration);return P(r,"onInited",t.publicContext,n.configuration),t.initCache.length=0,r}dispose(){for(const[e,t]of this.injectables)t.dispose();e.clearInstance(this,R)}register(t,n=!1){const s=!0===n?(e=>({identifier:e[0],concrete:e[0],instance:e[1],configuration:{}}))(t):(t=>e.switchCase({1:()=>(e=>({identifier:e[0],concrete:e[0],configuration:{}}))(t),2:()=>u(t),3:()=>(e=>({identifier:e[0],concrete:e[1],configuration:e[2]}))(t)},(()=>{throw new Error("Injection tuple must be of length 1, 2 or 3.")}))(t.length))(t);if(this.has(s.identifier))throw new Error(`An assemblage is already registered with identifier '${s.identifier.name}'.`);const i=j.of(s,this.privateContext,this.publicContext);return this.injectables.set(i.identifier,i),P(i.concrete,"onRegister",this.publicContext,i.configuration),i}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,t){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(t)}}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(),this.injectables=new Map,this.objects=new Map,this.initCache=[],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 g{},exports.AbstractEventManager=g,exports.AbstractListenerCollection=class{},exports.Assemblage=e=>t=>d(t,e),exports.Assembler=R,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=C,exports.ConstructorDecorator=A,exports.Context=b,exports.Definition=v,exports.Dispose=y,exports.EventManager=x,exports.ListenerCollection=p,exports.Use=e=>(t,n,s)=>{I(e,t,s)},exports.createConstructorDecorator=e=>t=>A(e,t),exports.decorateAssemblage=d,exports.decorateUse=I,exports.getAssemblageDefinition=e=>r(s.AssemblageDefinition,e),exports.getDecoratedParametersIndexes=w,exports.isAssemblage=a;
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("@assemblerjs/core");const t="design:paramtypes";var n,s;(n||(n={})).IsAssemblage="is_assemblage",(s||(s={})).AssemblageDefinition="assemblage:definition.value";const i=(e,t,n)=>{Reflect.defineMetadata(`__${e}__`,t,n)},o=(e,t)=>Reflect.getOwnMetadata(`__${e}__`,t),r=e=>Reflect.getMetadata(t,e)||[],a=e=>o(n.IsAssemblage,e)||!1,c={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},global:{test:e=>("object"==typeof e||void 0===e)&&!Array.isArray(e),throw:()=>{throw new Error("'global' property must be of type 'object' or 'undefined'.")},transform:e=>e}},l=e=>{const t={...e};for(const n in t)if(!Object.keys(c).includes(n))throw new Error(`Property '${n}' is not a valid assemblage definition property.`);for(const n in c){const e=c[n].test,s=c[n].throw,i=c[n].transform;e(t[n])||s(),t[n]=i(t[n])}return t},h=e=>{if(!a(e))throw new Error(`Class '${e.name}' is not an assemblage.`);return o(s.AssemblageDefinition,e)},f=(e,t)=>h(t)[e],d=t=>e.pipe(e.conditionally({if:()=>e.isClass(t[0])&&e.isClass(t[1]),then:()=>({identifier:t[0],concrete:t[1],configuration:{}})}),e.conditionally({if:()=>e.isClass(t[0])&&e.isObject(t[1]),then:()=>({identifier:t[0],concrete:t[0],configuration:t[1]}),else:e=>e}))(),p=(e,t)=>{const o=l(t||{});return i(n.IsAssemblage,!0,e),i(s.AssemblageDefinition,o,e),e};class u{dispose(){e.clearInstance(this,u)}add(...t){const n=e=>this.collection[e.channel].push(e.listener),s=e.conditionally({if:()=>2===t.length,then:()=>({channel:t[0],listener:t[1]}),else:()=>{const e=t[0];return{channel:e[0],listener:e[1]}}}),i=e.conditionally({if:t=>!e.isDefined(this.collection[t.channel]),then:e=>{this.collection[e.channel]=[],n(e)},else:e=>{n(e)}});return e.pipe(s,i)(),this}remove(t,n){const s=e=>this.collection[t].splice(e,1),i=e.conditionally({if:()=>this.collection[t]&&0===this.collection[t].length,then:()=>delete this.collection[t]}),o=e.conditionally({if:()=>e.isDefined(n),then:()=>s(this.collection[t].indexOf(n)),else:()=>delete this.collection[t]}),r=e.conditionally({if:e=>this.has(e),then:e=>this.collection[e]});return e.pipe(r,o,i)(),this}has(...t){return e.isOfType("string")(t[0])?Object.keys(this.collection).includes(t[0]):!!e.isOfType("function")(t[0])&&Object.values(this.collection).flat().includes(t[0])}get(...t){return e.isOfType("string")(t[0])?this.collection[t[0]]:e.isOfType("function")(t[0])?Object.values(this.collection).flat().filter((e=>e===t[0])):[]}clear(){const t=e.forIn(this.collection),n=t=>e.forOf(this.collection[t])((e=>this.remove(t,e)));return t((e=>n(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}[Symbol.iterator](){let e=-1;const t=this.collection?Object.keys(this.collection):[];return{next:()=>({value:t[++e],done:!(e in t)})}}constructor(){this.collection={};return e.proxifyIterable(this,u)}}class g{}class b{dispose(){this.listeners.dispose(),this.channels.clear(),e.clearInstance(this,b)}addChannels(...t){return e.forOf(t)((e=>{const t=this.cleanChannel(e);if(this.channels.has(t))throw new Error(`Channel '${t}' already exists.`);this.channels.add(t)})),this}removeChannels(...t){return e.forOf(t)((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);return this.listeners.add(n,t),this}once(e,t){const n=this.cleanChannel(e);return this.onceListeners.add(n,t),this}off(e,t){const n=this.cleanChannel(e);return this.listeners.remove(n,t),this}emit(t,...n){const s=this.cleanChannel(t);if(this.channels.has(s)){const t=this.onceListeners.get("*")||[],i=this.listeners.get("*")||[],o=this.onceListeners.get(s)||[],r=this.listeners.get(s)||[],a=e.forOf(t),c=e.forOf(i),l=e.forOf(o),h=e.forOf(r);a((e=>{this.run(e,...n),this.onceListeners.remove("*",e)})),c((e=>{this.run(e,...n)})),l((e=>{this.run(e,...n),this.onceListeners.remove(s,e)})),h((e=>{this.run(e,...n)}))}return this}run(t,...n){if(e.isAsync(t)){return t(...n).then((()=>Promise.resolve()))}t(...n)}cleanChannel(t){return e.onlyAlphanumeric(t,"*",":",".","-","_")}constructor(...e){this.listeners=new u,this.onceListeners=new u,this.channels=new Set(["*"]),this.addChannels(...e)}}exports.ReflectParamValue=void 0,function(e){e.UseIdentifier="assemblage:use.param.value",e.GlobalIdentifier="assemblage:global.param.value"}(exports.ReflectParamValue||(exports.ReflectParamValue={})),exports.ReflectParamIndex=void 0,function(e){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.Global="assemblage:global.param.index"}(exports.ReflectParamIndex||(exports.ReflectParamIndex={}));const x=e=>()=>(t,n,s)=>{const r=o(e,t)||[];r.push(s),i(e,r,t)},m=x(exports.ReflectParamIndex.Context),C=x(exports.ReflectParamIndex.Configuration),y=x(exports.ReflectParamIndex.Definition),v=x(exports.ReflectParamIndex.Dispose),I=(e,t,n)=>{const s=o(exports.ReflectParamIndex.Use,t)||[];s.push(n),i(exports.ReflectParamIndex.Use,s,t);const r=o(exports.ReflectParamValue.UseIdentifier,t)||{};r[n]=e,i(exports.ReflectParamValue.UseIdentifier,r,t)},w=(e,t,n)=>{const s=o(exports.ReflectParamIndex.Global,t)||[];s.push(n),i(exports.ReflectParamIndex.Global,s,t);const r=o(exports.ReflectParamValue.GlobalIdentifier,t)||{};r[n]=e,i(exports.ReflectParamValue.GlobalIdentifier,r,t)},A=e=>{const t=(e=>o(exports.ReflectParamIndex.Context,e)||[])(e)||[],n=(e=>o(exports.ReflectParamIndex.Definition,e)||[])(e)||[],s=(e=>o(exports.ReflectParamIndex.Configuration,e)||[])(e)||[],i=(e=>o(exports.ReflectParamIndex.Dispose,e)||[])(e)||[],r=(e=>o(exports.ReflectParamIndex.Use,e)||[])(e)||[],a=(e=>o(exports.ReflectParamIndex.Global,e)||[])(e)||[];return{Context:t,Definition:n,Configuration:s,Dispose:i,Use:r,Global:a}},P=(e,n)=>r=>{const a=class extends r{constructor(...t){super(...t),e&&e.call(this,n)}};Object.defineProperty(a,"name",{value:r.name});const c=Reflect.getOwnMetadata(t,r)||[],l=A(r),h=[];for(let e=0;e<c.length;e++)if(l.Context.includes(e)){const t=o(exports.ReflectParamIndex.Context,r)||[];t.push(e),i(exports.ReflectParamIndex.Context,t,a)}else if(l.Definition.includes(e)){const t=o(exports.ReflectParamIndex.Definition,r)||[];t.push(e),i(exports.ReflectParamIndex.Definition,t,a)}else if(l.Configuration.includes(e)){const t=o(exports.ReflectParamIndex.Configuration,r)||[];t.push(e),i(exports.ReflectParamIndex.Configuration,t,a)}else if(l.Dispose.includes(e)){const t=o(exports.ReflectParamIndex.Dispose,r)||[];t.push(e),i(exports.ReflectParamIndex.Dispose,t,a),h.push(c[e])}else if(l.Use.includes(e)){const t=o(exports.ReflectParamValue.UseIdentifier,r);I(t[e],a,e)}else;return p(a,o(s.AssemblageDefinition,r))};class R{static of(e,t,n){return new R(e,t,n)}dispose(){this.singletonInstance&&(((e,t)=>{if(e.concrete.prototype instanceof b){const n=t;for(const t of e.events)n.off(t);n.removeChannels(...e.events),e.privateContext.removeChannels(...e.events)}else for(const n of e.events)e.privateContext.events.has(n)&&e.privateContext.removeChannels(n)})(this,this.singletonInstance),j(this.singletonInstance,"onDispose",this.publicContext,this.configuration),e.clearInstance(this.singletonInstance,this.concrete)),e.clearInstance(this,R)}build(e){if(this.singletonInstance)return this.singletonInstance;const t=(e=>{const t=[],n=r(e.concrete),s=A(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=o(exports.ReflectParamValue.UseIdentifier,e.concrete)[i];t.push(e.privateContext.require(n)),i++}else if(s.Global.includes(i)){const n=o(exports.ReflectParamValue.GlobalIdentifier,e.concrete)[i];t.push(e.privateContext.global(n)),i++}else t.push(e.privateContext.require(r)),i++;return t})(this),n=new this.concrete(...t);if(((e,t)=>{if(e.concrete.prototype instanceof b){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)}))}else for(const n of e.events)e.privateContext.events.has(n)||e.privateContext.addChannels(n)})(this,n),this.isSingleton)return this.singletonInstance=n,this.privateContext.prepareInitHook(n,this.configuration),this.singletonInstance;let s={};return this.configuration&&(s=this.configuration),e&&(s={...s,...e}),j(n,"onInit",this.publicContext,s),n}get dependencies(){return this.dependenciesIds}get definition(){return h(this.concrete)||{}}get isSingleton(){return f("singleton",this.concrete)}get singleton(){return this.singletonInstance}get injections(){return f("inject",this.concrete)||[]}get objects(){return f("use",this.concrete)||[]}get tags(){return f("tags",this.concrete)||[]}get globals(){return f("global",this.concrete)}get events(){return f("events",this.concrete)||[]}constructor(t,n,s){if(this.privateContext=n,this.publicContext=s,this.dependenciesIds=[],this.identifier=t.identifier,this.concrete=t.concrete,this.configuration=t.configuration,!a(this.concrete))throw new Error(`Class '${this.concrete.name}' is not an Assemblage.`);const i=e.forOf(this.injections),o=e.forOf(this.objects);if(i((e=>this.privateContext.register(e))),o((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=r(e),s=A(e);let i=0;for(const o of n)s.Context.includes(i)||s.Configuration.includes(i)||s.Definition.includes(i)||s.Dispose.includes(i)||s.Use.includes(i)||s.Global.includes(i)||t.push(o),i++;return t})(this.concrete),this.globals)for(const e in this.globals)this.privateContext.addGlobal(e,this.globals[e]);t.instance?this.singletonInstance=t.instance:this.isSingleton}}const j=(t,n,s,i)=>new Promise((o=>{const r=t[n];if(r){if(e.isAsync(r))return void r.bind(t)(s,i).then((()=>{o()}));o(r.bind(t)(s,i))}}));class D extends b{static build(e){const t=new D;((e,t,n)=>{const o=h(n);o[e]=t;const r=l(o);i(s.AssemblageDefinition,r,n)})("singleton",!0,e);const n=t.register([e]),o=t.require(n.identifier),r=t.initCache.find((e=>e.instance===o));if(!r)throw new Error("Root instance not found in assemblages cache.");const a=t.initCache.indexOf(r);t.initCache.splice(a,1);for(const s of t.initCache)j(s.instance,"onInit",t.publicContext,s.configuration);j(o,"onInit",t.publicContext,n.configuration);for(const s of t.initCache.reverse())j(s.instance,"onInited",t.publicContext,s.configuration);return j(o,"onInited",t.publicContext,n.configuration),t.initCache.length=0,o}dispose(){for(const[e,t]of this.injectables)t.dispose();e.clearInstance(this,D)}register(t,n=!1){const s=!0===n?(e=>({identifier:e[0],concrete:e[0],instance:e[1],configuration:{}}))(t):(t=>e.switchCase({1:()=>(e=>({identifier:e[0],concrete:e[0],configuration:{}}))(t),2:()=>d(t),3:()=>(e=>({identifier:e[0],concrete:e[1],configuration:e[2]}))(t)},(()=>{throw new Error("Injection tuple must be of length 1, 2 or 3.")}))(t.length))(t);if(this.has(s.identifier))throw new Error(`An assemblage is already registered with identifier '${s.identifier.name}'.`);const i=R.of(s,this.privateContext,this.publicContext);return this.injectables.set(i.identifier,i),j(i.concrete,"onRegister",this.publicContext,i.configuration),i}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,t){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(t)}}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}addGlobal(e,t){if(this.globals.has(e))throw new Error(`Global value with key '${e}' has already been registered.`);this.globals.set(e,t)}global(e){return this.globals.get(e)}get size(){return this.injectables.size}constructor(){super(),this.injectables=new Map,this.objects=new Map,this.globals=new Map,this.initCache=[],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),global:this.global.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),addGlobal:this.addGlobal.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 g{},exports.AbstractEventManager=g,exports.AbstractListenerCollection=class{},exports.Assemblage=e=>t=>p(t,e),exports.Assembler=D,exports.Await=(e,t=25)=>(n,s,i)=>{const o=i.value;i.value=async function(){return new Promise((n=>{if(this[e])o.apply(this),n();else{const s=setInterval((()=>{this[e]&&(clearInterval(s),o.apply(this),n())}),t)}}))}},exports.Configuration=C,exports.ConstructorDecorator=P,exports.Context=m,exports.Definition=y,exports.Dispose=v,exports.EventManager=b,exports.Global=e=>(t,n,s)=>{w(e,t,s)},exports.ListenerCollection=u,exports.Use=e=>(t,n,s)=>{I(e,t,s)},exports.createConstructorDecorator=e=>t=>P(e,t),exports.decorateAssemblage=p,exports.decorateGlobal=w,exports.decorateUse=I,exports.getAssemblageDefinition=e=>o(s.AssemblageDefinition,e),exports.getDecoratedParametersIndexes=A,exports.isAssemblage=a;
|
package/dist/index.mjs
CHANGED
|
@@ -79,6 +79,13 @@ const n = {
|
|
|
79
79
|
throw new Error(`'metadata' property must be of type 'object' or 'undefined'.`);
|
|
80
80
|
},
|
|
81
81
|
transform: (r)=>r
|
|
82
|
+
},
|
|
83
|
+
global: {
|
|
84
|
+
test: (r)=>(typeof r === 'object' || typeof r === 'undefined') && !Array.isArray(r),
|
|
85
|
+
throw: ()=>{
|
|
86
|
+
throw new Error(`'global' property must be of type 'object' or 'undefined'.`);
|
|
87
|
+
},
|
|
88
|
+
transform: (r)=>r
|
|
82
89
|
}
|
|
83
90
|
};
|
|
84
91
|
const validateDefinition = (r)=>{
|
|
@@ -119,7 +126,7 @@ const setDefinitionValue = (e, o, n)=>{
|
|
|
119
126
|
return i;
|
|
120
127
|
};
|
|
121
128
|
|
|
122
|
-
const i$
|
|
129
|
+
const i$2 = (e)=>{
|
|
123
130
|
return {
|
|
124
131
|
identifier: e[0],
|
|
125
132
|
concrete: e[0],
|
|
@@ -159,7 +166,7 @@ const s$2 = (e)=>{
|
|
|
159
166
|
};
|
|
160
167
|
};
|
|
161
168
|
const resolveInjectionTuple = (e)=>switchCase({
|
|
162
|
-
1: ()=>i$
|
|
169
|
+
1: ()=>i$2(e),
|
|
163
170
|
2: ()=>c$2(e),
|
|
164
171
|
3: ()=>s$2(e)
|
|
165
172
|
}, ()=>{
|
|
@@ -449,6 +456,7 @@ const Await = (t, e = 25)=>{
|
|
|
449
456
|
var ReflectParamValue;
|
|
450
457
|
(function(e) {
|
|
451
458
|
e["UseIdentifier"] = "assemblage:use.param.value";
|
|
459
|
+
e["GlobalIdentifier"] = "assemblage:global.param.value";
|
|
452
460
|
})(ReflectParamValue || (ReflectParamValue = {}));
|
|
453
461
|
var ReflectParamIndex;
|
|
454
462
|
(function(e) {
|
|
@@ -457,19 +465,20 @@ var ReflectParamIndex;
|
|
|
457
465
|
e["Definition"] = "assemblage:definition.param.index";
|
|
458
466
|
e["Configuration"] = "assemblage:configuration.param.index";
|
|
459
467
|
e["Use"] = "assemblage:use.param.index";
|
|
468
|
+
e["Global"] = "assemblage:global.param.index";
|
|
460
469
|
})(ReflectParamIndex || (ReflectParamIndex = {}));
|
|
461
470
|
|
|
462
|
-
const i = (t)=>()=>{
|
|
471
|
+
const i$1 = (t)=>()=>{
|
|
463
472
|
return (i, s, r)=>{
|
|
464
473
|
const c = getOwnCustomMetadata(t, i) || [];
|
|
465
474
|
c.push(r);
|
|
466
475
|
defineCustomMetadata(t, c, i);
|
|
467
476
|
};
|
|
468
477
|
};
|
|
469
|
-
const s$1 = i(ReflectParamIndex.Context);
|
|
470
|
-
const r$1 = i(ReflectParamIndex.Configuration);
|
|
471
|
-
const c$1 = i(ReflectParamIndex.Definition);
|
|
472
|
-
const e$1 = i(ReflectParamIndex.Dispose);
|
|
478
|
+
const s$1 = i$1(ReflectParamIndex.Context);
|
|
479
|
+
const r$1 = i$1(ReflectParamIndex.Configuration);
|
|
480
|
+
const c$1 = i$1(ReflectParamIndex.Definition);
|
|
481
|
+
const e$1 = i$1(ReflectParamIndex.Dispose);
|
|
473
482
|
|
|
474
483
|
const Use = (e)=>{
|
|
475
484
|
return (o, t, s)=>{
|
|
@@ -485,33 +494,52 @@ const decorateUse = (r, n, c)=>{
|
|
|
485
494
|
defineCustomMetadata(ReflectParamValue.UseIdentifier, i, n);
|
|
486
495
|
};
|
|
487
496
|
|
|
497
|
+
const Global = (o)=>{
|
|
498
|
+
return (t, l, r)=>{
|
|
499
|
+
decorateGlobal(o, t, r);
|
|
500
|
+
};
|
|
501
|
+
};
|
|
502
|
+
const decorateGlobal = (e, a, n)=>{
|
|
503
|
+
const c = getOwnCustomMetadata(ReflectParamIndex.Global, a) || [];
|
|
504
|
+
c.push(n);
|
|
505
|
+
defineCustomMetadata(ReflectParamIndex.Global, c, a);
|
|
506
|
+
const b = getOwnCustomMetadata(ReflectParamValue.GlobalIdentifier, a) || {};
|
|
507
|
+
b[n] = e;
|
|
508
|
+
defineCustomMetadata(ReflectParamValue.GlobalIdentifier, b, a);
|
|
509
|
+
};
|
|
510
|
+
|
|
488
511
|
const o = (o)=>{
|
|
489
512
|
return getOwnCustomMetadata(ReflectParamIndex.Context, o) || [];
|
|
490
513
|
};
|
|
491
514
|
const r = (o)=>{
|
|
492
515
|
return getOwnCustomMetadata(ReflectParamIndex.Configuration, o) || [];
|
|
493
516
|
};
|
|
494
|
-
const
|
|
517
|
+
const s = (o)=>{
|
|
495
518
|
return getOwnCustomMetadata(ReflectParamIndex.Definition, o) || [];
|
|
496
519
|
};
|
|
497
|
-
const
|
|
520
|
+
const e = (o)=>{
|
|
498
521
|
return getOwnCustomMetadata(ReflectParamIndex.Dispose, o) || [];
|
|
499
522
|
};
|
|
500
523
|
const c = (o)=>{
|
|
501
524
|
return getOwnCustomMetadata(ReflectParamIndex.Use, o) || [];
|
|
502
525
|
};
|
|
526
|
+
const i = (o)=>{
|
|
527
|
+
return getOwnCustomMetadata(ReflectParamIndex.Global, o) || [];
|
|
528
|
+
};
|
|
503
529
|
const getDecoratedParametersIndexes = (t)=>{
|
|
504
530
|
const n = o(t) || [];
|
|
505
|
-
const i = e(t) || [];
|
|
506
|
-
const m = r(t) || [];
|
|
507
531
|
const u = s(t) || [];
|
|
508
|
-
const
|
|
532
|
+
const m = r(t) || [];
|
|
533
|
+
const a = e(t) || [];
|
|
534
|
+
const f = c(t) || [];
|
|
535
|
+
const p = i(t) || [];
|
|
509
536
|
return {
|
|
510
537
|
Context: n,
|
|
511
|
-
Definition:
|
|
538
|
+
Definition: u,
|
|
512
539
|
Configuration: m,
|
|
513
|
-
Dispose:
|
|
514
|
-
Use:
|
|
540
|
+
Dispose: a,
|
|
541
|
+
Use: f,
|
|
542
|
+
Global: p
|
|
515
543
|
};
|
|
516
544
|
};
|
|
517
545
|
|
|
@@ -599,6 +627,13 @@ const resolveInjectableParameters = (i)=>{
|
|
|
599
627
|
u++;
|
|
600
628
|
continue;
|
|
601
629
|
}
|
|
630
|
+
if (r.Global.includes(u)) {
|
|
631
|
+
const n = getOwnCustomMetadata(ReflectParamValue.GlobalIdentifier, i.concrete);
|
|
632
|
+
const t = n[u];
|
|
633
|
+
s.push(i.privateContext.global(t));
|
|
634
|
+
u++;
|
|
635
|
+
continue;
|
|
636
|
+
}
|
|
602
637
|
s.push(i.privateContext.require(n));
|
|
603
638
|
u++;
|
|
604
639
|
}
|
|
@@ -610,7 +645,7 @@ const resolveDependencies = (e)=>{
|
|
|
610
645
|
const s = getDecoratedParametersIndexes(e);
|
|
611
646
|
let c = 0;
|
|
612
647
|
for (const e of i){
|
|
613
|
-
if (s.Context.includes(c) || s.Configuration.includes(c) || s.Definition.includes(c) || s.Dispose.includes(c) || s.Use.includes(c)) {
|
|
648
|
+
if (s.Context.includes(c) || s.Configuration.includes(c) || s.Definition.includes(c) || s.Dispose.includes(c) || s.Use.includes(c) || s.Global.includes(c)) {
|
|
614
649
|
c++;
|
|
615
650
|
continue;
|
|
616
651
|
}
|
|
@@ -676,6 +711,9 @@ class Injectable {
|
|
|
676
711
|
get tags() {
|
|
677
712
|
return getDefinitionValue('tags', this.concrete) || [];
|
|
678
713
|
}
|
|
714
|
+
get globals() {
|
|
715
|
+
return getDefinitionValue('global', this.concrete);
|
|
716
|
+
}
|
|
679
717
|
get events() {
|
|
680
718
|
return getDefinitionValue('events', this.concrete) || [];
|
|
681
719
|
}
|
|
@@ -700,6 +738,11 @@ class Injectable {
|
|
|
700
738
|
}
|
|
701
739
|
});
|
|
702
740
|
this.dependenciesIds = resolveDependencies(this.concrete);
|
|
741
|
+
if (this.globals) {
|
|
742
|
+
for(const t in this.globals){
|
|
743
|
+
this.privateContext.addGlobal(t, this.globals[t]);
|
|
744
|
+
}
|
|
745
|
+
}
|
|
703
746
|
if (t.instance) {
|
|
704
747
|
this.singletonInstance = t.instance;
|
|
705
748
|
} else if (this.isSingleton) ;
|
|
@@ -817,6 +860,15 @@ class Assembler extends EventManager {
|
|
|
817
860
|
}
|
|
818
861
|
return t;
|
|
819
862
|
}
|
|
863
|
+
addGlobal(e, t) {
|
|
864
|
+
if (this.globals.has(e)) {
|
|
865
|
+
throw new Error(`Global value with key '${e}' has already been registered.`);
|
|
866
|
+
}
|
|
867
|
+
this.globals.set(e, t);
|
|
868
|
+
}
|
|
869
|
+
global(e) {
|
|
870
|
+
return this.globals.get(e);
|
|
871
|
+
}
|
|
820
872
|
get size() {
|
|
821
873
|
return this.injectables.size;
|
|
822
874
|
}
|
|
@@ -824,6 +876,7 @@ class Assembler extends EventManager {
|
|
|
824
876
|
super();
|
|
825
877
|
this.injectables = new Map();
|
|
826
878
|
this.objects = new Map();
|
|
879
|
+
this.globals = new Map();
|
|
827
880
|
this.initCache = [];
|
|
828
881
|
this.publicContext = {
|
|
829
882
|
has: this.has.bind(this),
|
|
@@ -831,6 +884,7 @@ class Assembler extends EventManager {
|
|
|
831
884
|
concrete: this.concrete.bind(this),
|
|
832
885
|
tagged: this.tagged.bind(this),
|
|
833
886
|
dispose: this.dispose.bind(this),
|
|
887
|
+
global: this.global.bind(this),
|
|
834
888
|
on: this.on.bind(this),
|
|
835
889
|
once: this.once.bind(this),
|
|
836
890
|
off: this.off.bind(this),
|
|
@@ -840,6 +894,7 @@ class Assembler extends EventManager {
|
|
|
840
894
|
...this.publicContext,
|
|
841
895
|
register: this.register.bind(this),
|
|
842
896
|
use: this.use.bind(this),
|
|
897
|
+
addGlobal: this.addGlobal.bind(this),
|
|
843
898
|
prepareInitHook: this.prepareInitHook.bind(this),
|
|
844
899
|
emit: this.emit.bind(this),
|
|
845
900
|
addChannels: this.addChannels.bind(this),
|
|
@@ -851,4 +906,4 @@ class Assembler extends EventManager {
|
|
|
851
906
|
class AbstractAssembler extends AbstractEventManager {
|
|
852
907
|
}
|
|
853
908
|
|
|
854
|
-
export { AbstractAssemblage, AbstractAssembler, AbstractEventManager, AbstractListenerCollection, Assemblage, Assembler, Await, r$1 as Configuration, ConstructorDecorator, s$1 as Context, c$1 as Definition, e$1 as Dispose, EventManager, ListenerCollection, ReflectParamIndex, ReflectParamValue, Use, createConstructorDecorator, decorateAssemblage, decorateUse, getAssemblageDefinition, getDecoratedParametersIndexes, isAssemblage };
|
|
909
|
+
export { AbstractAssemblage, AbstractAssembler, AbstractEventManager, AbstractListenerCollection, Assemblage, Assembler, Await, r$1 as Configuration, ConstructorDecorator, s$1 as Context, c$1 as Definition, e$1 as Dispose, EventManager, Global, ListenerCollection, ReflectParamIndex, ReflectParamValue, Use, createConstructorDecorator, decorateAssemblage, decorateGlobal, decorateUse, getAssemblageDefinition, getDecoratedParametersIndexes, isAssemblage };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "assemblerjs",
|
|
3
3
|
"description": "A general purpose Dependency Injection library for node and browser.",
|
|
4
|
-
"version": "0.9.
|
|
4
|
+
"version": "0.9.4",
|
|
5
5
|
"author": "Benoît LAHOZ <info@benoitlahoz.io>",
|
|
6
6
|
"bugs": "https://github.com/benoitlahoz/assemblerjs/issues",
|
|
7
7
|
"homepage": "https://github.com/benoitlahoz/assemblerjs#README",
|
|
@@ -50,6 +50,6 @@
|
|
|
50
50
|
"coverage": "vitest run --coverage --passWithNoTests && istanbul-badges-readme"
|
|
51
51
|
},
|
|
52
52
|
"dependencies": {
|
|
53
|
-
"@assemblerjs/core": "
|
|
53
|
+
"@assemblerjs/core": "0.9.1"
|
|
54
54
|
}
|
|
55
55
|
}
|