@illuma/core 0.2.0 → 1.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.
@@ -1,75 +1,3 @@
1
- /**
2
- * Represents a constructor function type.
3
- * @template T - The type that the constructor creates
4
- */
5
- type Ctor<T> = new (...args: any[]) => T;
6
- /**
7
- * Represents a token that can be either a NodeBase token or a constructor.
8
- * @template T - The type that the token represents
9
- */
10
- type Token<T> = NodeBase<T> | Ctor<T>;
11
- /**
12
- * Options for configuring a NodeToken or MultiNodeToken.
13
- * @template T - The type of value the token represents
14
- */
15
- interface iNodeTokenBaseOptions<T> {
16
- /**
17
- * Optional factory function to create instances of this token.
18
- */
19
- factory?: () => NoInfer<T>;
20
- }
21
- /**
22
- * Provider that supplies a static value for a token.
23
- * @template T - The type of value being provided
24
- */
25
- interface iNodeValueProvider<T> {
26
- /** The token this provider is for */
27
- provide: Token<T>;
28
- /** The static value to provide */
29
- value: NoInfer<T>;
30
- }
31
- /**
32
- * Provider that uses a factory function to create instances.
33
- * @template T - The type of value being provided
34
- */
35
- interface iNodeFactoryProvider<T> {
36
- /** The token this provider is for */
37
- provide: Token<T>;
38
- /** Factory function to create the value */
39
- factory: () => NoInfer<T>;
40
- }
41
- /**
42
- * Provider that uses a class constructor to create instances.
43
- * @template T - The type of value being provided
44
- */
45
- interface iNodeClassProvider<T> {
46
- /** The token this provider is for */
47
- provide: Token<T>;
48
- /** The class to instantiate */
49
- useClass: Ctor<T>;
50
- }
51
- /**
52
- * Provider that creates an alias to another token.
53
- * When this token is injected, the aliased token's value is returned instead.
54
- * @template T - The type of value being provided
55
- */
56
- interface iNodeAliasProvider<T> {
57
- /** The token this provider is for */
58
- provide: Token<T>;
59
- /** The token to alias to */
60
- alias: Token<T>;
61
- }
62
- /**
63
- * Union type of all possible provider configurations.
64
- * @template T - The type of value being provided
65
- */
66
- type iNodeProvider<T> = iNodeValueProvider<T> | iNodeFactoryProvider<T> | iNodeClassProvider<T> | iNodeAliasProvider<T>;
67
- /**
68
- * Union type of all values that can be provided to a container.
69
- * @template T - The type of value being provided
70
- */
71
- type Provider<T = unknown> = NodeBase<T> | iNodeProvider<T> | Ctor<T> | Provider<unknown>[];
72
-
73
1
  /**
74
2
  * Base class for dependency injection tokens.
75
3
  * This class should not be instantiated directly. Use {@link NodeToken} or {@link MultiNodeToken} instead.
@@ -150,4 +78,76 @@ declare function isNodeBase<T>(specimen: unknown): specimen is NodeToken<T> | Mu
150
78
  */
151
79
  declare function extractToken<T>(provider: Token<T>, isAlias?: boolean): NodeToken<T> | MultiNodeToken<T>;
152
80
 
