@illuma/core 1.2.1 → 1.3.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/index.d.ts CHANGED
@@ -1,12 +1,13 @@
1
- import { C as Ctor, N as NodeToken, a as NodeBase, P as Provider, T as Token, M as MultiNodeToken } from './token-BvQrvm-Q.js';
2
- export { e as extractToken, g as iNodeAliasProvider, f as iNodeClassProvider, d as iNodeFactoryProvider, h as iNodeProvider, b as iNodeTokenBaseOptions, c as iNodeValueProvider, i as isNodeBase } from './token-BvQrvm-Q.js';
3
- import { i as iNodeInjectorOptions, E as ExtractInjectedType } from './injection-D22uAh1O.js';
4
- export { N as NodeInjectFn, n as nodeInject } from './injection-D22uAh1O.js';
5
- import { i as iInjectionNode, T as TreeNode, I as Illuma } from './plugin-container-CUw26ZhP.js';
1
+ import { C as Ctor, N as NodeToken, P as Provider, T as Token, M as MultiNodeToken, a as NodeBase } from './types-5mX9F_24.js';
2
+ export { e as extractToken, i as isNodeBase } from './types-5mX9F_24.js';
3
+ import { I as InjectorFn, i as iNodeInjectorOptions, E as ExtractInjectedType } from './injection-C2z7Ycba.js';
4
+ export { N as NodeInjectFn, n as nodeInject } from './injection-C2z7Ycba.js';
5
+ import { T as TreeNode, I as Illuma, i as iInjectionNode } from './plugin-container-GF2OqmiJ.js';
6
6
 
7
7
  /**
8
8
  * Symbol used to mark classes as injectable and store their associated token.
9
9
  * @internal
10
+ * @deprecated Use internal registry instead
10
11
  */
11
12
  declare const INJECTION_SYMBOL: unique symbol;
12
13
  /**
@@ -40,6 +41,7 @@ declare function NodeInjectable<T>(): (ctor: Ctor<T>) => Ctor<T>;
40
41
  * @example
41
42
  * ```typescript
42
43
  * import { makeInjectable } from '@illuma/core';
44
+ import { InjectionError } from '../errors';
43
45
  *
44
46
  * class _UserService {
45
47
  * public getUser() {
@@ -53,55 +55,21 @@ declare function NodeInjectable<T>(): (ctor: Ctor<T>) => Ctor<T>;
53
55
  */
54
56
  declare function makeInjectable<T>(ctor: Ctor<T>): Ctor<T>;
55
57
  /** @internal */
56
- declare function isInjectable<T>(ctor: unknown): ctor is Ctor<T> & {
57
- [INJECTION_SYMBOL]: NodeToken<T>;
58
- };
58
+ declare function isInjectable<T>(ctor: unknown): ctor is Ctor<T>;
59
59
  /** @internal */
60
- declare function getInjectableToken<T>(ctor: Ctor<T> & {
61
- [INJECTION_SYMBOL]: NodeToken<T>;
62
- }): NodeToken<T>;
60
+ declare function getInjectableToken<T>(ctor: Ctor<T>): NodeToken<T>;
63
61
  declare function isConstructor(fn: unknown): fn is Ctor<any>;
64
-
65
- /** @internal */
66
- type InjectorFn = (token: NodeBase<any>, optional?: boolean) => any;
67
62
  /**
68
- * Internal context manager for tracking dependency injections during factory execution.
69
- * This class manages the injection context lifecycle and tracks all injection calls.
63
+ * Registers a class as injectable with a specific token.
64
+ * Use this function to manually associate a class with a token.
70
65
  *
71
- * @internal
66
+ * Normally, the {@link NodeInjectable} and {@link makeInjectable} decorator is used to mark classes as injectable,
67
+ * but if developing a plugin that requires manual registration, this function can be used.
68
+ *
69
+ * @param ctor - The class constructor to register
70
+ * @param token - The token to associate with the class
72
71
  */
