@adimm/x-injection 0.3.2 → 0.5.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/dist/index.js CHANGED
@@ -1,47 +1,55 @@
1
1
  var e = Object.defineProperty, i = (i, t) => e(i, "name", {
2
2
  value: t,
3
3
  configurable: !0
4
- }), t = "GlobalAppModule", n = "AnonymousModule";
4
+ }), t = "GlobalAppModule";
5
5
 
6
- import { injectable as o } from "inversify";
6
+ import { injectable as n } from "inversify";
7
7
 
8
- var r, s, d = function(e) {
8
+ var r, o, s = function(e) {
9
9
  return e[e.Singleton = 0] = "Singleton", e[e.Transient = 1] = "Transient", e[e.Request = 2] = "Request",
10
10
  e;
11
11
  }({});
12
12
 
13
- function c(e) {
13
+ function d(e) {
14
14
  switch (e) {
15
- case d.Singleton:
15
+ case s.Singleton:
16
16
  return "Singleton";
17
17
 
18
- case d.Transient:
18
+ case s.Transient:
19
19
  return "Transient";
20
20
 
21
- case d.Request:
21
+ case s.Request:
22
22
  return "Request";
23
23
  }
24
24
  }
25
25
 
26
- function a(e) {
26
+ function c(e) {
27
27
  switch (e) {
28
28
  case "Singleton":
29
- return d.Singleton;
29
+ return s.Singleton;
30
30
 
31
31
  case "Transient":
32
- return d.Transient;
32
+ return s.Transient;
33
33
 
34
34
  case "Request":
35
- return d.Request;
35
+ return s.Request;
36
36
  }
37
37
  }
38
38
 
39
- i(c, "injectionScopeToBindingScope"), i(a, "bindingScopeToInjectionScope");
39
+ i(d, "injectionScopeToBindingScope"), i(c, "bindingScopeToInjectionScope");
40
40
 
41
- import { getClassMetadata as u } from "@inversifyjs/core";
41
+ import { getClassMetadata as a } from "@inversifyjs/core";
42
+
43
+ function u(e) {
44
+ return "function" == typeof e && Function.prototype.toString.call(e).startsWith("class ");
45
+ }
42
46
 
43
47
  function h(e) {
44
- return "function" == typeof e && /^class\s/.test(Function.prototype.toString.call(e));
48
+ return "function" == typeof e;
49
+ }
50
+
51
+ function p(e) {
52
+ return "function" == typeof e && !Function.prototype.toString.call(e).startsWith("class ");
45
53
  }
46
54
 
47
55
  function f(e) {
@@ -56,59 +64,70 @@ function l(e) {
56
64
  return !1 !== f(t) && !1 !== t.hasOwnProperty("isPrototypeOf");
57
65
  }
58
66
 
59
- function p(e) {
60
- return "function" == typeof e;
67
+ function v(e) {
68
+ return void 0 === e ? n() : n(d(e));
61
69
  }
62
70
 
63
- function g(e) {
64
- return void 0 === e ? o() : o(c(e));
65
- }
66
-
67
- i(h, "isClass"), i(f, "isObject"), i(l, "isPlainObject"), function(e) {
71
+ i(u, "isClass"), i(h, "isClassOrFunction"), i(p, "isFunction"), i(f, "isObject"),
72
+ i(l, "isPlainObject"), function(e) {
68
73
  function t(e) {
69
- return v(e) && "useClass" in e;
74
+ return y(e) && "useClass" in e;
70
75
  }
71
76
  function n(e) {
72
- return v(e) && "useValue" in e;
73
- }
74
- function o(e) {
75
- return v(e) && "useFactory" in e;
77
+ return y(e) && "useValue" in e;
76
78
  }
77
79
  function r(e) {
78
- return "string" == typeof e || "symbol" == typeof e || h(e) || "function" == typeof e;
80
+ return y(e) && "useFactory" in e;
81
+ }
82
+ function o(e) {
83
+ return "string" == typeof e || "symbol" == typeof e || h(e);
79
84
  }
80
85
  function s(e) {
81
- return r(e) ? e : e.provide;
86
+ return o(e) ? e : e.provide;
82
87
  }
83
88
  function d(e) {
84
89
  return e.map((e => s(e)));
85
90
  }
86
- function c(e, i) {
87
- return p(e) ?? g(e) ?? i;
91
+ function f(e, i) {
92
+ return u(e) ? {
93
+ ...i,
94
+ provide: i?.provide ?? e,
95
+ useClass: i?.useClass ?? e
96
+ } : p(e) ? {
97
+ ...i,
98
+ provide: i?.provide ?? e,
99
+ useFactory: i?.useFactory ?? e
100
+ } : {
101
+ ...e,
102
+ ...i
103
+ };
104
+ }
105
+ function v(e, i) {
106
+ return m(e) ?? S(e) ?? i;
88
107
  }
89
- function f(e) {
90
- if (v(e)) return e;
108
+ function g(e) {
109
+ if (y(e)) return e;
91
110
  }
92
- function p(e) {
93
- const i = f(e);
111
+ function m(e) {
112
+ const i = g(e);
94
113
  if (i) return i.scope;
95
114
  }
96
- function g(e) {
115
+ function S(e) {
97
116
  const i = s(e);
98
- if (!h(i)) return;
99
- const t = u(i)?.scope;
100
- return t ? a(t) : void 0;
117
+ if (!u(i)) return;
118
+ const t = a(i)?.scope;
119
+ return t ? c(t) : void 0;
101
120
  }
102
- function v(e) {
121
+ function y(e) {
103
122
  return l(e) && "object" == typeof e && "provide" in e;
104
123
  }
105
124
  i(t, "isClassToken"), e.isClassToken = t, i(n, "isValueToken"), e.isValueToken = n,
106
- i(o, "isFactoryToken"), e.isFactoryToken = o, i(r, "isProviderIdentifier"), e.isProviderIdentifier = r,
125
+ i(r, "isFactoryToken"), e.isFactoryToken = r, i(o, "isProviderIdentifier"), e.isProviderIdentifier = o,
107
126
  i(s, "toServiceIdentifier"), e.toServiceIdentifier = s, i(d, "toServiceIdentifiers"),
108
- e.toServiceIdentifiers = d, i(c, "getInjectionScopeByPriority"), e.getInjectionScopeByPriority = c,
109
- i(f, "tryGetProviderOptions"), e.tryGetProviderOptions = f, i(p, "tryGetScopeFromProvider"),
110
- e.tryGetScopeFromProvider = p, i(g, "tryGetDecoratorScopeFromClass"), e.tryGetDecoratorScopeFromClass = g,
111
- i(v, "hasProvideProperty");
127
+ e.toServiceIdentifiers = d, i(f, "toDependencyProviderWithOptions"), e.toDependencyProviderWithOptions = f,
128
+ i(v, "getInjectionScopeByPriority"), e.getInjectionScopeByPriority = v, i(g, "tryGetProviderOptions"),
129
+ e.tryGetProviderOptions = g, i(m, "tryGetScopeFromProvider"), e.tryGetScopeFromProvider = m,
130
+ i(S, "tryGetDecoratorScopeFromClass"), e.tryGetDecoratorScopeFromClass = S, i(y, "hasProvideProperty");
112
131
  }(r || (r = {})), function(e) {
113
132
  function t(e) {
114
133
  return e;
@@ -118,79 +137,79 @@ i(h, "isClass"), i(f, "isObject"), i(l, "isPlainObject"), function(e) {
118
137
  }
119
138
  i(t, "buildInternalConstructorParams"), e.buildInternalConstructorParams = t, i(n, "isDynamicExport"),
120
139
  e.isDynamicExport = n;
121
- }(s || (s = {})), i(p, "isClassOrFunction"), i(g, "Injectable");
140
+ }(o || (o = {})), i(v, "Injectable");
122
141
 
123
- import { inject as v } from "inversify";
142
+ import { inject as g } from "inversify";
124
143
 
125
- function S(e) {
126
- return v(r.toServiceIdentifier(e));
144
+ function m(e) {
145
+ return g(r.toServiceIdentifier(e));
127
146
  }
128
147
 
129
- i(S, "Inject");
148
+ i(m, "Inject");
130
149
 
131
- import { multiInject as y } from "inversify";
150
+ import { multiInject as S } from "inversify";
132
151
 
133
- function m(e) {
134
- return y(r.toServiceIdentifier(e));
152
+ function y(e) {
153
+ return S(r.toServiceIdentifier(e));
135
154
  }
136
155
 
137
- i(m, "MultiInject");
156
+ i(y, "MultiInject");
138
157
 
139
158
  import { injectFromBase as I } from "inversify";
140
159
 
141
- function b(e) {
160
+ function E(e) {
142
161
  return I(e);
143
162
  }
144
163
 
145
- i(b, "InjectFromBase");
164
+ i(E, "InjectFromBase");
146
165
 
147
- import { named as E } from "inversify";
166
+ import { named as b } from "inversify";
148
167
 
149
168
  function T(e) {
150
- return E(e);
169
+ return b(e);
151
170
  }
152
171
 
153
172
  i(T, "Named");
154
173
 
155
174
  import { optional as _ } from "inversify";
156
175
 
157
- function B() {
176
+ function w() {
158
177
  return _();
159
178
  }
160
179
 
161
- i(B, "Optional");
180
+ i(w, "Optional");
162
181
 
163
- import { postConstruct as w } from "inversify";
182
+ import { postConstruct as B } from "inversify";
164
183
 
165
184
  function D() {
166
- return w();
185
+ return B();
167
186
  }
168
187
 
169
188
  i(D, "PostConstruct");
170
189
 
171
- import { preDestroy as C } from "inversify";
190
+ import { preDestroy as P } from "inversify";
172
191
 
173
- function k() {
174
- return C();
192
+ function M() {
193
+ return P();
175
194
  }
176
195
 
177
- i(k, "PreDestroy");
196
+ i(M, "PreDestroy");
178
197
 
179
- import { tagged as M } from "inversify";
198
+ import { tagged as C } from "inversify";
180
199
 
181
- function P(e, i) {
182
- return M(e, i);
200
+ function k(e, i) {
201
+ return C(e, i);
183
202
  }
184
203
 
185
- i(P, "Tagged");
204
+ i(k, "Tagged");
186
205
 
187
- import { unmanaged as j } from "inversify";
206
+ import { unmanaged as x } from "inversify";
188
207
 
189
- function x() {
190
- return j();
208
+ function j() {
209
+ return x();
191
210
  }
192
211
 
193
- i(x, "Unmanaged");
212
+ i(j, "Unmanaged");
194
213
 
195
214
  var A = class e extends Error {
196
215
  static {
@@ -205,7 +224,7 @@ var A = class e extends Error {
205
224
  constructor(e, i) {
206
225
  super(`{ProviderModule.${e.toString()}} => ${i}`);
207
226
  }
208
- }, O = class e extends R {
227
+ }, F = class e extends R {
209
228
  static {
210
229
  i(this, "XInjectionProviderModuleDisposedError");
211
230
  }
@@ -213,7 +232,7 @@ var A = class e extends Error {
213
232
  constructor(e) {
214
233
  super(e, "Has been disposed! You can re-initialize it by using the `_lazyInit` method.");
215
234
  }
216
- }, F = class e extends R {
235
+ }, O = class e extends R {
217
236
  static {
218
237
  i(this, "XInjectionDynamicExportsOutOfRange");
219
238
  }
@@ -221,17 +240,25 @@ var A = class e extends Error {
221
240
  constructor(e) {
222
241
  super(e, `The 'ProviderModule.${e.toString()}' is trying to dynamically export providers/modules out of the declared range of the static exports!`);
223
242
  }
243
+ }, N = class e extends R {
244
+ static {
245
+ i(this, "XInjectionProviderModuleMissingIdentifierError");
246
+ }
247
+ name=e.name;
248
+ constructor(e) {
249
+ super(e, "An `identifier` must be supplied!");
250
+ }
224
251
  };
225
252
 
226
- import { Container as N } from "inversify";
253
+ import { Container as U } from "inversify";
227
254
 
228
- var U = new N({
255
+ var G = new U({
229
256
  defaultScope: "Singleton"
230
257
  });
231
258
 
232
- import { Container as G } from "inversify";
259
+ import { Container as V } from "inversify";
233
260
 
234
- var V = class {
261
+ var W = class {
235
262
  static {
236
263
  i(this, "ProviderModuleUtils");
237
264
  }
@@ -246,9 +273,6 @@ var V = class {
246
273
  bindToContainer(e, i) {
247
274
  return r.isProviderIdentifier(e) ? this.bindSelfTokenToContainer(e, i) : r.isClassToken(e) ? this.bindClassTokenToContainer(e, i) : r.isValueToken(e) ? this.bindValueTokenToContainer(e) : !!r.isFactoryToken(e) && this.bindFactoryTokenToContainer(e, i);
248
275
  }
249
- bindManyToContainer(e, i) {
250
- e.forEach((e => this.bindToContainer(e, i)));
251
- }
252
276
  bindSelfTokenToContainer(e, i) {
253
277
  return this.setBindingScope(e, this.container.bind(r.toServiceIdentifier(e)).toSelf(), i),
254
278
  this.moduleNaked.__isCurrentBound(e);
@@ -268,13 +292,13 @@ var V = class {
268
292
  setBindingScope(e, i, t) {
269
293
  if (r.isValueToken(e)) return i;
270
294
  switch (r.getInjectionScopeByPriority(e, t)) {
271
- case d.Singleton:
295
+ case s.Singleton:
272
296
  return i.inSingletonScope();
273
297
 
274
- case d.Transient:
298
+ case s.Transient:
275
299
  return i.inTransientScope();
276
300
 
277
- case d.Request:
301
+ case s.Request:
278
302
  return i.inRequestScope();
279
303
  }
280
304
  }
@@ -300,22 +324,25 @@ var V = class {
300
324
  dynamicExports;
301
325
  onReady;
302
326
  onDispose;
327
+ importedProvidersMap;
303
328
  moduleUtils;
329
+ imports;
304
330
  providers;
331
+ importedProviders;
305
332
  exports;
306
- imports;
307
333
  registeredBindingSideEffects;
308
- constructor({identifier: e = Symbol(n), imports: i, providers: t, exports: o, defaultScope: r, dynamicExports: s, onReady: d, onDispose: c, ...a}) {
309
- const u = a;
310
- this.identifier = e, this.isAppModule = u.isAppModule ?? !1, this.isAppModule || this._lazyInit({
334
+ constructor({identifier: e, imports: i, providers: t, exports: n, defaultScope: r, dynamicExports: o, onReady: s, onDispose: d, ...c}) {
335
+ const a = c;
336
+ this.identifier = this.setIdentifier(e), this.isDisposed = a.isDisposed ?? !1, this.isAppModule = a.isAppModule ?? !1,
337
+ this.isAppModule || this._lazyInit({
311
338
  imports: i,
312
339
  providers: t,
313
- exports: o,
340
+ exports: n,
314
341
  defaultScope: r,
315
- dynamicExports: s,
316
- onReady: d,
317
- onDispose: c,
318
- ...a
342
+ dynamicExports: o,
343
+ onReady: s,
344
+ onDispose: d,
345
+ ...c
319
346
  });
320
347
  }
321
348
  get(e, i) {
@@ -330,21 +357,41 @@ var V = class {
330
357
  }));
331
358
  }
332
359
  onActivationEvent(e, i) {
333
- this.container.onActivation(r.toServiceIdentifier(e), i);
360
+ this.shouldThrowIfDisposed(), this.container.onActivation(r.toServiceIdentifier(e), i);
334
361
  }
335
362
  onDeactivationEvent(e, i) {
336
- this.container.onDeactivation(r.toServiceIdentifier(e), i);
363
+ this.shouldThrowIfDisposed(), this.container.onDeactivation(r.toServiceIdentifier(e), i);
337
364
  }
338
365
  toNaked() {
339
366
  return this;
340
367
  }
368
+ clone(i) {
369
+ let t = [ ...this.providers ];
370
+ return i?.providersMap && (t = t.map((e => i.providersMap(e, this, !1)))), new e(o.buildInternalConstructorParams({
371
+ isAppModule: this.isAppModule,
372
+ identifier: this.identifier,
373
+ isDisposed: this.isDisposed,
374
+ defaultScope: this.defaultScope.native,
375
+ dynamicExports: this.dynamicExports,
376
+ onReady: this.onReady,
377
+ onDispose: this.onDispose,
378
+ importedProvidersMap: i?.importedProvidersMap,
379
+ imports: [ ...this.imports ],
380
+ providers: t,
381
+ exports: [ ...this.exports ]
382
+ }));
383
+ }
341
384
  toString() {
342
- return this.identifier.description;
385
+ return this.identifier?.description ?? "Unknown";
386
+ }
387
+ setIdentifier(e) {
388
+ if (!e) throw new N(this);
389
+ return e;
343
390
  }
344
391
  prepareContainer(e) {
345
- return this.isAppModule ? e.container?.() ?? U : e.container ? (console.warn(`[xInjection]: The '${this.toString()}' module is using a dynamic container!`),
346
- e.container()) : new G({
347
- parent: U,
392
+ return this.isAppModule ? e.container?.() ?? G : e.container ? (console.warn(`[xInjection]: The '${this.toString()}' module is using a dynamic container!`),
393
+ e.container()) : new V({
394
+ parent: G,
348
395
  defaultScope: this.defaultScope.inversify
349
396
  });
350
397
  }
@@ -358,17 +405,18 @@ var V = class {
358
405
  const e = n.toNaked();
359
406
  return void this.injectImportedModules([ e ]);
360
407
  }
361
- const o = n;
362
- this.moduleUtils.bindToContainer({
408
+ const o = n, s = this.importedProvidersMap({
363
409
  scope: r.getInjectionScopeByPriority(o, t.defaultScope.native),
364
410
  provide: r.toServiceIdentifier(o),
365
411
  useFactory: i((() => t.get(o)), "useFactory")
366
- }, t.defaultScope.native);
412
+ }, o, t);
413
+ this.importedProviders.set(t, [ ...this.importedProviders.get(t) ?? [], s ]), this.moduleUtils.bindToContainer(s, t.defaultScope.native),
414
+ t._onUnbind(o, (() => this.__unbind(s)));
367
415
  }));
368
416
  }));
369
417
  }
370
418
  injectProviders() {
371
- this.moduleUtils.bindManyToContainer(this.providers, this.defaultScope.native);
419
+ this.providers.forEach((e => this.moduleUtils.bindToContainer(e, this.defaultScope.native)));
372
420
  }
373
421
  registerBindingSideEffect(e, i, t) {
374
422
  this.registeredBindingSideEffects.has(e) || this.registeredBindingSideEffects.set(e, {
@@ -388,25 +436,27 @@ var V = class {
388
436
  this.registeredBindingSideEffects.delete(e)) : this.registeredBindingSideEffects.forEach((({onUnbindEffects: e}) => e.forEach((e => e())))));
389
437
  }
390
438
  shouldThrowWhenModuleDynamicExportsDontMatchTheStaticExports(e, i, t) {
391
- if (t.length > i.length || t.some((e => !i.includes(e)))) throw new F(e);
439
+ if (t.length > i.length || t.some((e => !i.includes(e)))) throw new O(e);
392
440
  }
393
441
  shouldThrowIfDisposed() {
394
- if (null === this.container) throw new O(this);
442
+ if (null === this.container) throw new F(this);
395
443
  }
396
444
  async _dispose() {
397
445
  await (this.onDispose?.(this)), await this.__unbindAll(), this.container = null,
398
- this.imports = null, this.providers = null, this.exports = null, this.dynamicExports = null,
399
- this.registeredBindingSideEffects = null, this.isDisposed = !0;
446
+ this.imports = null, this.providers = null, this.importedProviders = null, this.exports = null,
447
+ this.dynamicExports = null, this.registeredBindingSideEffects = null, this.isDisposed = !0;
400
448
  }
401
- _lazyInit({imports: e = [], providers: i = [], exports: t = [], defaultScope: n = d.Singleton, dynamicExports: o, onReady: r, onDispose: s, ...a}) {
402
- this.isDisposed = !1, this.imports = e, this.providers = i, this.exports = t, this.defaultScope = {
449
+ _lazyInit({imports: e = [], providers: i = [], exports: t = [], defaultScope: n = s.Singleton, dynamicExports: r, onReady: o, onDispose: c, ...a}) {
450
+ return this.isDisposed = !1, this.imports = e, this.providers = i, this.importedProviders = a.importedProviders ?? new Map,
451
+ this.exports = t, this.defaultScope = {
403
452
  native: n,
404
- inversify: c(n)
405
- }, this.dynamicExports = o, this.onReady = r, this.onDispose = s, this.container = this.prepareContainer({
453
+ inversify: d(n)
454
+ }, this.dynamicExports = r, this.onReady = o, this.onDispose = c, this.importedProvidersMap = a.importedProvidersMap ?? (e => e),
455
+ this.container = this.prepareContainer({
406
456
  ...a
407
- }), this.moduleUtils = new V(this), this.registeredBindingSideEffects = new Map,
457
+ }), this.moduleUtils = new W(this), this.registeredBindingSideEffects = new Map,
408
458
  this.injectImportedModules(this._getImportedModules()), this.injectProviders(),
409
- this.onReady?.(this);
459
+ this.onReady?.(this), this;
410
460
  }
411
461
  _getImportedModules() {
412
462
  return this.shouldThrowIfDisposed(), this.imports;
@@ -479,14 +529,14 @@ var V = class {
479
529
  async __unbindAll() {
480
530
  this.shouldThrowIfDisposed(), await this.container.unbindAll(), this.removeRegisteredBindingSideEffects("all");
481
531
  }
482
- }, W = class extends q {
532
+ }, $ = class extends q {
483
533
  static {
484
534
  i(this, "GlobalAppModule");
485
535
  }
486
536
  nakedModule=this;
487
537
  isLoaded=!1;
488
538
  constructor() {
489
- super(s.buildInternalConstructorParams({
539
+ super(o.buildInternalConstructorParams({
490
540
  identifier: Symbol(t),
491
541
  isAppModule: !0
492
542
  }));
@@ -501,6 +551,6 @@ var V = class {
501
551
  async _dispose() {
502
552
  this.isLoaded = !1, super._dispose();
503
553
  }
504
- }, $ = new W;
554
+ }, z = new $;
505
555
 
506
- export { n as ANONYMOUS_MODULE_DEFAULT_ID, $ as AppModule, t as GLOBAL_APP_MODULE_ID, W as GlobalAppModule, U as GlobalContainer, S as Inject, b as InjectFromBase, g as Injectable, d as InjectionScope, m as MultiInject, T as Named, B as Optional, D as PostConstruct, k as PreDestroy, q as ProviderModule, s as ProviderModuleHelpers, r as ProviderTokenHelpers, P as Tagged, x as Unmanaged, F as XInjectionDynamicExportsOutOfRange, A as XInjectionError, O as XInjectionProviderModuleDisposedError, R as XInjectionProviderModuleError, a as bindingScopeToInjectionScope, c as injectionScopeToBindingScope, h as isClass, p as isClassOrFunction, l as isPlainObject };//# sourceMappingURL=index.js.map
556
+ export { z as AppModule, t as GLOBAL_APP_MODULE_ID, $ as GlobalAppModule, G as GlobalContainer, m as Inject, E as InjectFromBase, v as Injectable, s as InjectionScope, y as MultiInject, T as Named, w as Optional, D as PostConstruct, M as PreDestroy, q as ProviderModule, o as ProviderModuleHelpers, r as ProviderTokenHelpers, k as Tagged, j as Unmanaged, O as XInjectionDynamicExportsOutOfRange, A as XInjectionError, F as XInjectionProviderModuleDisposedError, R as XInjectionProviderModuleError, N as XInjectionProviderModuleMissingIdentifierError, c as bindingScopeToInjectionScope, d as injectionScopeToBindingScope, u as isClass, h as isClassOrFunction, p as isFunction, l as isPlainObject };//# sourceMappingURL=index.js.map