@adimm/x-injection 0.3.1

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.cjs ADDED
@@ -0,0 +1,531 @@
1
+ "use strict";
2
+
3
+ var e, i = Object.defineProperty, t = Object.getOwnPropertyDescriptor, n = Object.getOwnPropertyNames, r = Object.prototype.hasOwnProperty, o = (e, t) => i(e, "name", {
4
+ value: t,
5
+ configurable: !0
6
+ }), s = {};
7
+
8
+ ((e, t) => {
9
+ for (var n in t) i(e, n, {
10
+ get: t[n],
11
+ enumerable: !0
12
+ });
13
+ })(s, {
14
+ ANONYMOUSE_MODULE_DEFAULT_ID: () => c,
15
+ AppModule: () => z,
16
+ GLOBAL_APP_MODULE_ID: () => d,
17
+ GlobalAppModule: () => $,
18
+ GlobalContainer: () => V,
19
+ Inject: () => b,
20
+ InjectFromBase: () => B,
21
+ Injectable: () => I,
22
+ InjectionScope: () => u,
23
+ MultiInject: () => T,
24
+ Named: () => D,
25
+ Optional: () => M,
26
+ PostConstruct: () => C,
27
+ PreDestroy: () => A,
28
+ ProviderModule: () => W,
29
+ ProviderModuleHelpers: () => p,
30
+ ProviderTokenHelpers: () => f,
31
+ Tagged: () => O,
32
+ Unmanaged: () => q,
33
+ XInjectionDynamicExportsOutOfRange: () => G,
34
+ XInjectionError: () => F,
35
+ XInjectionProviderModuleDisposedError: () => U,
36
+ XInjectionProviderModuleError: () => N,
37
+ injectionScopeToBindingScope: () => h
38
+ }), module.exports = (e = s, ((e, o, s, d) => {
39
+ if (o && "object" == typeof o || "function" == typeof o) for (let c of n(o)) r.call(e, c) || c === s || i(e, c, {
40
+ get: () => o[c],
41
+ enumerable: !(d = t(o, c)) || d.enumerable
42
+ });
43
+ return e;
44
+ })(i({}, "__esModule", {
45
+ value: !0
46
+ }), e));
47
+
48
+ var d = "GlobalAppModule", c = "AnonymousModule", a = require("inversify"), u = function(e) {
49
+ return e[e.Singleton = 0] = "Singleton", e[e.Transient = 1] = "Transient", e[e.Request = 2] = "Request",
50
+ e;
51
+ }({});
52
+
53
+ function h(e) {
54
+ switch (e) {
55
+ case u.Singleton:
56
+ return "Singleton";
57
+
58
+ case u.Transient:
59
+ return "Transient";
60
+
61
+ case u.Request:
62
+ return "Request";
63
+ }
64
+ }
65
+
66
+ function l(e) {
67
+ switch (e) {
68
+ case "Singleton":
69
+ return u.Singleton;
70
+
71
+ case "Transient":
72
+ return u.Transient;
73
+
74
+ case "Request":
75
+ return u.Request;
76
+ }
77
+ }
78
+
79
+ o(h, "injectionScopeToBindingScope"), o(l, "bindingScopeToInjectionScope");
80
+
81
+ var f, p, v = require("@inversifyjs/core");
82
+
83
+ function g(e) {
84
+ return "function" == typeof e && /^class\s/.test(Function.prototype.toString.call(e));
85
+ }
86
+
87
+ function S(e) {
88
+ return "[object Object]" === Object.prototype.toString.call(e);
89
+ }
90
+
91
+ function y(e) {
92
+ if (!1 === S(e)) return !1;
93
+ const i = e.constructor;
94
+ if (void 0 === i) return !0;
95
+ const t = i.prototype;
96
+ return !1 !== S(t) && !1 !== t.hasOwnProperty("isPrototypeOf");
97
+ }
98
+
99
+ function I(e) {
100
+ return void 0 === e ? (0, a.injectable)() : (0, a.injectable)(h(e));
101
+ }
102
+
103
+ o(g, "isClass"), o(S, "isObject"), o(y, "isPlainObject"), function(e) {
104
+ function i(e) {
105
+ return f(e) && "useClass" in e;
106
+ }
107
+ function t(e) {
108
+ return f(e) && "useValue" in e;
109
+ }
110
+ function n(e) {
111
+ return f(e) && "useFactory" in e;
112
+ }
113
+ function r(e) {
114
+ return "string" == typeof e || "symbol" == typeof e || g(e) || "function" == typeof e;
115
+ }
116
+ function s(e) {
117
+ return r(e) ? e : e.provide;
118
+ }
119
+ function d(e) {
120
+ return e.map((e => s(e)));
121
+ }
122
+ function c(e, i) {
123
+ return u(e) ?? h(e) ?? i;
124
+ }
125
+ function a(e) {
126
+ if (f(e)) return e;
127
+ }
128
+ function u(e) {
129
+ const i = a(e);
130
+ if (i) return i.scope;
131
+ }
132
+ function h(e) {
133
+ const i = s(e);
134
+ if (!g(i)) return;
135
+ const t = (0, v.getClassMetadata)(i)?.scope;
136
+ return t ? l(t) : void 0;
137
+ }
138
+ function f(e) {
139
+ return y(e) && "object" == typeof e && "provide" in e;
140
+ }
141
+ o(i, "isClassToken"), e.isClassToken = i, o(t, "isValueToken"), e.isValueToken = t,
142
+ o(n, "isFactoryToken"), e.isFactoryToken = n, o(r, "isProviderIdentifier"), e.isProviderIdentifier = r,
143
+ o(s, "toServiceIdentifier"), e.toServiceIdentifier = s, o(d, "toServiceIdentifiers"),
144
+ e.toServiceIdentifiers = d, o(c, "getInjectionScopeByPriority"), e.getInjectionScopeByPriority = c,
145
+ o(a, "tryGetProviderOptions"), e.tryGetProviderOptions = a, o(u, "tryGetScopeFromProvider"),
146
+ e.tryGetScopeFromProvider = u, o(h, "tryGetDecoratorScopeFromClass"), e.tryGetDecoratorScopeFromClass = h,
147
+ o(f, "hasProvideProperty");
148
+ }(f || (f = {})), function(e) {
149
+ function i(e) {
150
+ return e;
151
+ }
152
+ function t(e) {
153
+ return !Array.isArray(e) && "function" == typeof e;
154
+ }
155
+ o(i, "buildInternalConstructorParams"), e.buildInternalConstructorParams = i, o(t, "isDynamicExport"),
156
+ e.isDynamicExport = t;
157
+ }(p || (p = {})), o(I, "Injectable");
158
+
159
+ var m = require("inversify");
160
+
161
+ function b(e) {
162
+ return (0, m.inject)(f.toServiceIdentifier(e));
163
+ }
164
+
165
+ o(b, "Inject");
166
+
167
+ var E = require("inversify");
168
+
169
+ function T(e) {
170
+ return (0, E.multiInject)(f.toServiceIdentifier(e));
171
+ }
172
+
173
+ o(T, "MultiInject");
174
+
175
+ var _ = require("inversify");
176
+
177
+ function B(e) {
178
+ return (0, _.injectFromBase)(e);
179
+ }
180
+
181
+ o(B, "InjectFromBase");
182
+
183
+ var w = require("inversify");
184
+
185
+ function D(e) {
186
+ return (0, w.named)(e);
187
+ }
188
+
189
+ o(D, "Named");
190
+
191
+ var j = require("inversify");
192
+
193
+ function M() {
194
+ return (0, j.optional)();
195
+ }
196
+
197
+ o(M, "Optional");
198
+
199
+ var P = require("inversify");
200
+
201
+ function C() {
202
+ return (0, P.postConstruct)();
203
+ }
204
+
205
+ o(C, "PostConstruct");
206
+
207
+ var k = require("inversify");
208
+
209
+ function A() {
210
+ return (0, k.preDestroy)();
211
+ }
212
+
213
+ o(A, "PreDestroy");
214
+
215
+ var x = require("inversify");
216
+
217
+ function O(e, i) {
218
+ return (0, x.tagged)(e, i);
219
+ }
220
+
221
+ o(O, "Tagged");
222
+
223
+ var R = require("inversify");
224
+
225
+ function q() {
226
+ return (0, R.unmanaged)();
227
+ }
228
+
229
+ o(q, "Unmanaged");
230
+
231
+ var F = class e extends Error {
232
+ static {
233
+ o(this, "XInjectionError");
234
+ }
235
+ name=e.name;
236
+ }, N = class e extends Error {
237
+ static {
238
+ o(this, "XInjectionProviderModuleError");
239
+ }
240
+ name=e.name;
241
+ constructor(e, i) {
242
+ super(`{ProviderModule.${e.toString()}} => ${i}`);
243
+ }
244
+ }, U = class e extends N {
245
+ static {
246
+ o(this, "XInjectionProviderModuleDisposedError");
247
+ }
248
+ name=e.name;
249
+ constructor(e) {
250
+ super(e, "Has been disposed! You can re-initialize it by using the `_lazyInit` method.");
251
+ }
252
+ }, G = class e extends N {
253
+ static {
254
+ o(this, "XInjectionDynamicExportsOutOfRange");
255
+ }
256
+ name=e.name;
257
+ constructor(e) {
258
+ super(e, `The 'ProviderModule.${e.toString()}' is trying to dynamically export providers/modules out of the declared range of the static exports!`);
259
+ }
260
+ }, V = new (require("inversify").Container)({
261
+ defaultScope: "Singleton"
262
+ }), L = require("inversify"), X = class {
263
+ static {
264
+ o(this, "ProviderModuleUtils");
265
+ }
266
+ get container() {
267
+ return this.module.toNaked().container;
268
+ }
269
+ module;
270
+ moduleNaked;
271
+ constructor(e) {
272
+ this.module = e, this.moduleNaked = e.toNaked();
273
+ }
274
+ bindToContainer(e, i) {
275
+ return f.isProviderIdentifier(e) ? this.bindSelfTokenToContainer(e, i) : f.isClassToken(e) ? this.bindClassTokenToContainer(e, i) : f.isValueToken(e) ? this.bindValueTokenToContainer(e) : !!f.isFactoryToken(e) && this.bindFactoryTokenToContainer(e, i);
276
+ }
277
+ bindManyToContainer(e, i) {
278
+ e.forEach((e => this.bindToContainer(e, i)));
279
+ }
280
+ bindSelfTokenToContainer(e, i) {
281
+ return this.setBindingScope(e, this.container.bind(f.toServiceIdentifier(e)).toSelf(), i),
282
+ this.moduleNaked.__isCurrentBound(e);
283
+ }
284
+ bindClassTokenToContainer(e, i) {
285
+ return this.setBindingOnEvent(e, this.setWhenBinding(e, this.setBindingScope(e, this.container.bind(f.toServiceIdentifier(e)).to(e.useClass), i))),
286
+ this.moduleNaked.__isCurrentBound(e);
287
+ }
288
+ bindValueTokenToContainer(e) {
289
+ return this.setBindingOnEvent(e, this.setWhenBinding(e, this.container.bind(f.toServiceIdentifier(e)).toConstantValue(e.useValue))),
290
+ this.moduleNaked.__isCurrentBound(e);
291
+ }
292
+ bindFactoryTokenToContainer(e, i) {
293
+ return this.setBindingOnEvent(e, this.setWhenBinding(e, this.setBindingScope(e, this.container.bind(f.toServiceIdentifier(e)).toResolvedValue(e.useFactory, f.toServiceIdentifiers(e.inject ?? [])), i))),
294
+ this.moduleNaked.__isCurrentBound(e);
295
+ }
296
+ setBindingScope(e, i, t) {
297
+ if (f.isValueToken(e)) return i;
298
+ switch (f.getInjectionScopeByPriority(e, t)) {
299
+ case u.Singleton:
300
+ return i.inSingletonScope();
301
+
302
+ case u.Transient:
303
+ return i.inTransientScope();
304
+
305
+ case u.Request:
306
+ return i.inRequestScope();
307
+ }
308
+ }
309
+ setWhenBinding(e, i) {
310
+ if (f.isProviderIdentifier(e)) return i;
311
+ const t = e.when;
312
+ return t ? i.when(t) : i;
313
+ }
314
+ setBindingOnEvent(e, i) {
315
+ if (f.isProviderIdentifier(e)) return;
316
+ const t = e;
317
+ t.onEvent?.activation && i.onActivation(t.onEvent.activation), t.onEvent?.deactivation && i.onDeactivation(t.onEvent.deactivation);
318
+ }
319
+ }, W = class e {
320
+ static {
321
+ o(this, "ProviderModule");
322
+ }
323
+ identifier;
324
+ isAppModule;
325
+ container;
326
+ defaultScope;
327
+ dynamicExports;
328
+ onReady;
329
+ onDispose;
330
+ moduleUtils;
331
+ providers;
332
+ exports;
333
+ imports;
334
+ registeredBindingSideEffects;
335
+ constructor({identifier: e = Symbol(c), imports: i, providers: t, exports: n, defaultScope: r, dynamicExports: o, onReady: s, onDispose: d, ...a}) {
336
+ const u = a;
337
+ this.identifier = e, this.isAppModule = u.isAppModule ?? !1, this.isAppModule || this._lazyInit({
338
+ imports: i,
339
+ providers: t,
340
+ exports: n,
341
+ defaultScope: r,
342
+ dynamicExports: o,
343
+ onReady: s,
344
+ onDispose: d,
345
+ ...a
346
+ });
347
+ }
348
+ get(e, i) {
349
+ return this.__get(e, {
350
+ optional: i ?? !1
351
+ });
352
+ }
353
+ getMany(...e) {
354
+ return e.map((e => {
355
+ const i = y(e) && "provider" in e;
356
+ return this.get(i ? e.provider : e, !!i && e.isOptional);
357
+ }));
358
+ }
359
+ onActivationEvent(e, i) {
360
+ this.container.onActivation(f.toServiceIdentifier(e), i);
361
+ }
362
+ onDeactivationEvent(e, i) {
363
+ this.container.onDeactivation(f.toServiceIdentifier(e), i);
364
+ }
365
+ toNaked() {
366
+ return this;
367
+ }
368
+ toString() {
369
+ return this.identifier.description;
370
+ }
371
+ prepareContainer(e) {
372
+ return this.isAppModule ? e.container?.() ?? V : e.container ? (console.warn(`[xInjection]: The '${this.toString()}' module is using a dynamic container!`),
373
+ e.container()) : new L.Container({
374
+ parent: V,
375
+ defaultScope: this.defaultScope.inversify
376
+ });
377
+ }
378
+ injectImportedModules(i) {
379
+ i && 0 !== i.length && i.forEach((i => {
380
+ if ("GlobalAppModule" === i.toString()) throw new N(this, "The 'GlobalAppModule' can't be imported!");
381
+ const t = i._getExportableModulesAndProviders(), n = i.dynamicExports?.(this, t);
382
+ void 0 !== n && this.shouldThrowWhenModuleDynamicExportsDontMatchTheStaticExports(i, t, n),
383
+ (n ?? t).forEach((t => {
384
+ if (t instanceof e) {
385
+ const e = t.toNaked();
386
+ return void this.injectImportedModules([ e ]);
387
+ }
388
+ const n = t;
389
+ this.moduleUtils.bindToContainer({
390
+ scope: f.getInjectionScopeByPriority(n, i.defaultScope.native),
391
+ provide: f.toServiceIdentifier(n),
392
+ useFactory: o((() => i.get(n)), "useFactory")
393
+ }, i.defaultScope.native);
394
+ }));
395
+ }));
396
+ }
397
+ injectProviders() {
398
+ this.moduleUtils.bindManyToContainer(this.providers, this.defaultScope.native);
399
+ }
400
+ registerBindingSideEffect(e, i, t) {
401
+ this.registeredBindingSideEffects.has(e) || this.registeredBindingSideEffects.set(e, {
402
+ onBindEffects: [],
403
+ onRebindEffects: [],
404
+ onUnbindEffects: []
405
+ });
406
+ const n = this.registeredBindingSideEffects.get(e);
407
+ "bind" === i ? n.onBindEffects.push(t) : "rebind" === i ? n.onRebindEffects.push(t) : n.onUnbindEffects.push(t);
408
+ }
409
+ invokeRegisteredBindingSideEffects(e, i) {
410
+ const t = this.registeredBindingSideEffects.get(e);
411
+ t && t[`${i}Effects`].forEach((e => e()));
412
+ }
413
+ removeRegisteredBindingSideEffects(e) {
414
+ this.registeredBindingSideEffects && ("all" !== e ? this.registeredBindingSideEffects.has(e) && (this.registeredBindingSideEffects.get(e)?.onUnbindEffects.forEach((e => e())),
415
+ this.registeredBindingSideEffects.delete(e)) : this.registeredBindingSideEffects.forEach((({onUnbindEffects: e}) => e.forEach((e => e())))));
416
+ }
417
+ shouldThrowWhenModuleDynamicExportsDontMatchTheStaticExports(e, i, t) {
418
+ if (t.length > i.length || t.some((e => !i.includes(e)))) throw new G(e);
419
+ }
420
+ shouldThrowIfDisposed() {
421
+ if (null === this.container) throw new U(this);
422
+ }
423
+ async _dispose() {
424
+ await (this.onDispose?.(this)), await this.__unbindAll(), this.container = null,
425
+ this.imports = null, this.providers = null, this.exports = null, this.dynamicExports = null,
426
+ this.registeredBindingSideEffects = null;
427
+ }
428
+ _lazyInit({imports: e = [], providers: i = [], exports: t = [], defaultScope: n = u.Singleton, dynamicExports: r, onReady: o, onDispose: s, ...d}) {
429
+ this.imports = e, this.providers = i, this.exports = t, this.defaultScope = {
430
+ native: n,
431
+ inversify: h(n)
432
+ }, this.dynamicExports = r, this.onReady = o, this.onDispose = s, this.container = this.prepareContainer({
433
+ ...d
434
+ }), this.moduleUtils = new X(this), this.registeredBindingSideEffects = new Map,
435
+ this.injectImportedModules(this._getImportedModules()), this.injectProviders(),
436
+ this.onReady?.(this);
437
+ }
438
+ _getImportedModules() {
439
+ return this.shouldThrowIfDisposed(), this.imports;
440
+ }
441
+ _getProviders() {
442
+ return this.shouldThrowIfDisposed(), this.providers;
443
+ }
444
+ _getExportableModulesAndProviders() {
445
+ return this.shouldThrowIfDisposed(), this.exports;
446
+ }
447
+ _onBind(e, i) {
448
+ this.shouldThrowIfDisposed(), this.registerBindingSideEffect(e, "bind", i);
449
+ }
450
+ _onRebind(e, i) {
451
+ this.shouldThrowIfDisposed(), this.registerBindingSideEffect(e, "rebind", i);
452
+ }
453
+ _onUnbind(e, i) {
454
+ this.shouldThrowIfDisposed(), this.registerBindingSideEffect(e, "unbind", i);
455
+ }
456
+ _overwriteContainer(e) {
457
+ this.container = e();
458
+ }
459
+ __bind(e) {
460
+ this.shouldThrowIfDisposed();
461
+ const i = this.container.bind(f.toServiceIdentifier(e));
462
+ return this.invokeRegisteredBindingSideEffects(e, "onBind"), i;
463
+ }
464
+ __get(e, i) {
465
+ return this.shouldThrowIfDisposed(), this.container.get(f.toServiceIdentifier(e), i);
466
+ }
467
+ async __getAsync(e, i) {
468
+ return this.shouldThrowIfDisposed(), this.container.getAsync(f.toServiceIdentifier(e), i);
469
+ }
470
+ __getAll(e, i) {
471
+ return this.shouldThrowIfDisposed(), this.container.getAll(f.toServiceIdentifier(e), i);
472
+ }
473
+ async __getAllAsync(e, i) {
474
+ return this.shouldThrowIfDisposed(), this.container.getAllAsync(f.toServiceIdentifier(e), i);
475
+ }
476
+ __isBound(e, i) {
477
+ return this.shouldThrowIfDisposed(), this.container.isBound(f.toServiceIdentifier(e), i);
478
+ }
479
+ __isCurrentBound(e, i) {
480
+ return this.shouldThrowIfDisposed(), this.container.isCurrentBound(f.toServiceIdentifier(e), i);
481
+ }
482
+ __takeSnapshot() {
483
+ this.shouldThrowIfDisposed(), this.container.snapshot();
484
+ }
485
+ __restoreSnapshot() {
486
+ this.shouldThrowIfDisposed(), this.container.restore();
487
+ }
488
+ async __rebind(e) {
489
+ this.shouldThrowIfDisposed();
490
+ const i = await this.container.rebind(f.toServiceIdentifier(e));
491
+ return this.invokeRegisteredBindingSideEffects(e, "onRebind"), i;
492
+ }
493
+ __rebindSync(e) {
494
+ this.shouldThrowIfDisposed();
495
+ const i = this.container.rebindSync(f.toServiceIdentifier(e));
496
+ return this.invokeRegisteredBindingSideEffects(e, "onRebind"), i;
497
+ }
498
+ async __unbind(e) {
499
+ this.shouldThrowIfDisposed(), await this.container.unbind(f.toServiceIdentifier(e)),
500
+ this.removeRegisteredBindingSideEffects(e);
501
+ }
502
+ __unbindSync(e) {
503
+ this.shouldThrowIfDisposed(), this.container.unbindSync(f.toServiceIdentifier(e)),
504
+ this.removeRegisteredBindingSideEffects(e);
505
+ }
506
+ async __unbindAll() {
507
+ this.shouldThrowIfDisposed(), await this.container.unbindAll(), this.removeRegisteredBindingSideEffects("all");
508
+ }
509
+ }, $ = class extends W {
510
+ static {
511
+ o(this, "GlobalAppModule");
512
+ }
513
+ nakedModule=this;
514
+ isLoaded=!1;
515
+ constructor() {
516
+ super(p.buildInternalConstructorParams({
517
+ identifier: Symbol(d),
518
+ isAppModule: !0
519
+ }));
520
+ }
521
+ register(e) {
522
+ if (this.isLoaded) throw new F(`The '${this.toString()}' has already been registered!`);
523
+ return this.nakedModule._lazyInit(e), this.isLoaded = !0, this;
524
+ }
525
+ toNaked() {
526
+ return super.toNaked();
527
+ }
528
+ async _dispose() {
529
+ this.isLoaded = !1, super._dispose();
530
+ }
531
+ }, z = new $;//# sourceMappingURL=index.cjs.map