73
- declare abstract class InjectionContext {
74
- static contextOpen: boolean;
75
- static readonly _calls: Set<iInjectionNode<any>>;
76
- static injector: InjectorFn | null;
77
- private static readonly _scanners;
78
- /**
79
- * Adds a dependency to the current injection context.
80
- * Called by `nodeInject` when a dependency is requested.
81
- *
82
- * @param node - The injection node representing the dependency
83
- * @throws {InjectionError} If called outside of an active injection context
84
- */
85
- static addDep(node: iInjectionNode<any>): void;
86
- /**
87
- * Opens a new injection context.
88
- * Resets the calls set and sets the injector if provided.
89
- *
90
- * @param injector - Optional injector function to use for resolving dependencies
91
- */
92
- /**
93
- * Scans a factory function for dependencies.
94
- * Executes the factory in a dry-run mode to capture `nodeInject` calls.
95
- * Also runs registered context scanners.
96
- *
97
- * @param factory - The factory function to scan
98
- * @returns A set of detected injection nodes
99
- */
100
- static open(injector?: InjectorFn): void;
101
- static scan(factory: any): Set<iInjectionNode<any>>;
102
- static instantiate<T>(factory: () => T, injector: InjectorFn): T;
103
- static closeAndReport(): Set<iInjectionNode<any>>;
104
- }
72
+ declare function registerClassAsInjectable<T>(ctor: Ctor<T>, token: NodeToken<T>): void;
105
73
 
