@fluixi/di 1.0.0-alpha.107 → 1.0.0-alpha.109
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/chunk-2GEQYTZE.mjs +8 -0
- package/dist/chunk-5BUN4TXK.mjs +16 -0
- package/dist/chunk-6Y4QWQA7.mjs +12 -0
- package/dist/chunk-BXVK24LH.mjs +114 -0
- package/dist/chunk-DA5NMOSZ.mjs +246 -0
- package/dist/chunk-I6KKUHEY.mjs +15 -0
- package/dist/chunk-OCG6UOTN.mjs +26 -0
- package/dist/errors.mjs +4 -12
- package/dist/hierarchy.cjs +14 -7
- package/dist/hierarchy.d.ts +3 -2
- package/dist/hierarchy.d.ts.map +1 -1
- package/dist/hierarchy.js +22 -9
- package/dist/hierarchy.mjs +11 -265
- package/dist/index.cjs +50 -20
- package/dist/index.mjs +32 -332
- package/dist/initializers.cjs +43 -8
- package/dist/initializers.d.ts +5 -0
- package/dist/initializers.d.ts.map +1 -1
- package/dist/initializers.js +31 -1
- package/dist/initializers.mjs +14 -263
- package/dist/inject.cjs +44 -10
- package/dist/inject.d.ts +5 -3
- package/dist/inject.d.ts.map +1 -1
- package/dist/inject.js +2 -0
- package/dist/inject.mjs +17 -321
- package/dist/injector.cjs +2 -1
- package/dist/injector.mjs +8 -236
- package/dist/state.cjs +2 -1
- package/dist/state.d.ts +2 -0
- package/dist/state.d.ts.map +1 -1
- package/dist/state.js +1 -0
- package/dist/state.mjs +6 -19
- package/dist/testing.cjs +3 -2
- package/dist/testing.mjs +12 -237
- package/dist/token.mjs +4 -4
- package/dist/tokens.mjs +6 -8
- package/dist/tsconfig.lib.tsbuildinfo +1 -1
- package/package.json +6 -6
package/dist/index.mjs
CHANGED
|
@@ -1,335 +1,35 @@
|
|
|
1
|
-
/*! @fluixi/di v1.0.0-alpha.
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
// src/injector.ts
|
|
34
|
-
import { openReactiveScope, withReactiveScope } from "@fluixi/reactive/signal";
|
|
35
|
-
|
|
36
|
-
// src/hierarchy.ts
|
|
37
|
-
import { getRequestContext, onRequestEnd } from "@fluixi/dom";
|
|
38
|
-
import { getOwner } from "@fluixi/reactive/signal";
|
|
39
|
-
|
|
40
|
-
// src/state.ts
|
|
41
|
-
function freshState() {
|
|
42
|
-
return {
|
|
43
|
-
platformProviders: [],
|
|
44
|
-
rootProviders: [],
|
|
45
|
-
platform: null,
|
|
46
|
-
clientRoot: null,
|
|
47
|
-
requestRoots: /* @__PURE__ */ new WeakMap(),
|
|
48
|
-
ownerInjectors: /* @__PURE__ */ new WeakMap(),
|
|
49
|
-
moduleScopes: /* @__PURE__ */ new WeakMap(),
|
|
50
|
-
overrides: /* @__PURE__ */ new Map(),
|
|
51
|
-
explicit: null,
|
|
52
|
-
path: []
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
var state = globalThis[/* @__PURE__ */ Symbol.for("fluixi.di")] ??= freshState();
|
|
56
|
-
function setModuleScope(provider, scope) {
|
|
57
|
-
state.moduleScopes.set(provider, scope);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
// src/hierarchy.ts
|
|
61
|
-
function platformInjector() {
|
|
62
|
-
return state.platform ??= new Injector(null, "platform").register(state.platformProviders);
|
|
63
|
-
}
|
|
64
|
-
function rootInjector() {
|
|
65
|
-
const ctx = getRequestContext();
|
|
66
|
-
if (ctx) {
|
|
67
|
-
let inj = state.requestRoots.get(ctx);
|
|
68
|
-
if (!inj) {
|
|
69
|
-
const root = new Injector(platformInjector(), "root").register(state.rootProviders);
|
|
70
|
-
state.requestRoots.set(ctx, root);
|
|
71
|
-
onRequestEnd(() => {
|
|
72
|
-
state.requestRoots.delete(ctx);
|
|
73
|
-
root.dispose();
|
|
74
|
-
});
|
|
75
|
-
inj = root;
|
|
76
|
-
}
|
|
77
|
-
return inj;
|
|
78
|
-
}
|
|
79
|
-
return state.clientRoot ??= new Injector(platformInjector(), "root").register(state.rootProviders);
|
|
80
|
-
}
|
|
81
|
-
function nearestOwnerInjector(owner) {
|
|
82
|
-
let o = owner;
|
|
83
|
-
while (o) {
|
|
84
|
-
const inj = state.ownerInjectors.get(o);
|
|
85
|
-
if (inj) return inj;
|
|
86
|
-
o = o.parent;
|
|
87
|
-
}
|
|
88
|
-
return null;
|
|
89
|
-
}
|
|
90
|
-
function currentInjector() {
|
|
91
|
-
return state.explicit ?? nearestOwnerInjector(getOwner()) ?? rootInjector();
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
// src/injector.ts
|
|
95
|
-
var _records, _created, _hooks, _destroyed, _Injector_instances, home_fn, fromProvider_fn, build_fn, make_fn;
|
|
96
|
-
var Injector = class {
|
|
97
|
-
/**
|
|
98
|
-
* @param parent - Where a token this injector does not hold is looked for next; `null` for the
|
|
99
|
-
* top of a chain.
|
|
100
|
-
* @param kind - Where the injector sits in the hierarchy; see {@link InjectorKind}.
|
|
101
|
-
*/
|
|
102
|
-
constructor(parent = null, kind = "standalone") {
|
|
103
|
-
this.parent = parent;
|
|
104
|
-
this.kind = kind;
|
|
105
|
-
__privateAdd(this, _Injector_instances);
|
|
106
|
-
__privateAdd(this, _records, /* @__PURE__ */ new Map());
|
|
107
|
-
__privateAdd(this, _created, []);
|
|
108
|
-
__privateAdd(this, _hooks, []);
|
|
109
|
-
__privateAdd(this, _destroyed, false);
|
|
110
|
-
const self = this;
|
|
111
|
-
this.destroyRef = {
|
|
112
|
-
onDestroy(fn) {
|
|
113
|
-
__privateGet(self, _hooks).push(fn);
|
|
114
|
-
return () => {
|
|
115
|
-
const i = __privateGet(self, _hooks).indexOf(fn);
|
|
116
|
-
if (i >= 0) __privateGet(self, _hooks).splice(i, 1);
|
|
117
|
-
};
|
|
118
|
-
},
|
|
119
|
-
get destroyed() {
|
|
120
|
-
return __privateGet(self, _destroyed);
|
|
121
|
-
}
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
|
-
/** Whether {@link Injector.dispose} has run. A disposed injector refuses every `get`. */
|
|
125
|
-
get destroyed() {
|
|
126
|
-
return __privateGet(this, _destroyed);
|
|
127
|
-
}
|
|
128
|
-
/**
|
|
129
|
-
* Add providers to this injector. A provider for a token already held replaces it, except
|
|
130
|
-
* `multi` providers, which extend the token's list. A provider whose token has a test override
|
|
131
|
-
* is replaced by the override.
|
|
132
|
-
*
|
|
133
|
-
* @param providers - The providers to add, in order.
|
|
134
|
-
* @returns This injector, for chaining.
|
|
135
|
-
*/
|
|
136
|
-
register(providers) {
|
|
137
|
-
for (const given of providers) {
|
|
138
|
-
const p = state.overrides.get(given.provide.id) ?? given;
|
|
139
|
-
if (p.multi) {
|
|
140
|
-
const rec = __privateGet(this, _records).get(p.provide.id);
|
|
141
|
-
if (rec?.multi) {
|
|
142
|
-
rec.providers.push(p);
|
|
143
|
-
rec.built = false;
|
|
144
|
-
} else __privateGet(this, _records).set(p.provide.id, { multi: true, providers: [p], built: false, building: false, value: [] });
|
|
145
|
-
} else {
|
|
146
|
-
__privateGet(this, _records).set(p.provide.id, { multi: false, provider: p, built: false, building: false, value: void 0 });
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
return this;
|
|
150
|
-
}
|
|
151
|
-
get(token, options = {}) {
|
|
152
|
-
var _a, _b, _c;
|
|
153
|
-
if (__privateGet(this, _destroyed)) throw new InjectionError(`[fluixi/di] "${token.name}" requested from a disposed injector.`, [...state.path, token.name]);
|
|
154
|
-
if (token.id === INJECTOR.id) return this;
|
|
155
|
-
if (token.id === DestroyRef.id) return this.destroyRef;
|
|
156
|
-
let inj = options.skipSelf ? this.parent : this;
|
|
157
|
-
while (inj) {
|
|
158
|
-
const rec = __privateGet(inj, _records).get(token.id);
|
|
159
|
-
if (rec) return __privateMethod(_a = inj, _Injector_instances, build_fn).call(_a, token, rec);
|
|
160
|
-
if (options.self || options.host && inj.kind === "owner") break;
|
|
161
|
-
inj = inj.parent;
|
|
162
|
-
}
|
|
163
|
-
const override = state.overrides.get(token.id);
|
|
164
|
-
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);
|
|
165
|
-
if (token.factory && !options.self && !options.host) {
|
|
166
|
-
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 });
|
|
167
|
-
}
|
|
168
|
-
if (options.optional) return null;
|
|
169
|
-
const path = [...state.path, token.name];
|
|
170
|
-
throw new InjectionError(
|
|
171
|
-
`[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 }.`,
|
|
172
|
-
path
|
|
173
|
-
);
|
|
174
|
-
}
|
|
175
|
-
/**
|
|
176
|
-
* Release this injector: run the hooks registered on its `DestroyRef`, latest first, then the
|
|
177
|
-
* `onDestroy()` method of every service it built, latest built first, then forget its
|
|
178
|
-
* providers. A hook or `onDestroy()` that throws is logged and the rest still run. Running it
|
|
179
|
-
* again does nothing.
|
|
180
|
-
*
|
|
181
|
-
* Component injectors are disposed when the component unmounts, request roots when the request
|
|
182
|
-
* ends; call it yourself only for injectors you created.
|
|
183
|
-
*/
|
|
184
|
-
dispose() {
|
|
185
|
-
if (__privateGet(this, _destroyed)) return;
|
|
186
|
-
__privateSet(this, _destroyed, true);
|
|
187
|
-
const fail = (error) => console.error("[fluixi/di] a destroy hook threw:", error);
|
|
188
|
-
for (const hook of __privateGet(this, _hooks).splice(0).reverse()) {
|
|
189
|
-
try {
|
|
190
|
-
hook();
|
|
191
|
-
} catch (error) {
|
|
192
|
-
fail(error);
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
for (const value of __privateGet(this, _created).splice(0).reverse()) {
|
|
196
|
-
const service = value;
|
|
197
|
-
if (service && typeof service.onDestroy === "function") {
|
|
198
|
-
try {
|
|
199
|
-
service.onDestroy();
|
|
200
|
-
} catch (error) {
|
|
201
|
-
fail(error);
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
__privateGet(this, _records).clear();
|
|
206
|
-
}
|
|
207
|
-
};
|
|
208
|
-
_records = new WeakMap();
|
|
209
|
-
_created = new WeakMap();
|
|
210
|
-
_hooks = new WeakMap();
|
|
211
|
-
_destroyed = new WeakMap();
|
|
212
|
-
_Injector_instances = new WeakSet();
|
|
213
|
-
/** The injector a self-providing token builds in: the nearest root, or the platform. */
|
|
214
|
-
home_fn = function(scope) {
|
|
215
|
-
let inj = this;
|
|
216
|
-
if (scope === "platform") {
|
|
217
|
-
while (inj.parent) inj = inj.parent;
|
|
218
|
-
return inj.kind === "platform" ? inj : platformInjector();
|
|
219
|
-
}
|
|
220
|
-
let probe = this;
|
|
221
|
-
while (probe) {
|
|
222
|
-
if (probe.kind === "root") return probe;
|
|
223
|
-
probe = probe.parent;
|
|
224
|
-
}
|
|
225
|
-
while (inj.parent && inj.parent.kind !== "platform") inj = inj.parent;
|
|
226
|
-
return inj;
|
|
227
|
-
};
|
|
228
|
-
fromProvider_fn = function(token, provider) {
|
|
229
|
-
let rec = __privateGet(this, _records).get(token.id);
|
|
230
|
-
if (!rec) {
|
|
231
|
-
rec = { multi: false, provider, built: false, building: false, value: void 0 };
|
|
232
|
-
__privateGet(this, _records).set(token.id, rec);
|
|
233
|
-
}
|
|
234
|
-
return __privateMethod(this, _Injector_instances, build_fn).call(this, token, rec);
|
|
235
|
-
};
|
|
236
|
-
build_fn = function(token, rec) {
|
|
237
|
-
if (rec.built) return rec.value;
|
|
238
|
-
if (rec.building) {
|
|
239
|
-
const path = [...state.path, token.name];
|
|
240
|
-
throw new InjectionError(`[fluixi/di] Circular dependency: ${path.join(" → ")}.`, path);
|
|
241
|
-
}
|
|
242
|
-
rec.building = true;
|
|
243
|
-
state.path.push(token.name);
|
|
244
|
-
const previous = state.explicit;
|
|
245
|
-
state.explicit = this;
|
|
246
|
-
try {
|
|
247
|
-
if (rec.multi) {
|
|
248
|
-
rec.value = rec.providers.map((p) => __privateMethod(this, _Injector_instances, make_fn).call(this, token, p));
|
|
249
|
-
} else {
|
|
250
|
-
const module = state.moduleScopes.get(rec.provider);
|
|
251
|
-
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);
|
|
252
|
-
}
|
|
253
|
-
rec.built = true;
|
|
254
|
-
return rec.value;
|
|
255
|
-
} finally {
|
|
256
|
-
state.explicit = previous;
|
|
257
|
-
state.path.pop();
|
|
258
|
-
rec.building = false;
|
|
259
|
-
}
|
|
260
|
-
};
|
|
261
|
-
make_fn = function(token, p) {
|
|
262
|
-
if ("useValue" in p) return p.useValue;
|
|
263
|
-
if ("useExisting" in p) return this.get(p.useExisting);
|
|
264
|
-
const deps = (p.deps ?? []).map((d) => "token" in d ? this.get(d.token, d) : this.get(d));
|
|
265
|
-
const scope = openReactiveScope({ kind: "service", name: token.name, detail: "useClass" in p ? "useClass" : "useFactory" });
|
|
266
|
-
const value = withReactiveScope(scope, () => "useClass" in p ? new p.useClass(...deps) : p.useFactory(...deps));
|
|
267
|
-
__privateGet(this, _created).push(value);
|
|
268
|
-
return value;
|
|
269
|
-
};
|
|
270
|
-
|
|
271
|
-
// src/inject.ts
|
|
272
|
-
import { getRequestContext as getRequestContext2 } from "@fluixi/dom";
|
|
273
|
-
import { getOwner as getOwner2, onCleanup } from "@fluixi/reactive/signal";
|
|
274
|
-
function inject(token, options) {
|
|
275
|
-
return currentInjector().get(token, options ?? {});
|
|
276
|
-
}
|
|
277
|
-
function provide(providers) {
|
|
278
|
-
const owner = getOwner2();
|
|
279
|
-
if (!owner) {
|
|
280
|
-
throw new Error("[fluixi/di] provide() runs inside a component. Use provideRoot() for services of the whole app.");
|
|
281
|
-
}
|
|
282
|
-
let inj = state.ownerInjectors.get(owner);
|
|
283
|
-
if (!inj) {
|
|
284
|
-
const created = new Injector(nearestOwnerInjector(owner.parent) ?? rootInjector(), "owner");
|
|
285
|
-
state.ownerInjectors.set(owner, created);
|
|
286
|
-
onCleanup(() => created.dispose());
|
|
287
|
-
inj = created;
|
|
288
|
-
}
|
|
289
|
-
return inj.register(providers);
|
|
290
|
-
}
|
|
291
|
-
function provideRoot(providers) {
|
|
292
|
-
state.rootProviders.push(...providers);
|
|
293
|
-
state.clientRoot?.register(providers);
|
|
294
|
-
const ctx = getRequestContext2();
|
|
295
|
-
if (ctx) state.requestRoots.get(ctx)?.register(providers);
|
|
296
|
-
}
|
|
297
|
-
function providePlatform(providers) {
|
|
298
|
-
state.platformProviders.push(...providers);
|
|
299
|
-
state.platform?.register(providers);
|
|
300
|
-
}
|
|
301
|
-
function runInInjectionContext(injector, fn) {
|
|
302
|
-
const previous = state.explicit;
|
|
303
|
-
state.explicit = injector;
|
|
304
|
-
try {
|
|
305
|
-
return fn();
|
|
306
|
-
} finally {
|
|
307
|
-
state.explicit = previous;
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
function isInInjectionContext() {
|
|
311
|
-
return state.explicit !== null || getOwner2() !== null || getRequestContext2() !== void 0;
|
|
312
|
-
}
|
|
313
|
-
function assertInInjectionContext(name) {
|
|
314
|
-
if (!isInInjectionContext()) {
|
|
315
|
-
throw new Error(`[fluixi/di] ${name}() runs inside a component, a request or runInInjectionContext().`);
|
|
316
|
-
}
|
|
317
|
-
}
|
|
318
|
-
function getInjector() {
|
|
319
|
-
return currentInjector();
|
|
320
|
-
}
|
|
321
|
-
function createInjector(providers, parent) {
|
|
322
|
-
return new Injector(parent === void 0 ? rootInjector() : parent, "standalone").register(providers);
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
// src/initializers.ts
|
|
326
|
-
var APP_INITIALIZER = injectionToken("AppInitializer");
|
|
327
|
-
function provideAppInitializer(fn) {
|
|
328
|
-
return { provide: APP_INITIALIZER, useValue: fn, multi: true };
|
|
329
|
-
}
|
|
330
|
-
async function runAppInitializers(injector = rootInjector()) {
|
|
331
|
-
for (const run of injector.get(APP_INITIALIZER, { optional: true }) ?? []) await run();
|
|
332
|
-
}
|
|
1
|
+
/*! @fluixi/di v1.0.0-alpha.109 | (c) 2026 Ibrahima Touré and Fluixi contributors | MIT */
|
|
2
|
+
import {
|
|
3
|
+
APP_INITIALIZER,
|
|
4
|
+
assertInInjectionContext,
|
|
5
|
+
createInjector,
|
|
6
|
+
getInjector,
|
|
7
|
+
inject,
|
|
8
|
+
isInInjectionContext,
|
|
9
|
+
provide,
|
|
10
|
+
provideAppInitializer,
|
|
11
|
+
providePlatform,
|
|
12
|
+
provideRoot,
|
|
13
|
+
runAppInitializers,
|
|
14
|
+
runInInjectionContext
|
|
15
|
+
} from "./chunk-BXVK24LH.mjs";
|
|
16
|
+
import {
|
|
17
|
+
Injector
|
|
18
|
+
} from "./chunk-DA5NMOSZ.mjs";
|
|
19
|
+
import {
|
|
20
|
+
DestroyRef,
|
|
21
|
+
INJECTOR
|
|
22
|
+
} from "./chunk-6Y4QWQA7.mjs";
|
|
23
|
+
import {
|
|
24
|
+
injectionToken
|
|
25
|
+
} from "./chunk-2GEQYTZE.mjs";
|
|
26
|
+
import {
|
|
27
|
+
InjectionError
|
|
28
|
+
} from "./chunk-5BUN4TXK.mjs";
|
|
29
|
+
import {
|
|
30
|
+
setModuleScope
|
|
31
|
+
} from "./chunk-OCG6UOTN.mjs";
|
|
32
|
+
import "./chunk-I6KKUHEY.mjs";
|
|
333
33
|
export {
|
|
334
34
|
APP_INITIALIZER,
|
|
335
35
|
DestroyRef,
|
package/dist/initializers.cjs
CHANGED
|
@@ -30,9 +30,11 @@ var initializers_exports = {};
|
|
|
30
30
|
__export(initializers_exports, {
|
|
31
31
|
APP_INITIALIZER: () => APP_INITIALIZER,
|
|
32
32
|
provideAppInitializer: () => provideAppInitializer,
|
|
33
|
-
runAppInitializers: () => runAppInitializers
|
|
33
|
+
runAppInitializers: () => runAppInitializers,
|
|
34
|
+
warnLateInitializers: () => warnLateInitializers
|
|
34
35
|
});
|
|
35
36
|
module.exports = __toCommonJS(initializers_exports);
|
|
37
|
+
var import_dom3 = require("@fluixi/dom");
|
|
36
38
|
|
|
37
39
|
// src/hierarchy.ts
|
|
38
40
|
var import_dom = require("@fluixi/dom");
|
|
@@ -66,7 +68,8 @@ function freshState() {
|
|
|
66
68
|
moduleScopes: /* @__PURE__ */ new WeakMap(),
|
|
67
69
|
overrides: /* @__PURE__ */ new Map(),
|
|
68
70
|
explicit: null,
|
|
69
|
-
path: []
|
|
71
|
+
path: [],
|
|
72
|
+
initialized: false
|
|
70
73
|
};
|
|
71
74
|
}
|
|
72
75
|
var state = globalThis[/* @__PURE__ */ Symbol.for("fluixi.di")] ??= freshState();
|
|
@@ -262,14 +265,14 @@ function platformInjector() {
|
|
|
262
265
|
return state.platform ??= new Injector(null, "platform").register(state.platformProviders);
|
|
263
266
|
}
|
|
264
267
|
function rootInjector() {
|
|
265
|
-
const
|
|
266
|
-
if (
|
|
267
|
-
let inj = state.requestRoots.get(
|
|
268
|
+
const scope = (0, import_dom.requestScope)();
|
|
269
|
+
if (scope) {
|
|
270
|
+
let inj = state.requestRoots.get(scope);
|
|
268
271
|
if (!inj) {
|
|
269
272
|
const root = new Injector(platformInjector(), "root").register(state.rootProviders);
|
|
270
|
-
state.requestRoots.set(
|
|
273
|
+
state.requestRoots.set(scope, root);
|
|
271
274
|
(0, import_dom.onRequestEnd)(() => {
|
|
272
|
-
state.requestRoots.delete(
|
|
275
|
+
state.requestRoots.delete(scope);
|
|
273
276
|
root.dispose();
|
|
274
277
|
});
|
|
275
278
|
inj = root;
|
|
@@ -279,11 +282,43 @@ function rootInjector() {
|
|
|
279
282
|
return state.clientRoot ??= new Injector(platformInjector(), "root").register(state.rootProviders);
|
|
280
283
|
}
|
|
281
284
|
|
|
285
|
+
// src/inject.ts
|
|
286
|
+
var import_dom2 = require("@fluixi/dom");
|
|
287
|
+
var import_signal3 = require("@fluixi/reactive/signal");
|
|
288
|
+
function runInInjectionContext(injector, fn) {
|
|
289
|
+
const previous = state.explicit;
|
|
290
|
+
state.explicit = injector;
|
|
291
|
+
try {
|
|
292
|
+
return fn();
|
|
293
|
+
} finally {
|
|
294
|
+
state.explicit = previous;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
282
298
|
// src/initializers.ts
|
|
283
299
|
var APP_INITIALIZER = injectionToken("AppInitializer");
|
|
284
300
|
function provideAppInitializer(fn) {
|
|
285
301
|
return { provide: APP_INITIALIZER, useValue: fn, multi: true };
|
|
286
302
|
}
|
|
287
303
|
async function runAppInitializers(injector = rootInjector()) {
|
|
288
|
-
for (const run of injector.get(APP_INITIALIZER, { optional: true }) ?? []) await run
|
|
304
|
+
for (const run of injector.get(APP_INITIALIZER, { optional: true }) ?? []) await runInInjectionContext(injector, run);
|
|
305
|
+
}
|
|
306
|
+
(0, import_dom3.beforeFirstRender)(() => {
|
|
307
|
+
state.initialized = true;
|
|
308
|
+
if (!state.rootProviders.some(isInitializer)) return;
|
|
309
|
+
return runAppInitializers();
|
|
310
|
+
});
|
|
311
|
+
var isInitializer = (p) => p.provide.id === APP_INITIALIZER.id;
|
|
312
|
+
function production() {
|
|
313
|
+
try {
|
|
314
|
+
return true;
|
|
315
|
+
} catch {
|
|
316
|
+
return false;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
function warnLateInitializers(providers) {
|
|
320
|
+
if (!state.initialized || !providers.some(isInitializer) || production()) return;
|
|
321
|
+
console.warn(
|
|
322
|
+
"[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."
|
|
323
|
+
);
|
|
289
324
|
}
|
package/dist/initializers.d.ts
CHANGED
|
@@ -33,4 +33,9 @@ export declare function provideAppInitializer(fn: () => unknown): Provider<() =>
|
|
|
33
33
|
* @returns A promise settled when every initializer has finished.
|
|
34
34
|
*/
|
|
35
35
|
export declare function runAppInitializers(injector?: Injector): Promise<void>;
|
|
36
|
+
/**
|
|
37
|
+
* Warn, outside production, about an initializer registered once the app has started rendering:
|
|
38
|
+
* from a module a route imports, typically. It missed that render, and the page's in the browser.
|
|
39
|
+
*/
|
|
40
|
+
export declare function warnLateInitializers(providers: ReadonlyArray<Provider>): void;
|
|
36
41
|
//# sourceMappingURL=initializers.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"initializers.d.ts","sourceRoot":"","sources":["../src/initializers.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"initializers.d.ts","sourceRoot":"","sources":["../src/initializers.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAC9C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAC9C,OAAO,EAAkB,KAAK,KAAK,EAAE,MAAM,YAAY,CAAC;AAExD;;;GAGG;AACH,eAAO,MAAM,eAAe,EAAE,KAAK,CAAC,KAAK,CAAC,MAAM,OAAO,CAAC,CAA0D,CAAC;AAEnH;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,qBAAqB,CAAC,EAAE,EAAE,MAAM,OAAO,GAAG,QAAQ,CAAC,MAAM,OAAO,CAAC,CAEhF;AAED;;;;;;;GAOG;AACH,wBAAsB,kBAAkB,CAAC,QAAQ,GAAE,QAAyB,GAAG,OAAO,CAAC,IAAI,CAAC,CAE3F;AAuBD;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,SAAS,EAAE,aAAa,CAAC,QAAQ,CAAC,GAAG,IAAI,CAM7E"}
|
package/dist/initializers.js
CHANGED
|
@@ -4,7 +4,10 @@
|
|
|
4
4
|
*
|
|
5
5
|
* @module
|
|
6
6
|
*/
|
|
7
|
+
import { beforeFirstRender } from '@fluixi/dom';
|
|
7
8
|
import { rootInjector } from './hierarchy.js';
|
|
9
|
+
import { runInInjectionContext } from './inject.js';
|
|
10
|
+
import { state } from './state.js';
|
|
8
11
|
import { injectionToken } from './token.js';
|
|
9
12
|
/**
|
|
10
13
|
* The functions given to {@link provideAppInitializer}, as a `multi` token: `inject(APP_INITIALIZER)`
|
|
@@ -41,5 +44,32 @@ export function provideAppInitializer(fn) {
|
|
|
41
44
|
*/
|
|
42
45
|
export async function runAppInitializers(injector = rootInjector()) {
|
|
43
46
|
for (const run of injector.get(APP_INITIALIZER, { optional: true }) ?? [])
|
|
44
|
-
await run
|
|
47
|
+
await runInInjectionContext(injector, run);
|
|
48
|
+
}
|
|
49
|
+
// Before the app's first render: in each request's root on the server, in the page's root in the
|
|
50
|
+
// browser. Without initializers nothing runs, and a request builds no root for them.
|
|
51
|
+
beforeFirstRender(() => {
|
|
52
|
+
state.initialized = true;
|
|
53
|
+
if (!state.rootProviders.some(isInitializer))
|
|
54
|
+
return;
|
|
55
|
+
return runAppInitializers();
|
|
56
|
+
});
|
|
57
|
+
const isInitializer = (p) => p.provide.id === APP_INITIALIZER.id;
|
|
58
|
+
function production() {
|
|
59
|
+
try {
|
|
60
|
+
return process.env.NODE_ENV === 'production';
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Warn, outside production, about an initializer registered once the app has started rendering:
|
|
68
|
+
* from a module a route imports, typically. It missed that render, and the page's in the browser.
|
|
69
|
+
*/
|
|
70
|
+
export function warnLateInitializers(providers) {
|
|
71
|
+
if (!state.initialized || !providers.some(isInitializer) || production())
|
|
72
|
+
return;
|
|
73
|
+
console.warn('[fluixi/di] provideAppInitializer() was registered after the app started rendering, so it did not run before ' +
|
|
74
|
+
'this render, and in the browser it never runs. Register it in an app module (app.config.ts) or the entry.');
|
|
45
75
|
}
|