@adimm/x-injection 0.6.4 → 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
@@ -356,12 +356,6 @@ const CatModule = new ProviderModule({
356
356
 
357
357
  Hopefully the provided example shows how powerful the `dynamicExports` property it is, but, there are some things to keep in mind in order to avoid nasty bugs!
358
358
 
359
- 1. You **must always** return only the providers/modules declared into the static `exports` array.
360
- 2. You **can** return _less_ providers/modules as long as they are still part of the static `exports` array.
361
- 3. You **cannot** return _more_ providers/modules than the static `exports` array!
362
-
363
- > When point number `1` and point number `3` are triggered, the library will throw a descriptive error which should help to find the culprit module.
364
-
365
359
  ## Advanced Usage
366
360
 
367
361
  Each `ProviderModule` instance implements the `IProviderModule` interface for simplicity, but can be cast to `IProviderModuleNaked` for advanced operations:
package/dist/index.cjs CHANGED
@@ -11,36 +11,35 @@ var e, i = Object.defineProperty, t = Object.getOwnPropertyDescriptor, n = Objec
11
11
  enumerable: !0
12
12
  });
13
13
  })(s, {
14
- AppModule: () => Q,
14
+ AppModule: () => K,
15
15
  GLOBAL_APP_MODULE_ID: () => d,
16
- GlobalAppModule: () => K,
17
- GlobalContainer: () => $,
16
+ GlobalAppModule: () => J,
17
+ GlobalContainer: () => L,
18
18
  Inject: () => E,
19
- InjectFromBase: () => w,
20
- Injectable: () => I,
21
- InjectionDynamicExportsOutOfRange: () => V,
19
+ InjectFromBase: () => k,
20
+ Injectable: () => b,
22
21
  InjectionError: () => q,
23
22
  InjectionProviderModuleDisposedError: () => U,
24
23
  InjectionProviderModuleError: () => N,
25
- InjectionProviderModuleGlobalMarkError: () => L,
26
- InjectionProviderModuleMissingIdentifierError: () => W,
24
+ InjectionProviderModuleGlobalMarkError: () => W,
25
+ InjectionProviderModuleMissingIdentifierError: () => V,
27
26
  InjectionScope: () => c,
28
27
  MultiInject: () => M,
29
- Named: () => B,
28
+ Named: () => w,
30
29
  Optional: () => D,
31
30
  PostConstruct: () => A,
32
- PreDestroy: () => x,
33
- ProviderModule: () => Y,
31
+ PreDestroy: () => R,
32
+ ProviderModule: () => z,
34
33
  ProviderModuleHelpers: () => p,
35
34
  ProviderTokenHelpers: () => h,
36
- Tagged: () => O,
35
+ Tagged: () => G,
37
36
  Unmanaged: () => F,
38
37
  bindingScopeToInjectionScope: () => l,
39
38
  injectionScopeToBindingScope: () => u,
40
39
  isClass: () => v,
41
40
  isClassOrFunction: () => g,
42
- isFunction: () => m,
43
- isPlainObject: () => y
41
+ isFunction: () => S,
42
+ isPlainObject: () => m
44
43
  }), module.exports = (e = s, ((e, o, s, d) => {
45
44
  if (o && "object" == typeof o || "function" == typeof o) for (let a of n(o)) r.call(e, a) || a === s || i(e, a, {
46
45
  get: () => o[a],
@@ -94,36 +93,36 @@ function g(e) {
94
93
  return "function" == typeof e;
95
94
  }
96
95
 
97
- function m(e) {
96
+ function S(e) {
98
97
  return "function" == typeof e && !Function.prototype.toString.call(e).startsWith("class ");
99
98
  }
100
99
 
101
- function S(e) {
100
+ function y(e) {
102
101
  return "[object Object]" === Object.prototype.toString.call(e);
103
102
  }
104
103
 
105
- function y(e) {
106
- if (!1 === S(e)) return !1;
104
+ function m(e) {
105
+ if (!1 === y(e)) return !1;
107
106
  const i = e.constructor;
108
107
  if (void 0 === i) return !0;
109
108
  const t = i.prototype;
110
- return !1 !== S(t) && !1 !== t.hasOwnProperty("isPrototypeOf");
109
+ return !1 !== y(t) && !1 !== t.hasOwnProperty("isPrototypeOf");
111
110
  }
112
111
 
113
- function I(e) {
112
+ function b(e) {
114
113
  return void 0 === e ? (0, a.injectable)() : (0, a.injectable)(u(e));
115
114
  }
116
115
 
117
- o(v, "isClass"), o(g, "isClassOrFunction"), o(m, "isFunction"), o(S, "isObject"),
118
- o(y, "isPlainObject"), function(e) {
116
+ o(v, "isClass"), o(g, "isClassOrFunction"), o(S, "isFunction"), o(y, "isObject"),
117
+ o(m, "isPlainObject"), function(e) {
119
118
  function i(e) {
120
- return S(e) && "useClass" in e;
119
+ return y(e) && "useClass" in e;
121
120
  }
122
121
  function t(e) {
123
- return S(e) && "useValue" in e;
122
+ return y(e) && "useValue" in e;
124
123
  }
125
124
  function n(e) {
126
- return S(e) && "useFactory" in e;
125
+ return y(e) && "useFactory" in e;
127
126
  }
128
127
  function r(e) {
129
128
  return "string" == typeof e || "symbol" == typeof e || g(e);
@@ -139,7 +138,7 @@ o(y, "isPlainObject"), function(e) {
139
138
  ...i,
140
139
  provide: i?.provide ?? e,
141
140
  useClass: i?.useClass ?? e
142
- } : m(e) ? {
141
+ } : S(e) ? {
143
142
  ...i,
144
143
  provide: i?.provide ?? e,
145
144
  useFactory: i?.useFactory ?? e
@@ -152,7 +151,7 @@ o(y, "isPlainObject"), function(e) {
152
151
  return h(e) ?? p(e) ?? i;
153
152
  }
154
153
  function u(e) {
155
- if (S(e)) return e;
154
+ if (y(e)) return e;
156
155
  }
157
156
  function h(e) {
158
157
  const i = u(e);
@@ -164,8 +163,8 @@ o(y, "isPlainObject"), function(e) {
164
163
  const t = (0, f.getClassMetadata)(i)?.scope;
165
164
  return t ? l(t) : void 0;
166
165
  }
167
- function S(e) {
168
- return y(e) && "object" == typeof e && "provide" in e;
166
+ function y(e) {
167
+ return m(e) && "object" == typeof e && "provide" in e;
169
168
  }
170
169
  o(i, "isClassToken"), e.isClassToken = i, o(t, "isValueToken"), e.isValueToken = t,
171
170
  o(n, "isFactoryToken"), e.isFactoryToken = n, o(r, "isProviderIdentifier"), e.isProviderIdentifier = r,
@@ -173,7 +172,7 @@ o(y, "isPlainObject"), function(e) {
173
172
  e.toServiceIdentifiers = d, o(a, "toDependencyProviderWithOptions"), e.toDependencyProviderWithOptions = a,
174
173
  o(c, "getInjectionScopeByPriority"), e.getInjectionScopeByPriority = c, o(u, "tryGetProviderOptions"),
175
174
  e.tryGetProviderOptions = u, o(h, "tryGetScopeFromProvider"), e.tryGetScopeFromProvider = h,
176
- o(p, "tryGetDecoratorScopeFromClass"), e.tryGetDecoratorScopeFromClass = p, o(S, "hasProvideProperty");
175
+ o(p, "tryGetDecoratorScopeFromClass"), e.tryGetDecoratorScopeFromClass = p, o(y, "hasProvideProperty");
177
176
  }(h || (h = {})), function(e) {
178
177
  function i(e) {
179
178
  return e;
@@ -183,12 +182,12 @@ o(y, "isPlainObject"), function(e) {
183
182
  }
184
183
  o(i, "buildInternalConstructorParams"), e.buildInternalConstructorParams = i, o(t, "isDynamicExport"),
185
184
  e.isDynamicExport = t;
186
- }(p || (p = {})), o(I, "Injectable");
185
+ }(p || (p = {})), o(b, "Injectable");
187
186
 
188
- var b = require("inversify");
187
+ var I = require("inversify");
189
188
 
190
189
  function E(e) {
191
- return (0, b.inject)(h.toServiceIdentifier(e));
190
+ return (0, I.inject)(h.toServiceIdentifier(e));
192
191
  }
193
192
 
194
193
  o(E, "Inject");
@@ -203,24 +202,24 @@ o(M, "MultiInject");
203
202
 
204
203
  var P = require("inversify");
205
204
 
206
- function w(e) {
205
+ function k(e) {
207
206
  return (0, P.injectFromBase)(e);
208
207
  }
209
208
 
210
- o(w, "InjectFromBase");
209
+ o(k, "InjectFromBase");
211
210
 
212
211
  var _ = require("inversify");
213
212
 
214
- function B(e) {
213
+ function w(e) {
215
214
  return (0, _.named)(e);
216
215
  }
217
216
 
218
- o(B, "Named");
217
+ o(w, "Named");
219
218
 
220
- var k = require("inversify");
219
+ var B = require("inversify");
221
220
 
222
221
  function D() {
223
- return (0, k.optional)();
222
+ return (0, B.optional)();
224
223
  }
225
224
 
226
225
  o(D, "Optional");
@@ -235,24 +234,24 @@ o(A, "PostConstruct");
235
234
 
236
235
  var C = require("inversify");
237
236
 
238
- function x() {
237
+ function R() {
239
238
  return (0, C.preDestroy)();
240
239
  }
241
240
 
242
- o(x, "PreDestroy");
241
+ o(R, "PreDestroy");
243
242
 
244
- var R = require("inversify");
243
+ var x = require("inversify");
245
244
 
246
- function O(e, i) {
247
- return (0, R.tagged)(e, i);
245
+ function G(e, i) {
246
+ return (0, x.tagged)(e, i);
248
247
  }
249
248
 
250
- o(O, "Tagged");
249
+ o(G, "Tagged");
251
250
 
252
- var G = require("inversify");
251
+ var O = require("inversify");
253
252
 
254
253
  function F() {
255
- return (0, G.unmanaged)();
254
+ return (0, O.unmanaged)();
256
255
  }
257
256
 
258
257
  o(F, "Unmanaged");
@@ -279,14 +278,6 @@ var q = class e extends Error {
279
278
  super(e, "Has been disposed! You can re-initialize it by using the `_lazyInit` method.");
280
279
  }
281
280
  }, V = class e extends N {
282
- static {
283
- o(this, "InjectionDynamicExportsOutOfRange");
284
- }
285
- name=e.name;
286
- constructor(e) {
287
- super(e, `The 'ProviderModule.${e.toString()}' is trying to dynamically export providers/modules out of the declared range of the static exports!`);
288
- }
289
- }, W = class e extends N {
290
281
  static {
291
282
  o(this, "InjectionProviderModuleMissingIdentifierError");
292
283
  }
@@ -294,7 +285,7 @@ var q = class e extends Error {
294
285
  constructor(e) {
295
286
  super(e, "An `identifier` must be supplied!");
296
287
  }
297
- }, L = class e extends N {
288
+ }, W = class e extends N {
298
289
  static {
299
290
  o(this, "InjectionProviderModuleGlobalMarkError");
300
291
  }
@@ -302,9 +293,9 @@ var q = class e extends Error {
302
293
  constructor(e, i) {
303
294
  super(e, i);
304
295
  }
305
- }, $ = new (require("inversify").Container)({
296
+ }, L = new (require("inversify").Container)({
306
297
  defaultScope: "Singleton"
307
- }), z = require("inversify"), H = class {
298
+ }), H = require("inversify"), $ = class {
308
299
  static {
309
300
  o(this, "ProviderModuleUtils");
310
301
  }
@@ -313,7 +304,7 @@ var q = class e extends Error {
313
304
  }
314
305
  module;
315
306
  moduleNaked;
316
- appModule=Q;
307
+ appModule=K;
317
308
  constructor(e, i) {
318
309
  this.module = e, this.moduleNaked = e.toNaked(), this.appModule = i.appModule?.() ?? this.appModule,
319
310
  this.checkIfShouldBeAddedToTheGlobalRegister();
@@ -322,7 +313,7 @@ var q = class e extends Error {
322
313
  return h.isProviderIdentifier(e) ? this.bindSelfTokenToContainer(e, i) : h.isClassToken(e) ? this.bindClassTokenToContainer(e, i) : h.isValueToken(e) ? this.bindValueTokenToContainer(e) : !!h.isFactoryToken(e) && this.bindFactoryTokenToContainer(e, i);
323
314
  }
324
315
  checkIfShouldBeAddedToTheGlobalRegister() {
325
- !this.moduleNaked.isAppModule && this.module.isMarkedAsGlobal && J.add(this.module);
316
+ !this.moduleNaked.isAppModule && this.module.isMarkedAsGlobal && Y.add(this.module);
326
317
  }
327
318
  bindSelfTokenToContainer(e, i) {
328
319
  return this.setBindingScope(e, this.container.bind(h.toServiceIdentifier(e)).toSelf(), i),
@@ -363,7 +354,7 @@ var q = class e extends Error {
363
354
  const t = e;
364
355
  t.onEvent?.activation && i.onActivation(t.onEvent.activation), t.onEvent?.deactivation && i.onDeactivation(t.onEvent.deactivation);
365
356
  }
366
- }, Y = class e {
357
+ }, z = class e {
367
358
  static {
368
359
  o(this, "ProviderModule");
369
360
  }
@@ -405,7 +396,7 @@ var q = class e extends Error {
405
396
  }
406
397
  getMany(...e) {
407
398
  return e.map((e => {
408
- const i = y(e) && "provider" in e;
399
+ const i = m(e) && "provider" in e;
409
400
  return this.get(i ? e.provider : e, !!i && e.isOptional);
410
401
  }));
411
402
  }
@@ -419,8 +410,8 @@ var q = class e extends Error {
419
410
  return this;
420
411
  }
421
412
  clone(i) {
422
- let t = [ ...this.providers ];
423
- return i?.providersMap && (t = t.map((e => i.providersMap(e, this)))), new e(p.buildInternalConstructorParams({
413
+ const t = i;
414
+ return new e(p.buildInternalConstructorParams({
424
415
  isAppModule: this.isAppModule,
425
416
  markAsGlobal: this.isMarkedAsGlobal,
426
417
  identifier: this.identifier,
@@ -428,31 +419,36 @@ var q = class e extends Error {
428
419
  dynamicExports: this.dynamicExports,
429
420
  onReady: this.onReady,
430
421
  onDispose: this.onDispose,
431
- importedProvidersMap: i?.importedProvidersMap,
422
+ importedProvidersMap: this.importedProvidersMap,
432
423
  imports: [ ...this.imports ],
433
- providers: t,
434
- exports: [ ...this.exports ]
424
+ providers: [ ...this.providers ],
425
+ exports: [ ...this.exports ],
426
+ ...t
435
427
  }));
436
428
  }
429
+ async dispose() {
430
+ await (this.onDispose?.(this)), await this.__unbindAll(), this.container = null,
431
+ this.imports = null, this.providers = null, this.importedProviders = null, this.exports = null,
432
+ this.dynamicExports = null, this.registeredBindingSideEffects = null, this.isDisposed = !0;
433
+ }
437
434
  toString() {
438
435
  return this.identifier?.description ?? "Unknown";
439
436
  }
440
437
  setIdentifier(e) {
441
- if (!e) throw new W(this);
438
+ if (!e) throw new V(this);
442
439
  return e;
443
440
  }
444
441
  prepareContainer(e) {
445
- return this.isAppModule ? e.container?.() ?? $ : e.container ? (console.warn(`[xInjection]: The '${this.toString()}' module is using a dynamic container!`),
446
- e.container()) : new z.Container({
447
- parent: $,
442
+ return this.isAppModule ? e.container?.() ?? L : e.container ? (console.warn(`[xInjection]: The '${this.toString()}' module is using a dynamic container!`),
443
+ e.container()) : new H.Container({
444
+ parent: L,
448
445
  defaultScope: this.defaultScope.inversify
449
446
  });
450
447
  }
451
448
  injectImportedModules(i) {
452
449
  i && 0 !== i.length && i.forEach((i => {
453
450
  if ("GlobalAppModule" === i.toString()) throw new N(this, "The 'GlobalAppModule' can't be imported!");
454
- const t = i._getExportableModulesAndProviders(), n = i.dynamicExports?.(this, t);
455
- void 0 !== n && this.shouldThrowWhenModuleDynamicExportsDontMatchTheStaticExports(i, t, n),
451
+ const t = (i = "function" == typeof i ? i() : i)._getExportableModulesAndProviders(), n = i.dynamicExports?.(this, t);
456
452
  (n ?? t).forEach((t => {
457
453
  if (t instanceof e) {
458
454
  const e = t.toNaked();
@@ -488,17 +484,9 @@ var q = class e extends Error {
488
484
  this.registeredBindingSideEffects && ("all" !== e ? this.registeredBindingSideEffects.has(e) && (this.registeredBindingSideEffects.get(e)?.onUnbindEffects.forEach((e => e())),
489
485
  this.registeredBindingSideEffects.delete(e)) : this.registeredBindingSideEffects.forEach((({onUnbindEffects: e}) => e.forEach((e => e())))));
490
486
  }
491
- shouldThrowWhenModuleDynamicExportsDontMatchTheStaticExports(e, i, t) {
492
- if (t.length > i.length || t.some((e => !i.includes(e)))) throw new V(e);
493
- }
494
487
  shouldThrowIfDisposed() {
495
488
  if (null === this.container) throw new U(this);
496
489
  }
497
- async _dispose() {
498
- await (this.onDispose?.(this)), await this.__unbindAll(), this.container = null,
499
- this.imports = null, this.providers = null, this.importedProviders = null, this.exports = null,
500
- this.dynamicExports = null, this.registeredBindingSideEffects = null, this.isDisposed = !0;
501
- }
502
490
  _lazyInit({markAsGlobal: e, imports: i = [], providers: t = [], exports: n = [], defaultScope: r = c.Singleton, dynamicExports: o, onReady: s, onDispose: d, ...a}) {
503
491
  return this.isMarkedAsGlobal = e ?? !1, this.isDisposed = !1, this.imports = i,
504
492
  this.providers = t, this.importedProviders = a.importedProviders ?? new Map, this.exports = n,
@@ -508,7 +496,7 @@ var q = class e extends Error {
508
496
  }, this.dynamicExports = o, this.onReady = s, this.onDispose = d, this.importedProvidersMap = a.importedProvidersMap ?? (e => e),
509
497
  this.container = this.prepareContainer({
510
498
  ...a
511
- }), this.moduleUtils = new H(this, a), this.registeredBindingSideEffects = new Map,
499
+ }), this.moduleUtils = new $(this, a), this.registeredBindingSideEffects = new Map,
512
500
  this.injectImportedModules(this._getImportedModules()), this.injectProviders(),
513
501
  this.onReady?.(this), this;
514
502
  }
@@ -583,7 +571,7 @@ var q = class e extends Error {
583
571
  async __unbindAll() {
584
572
  this.shouldThrowIfDisposed(), await this.container.unbindAll(), this.removeRegisteredBindingSideEffects("all");
585
573
  }
586
- }, J = new Set, K = class extends Y {
574
+ }, Y = new Set, J = class extends z {
587
575
  static {
588
576
  o(this, "GlobalAppModule");
589
577
  }
@@ -597,21 +585,23 @@ var q = class e extends Error {
597
585
  }
598
586
  register(e) {
599
587
  if (this.isLoaded) throw new q(`The '${this.toString()}' has already been registered!`);
600
- return this.nakedModule._lazyInit(e), this.checkIfImportsHaveGlobalMark(), this.isLoaded = !0,
601
- this;
588
+ return this.nakedModule._lazyInit(e), this.checkIfRegisteredModulesHaveGlobalMark(this.toNaked(), this.imports),
589
+ this.isLoaded = !0, this;
602
590
  }
603
591
  toNaked() {
604
592
  return super.toNaked();
605
593
  }
606
- async _dispose() {
607
- this.isLoaded = !1, super._dispose();
608
- }
609
- checkIfImportsHaveGlobalMark() {
610
- this.imports.forEach((e => {
611
- if (!e.isMarkedAsGlobal) throw new L(e, "Is not marked as `global` but has been imported into the `AppModule`!");
612
- J.delete(e);
613
- })), J.forEach((e => {
614
- throw new L(e, "Is marked as 'global' and has not been imported into the 'AppModule'!");
594
+ async dispose() {
595
+ this.isLoaded = !1, super.dispose();
596
+ }
597
+ checkIfRegisteredModulesHaveGlobalMark(e, i, t = !1) {
598
+ i.forEach((i => {
599
+ if (i instanceof z) {
600
+ if (i.isMarkedAsGlobal) return Y.delete(i), void (i.exports && this.checkIfRegisteredModulesHaveGlobalMark(i, i.exports, !0));
601
+ throw new W(i, t ? `Is not marked as \`global\` but has been imported into the \`AppModule\`via the \`${e.toString()}\` module!` : "Is not marked as `global` but has been imported into the `AppModule`!");
602
+ }
603
+ })), t || Y.forEach((e => {
604
+ throw new W(e, "Is marked as 'global' and has not been imported into the 'AppModule'!");
615
605
  }));
616
606
  }
617
- }, Q = new K;//# sourceMappingURL=index.cjs.map
607
+ }, K = new J;//# sourceMappingURL=index.cjs.map