106
74
  /**
107
75
  * Configuration options for the NodeContainer.
@@ -257,6 +225,45 @@ declare class NodeContainer extends Illuma implements iDIContainer {
257
225
  produce<T>(fn: Ctor<T> | (() => T)): T;
258
226
  }
259
227
 
228
+ /**
229
+ * Internal context manager for tracking dependency injections during factory execution.
230
+ * This class manages the injection context lifecycle and tracks all injection calls.
231
+ *
232
+ * @internal
233
+ */
234
+ declare abstract class InjectionContext {
235
+ static contextOpen: boolean;
236
+ static readonly _calls: Set<iInjectionNode<any>>;
237
+ static injector: InjectorFn | null;
238
+ private static readonly _scanners;
239
+ /**
240
+ * Adds a dependency to the current injection context.
241
+ * Called by `nodeInject` when a dependency is requested.
242
+ *
243
+ * @param node - The injection node representing the dependency
244
+ * @throws {InjectionError} If called outside of an active injection context
245
+ */
246
+ static addDep(node: iInjectionNode<any>): void;
247
+ /**
248
+ * Opens a new injection context.
249
+ * Resets the calls set and sets the injector if provided.
250
+ *
251
+ * @param injector - Optional injector function to use for resolving dependencies
252
+ */
253
+ /**
254
+ * Scans a factory function for dependencies.
255
+ * Executes the factory in a dry-run mode to capture `nodeInject` calls.
256
+ * Also runs registered context scanners.
257
+ *
258
+ * @param factory - The factory function to scan
259
+ * @returns A set of detected injection nodes
260
+ */
261
+ static open(injector?: InjectorFn): void;
262
+ static scan(factory: any): Set<iInjectionNode<any>>;
263
+ static instantiate<T>(factory: () => T, injector: InjectorFn): T;
264
+ static closeAndReport(): Set<iInjectionNode<any>>;
265
+ }
266
+
260
267
  declare const ERR_CODES: {
261
268
  readonly DUPLICATE_PROVIDER: 100;
262
269
  readonly DUPLICATE_FACTORY: 101;
@@ -419,4 +426,4 @@ declare function injectEntryAsync<T>(fn: MaybeAsyncFactory<iEntrypointConfig<Nod
419
426
  declare function injectEntryAsync<T>(fn: MaybeAsyncFactory<iEntrypointConfig<Ctor<T>>>, opts?: iInjectionOptions): () => Promise<T>;
420
427
  declare function injectEntryAsync<T>(fn: MaybeAsyncFactory<iEntrypointConfig<MultiNodeToken<T>>>, opts?: iInjectionOptions): () => Promise<T>;
421
428
 
422
- export { Ctor, ExtractInjectedType, ERR_CODES as ILLUMA_ERR_CODES, INJECTION_SYMBOL, InjectionContext, InjectionError, Injector, type InjectorFn, InjectorImpl, MultiNodeToken, NodeBase, NodeContainer, NodeInjectable, NodeToken, Provider, Token, getInjectableToken, type iEntrypointConfig, iInjectionNode, type iInjector, iNodeInjectorOptions, injectAsync, injectDefer, injectEntryAsync, injectGroupAsync, isConstructor, isInjectable, makeInjectable };
429
+ export { ExtractInjectedType, ERR_CODES as ILLUMA_ERR_CODES, INJECTION_SYMBOL, InjectionContext, InjectionError, Injector, InjectorFn, InjectorImpl, MultiNodeToken, NodeBase, NodeContainer, NodeInjectable, NodeToken, getInjectableToken, type iContainerOptions, type iDIContainer, type iEntrypointConfig, iInjectionNode, type iInjector, iNodeInjectorOptions, injectAsync, injectDefer, injectEntryAsync, injectGroupAsync, isConstructor, isInjectable, makeInjectable, registerClassAsInjectable };
package/dist/index.js CHANGED
@@ -1,6 +1,84 @@
1
1
  var __defProp = Object.defineProperty;
2
2
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
3
 
4
+ // src/lib/api/token.ts
5
+ var NodeBase = class {
6
+ static {
7
+ __name(this, "NodeBase");
8
+ }
9
+ name;
10
+ opts;
11
+ constructor(name, opts) {
12
+ this.name = name;
13
+ this.opts = opts;
14
+ }
15
+ /** Provides this token with a value */
16
+ withValue(value) {
17
+ return {
18
+ provide: this,
19
+ value
20
+ };
21
+ }
22
+ /** Provides this token using a factory function */
23
+ withFactory(factory) {
24
+ return {
25
+ provide: this,
26
+ factory
27
+ };
28
+ }
29
+ /** Provides this token using a class constructor */
30
+ withClass(ctor) {
31
+ return {
32
+ provide: this,
33
+ useClass: ctor
34
+ };
35
+ }
36
+ /** Creates an alias to another token */
37
+ withAlias(alias) {
38
+ return {
39
+ provide: this,
40
+ alias
41
+ };
42
+ }
43
+ toString() {
44
+ return `Token[${this.name}]`;
45
+ }
46
+ };
47
+ var NodeToken = class extends NodeBase {
48
+ static {
49
+ __name(this, "NodeToken");
50
+ }
51
+ multi = false;
52
+ toString() {
53
+ return `NodeToken[${this.name}]`;
54
+ }
55
+ };
56
+ var MultiNodeToken = class extends NodeBase {
57
+ static {
58
+ __name(this, "MultiNodeToken");
59
+ }
60
+ multi = true;
61
+ toString() {
62
+ return `MultiNodeToken[${this.name}]`;
63
+ }
64
+ };
65
+ function isNodeBase(specimen) {
66
+ return specimen instanceof NodeToken || specimen instanceof MultiNodeToken;
67
+ }
68
+ __name(isNodeBase, "isNodeBase");
69
+ function extractToken(provider, isAlias = false) {
70
+ let token = null;
71
+ if (isInjectable(provider)) {
72
+ token = getInjectableToken(provider);
73
+ } else if (isNodeBase(provider)) token = provider;
74
+ if (!token || !isNodeBase(token)) {
75
+ if (isAlias) throw InjectionError.invalidAlias(provider);
76
+ throw InjectionError.invalidProvider(JSON.stringify(provider));
77
+ }
78
+ return token;
79
+ }
80
+ __name(extractToken, "extractToken");
81
+
4
82
  // src/lib/errors.ts
5
83
  var ERR_CODES = {
6
84
  // Provider errors
@@ -101,92 +179,15 @@ function isNotFoundError(error) {
101
179
  }
102
180
  __name(isNotFoundError, "isNotFoundError");
103
181
 
104
- // src/lib/api/token.ts
105
- var NodeBase = class {
106
- static {
107
- __name(this, "NodeBase");
108
- }
109
- name;
110
- opts;
111
- constructor(name, opts) {
112
- this.name = name;
113
- this.opts = opts;
114
- }
115
- /** Provides this token with a value */
116
- withValue(value) {
117
- return {
118
- provide: this,
119
- value
120
- };
121
- }
122
- /** Provides this token using a factory function */
123
- withFactory(factory) {
124
- return {
125
- provide: this,
126
- factory
127
- };
128
- }
129
- /** Provides this token using a class constructor */
130
- withClass(ctor) {
131
- return {
132
- provide: this,
133
- useClass: ctor
134
- };
135
- }
136
- /** Creates an alias to another token */
137
- withAlias(alias) {
138
- return {
139
- provide: this,
140
- alias
141
- };
142
- }
143
- toString() {
144
- return `Token[${this.name}]`;
145
- }
146
- };
147
- var NodeToken = class extends NodeBase {
148
- static {
149
- __name(this, "NodeToken");
150
- }
151
- multi = false;
152
- toString() {
153
- return `NodeToken[${this.name}]`;
154
- }
155
- };
156
- var MultiNodeToken = class extends NodeBase {
157
- static {
158
- __name(this, "MultiNodeToken");
159
- }
160
- multi = true;
161
- toString() {
162
- return `MultiNodeToken[${this.name}]`;
163
- }
164
- };
165
- function isNodeBase(specimen) {
166
- return specimen instanceof NodeToken || specimen instanceof MultiNodeToken;
167
- }
168
- __name(isNodeBase, "isNodeBase");
169
- function extractToken(provider, isAlias = false) {
170
- let token = null;
171
- if (isInjectable(provider)) {
172
- token = getInjectableToken(provider);
173
- } else if (isNodeBase(provider)) token = provider;
174
- if (!token || !isNodeBase(token)) {
175
- if (isAlias) throw InjectionError.invalidAlias(provider);
176
- throw InjectionError.invalidProvider(JSON.stringify(provider));
177
- }
178
- return token;
179
- }
180
- __name(extractToken, "extractToken");
181
-
182
182
  // src/lib/api/decorator.ts
183
+ var tokenRegistry = /* @__PURE__ */ new WeakMap();
183
184
  var INJECTION_SYMBOL = Symbol("Injectable");
184
185
  function NodeInjectable() {
185
186
  return (ctor) => {
186
187
  const nodeToken = new NodeToken(`_${ctor.name}`, {
187
188
  factory: /* @__PURE__ */ __name(() => new ctor(), "factory")
188
189
  });
189
- ctor[INJECTION_SYMBOL] = nodeToken;
190
+ registerClassAsInjectable(ctor, nodeToken);
190
191
  return ctor;
191
192
  };
192
193
  }
@@ -195,22 +196,30 @@ function makeInjectable(ctor) {
195
196
  const nodeToken = new NodeToken(`_${ctor.name}`, {
196
197
  factory: /* @__PURE__ */ __name(() => new ctor(), "factory")
197
198
  });
198
- ctor[INJECTION_SYMBOL] = nodeToken;
199
+ registerClassAsInjectable(ctor, nodeToken);
199
200
  return ctor;
200
201
  }
201
202
  __name(makeInjectable, "makeInjectable");
202
203
  function isInjectable(ctor) {
203
- return typeof ctor === "function" && isConstructor(ctor) && INJECTION_SYMBOL in ctor && ctor[INJECTION_SYMBOL] instanceof NodeToken;
204
+ return typeof ctor === "function" && isConstructor(ctor) && (tokenRegistry.has(ctor) || INJECTION_SYMBOL in ctor);
204
205
  }
205
206
  __name(isInjectable, "isInjectable");
206
207
  function getInjectableToken(ctor) {
207
- return ctor[INJECTION_SYMBOL];
208
+ if (tokenRegistry.has(ctor)) return tokenRegistry.get(ctor);
209
+ if (INJECTION_SYMBOL in ctor) {
210
+ return ctor[INJECTION_SYMBOL];
211
+ }
212
+ throw InjectionError.invalidCtor(ctor);
208
213
  }
209
214
  __name(getInjectableToken, "getInjectableToken");
210
215
  function isConstructor(fn) {
211
216
  return typeof fn === "function" && fn.prototype && fn.prototype.constructor === fn;
212
217
  }
213
218
  __name(isConstructor, "isConstructor");
219
+ function registerClassAsInjectable(ctor, token) {
220
+ tokenRegistry.set(ctor, token);
221
+ }
222
+ __name(registerClassAsInjectable, "registerClassAsInjectable");
214
223
 
215
224
  // src/lib/plugins/core/plugin-container.ts
216
225
  var Illuma = class _Illuma {
@@ -464,7 +473,7 @@ var TreeRootNode = class {
464
473
  }
465
474
  instant;
466
475
  _deps = /* @__PURE__ */ new Set();
467
- _treePool = /* @__PURE__ */ new Map();
476
+ _treePool = /* @__PURE__ */ new WeakMap();
468
477
  constructor(instant = true) {
469
478
  this.instant = instant;
470
479
  }
@@ -1126,6 +1135,7 @@ export {
1126
1135
  isInjectable,
1127
1136
  isNodeBase,
1128
1137
  makeInjectable,
1129
- nodeInject
1138
+ nodeInject,
1139
+ registerClassAsInjectable
1130
1140
  };
1131
1141
  //# sourceMappingURL=index.js.map