@adimm/x-injection 2.0.4 → 2.1.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
@@ -310,7 +310,7 @@ The below list shows them in order of priority _(highest to lowest)_, meaning th
310
310
  3. By providing the [defaultScope](https://adimarianmutu.github.io/x-injection/interfaces/ProviderModuleOptions.html#defaultscope) property when initializing a `ProviderModule`:
311
311
  ```ts
312
312
  const RainModuleDef = new ProviderModuleDef({
313
- identifier: 'RainModule',
313
+ id: 'RainModule',
314
314
  defaultScope: InjectionScope.Transient,
315
315
  });
316
316
  ```
@@ -499,7 +499,7 @@ const UnknownModule = ProviderModule.create({
499
499
  exports: [SecondService],
500
500
  });
501
501
 
502
- InnerModule.update.addImport(UnknownModule);
502
+ InnerModule.update.addImport(UnknownModule, true); // Don't forget to provide `true` to the `addToExports` optional parameter!
503
503
 
504
504
  const secondService = OuterModule.get(SecondService);
505
505
  ```
@@ -543,13 +543,9 @@ The [ProviderModuleBlueprint](https://adimarianmutu.github.io/x-injection/classe
543
543
 
544
544
  Whenever you _import_ a `blueprint` into a `module`, it'll automatically be "transformed" to a `ProviderModule` instance by the engine, this step is crucial as a `blueprint` per se does not contain a _container_, just its _definitions_.
545
545
 
546
- This has a "gotcha", because the conversion happens internally, you'll "not" be able to get a reference to that imported module, not directly at least _(I may change this in a future patch)_, this means that if you don't have the reference of the `ProviderModule`, you can't remove it from the _imported_ module _(if you'll ever need to remove it)_.
547
-
548
546
  > [!NOTE]
549
547
  >
550
- > There's currently an workaround for this, you can `subscribe` to the `Import` event and get from there the `ProviderModule` instance, just make sure to check the `id` of the module via `module.toString()` when doing so.
551
-
552
- It is also important, to understand the _injection_ `scope` of an imported `blueprint`; we previously learned that when we import a `blueprint` into a `module` it automatically creates an instance of the `ProviderModule` from it, this means that all the `singleton` providers of the `blueprint` definition are now _scoped singleton_, where _scoped_ means _singleton in relation to their imported module_.
548
+ > Therefore it is important to understand the _injection_ `scope` of an imported `blueprint`; we previously learned that when we import a `blueprint` into a `module` it automatically creates an instance of the `ProviderModule` from it, this means that all the `singleton` providers of the `blueprint` definition are now _scoped singleton_, where _scoped_ means _singleton in relation to their imported module_.
553
549
 
554
550
  ### isGlobal
555
551
 
@@ -812,7 +808,7 @@ const ApiModuleBp = new ProviderModule.blueprint({
812
808
 
813
809
  // Clone returns a `deep` clone and wraps all the `methods` to break their reference!
814
810
  const ApiModuleBpMocked = ApiModuleBp.clone().updateDefinition({
815
- identifier: 'ApiModuleMocked',
811
+ id: 'ApiModuleMocked',
816
812
  providers: [
817
813
  {
818
814
  provide: UserService,
package/dist/index.cjs CHANGED
@@ -17,11 +17,11 @@ var e, t = Object.defineProperty, i = Object.getOwnPropertyDescriptor, o = Objec
17
17
  InjectFromBase: () => N,
18
18
  Injectable: () => q,
19
19
  InjectionError: () => I,
20
- InjectionProviderModuleDisposedError: () => P,
20
+ InjectionProviderModuleDisposedError: () => w,
21
21
  InjectionProviderModuleError: () => b,
22
22
  InjectionProviderModuleMissingIdentifierError: () => x,
23
23
  InjectionProviderModuleMissingProviderError: () => E,
24
- InjectionProviderModuleUnknownProviderError: () => w,
24
+ InjectionProviderModuleUnknownProviderError: () => P,
25
25
  InjectionScope: () => u,
26
26
  MiddlewareType: () => a,
27
27
  MultiInject: () => U,
@@ -194,7 +194,7 @@ var g, I = class e extends Error {
194
194
  } catch {}
195
195
  super(`{ProviderModule.${i}} => ${t}`);
196
196
  }
197
- }, w = class e extends b {
197
+ }, P = class e extends b {
198
198
  static {
199
199
  n(this, "InjectionProviderModuleUnknownProviderError");
200
200
  }
@@ -202,7 +202,7 @@ var g, I = class e extends Error {
202
202
  constructor(e, t) {
203
203
  super(e, `The [${h.providerTokenToString(t)}] provider is of an unknown type!`);
204
204
  }
205
- }, P = class e extends b {
205
+ }, w = class e extends b {
206
206
  static {
207
207
  n(this, "InjectionProviderModuleDisposedError");
208
208
  }
@@ -270,7 +270,7 @@ var g, I = class e extends Error {
270
270
  return t.useFactory(...i);
271
271
  }))), "bind")
272
272
  } ], {bind: o} = i.find((({providerTypeMatches: t}) => t(e))) ?? {};
273
- if (!o) throw new w(this.providerModule, e);
273
+ if (!o) throw new P(this.providerModule, e);
274
274
  const r = h.isProviderIdentifier(e);
275
275
  let s = o();
276
276
  if (h.isValueToken(e) || r || (s = this.setBindingScope(e, s)), r) return;
@@ -388,7 +388,7 @@ var g, I = class e extends Error {
388
388
  return this.providerModule.moduleContainer;
389
389
  }
390
390
  get subscribe() {
391
- if (null === this.event$) throw new P(this.providerModule);
391
+ if (null === this.event$) throw new w(this.providerModule);
392
392
  return this.event$.subscribe.bind(this.event$);
393
393
  }
394
394
  moduleDef;
@@ -444,22 +444,24 @@ var g, I = class e extends Error {
444
444
  this.addProvider(i, t);
445
445
  }
446
446
  removeImport(e) {
447
- if (!this.moduleDef.imports.has(e)) return !1;
448
- if (!1 === this.providerModule.middlewaresManager.applyMiddlewares(a.BeforeRemoveImport, e)) return !1;
449
- this.unsubscribeFromImportedModuleEvents(e);
450
- return this.providerModule.importedModuleContainers.get(e).dispose(), this.providerModule.importedModuleContainers.delete(e),
451
- this.moduleDef.imports.delete(e), this.emitEventSafely({
447
+ const t = g.isModule(e) ? e : this.getImportedModuleById(e);
448
+ if (!t) return !1;
449
+ if (!1 === this.providerModule.middlewaresManager.applyMiddlewares(a.BeforeRemoveImport, t)) return !1;
450
+ this.unsubscribeFromImportedModuleEvents(t);
451
+ return this.providerModule.importedModuleContainers.get(t).dispose(), this.providerModule.importedModuleContainers.delete(t),
452
+ this.moduleDef.imports.delete(t), this.emitEventSafely({
452
453
  type: p.ImportRemoved,
453
- change: e
454
- }), this.removeFromExports(e), !0;
454
+ change: t
455
+ }), this.removeFromExports(t), !0;
455
456
  }
456
457
  removeProvider(e) {
457
- if (!this.moduleDef.providers.has(e)) return !1;
458
- return !1 !== this.providerModule.middlewaresManager.applyMiddlewares(a.BeforeRemoveProvider, e) && (this.moduleDef.providers.delete(e),
459
- this.moduleContainer.container.unbindSync(h.toProviderIdentifier(e)), this.emitEventSafely({
458
+ const t = h.isProviderIdentifier(e) ? this.getProviderByIdentifier(e) : e;
459
+ if (!t) return !1;
460
+ return !1 !== this.providerModule.middlewaresManager.applyMiddlewares(a.BeforeRemoveProvider, t) && (this.moduleDef.providers.delete(t),
461
+ this.moduleContainer.container.unbindSync(h.toProviderIdentifier(t)), this.emitEventSafely({
460
462
  type: p.ProviderRemoved,
461
- change: e
462
- }), this.removeFromExports(e), !0);
463
+ change: t
464
+ }), this.removeFromExports(t), !0);
463
465
  }
464
466
  removeFromExports(e) {
465
467
  if (!this.moduleDef.exports.has(e)) return !1;
@@ -475,6 +477,12 @@ var g, I = class e extends Error {
475
477
  change: e
476
478
  }), !0);
477
479
  }
480
+ getImportedModuleById(e) {
481
+ return this.moduleDef.imports.values().find((t => t.id === e));
482
+ }
483
+ getProviderByIdentifier(e) {
484
+ return this.moduleDef.providers.values().find((t => h.toProviderIdentifier(t) === e));
485
+ }
478
486
  emitEventSafely(e) {
479
487
  if (!this.emittingModules.has(this.providerModule)) try {
480
488
  this.emittingModules.add(this.providerModule), this.event$.emit(e);
@@ -535,7 +543,7 @@ var g, I = class e extends Error {
535
543
  i ? i.push(t) : this.middlewaresMap.set(e, [ t ]);
536
544
  }
537
545
  applyMiddlewares(e, ...t) {
538
- if (null === this.middlewaresMap) throw new P(this.providerModule);
546
+ if (null === this.middlewaresMap) throw new w(this.providerModule);
539
547
  const i = this.middlewaresMap.get(e);
540
548
  switch (e) {
541
549
  case a.BeforeAddImport:
@@ -709,7 +717,7 @@ var g, I = class e extends Error {
709
717
  if (!this.options.id || 0 === this.options.id.toString().trim().length) throw new x(this);
710
718
  }
711
719
  throwIfDisposed() {
712
- if (this.isDisposed) throw new P(this);
720
+ if (this.isDisposed) throw new w(this);
713
721
  }
714
722
  };
715
723