153
- export { type Ctor as C, MultiNodeToken as M, NodeToken as N, type Provider as P, type Token as T, NodeBase as a, type iNodeTokenBaseOptions as b, type iNodeValueProvider as c, type iNodeFactoryProvider as d, extractToken as e, type iNodeClassProvider as f, type iNodeAliasProvider as g, type iNodeProvider as h, isNodeBase as i };
81
+ /**
82
+ * Represents a constructor function type.
83
+ * @template T - The type that the constructor creates
84
+ */
85
+ type Ctor<T> = new (...args: any[]) => T;
86
+ /**
87
+ * Represents a token that can be either a NodeBase token or a constructor.
88
+ * @template T - The type that the token represents
89
+ */
90
+ type Token<T> = NodeBase<T> | Ctor<T>;
91
+ /**
92
+ * Options for configuring a NodeToken or MultiNodeToken.
93
+ * @template T - The type of value the token represents
94
+ */
95
+ interface iNodeTokenBaseOptions<T> {
96
+ /**
97
+ * Optional factory function to create instances of this token.
98
+ */
99
+ factory?: () => NoInfer<T>;
100
+ }
101
+ /**
102
+ * Provider that supplies a static value for a token.
103
+ * @template T - The type of value being provided
104
+ */
105
+ interface iNodeValueProvider<T> {
106
+ /** The token this provider is for */
107
+ provide: Token<T>;
108
+ /** The static value to provide */
109
+ value: NoInfer<T>;
110
+ }
111
+ /**
112
+ * Provider that uses a factory function to create instances.
113
+ * @template T - The type of value being provided
114
+ */
115
+ interface iNodeFactoryProvider<T> {
116
+ /** The token this provider is for */
117
+ provide: Token<T>;
118
+ /** Factory function to create the value */
119
+ factory: () => NoInfer<T>;
120
+ }
121
+ /**
122
+ * Provider that uses a class constructor to create instances.
123
+ * @template T - The type of value being provided
124
+ */
125
+ interface iNodeClassProvider<T> {
126
+ /** The token this provider is for */
127
+ provide: Token<T>;
128
+ /** The class to instantiate */
129
+ useClass: Ctor<T>;
130
+ }
131
+ /**
132
+ * Provider that creates an alias to another token.
133
+ * When this token is injected, the aliased token's value is returned instead.
134
+ * @template T - The type of value being provided
135
+ */
136
+ interface iNodeAliasProvider<T> {
137
+ /** The token this provider is for */
138
+ provide: Token<T>;
139
+ /** The token to alias to */
140
+ alias: Token<T>;
141
+ }
142
+ /**
143
+ * Union type of all possible provider configurations.
144
+ * @template T - The type of value being provided
145
+ */
146
+ type iNodeProvider<T> = iNodeValueProvider<T> | iNodeFactoryProvider<T> | iNodeClassProvider<T> | iNodeAliasProvider<T>;
147
+ /**
148
+ * Union type of all values that can be provided to a container.
149
+ * @template T - The type of value being provided
150
+ */
151
+ type Provider<T = unknown> = NodeBase<T> | iNodeProvider<T> | Ctor<T> | Provider<unknown>[];
152
+
153
+ export { type Ctor as C, MultiNodeToken as M, NodeBase as N, type Provider as P, type Token as T, NodeToken as a, type iNodeTokenBaseOptions as b, type iNodeValueProvider as c, type iNodeFactoryProvider as d, extractToken as e, type iNodeClassProvider as f, type iNodeAliasProvider as g, type iNodeProvider as h, isNodeBase as i };
@@ -1,75 +1,3 @@
1
- /**
2
- * Represents a constructor function type.
3
- * @template T - The type that the constructor creates
4
- */
5
- type Ctor<T> = new (...args: any[]) => T;
6
- /**
7
- * Represents a token that can be either a NodeBase token or a constructor.
8
- * @template T - The type that the token represents
9
- */
10
- type Token<T> = NodeBase<T> | Ctor<T>;
11
- /**
12
- * Options for configuring a NodeToken or MultiNodeToken.
13
- * @template T - The type of value the token represents
14
- */
15
- interface iNodeTokenBaseOptions<T> {
16
- /**
17
- * Optional factory function to create instances of this token.
18
- */
19
- factory?: () => NoInfer<T>;
20
- }
21
- /**
22
- * Provider that supplies a static value for a token.
23
- * @template T - The type of value being provided
24
- */
25
- interface iNodeValueProvider<T> {
26
- /** The token this provider is for */
27
- provide: Token<T>;
28
- /** The static value to provide */
29
- value: NoInfer<T>;
30
- }
31
- /**
32
- * Provider that uses a factory function to create instances.
33
- * @template T - The type of value being provided
34
- */
35
- interface iNodeFactoryProvider<T> {
36
- /** The token this provider is for */
37
- provide: Token<T>;
38
- /** Factory function to create the value */
39
- factory: () => NoInfer<T>;
40
- }
41
- /**
42
- * Provider that uses a class constructor to create instances.
43
- * @template T - The type of value being provided
44
- */
45
- interface iNodeClassProvider<T> {
46
- /** The token this provider is for */
47
- provide: Token<T>;
48
- /** The class to instantiate */
49
- useClass: Ctor<T>;
50
- }
51
- /**
52
- * Provider that creates an alias to another token.
53
- * When this token is injected, the aliased token's value is returned instead.
54
- * @template T - The type of value being provided
55
- */
56
- interface iNodeAliasProvider<T> {
57
- /** The token this provider is for */
58
- provide: Token<T>;
59
- /** The token to alias to */
60
- alias: Token<T>;
61
- }
62
- /**
63
- * Union type of all possible provider configurations.
64
- * @template T - The type of value being provided
65
- */
66
- type iNodeProvider<T> = iNodeValueProvider<T> | iNodeFactoryProvider<T> | iNodeClassProvider<T> | iNodeAliasProvider<T>;
67
- /**
68
- * Union type of all values that can be provided to a container.
69
- * @template T - The type of value being provided
70
- */
71
- type Provider<T = unknown> = NodeBase<T> | iNodeProvider<T> | Ctor<T> | Provider<unknown>[];
72
-
73
1
  /**
74
2
  * Base class for dependency injection tokens.
75
3
  * This class should not be instantiated directly. Use {@link NodeToken} or {@link MultiNodeToken} instead.
@@ -150,4 +78,76 @@ declare function isNodeBase<T>(specimen: unknown): specimen is NodeToken<T> | Mu
150
78
  */
