@lppedd/di-wise-neo 0.8.0 → 0.8.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/cjs/index.js CHANGED
@@ -1,5 +1,32 @@
1
1
  Object.defineProperty(exports, '__esModule', { value: true });
2
2
 
3
+ /**
4
+ * Type API.
5
+ */ /**
6
+ * Creates a type token.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * const ISpell = createType<Spell>("Spell");
11
+ * ```
12
+ *
13
+ * @__NO_SIDE_EFFECTS__
14
+ */ function createType(typeName) {
15
+ const type = {
16
+ name: `Type<${typeName}>`,
17
+ inter: createType,
18
+ union: createType,
19
+ toString () {
20
+ return type.name;
21
+ }
22
+ };
23
+ return type;
24
+ }
25
+ // @internal
26
+ function isConstructor(token) {
27
+ return typeof token === "function";
28
+ }
29
+
3
30
  // @internal
4
31
  function assert(condition, message) {
5
32
  if (!condition) {
@@ -12,7 +39,8 @@ function expectNever(value) {
12
39
  }
13
40
  // @internal
14
41
  function throwUnregisteredError(token) {
15
- throw new Error(tag(`unregistered token ${token.name}`));
42
+ const type = isConstructor(token) ? "class" : "token";
43
+ throw new Error(tag(`unregistered ${type} ${token.name}`));
16
44
  }
17
45
  // @internal
18
46
  function throwExistingUnregisteredError(sourceToken, targetTokenOrError) {
@@ -290,33 +318,6 @@ const Scope = {
290
318
  Container: "Container"
291
319
  };
292
320
 
293
- /**
294
- * Type API.
295
- */ /**
296
- * Creates a type token.
297
- *
298
- * @example
299
- * ```ts
300
- * const ISpell = createType<Spell>("Spell");
301
- * ```
302
- *
303
- * @__NO_SIDE_EFFECTS__
304
- */ function createType(typeName) {
305
- const type = {
306
- name: `Type<${typeName}>`,
307
- inter: createType,
308
- union: createType,
309
- toString () {
310
- return type.name;
311
- }
312
- };
313
- return type;
314
- }
315
- // @internal
316
- function isConstructor(token) {
317
- return typeof token === "function";
318
- }
319
-
320
321
  // @internal
321
322
  function getTypeName(value) {
322
323
  switch(typeof value){
@@ -621,7 +622,7 @@ function isDisposable(value) {
621
622
  if (isConstructor(token)) {
622
623
  return this.instantiateClass(token, localOptional);
623
624
  }
624
- return optionalOrName ? undefined : throwUnregisteredError(token);
625
+ return localOptional ? undefined : throwUnregisteredError(token);
625
626
  }
626
627
  resolveAll(token, optional) {
627
628
  this.checkDisposed();
@@ -988,10 +989,22 @@ function updateParameterMetadata(decorator, target, propertyKey, parameterIndex,
988
989
  updateFn(dependency);
989
990
  }
990
991
  }
992
+ // Checks that a constructor or method parameter has only one injection decorator.
993
+ // For example, if both `@Inject` and `@Optional` are used, it becomes difficult to
994
+ // understand which one "wins", unless the user is aware of the decorator evaluation order.
995
+ //
996
+ // @internal
997
+ function checkSingleDecorator(dependency, target, propertyKey, parameterIndex) {
998
+ assert(!dependency.appliedBy, ()=>{
999
+ const where = propertyKey === undefined ? `${target.name} constructor` : `${target.constructor.name}.${String(propertyKey)}`;
1000
+ return `${where} parameter ${parameterIndex} declares multiple injection decorators, but only one is allowed`;
1001
+ });
1002
+ }
991
1003
 
992
1004
  function Inject(token) {
993
1005
  return function(target, propertyKey, parameterIndex) {
994
1006
  updateParameterMetadata("Inject", target, propertyKey, parameterIndex, (dependency)=>{
1007
+ checkSingleDecorator(dependency, target, propertyKey, parameterIndex);
995
1008
  dependency.appliedBy = "Inject";
996
1009
  dependency.tokenRef = isTokenRef(token) ? token : forwardRef(()=>token);
997
1010
  });
@@ -1021,6 +1034,7 @@ function Inject(token) {
1021
1034
  function InjectAll(token) {
1022
1035
  return function(target, propertyKey, parameterIndex) {
1023
1036
  updateParameterMetadata("InjectAll", target, propertyKey, parameterIndex, (dependency)=>{
1037
+ checkSingleDecorator(dependency, target, propertyKey, parameterIndex);
1024
1038
  dependency.appliedBy = "InjectAll";
1025
1039
  dependency.tokenRef = isTokenRef(token) ? token : forwardRef(()=>token);
1026
1040
  });
@@ -1068,6 +1082,7 @@ function InjectAll(token) {
1068
1082
  function Optional(token) {
1069
1083
  return function(target, propertyKey, parameterIndex) {
1070
1084
  updateParameterMetadata("Optional", target, propertyKey, parameterIndex, (dependency)=>{
1085
+ checkSingleDecorator(dependency, target, propertyKey, parameterIndex);
1071
1086
  dependency.appliedBy = "Optional";
1072
1087
  dependency.tokenRef = isTokenRef(token) ? token : forwardRef(()=>token);
1073
1088
  });
@@ -1077,6 +1092,7 @@ function Optional(token) {
1077
1092
  function OptionalAll(token) {
1078
1093
  return function(target, propertyKey, parameterIndex) {
1079
1094
  updateParameterMetadata("OptionalAll", target, propertyKey, parameterIndex, (dependency)=>{
1095
+ checkSingleDecorator(dependency, target, propertyKey, parameterIndex);
1080
1096
  dependency.appliedBy = "OptionalAll";
1081
1097
  dependency.tokenRef = isTokenRef(token) ? token : forwardRef(()=>token);
1082
1098
  });
@@ -1142,7 +1158,7 @@ function OptionalAll(token) {
1142
1158
  const resolution = context.resolution;
1143
1159
  const dependentFrame = resolution.stack.peek();
1144
1160
  const dependentRef = dependentFrame && resolution.dependents.get(dependentFrame.provider);
1145
- function withContext(fn) {
1161
+ const runInContext = (fn)=>{
1146
1162
  if (useInjectionContext()) {
1147
1163
  return fn();
1148
1164
  }
@@ -1156,13 +1172,13 @@ function OptionalAll(token) {
1156
1172
  } finally{
1157
1173
  cleanups.forEach((cleanup)=>cleanup?.());
1158
1174
  }
1159
- }
1175
+ };
1160
1176
  return {
1161
- inject: (token, name)=>withContext(()=>inject(token, name)),
1162
- injectAll: (token)=>withContext(()=>injectAll(token)),
1163
- optional: (token, name)=>withContext(()=>optional(token, name)),
1164
- optionalAll: (token)=>withContext(()=>optionalAll(token)),
1165
- runInContext: withContext
1177
+ inject: (token, name)=>runInContext(()=>inject(token, name)),
1178
+ injectAll: (token)=>runInContext(()=>injectAll(token)),
1179
+ optional: (token, name)=>runInContext(()=>optional(token, name)),
1180
+ optionalAll: (token)=>runInContext(()=>optionalAll(token)),
1181
+ runInContext
1166
1182
  };
1167
1183
  }, "Injector");
1168
1184