@lppedd/di-wise-neo 0.3.1 → 0.4.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/cjs/index.js CHANGED
@@ -209,10 +209,6 @@ function isClassProvider(provider) {
209
209
  return "useClass" in provider;
210
210
  }
211
211
  // @internal
212
- function isExistingProvider(provider) {
213
- return "useExisting" in provider;
214
- }
215
- // @internal
216
212
  function isFactoryProvider(provider) {
217
213
  return "useFactory" in provider;
218
214
  }
@@ -220,6 +216,10 @@ function isFactoryProvider(provider) {
220
216
  function isValueProvider(provider) {
221
217
  return "useValue" in provider;
222
218
  }
219
+ // @internal
220
+ function isExistingProvider(provider) {
221
+ return "useExisting" in provider;
222
+ }
223
223
 
224
224
  const Scope = {
225
225
  Inherited: "Inherited",
@@ -231,19 +231,19 @@ const Scope = {
231
231
  /**
232
232
  * Type API.
233
233
  */ /**
234
- * Create a type token.
234
+ * Creates a type token.
235
235
  *
236
236
  * @example
237
237
  * ```ts
238
- * const Spell = Type<Spell>("Spell");
238
+ * const ISpell = createType<Spell>("Spell");
239
239
  * ```
240
240
  *
241
241
  * @__NO_SIDE_EFFECTS__
242
- */ function Type(typeName) {
242
+ */ function createType(typeName) {
243
243
  const type = {
244
244
  name: `Type<${typeName}>`,
245
- inter: Type,
246
- union: Type,
245
+ inter: createType,
246
+ union: createType,
247
247
  toString () {
248
248
  return type.name;
249
249
  }
@@ -281,8 +281,8 @@ function getTypeName(value) {
281
281
  // @internal
282
282
  class TokenRegistry {
283
283
  constructor(parent){
284
- this.parent = parent;
285
284
  this.myMap = new Map();
285
+ this.myParent = parent;
286
286
  }
287
287
  get(token) {
288
288
  // To clarify, at(-1) means we take the last added registration for this token
@@ -335,7 +335,7 @@ class TokenRegistry {
335
335
  }
336
336
  getAllFromParent(token) {
337
337
  const registrations = this.myMap.get(token);
338
- return registrations || this.parent?.getAllFromParent(token);
338
+ return registrations || this.myParent?.getAllFromParent(token);
339
339
  }
340
340
  }
341
341
  // @internal
@@ -349,7 +349,7 @@ function isBuilder(provider) {
349
349
  * ```ts
350
350
  * class Wizard {
351
351
  * wand = inject(
352
- * Build(() => {
352
+ * build(() => {
353
353
  * const wand = inject(Wand);
354
354
  * wand.owner = this;
355
355
  * // ...
@@ -360,8 +360,8 @@ function isBuilder(provider) {
360
360
  * ```
361
361
  *
362
362
  * @__NO_SIDE_EFFECTS__
363
- */ function Build(factory) {
364
- const token = Type(`Build<${getTypeName(factory)}>`);
363
+ */ function build(factory) {
364
+ const token = createType(`Build<${getTypeName(factory)}>`);
365
365
  const provider = {
366
366
  useFactory: factory
367
367
  };
@@ -386,12 +386,11 @@ function isDisposable(value) {
386
386
 
387
387
  /**
388
388
  * The default implementation of a di-wise-neo {@link Container}.
389
- */ class DefaultContainer {
390
- constructor(myParent, options){
391
- this.myParent = myParent;
392
- // eslint-disable-next-line no-use-before-define
389
+ */ class ContainerImpl {
390
+ constructor(parent, options){
393
391
  this.myChildren = new Set();
394
392
  this.myDisposed = false;
393
+ this.myParent = parent;
395
394
  this.myOptions = {
396
395
  autoRegister: false,
397
396
  defaultScope: Scope.Inherited,
@@ -415,7 +414,7 @@ function isDisposable(value) {
415
414
  }
416
415
  createChild(options) {
417
416
  this.checkDisposed();
418
- const container = new DefaultContainer(this, {
417
+ const container = new ContainerImpl(this, {
419
418
  ...this.myOptions,
420
419
  ...options
421
420
  });
@@ -462,6 +461,36 @@ function isDisposable(value) {
462
461
  this.checkDisposed();
463
462
  return this.myTokenRegistry.get(token) !== undefined;
464
463
  }
464
+ registerClass(token, Class, options) {
465
+ // This mess will go away once/if we remove the register method
466
+ // in favor of the multiple specialized ones
467
+ if (Class) {
468
+ const ctor = Class ?? token;
469
+ this.register(token, {
470
+ useClass: ctor
471
+ }, options);
472
+ } else {
473
+ this.register(token);
474
+ }
475
+ }
476
+ registerFactory(token, factory, options) {
477
+ this.register(token, {
478
+ useFactory: factory
479
+ }, options);
480
+ }
481
+ registerValue(token, value) {
482
+ this.register(token, {
483
+ useValue: value
484
+ });
485
+ }
486
+ registerAlias(targetToken, aliasTokens) {
487
+ // De-duplicate tokens
488
+ for (const alias of new Set(aliasTokens)){
489
+ this.register(alias, {
490
+ useExisting: targetToken
491
+ });
492
+ }
493
+ }
465
494
  register(...args) {
466
495
  this.checkDisposed();
467
496
  if (args.length == 1) {
@@ -787,12 +816,12 @@ function isDisposable(value) {
787
816
  autoRegister: false,
788
817
  defaultScope: Scope.Inherited
789
818
  }) {
790
- return new DefaultContainer(undefined, options);
819
+ return new ContainerImpl(undefined, options);
791
820
  }
792
821
 
793
822
  /**
794
- * Class decorator for enabling auto-registration of an unregistered class
795
- * when first resolving it from the container.
823
+ * Class decorator that enables auto-registration of an unregistered class,
824
+ * when the class is first resolved from the container.
796
825
  *
797
826
  * @example
798
827
  * ```ts
@@ -961,7 +990,7 @@ function OptionalAll(token) {
961
990
  * const wizard = container.resolve(Wizard);
962
991
  * wizard.getWand(); // => Wand
963
992
  * ```
964
- */ const Injector = /*@__PURE__*/ Build(function Injector() {
993
+ */ const Injector = /*@__PURE__*/ build(function Injector() {
965
994
  const context = ensureInjectionContext(Injector);
966
995
  const resolution = context.resolution;
967
996
  const dependentFrame = resolution.stack.peek();
@@ -992,7 +1021,7 @@ function OptionalAll(token) {
992
1021
  });
993
1022
 
994
1023
  /**
995
- * Apply middleware functions to a container.
1024
+ * Applies middleware functions to a container.
996
1025
  *
997
1026
  * Middlewares are applied in array order, but execute in reverse order.
998
1027
  *
@@ -1036,7 +1065,6 @@ function OptionalAll(token) {
1036
1065
  }
1037
1066
 
1038
1067
  exports.AutoRegister = AutoRegister;
1039
- exports.Build = Build;
1040
1068
  exports.Inject = Inject;
1041
1069
  exports.InjectAll = InjectAll;
1042
1070
  exports.Injectable = Injectable;
@@ -1045,9 +1073,10 @@ exports.Optional = Optional;
1045
1073
  exports.OptionalAll = OptionalAll;
1046
1074
  exports.Scope = Scope;
1047
1075
  exports.Scoped = Scoped;
1048
- exports.Type = Type;
1049
1076
  exports.applyMiddleware = applyMiddleware;
1077
+ exports.build = build;
1050
1078
  exports.createContainer = createContainer;
1079
+ exports.createType = createType;
1051
1080
  exports.forwardRef = forwardRef;
1052
1081
  exports.inject = inject;
1053
1082
  exports.injectAll = injectAll;