@fluixi/di 1.0.0-alpha.108 → 1.0.0-alpha.110

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,268 +1,14 @@
1
- var __typeError = (msg) => {
2
- throw TypeError(msg);
3
- };
4
- var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
5
- var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
6
- var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
7
- var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
8
- var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
9
-
10
- // src/hierarchy.ts
11
- import { getRequestContext, onRequestEnd } from "@fluixi/dom";
12
- import { getOwner } from "@fluixi/reactive/signal";
13
-
14
- // src/injector.ts
15
- import { openReactiveScope, withReactiveScope } from "@fluixi/reactive/signal";
16
-
17
- // src/errors.ts
18
- var InjectionError = class extends Error {
19
- /**
20
- * @param message - What went wrong, with the path rendered into it.
21
- * @param path - The tokens being resolved, outermost first; the last one is the one that failed.
22
- */
23
- constructor(message, path) {
24
- super(message);
25
- this.path = path;
26
- this.name = "InjectionError";
27
- }
28
- };
29
-
30
- // src/state.ts
31
- function freshState() {
32
- return {
33
- platformProviders: [],
34
- rootProviders: [],
35
- platform: null,
36
- clientRoot: null,
37
- requestRoots: /* @__PURE__ */ new WeakMap(),
38
- ownerInjectors: /* @__PURE__ */ new WeakMap(),
39
- moduleScopes: /* @__PURE__ */ new WeakMap(),
40
- overrides: /* @__PURE__ */ new Map(),
41
- explicit: null,
42
- path: []
43
- };
44
- }
45
- var state = globalThis[/* @__PURE__ */ Symbol.for("fluixi.di")] ??= freshState();
46
-
47
- // src/token.ts
48
- function injectionToken(name, options) {
49
- return { id: Symbol(name), name, factory: options?.factory, scope: options?.scope ?? "root" };
50
- }
51
-
52
- // src/tokens.ts
53
- var DestroyRef = injectionToken("DestroyRef");
54
- var INJECTOR = injectionToken("Injector");
55
-
56
- // src/injector.ts
57
- var _records, _created, _hooks, _destroyed, _Injector_instances, home_fn, fromProvider_fn, build_fn, make_fn;
58
- var Injector = class {
59
- /**
60
- * @param parent - Where a token this injector does not hold is looked for next; `null` for the
61
- * top of a chain.
62
- * @param kind - Where the injector sits in the hierarchy; see {@link InjectorKind}.
63
- */
64
- constructor(parent = null, kind = "standalone") {
65
- this.parent = parent;
66
- this.kind = kind;
67
- __privateAdd(this, _Injector_instances);
68
- __privateAdd(this, _records, /* @__PURE__ */ new Map());
69
- __privateAdd(this, _created, []);
70
- __privateAdd(this, _hooks, []);
71
- __privateAdd(this, _destroyed, false);
72
- const self = this;
73
- this.destroyRef = {
74
- onDestroy(fn) {
75
- __privateGet(self, _hooks).push(fn);
76
- return () => {
77
- const i = __privateGet(self, _hooks).indexOf(fn);
78
- if (i >= 0) __privateGet(self, _hooks).splice(i, 1);
79
- };
80
- },
81
- get destroyed() {
82
- return __privateGet(self, _destroyed);
83
- }
84
- };
85
- }
86
- /** Whether {@link Injector.dispose} has run. A disposed injector refuses every `get`. */
87
- get destroyed() {
88
- return __privateGet(this, _destroyed);
89
- }
90
- /**
91
- * Add providers to this injector. A provider for a token already held replaces it, except
92
- * `multi` providers, which extend the token's list. A provider whose token has a test override
93
- * is replaced by the override.
94
- *
95
- * @param providers - The providers to add, in order.
96
- * @returns This injector, for chaining.
97
- */
98
- register(providers) {
99
- for (const given of providers) {
100
- const p = state.overrides.get(given.provide.id) ?? given;
101
- if (p.multi) {
102
- const rec = __privateGet(this, _records).get(p.provide.id);
103
- if (rec?.multi) {
104
- rec.providers.push(p);
105
- rec.built = false;
106
- } else __privateGet(this, _records).set(p.provide.id, { multi: true, providers: [p], built: false, building: false, value: [] });
107
- } else {
108
- __privateGet(this, _records).set(p.provide.id, { multi: false, provider: p, built: false, building: false, value: void 0 });
109
- }
110
- }
111
- return this;
112
- }
113
- get(token, options = {}) {
114
- var _a, _b, _c;
115
- if (__privateGet(this, _destroyed)) throw new InjectionError(`[fluixi/di] "${token.name}" requested from a disposed injector.`, [...state.path, token.name]);
116
- if (token.id === INJECTOR.id) return this;
117
- if (token.id === DestroyRef.id) return this.destroyRef;
118
- let inj = options.skipSelf ? this.parent : this;
119
- while (inj) {
120
- const rec = __privateGet(inj, _records).get(token.id);
121
- if (rec) return __privateMethod(_a = inj, _Injector_instances, build_fn).call(_a, token, rec);
122
- if (options.self || options.host && inj.kind === "owner") break;
123
- inj = inj.parent;
124
- }
125
- const override = state.overrides.get(token.id);
126
- if (override && !options.self) return __privateMethod(_b = __privateMethod(this, _Injector_instances, home_fn).call(this, token.scope), _Injector_instances, fromProvider_fn).call(_b, token, override);
127
- if (token.factory && !options.self && !options.host) {
128
- return __privateMethod(_c = __privateMethod(this, _Injector_instances, home_fn).call(this, token.scope), _Injector_instances, fromProvider_fn).call(_c, token, { provide: token, useFactory: token.factory });
129
- }
130
- if (options.optional) return null;
131
- const path = [...state.path, token.name];
132
- throw new InjectionError(
133
- `[fluixi/di] No provider for "${token.name}" (${path.join(" → ")}). Provide it in an app module, with provide() or provideRoot(), or give the token a { factory }.`,
134
- path
135
- );
136
- }
137
- /**
138
- * Release this injector: run the hooks registered on its `DestroyRef`, latest first, then the
139
- * `onDestroy()` method of every service it built, latest built first, then forget its
140
- * providers. A hook or `onDestroy()` that throws is logged and the rest still run. Running it
141
- * again does nothing.
142
- *
143
- * Component injectors are disposed when the component unmounts, request roots when the request
144
- * ends; call it yourself only for injectors you created.
145
- */
146
- dispose() {
147
- if (__privateGet(this, _destroyed)) return;
148
- __privateSet(this, _destroyed, true);
149
- const fail = (error) => console.error("[fluixi/di] a destroy hook threw:", error);
150
- for (const hook of __privateGet(this, _hooks).splice(0).reverse()) {
151
- try {
152
- hook();
153
- } catch (error) {
154
- fail(error);
155
- }
156
- }
157
- for (const value of __privateGet(this, _created).splice(0).reverse()) {
158
- const service = value;
159
- if (service && typeof service.onDestroy === "function") {
160
- try {
161
- service.onDestroy();
162
- } catch (error) {
163
- fail(error);
164
- }
165
- }
166
- }
167
- __privateGet(this, _records).clear();
168
- }
169
- };
170
- _records = new WeakMap();
171
- _created = new WeakMap();
172
- _hooks = new WeakMap();
173
- _destroyed = new WeakMap();
174
- _Injector_instances = new WeakSet();
175
- /** The injector a self-providing token builds in: the nearest root, or the platform. */
176
- home_fn = function(scope) {
177
- let inj = this;
178
- if (scope === "platform") {
179
- while (inj.parent) inj = inj.parent;
180
- return inj.kind === "platform" ? inj : platformInjector();
181
- }
182
- let probe = this;
183
- while (probe) {
184
- if (probe.kind === "root") return probe;
185
- probe = probe.parent;
186
- }
187
- while (inj.parent && inj.parent.kind !== "platform") inj = inj.parent;
188
- return inj;
189
- };
190
- fromProvider_fn = function(token, provider) {
191
- let rec = __privateGet(this, _records).get(token.id);
192
- if (!rec) {
193
- rec = { multi: false, provider, built: false, building: false, value: void 0 };
194
- __privateGet(this, _records).set(token.id, rec);
195
- }
196
- return __privateMethod(this, _Injector_instances, build_fn).call(this, token, rec);
197
- };
198
- build_fn = function(token, rec) {
199
- if (rec.built) return rec.value;
200
- if (rec.building) {
201
- const path = [...state.path, token.name];
202
- throw new InjectionError(`[fluixi/di] Circular dependency: ${path.join(" → ")}.`, path);
203
- }
204
- rec.building = true;
205
- state.path.push(token.name);
206
- const previous = state.explicit;
207
- state.explicit = this;
208
- try {
209
- if (rec.multi) {
210
- rec.value = rec.providers.map((p) => __privateMethod(this, _Injector_instances, make_fn).call(this, token, p));
211
- } else {
212
- const module = state.moduleScopes.get(rec.provider);
213
- rec.value = module ? withReactiveScope(module, () => __privateMethod(this, _Injector_instances, make_fn).call(this, token, rec.provider)) : __privateMethod(this, _Injector_instances, make_fn).call(this, token, rec.provider);
214
- }
215
- rec.built = true;
216
- return rec.value;
217
- } finally {
218
- state.explicit = previous;
219
- state.path.pop();
220
- rec.building = false;
221
- }
222
- };
223
- make_fn = function(token, p) {
224
- if ("useValue" in p) return p.useValue;
225
- if ("useExisting" in p) return this.get(p.useExisting);
226
- const deps = (p.deps ?? []).map((d) => "token" in d ? this.get(d.token, d) : this.get(d));
227
- const scope = openReactiveScope({ kind: "service", name: token.name, detail: "useClass" in p ? "useClass" : "useFactory" });
228
- const value = withReactiveScope(scope, () => "useClass" in p ? new p.useClass(...deps) : p.useFactory(...deps));
229
- __privateGet(this, _created).push(value);
230
- return value;
231
- };
232
-
233
- // src/hierarchy.ts
234
- function platformInjector() {
235
- return state.platform ??= new Injector(null, "platform").register(state.platformProviders);
236
- }
237
- function rootInjector() {
238
- const ctx = getRequestContext();
239
- if (ctx) {
240
- let inj = state.requestRoots.get(ctx);
241
- if (!inj) {
242
- const root = new Injector(platformInjector(), "root").register(state.rootProviders);
243
- state.requestRoots.set(ctx, root);
244
- onRequestEnd(() => {
245
- state.requestRoots.delete(ctx);
246
- root.dispose();
247
- });
248
- inj = root;
249
- }
250
- return inj;
251
- }
252
- return state.clientRoot ??= new Injector(platformInjector(), "root").register(state.rootProviders);
253
- }
254
- function nearestOwnerInjector(owner) {
255
- let o = owner;
256
- while (o) {
257
- const inj = state.ownerInjectors.get(o);
258
- if (inj) return inj;
259
- o = o.parent;
260
- }
261
- return null;
262
- }
263
- function currentInjector() {
264
- return state.explicit ?? nearestOwnerInjector(getOwner()) ?? rootInjector();
265
- }
1
+ import {
2
+ currentInjector,
3
+ nearestOwnerInjector,
4
+ platformInjector,
5
+ rootInjector
6
+ } from "./chunk-NZ4DEJ6M.mjs";
7
+ import "./chunk-6Y4QWQA7.mjs";
8
+ import "./chunk-2GEQYTZE.mjs";
9
+ import "./chunk-5BUN4TXK.mjs";
10
+ import "./chunk-E32UTSJA.mjs";
11
+ import "./chunk-I6KKUHEY.mjs";
266
12
  export {
267
13
  currentInjector,
268
14
  nearestOwnerInjector,
package/dist/index.cjs CHANGED
@@ -1,4 +1,4 @@
1
- /*! @fluixi/di v1.0.0-alpha.108 | (c) 2026 Ibrahima Touré and Fluixi contributors | MIT */
1
+ /*! @fluixi/di v1.0.0-alpha.110 | (c) 2026 Ibrahima Touré and Fluixi contributors | MIT */
2
2
  "use strict";
3
3
  var __defProp = Object.defineProperty;
4
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
@@ -104,14 +104,14 @@ function platformInjector() {
104
104
  return state.platform ??= new Injector(null, "platform").register(state.platformProviders);
105
105
  }
106
106
  function rootInjector() {
107
- const ctx = (0, import_dom.getRequestContext)();
108
- if (ctx) {
109
- let inj = state.requestRoots.get(ctx);
107
+ const scope = (0, import_dom.requestScope)();
108
+ if (scope) {
109
+ let inj = state.requestRoots.get(scope);
110
110
  if (!inj) {
111
111
  const root = new Injector(platformInjector(), "root").register(state.rootProviders);
112
- state.requestRoots.set(ctx, root);
112
+ state.requestRoots.set(scope, root);
113
113
  (0, import_dom.onRequestEnd)(() => {
114
- state.requestRoots.delete(ctx);
114
+ state.requestRoots.delete(scope);
115
115
  root.dispose();
116
116
  });
117
117
  inj = root;
@@ -130,7 +130,13 @@ function nearestOwnerInjector(owner) {
130
130
  return null;
131
131
  }
132
132
  function currentInjector() {
133
- return state.explicit ?? nearestOwnerInjector((0, import_signal.getOwner)()) ?? rootInjector();
133
+ if (state.explicit) return state.explicit;
134
+ const owner = (0, import_signal.getOwner)();
135
+ if (owner) return nearestOwnerInjector(owner) ?? rootInjector();
136
+ if ((0, import_dom.requestScope)()) return rootInjector();
137
+ throw new Error(
138
+ "[fluixi/di] inject() called outside an injection context (after an await? in an event handler?). Inject during setup and keep the value, or keep the injector (getInjector()) and use runInInjectionContext()."
139
+ );
134
140
  }
135
141
 
136
142
  // src/injector.ts
@@ -311,8 +317,38 @@ make_fn = function(token, p) {
311
317
  };
312
318
 
313
319
  // src/inject.ts
314
- var import_dom2 = require("@fluixi/dom");
320
+ var import_dom3 = require("@fluixi/dom");
315
321
  var import_signal3 = require("@fluixi/reactive/signal");
322
+
323
+ // src/initializers.ts
324
+ var import_dom2 = require("@fluixi/dom");
325
+ var APP_INITIALIZER = injectionToken("AppInitializer");
326
+ function provideAppInitializer(fn) {
327
+ return { provide: APP_INITIALIZER, useValue: fn, multi: true };
328
+ }
329
+ async function runAppInitializers(injector = rootInjector()) {
330
+ for (const run of injector.get(APP_INITIALIZER, { optional: true }) ?? []) await runInInjectionContext(injector, run);
331
+ }
332
+ (0, import_dom2.beforeFirstRender)(() => {
333
+ if (!state.rootProviders.some(isInitializer)) return;
334
+ return runAppInitializers();
335
+ });
336
+ var isInitializer = (p) => p.provide.id === APP_INITIALIZER.id;
337
+ function production() {
338
+ try {
339
+ return true;
340
+ } catch {
341
+ return false;
342
+ }
343
+ }
344
+ function warnLateInitializers(providers) {
345
+ if (!(0, import_dom2.renderStarted)() || !providers.some(isInitializer) || production()) return;
346
+ console.warn(
347
+ "[fluixi/di] provideAppInitializer() was registered after the app started rendering, so it did not run before this render, and in the browser it never runs. Register it in an app module (app.config.ts) or the entry."
348
+ );
349
+ }
350
+
351
+ // src/inject.ts
316
352
  function inject(token, options) {
317
353
  return currentInjector().get(token, options ?? {});
318
354
  }
@@ -331,9 +367,10 @@ function provide(providers) {
331
367
  return inj.register(providers);
332
368
  }
333
369
  function provideRoot(providers) {
370
+ warnLateInitializers(providers);
334
371
  state.rootProviders.push(...providers);
335
372
  state.clientRoot?.register(providers);
336
- const ctx = (0, import_dom2.getRequestContext)();
373
+ const ctx = (0, import_dom3.getRequestContext)();
337
374
  if (ctx) state.requestRoots.get(ctx)?.register(providers);
338
375
  }
339
376
  function providePlatform(providers) {
@@ -350,7 +387,7 @@ function runInInjectionContext(injector, fn) {
350
387
  }
351
388
  }
352
389
  function isInInjectionContext() {
353
- return state.explicit !== null || (0, import_signal3.getOwner)() !== null || (0, import_dom2.getRequestContext)() !== void 0;
390
+ return state.explicit !== null || (0, import_signal3.getOwner)() !== null || (0, import_dom3.getRequestContext)() !== void 0;
354
391
  }
355
392
  function assertInInjectionContext(name) {
356
393
  if (!isInInjectionContext()) {
@@ -363,12 +400,3 @@ function getInjector() {
363
400
  function createInjector(providers, parent) {
364
401
  return new Injector(parent === void 0 ? rootInjector() : parent, "standalone").register(providers);
365
402
  }
366
-
367
- // src/initializers.ts
368
- var APP_INITIALIZER = injectionToken("AppInitializer");
369
- function provideAppInitializer(fn) {
370
- return { provide: APP_INITIALIZER, useValue: fn, multi: true };
371
- }
372
- async function runAppInitializers(injector = rootInjector()) {
373
- for (const run of injector.get(APP_INITIALIZER, { optional: true }) ?? []) await run();
374
- }