151
79
  declare function extractToken<T>(provider: Token<T>, isAlias?: boolean): NodeToken<T> | MultiNodeToken<T>;
152
80
 
153
- export { type Ctor as C, MultiNodeToken as M, NodeToken as N, type Provider as P, type Token as T, NodeBase as a, type iNodeTokenBaseOptions as b, type iNodeValueProvider as c, type iNodeFactoryProvider as d, extractToken as e, type iNodeClassProvider as f, type iNodeAliasProvider as g, type iNodeProvider as h, isNodeBase as i };
81
+ /**
82
+ * Represents a constructor function type.
83
+ * @template T - The type that the constructor creates
84
+ */
85
+ type Ctor<T> = new (...args: any[]) => T;
86
+ /**
87
+ * Represents a token that can be either a NodeBase token or a constructor.
88
+ * @template T - The type that the token represents
89
+ */
90
+ type Token<T> = NodeBase<T> | Ctor<T>;
91
+ /**
92
+ * Options for configuring a NodeToken or MultiNodeToken.
93
+ * @template T - The type of value the token represents
94
+ */
95
+ interface iNodeTokenBaseOptions<T> {
96
+ /**
97
+ * Optional factory function to create instances of this token.
98
+ */
99
+ factory?: () => NoInfer<T>;
100
+ }
101
+ /**
102
+ * Provider that supplies a static value for a token.
103
+ * @template T - The type of value being provided
104
+ */
105
+ interface iNodeValueProvider<T> {
106
+ /** The token this provider is for */
107
+ provide: Token<T>;
108
+ /** The static value to provide */
109
+ value: NoInfer<T>;
110
+ }
111
+ /**
112
+ * Provider that uses a factory function to create instances.
113
+ * @template T - The type of value being provided
114
+ */
115
+ interface iNodeFactoryProvider<T> {
116
+ /** The token this provider is for */
117
+ provide: Token<T>;
118
+ /** Factory function to create the value */
119
+ factory: () => NoInfer<T>;
120
+ }
121
+ /**
122
+ * Provider that uses a class constructor to create instances.
123
+ * @template T - The type of value being provided
124
+ */
125
+ interface iNodeClassProvider<T> {
126
+ /** The token this provider is for */
127
+ provide: Token<T>;
128
+ /** The class to instantiate */
129
+ useClass: Ctor<T>;
130
+ }
131
+ /**
132
+ * Provider that creates an alias to another token.
133
+ * When this token is injected, the aliased token's value is returned instead.
134
+ * @template T - The type of value being provided
135
+ */
136
+ interface iNodeAliasProvider<T> {
137
+ /** The token this provider is for */
138
+ provide: Token<T>;
139
+ /** The token to alias to */
140
+ alias: Token<T>;
141
+ }
142
+ /**
143
+ * Union type of all possible provider configurations.
144
+ * @template T - The type of value being provided
145
+ */
146
+ type iNodeProvider<T> = iNodeValueProvider<T> | iNodeFactoryProvider<T> | iNodeClassProvider<T> | iNodeAliasProvider<T>;
147
+ /**
148
+ * Union type of all values that can be provided to a container.
149
+ * @template T - The type of value being provided
150
+ */
151
+ type Provider<T = unknown> = NodeBase<T> | iNodeProvider<T> | Ctor<T> | Provider<unknown>[];
152
+
153
+ export { type Ctor as C, MultiNodeToken as M, NodeBase as N, type Provider as P, type Token as T, NodeToken as a, type iNodeTokenBaseOptions as b, type iNodeValueProvider as c, type iNodeFactoryProvider as d, extractToken as e, type iNodeClassProvider as f, type iNodeAliasProvider as g, type iNodeProvider as h, isNodeBase as i };
package/dist/testkit.cjs CHANGED
@@ -26,29 +26,6 @@ __export(testkit_exports, {
26
26
  module.exports = __toCommonJS(testkit_exports);
27
27
 
28
28
  // src/lib/errors.ts
29
- var ERR_CODES = {
30
- // Provider errors
31
- DUPLICATE_PROVIDER: 100,
32
- DUPLICATE_FACTORY: 101,
33
- INVALID_CTOR: 102,
34
- INVALID_PROVIDER: 103,
35
- // Alias errors
36
- INVALID_ALIAS: 200,
37
- LOOP_ALIAS: 201,
38
- // Bootstrap errors
39
- NOT_BOOTSTRAPPED: 300,
40
- BOOTSTRAPPED: 301,
41
- DOUBLE_BOOTSTRAP: 302,
42
- // Retrieval errors
43
- NOT_FOUND: 400,
44
- CIRCULAR_DEPENDENCY: 401,
45
- // Instantiation errors
46
- UNTRACKED: 500,
47
- OUTSIDE_CONTEXT: 501,
48
- CALLED_UTILS_OUTSIDE_CONTEXT: 502,
49
- INSTANCE_ACCESS_FAILED: 503,
50
- ACCESS_FAILED: 504
51
- };
52
29
  var InjectionError = class _InjectionError extends Error {
53
30
  static {
54
31
  __name(this, "InjectionError");
@@ -60,64 +37,64 @@ var InjectionError = class _InjectionError extends Error {
60
37
  }
61
38
  // Provider errors
62
39
  static duplicate(token) {
63
- return new _InjectionError(ERR_CODES.DUPLICATE_PROVIDER, `Duplicate provider for token "${token.toString()}" detected.`);
40
+ return new _InjectionError(100, `Duplicate provider for token "${token.toString()}" detected.`);
64
41
  }
65
42
  static duplicateFactory(token) {
66
- return new _InjectionError(ERR_CODES.DUPLICATE_FACTORY, `Tried to re-provide factory for token "${token.toString()}" detected.`);
43
+ return new _InjectionError(101, `Tried to re-provide factory for token "${token.toString()}" detected.`);
67
44
  }
68
45
  static invalidCtor(ctor) {
69
- return new _InjectionError(ERR_CODES.INVALID_CTOR, `Cannot use constructor for token "${ctor.name}". Please make sure to use @nodeInjectable() decorator`);
46
+ return new _InjectionError(102, `Cannot use constructor for token "${ctor.name}". Please make sure to use @nodeInjectable() decorator`);
70
47
  }
71
48
  static invalidProvider(provider) {
72
- return new _InjectionError(ERR_CODES.INVALID_PROVIDER, `Cannot use provider as it is neither a NodeToken nor MultiNodeToken nor a valid constructor.:
49
+ return new _InjectionError(103, `Cannot use provider as it is neither a NodeToken nor MultiNodeToken nor a valid constructor.:
73
50
  ${provider}`);
74
51
  }
75
52
  // Alias errors
76
53
  static invalidAlias(alias) {
77
54
  const aliasStr = typeof alias === "function" ? alias.name || "Unknown" : String(alias);
78
- return new _InjectionError(ERR_CODES.INVALID_ALIAS, `Invalid alias target "${aliasStr}". Alias must be a NodeToken, MultiNodeToken, or a class decorated with @NodeInjectable().`);
55
+ return new _InjectionError(200, `Invalid alias target "${aliasStr}". Alias must be a NodeToken, MultiNodeToken, or a class decorated with @NodeInjectable().`);
79
56
  }
80
57
  static loopAlias(alias) {
81
- return new _InjectionError(ERR_CODES.LOOP_ALIAS, `Token "${alias.toString()}" cannot alias itself in a loop.`);
58
+ return new _InjectionError(201, `Token "${alias.toString()}" cannot alias itself in a loop.`);
82
59
  }
83
60
  // Bootstrap errors
84
61
  static notBootstrapped() {
85
- return new _InjectionError(ERR_CODES.NOT_BOOTSTRAPPED, "Cannot retrieve providers before the container has been bootstrapped.");
62
+ return new _InjectionError(300, "Cannot retrieve providers before the container has been bootstrapped.");
86
63
  }
87
64
  static bootstrapped() {
88
- return new _InjectionError(ERR_CODES.BOOTSTRAPPED, "Cannot modify providers after the container has been bootstrapped.");
65
+ return new _InjectionError(301, "Cannot modify providers after the container has been bootstrapped.");
89
66
  }
90
67
  static doubleBootstrap() {
91
- return new _InjectionError(ERR_CODES.DOUBLE_BOOTSTRAP, "Container has already been bootstrapped and cannot be bootstrapped again.");
68
+ return new _InjectionError(302, "Container has already been bootstrapped and cannot be bootstrapped again.");
92
69
  }
93
70
  // Retrieval errors
94
71
  static notFound(token) {
95
- return new _InjectionError(ERR_CODES.NOT_FOUND, `No provider found for "${token.toString()}".`);
72
+ return new _InjectionError(400, `No provider found for "${token.toString()}".`);
96
73
  }
97
74
  static circularDependency(provider, path) {
98
75
  const providerStr = provider instanceof NodeBase ? provider.toString() : provider.name;
99
76
  const pathStr = path.map((p) => p instanceof NodeBase ? p.toString() : p.name).join(" -> ");
100
- return new _InjectionError(ERR_CODES.CIRCULAR_DEPENDENCY, `Circular dependency detected while resolving "${providerStr}":
77
+ return new _InjectionError(401, `Circular dependency detected while resolving "${providerStr}":
101
78
  ${pathStr}`);
102
79
  }
103
80
  // Instantiation errors
104
81
  static untracked(token, parent) {
105
82
  const tokenStr = token instanceof NodeBase ? token.toString() : token.name;
106
83
  const parentStr = parent instanceof NodeBase ? parent.toString() : parent.name;
107
- return new _InjectionError(ERR_CODES.UNTRACKED, `Cannot instantiate ${parentStr} because it depends on untracked injection ${tokenStr}. Please make sure all injections are properly tracked.`);
84
+ return new _InjectionError(500, `Cannot instantiate ${parentStr} because it depends on untracked injection ${tokenStr}. Please make sure all injections are properly tracked.`);
108
85
  }
109
86
  static outsideContext(token) {
110
87
  const tokenStr = token instanceof NodeBase ? token.toString() : token.name;
111
- return new _InjectionError(ERR_CODES.OUTSIDE_CONTEXT, `Cannot inject "${tokenStr}" outside of an injection context.`);
88
+ return new _InjectionError(501, `Cannot inject "${tokenStr}" outside of an injection context.`);
112
89
  }
113
90
  static calledUtilsOutsideContext() {
114
- return new _InjectionError(ERR_CODES.CALLED_UTILS_OUTSIDE_CONTEXT, "Cannot call injection utilities outside of an injection context.");
91
+ return new _InjectionError(502, "Cannot call injection utilities outside of an injection context.");
115
92
  }
116
93
  static instanceAccessFailed(token) {
117
- return new _InjectionError(ERR_CODES.INSTANCE_ACCESS_FAILED, `Failed to access instance for token "${token.toString()}". It was not properly instantiated.`);
94
+ return new _InjectionError(503, `Failed to access instance for token "${token.toString()}". It was not properly instantiated.`);
118
95
  }
119
96
  static accessFailed() {
120
- return new _InjectionError(ERR_CODES.ACCESS_FAILED, "Failed to access the requested instance due to an unknown error.");
97
+ return new _InjectionError(504, "Failed to access the requested instance due to an unknown error.");
121
98
  }
122
99
  };
123
100
 
@@ -464,29 +441,23 @@ var TreeRootNode = class {
464
441
  static {
465
442
  __name(this, "TreeRootNode");
466
443
  }
467
- instant;
468
444
  _deps = /* @__PURE__ */ new Set();
469
445
  _treePool = /* @__PURE__ */ new Map();
470
- constructor(instant = true) {
471
- this.instant = instant;
472
- }
473
446
  get dependencies() {
474
447
  return this._deps;
475
448
  }
476
449
  addDependency(node) {
477
450
  this._deps.add(node);
478
451
  }
479
- build() {
452
+ instantiate() {
480
453
  for (const dep of this._deps) {
454
+ dep.instantiate(this._treePool);
481
455
  if ("token" in dep.proto) this._treePool.set(dep.proto.token, dep);
482
- if (this.instant) dep.instantiate(this._treePool);
483
- else dep.collectPool(this._treePool);
484
456
  }
485
457
  }
486
458
  find(token) {
487
459
  const node = this._treePool.get(token);
488
460
  if (!node) return null;
489
- if (!this.instant) node.instantiate(this._treePool);
490
461
  return node;
491
462
  }
492
463
  toString() {
@@ -517,11 +488,6 @@ var TreeNodeSingle = class {
517
488
  else this._deps.set(node.proto.token, node);
518
489
  node.allocations++;
519
490
  }
520
- collectPool(pool) {
521
- for (const node of this._deps.values()) node.collectPool(pool);
522
- for (const dep of this._transparent) dep.collectPool(pool);
523
- pool.set(this.proto.token, this);
524
- }
525
491
  instantiate(pool) {
526
492
  if (this._resolved) return;
527
493
  for (const node of this._deps.values()) node.instantiate(pool);
@@ -559,10 +525,6 @@ var TreeNodeTransparent = class _TreeNodeTransparent {
559
525
  else this._deps.set(node.proto.token, node);
560
526
  node.allocations++;
561
527
  }
562
- collectPool(pool) {
563
- for (const node of this._deps.values()) node.collectPool(pool);
564
- for (const dep of this._transparent) dep.collectPool(pool);
565
- }
566
528
  instantiate(pool) {
567
529
  if (this._resolved) return;
568
530
  for (const dep of this._transparent) dep.instantiate(pool);
@@ -587,10 +549,6 @@ var TreeNodeMulti = class _TreeNodeMulti {
587
549
  constructor(proto) {
588
550
  this.proto = proto;
589
551
  }
590
- collectPool(pool) {
591
- for (const dep of this._deps) dep.collectPool(pool);
592
- pool.set(this.proto.token, this);
593
- }
594
552
  instantiate(pool) {
595
553
  if (this._resolved) return;
596
554
  for (const dep of this._deps) {
@@ -896,7 +854,7 @@ var NodeContainer = class extends Illuma {
896
854
  return parentNode.findNode(token);
897
855
  }
898
856
  _buildInjectionTree() {
899
- const root = new TreeRootNode(this._opts?.instant);
857
+ const root = new TreeRootNode();
900
858
  const cache = /* @__PURE__ */ new Map();
901
859
  const nodes = [
902
860
  ...this._protoNodes.values(),
@@ -941,7 +899,7 @@ var NodeContainer = class extends Illuma {
941
899
  value: new InjectorImpl(this)
942
900
  });
943
901
  this._rootNode = this._buildInjectionTree();
944
- this._rootNode.build();
902
+ this._rootNode.instantiate();
945
903
  this._bootstrapped = true;
946
904
  const end = performance.now();
947
905
  const duration = end - start;