@fluixi/di 1.0.0-alpha.108 → 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/initializers.mjs
CHANGED
|
@@ -1,267 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
import
|
|
12
|
-
import
|
|
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
|
-
|
|
255
|
-
// src/initializers.ts
|
|
256
|
-
var APP_INITIALIZER = injectionToken("AppInitializer");
|
|
257
|
-
function provideAppInitializer(fn) {
|
|
258
|
-
return { provide: APP_INITIALIZER, useValue: fn, multi: true };
|
|
259
|
-
}
|
|
260
|
-
async function runAppInitializers(injector = rootInjector()) {
|
|
261
|
-
for (const run of injector.get(APP_INITIALIZER, { optional: true }) ?? []) await run();
|
|
262
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
APP_INITIALIZER,
|
|
3
|
+
provideAppInitializer,
|
|
4
|
+
runAppInitializers,
|
|
5
|
+
warnLateInitializers
|
|
6
|
+
} from "./chunk-BXVK24LH.mjs";
|
|
7
|
+
import "./chunk-DA5NMOSZ.mjs";
|
|
8
|
+
import "./chunk-6Y4QWQA7.mjs";
|
|
9
|
+
import "./chunk-2GEQYTZE.mjs";
|
|
10
|
+
import "./chunk-5BUN4TXK.mjs";
|
|
11
|
+
import "./chunk-OCG6UOTN.mjs";
|
|
12
|
+
import "./chunk-I6KKUHEY.mjs";
|
|
263
13
|
export {
|
|
264
14
|
APP_INITIALIZER,
|
|
265
15
|
provideAppInitializer,
|
|
266
|
-
runAppInitializers
|
|
16
|
+
runAppInitializers,
|
|
17
|
+
warnLateInitializers
|
|
267
18
|
};
|
package/dist/inject.cjs
CHANGED
|
@@ -39,7 +39,7 @@ __export(inject_exports, {
|
|
|
39
39
|
runInInjectionContext: () => runInInjectionContext
|
|
40
40
|
});
|
|
41
41
|
module.exports = __toCommonJS(inject_exports);
|
|
42
|
-
var
|
|
42
|
+
var import_dom3 = require("@fluixi/dom");
|
|
43
43
|
var import_signal3 = require("@fluixi/reactive/signal");
|
|
44
44
|
|
|
45
45
|
// src/hierarchy.ts
|
|
@@ -74,7 +74,8 @@ function freshState() {
|
|
|
74
74
|
moduleScopes: /* @__PURE__ */ new WeakMap(),
|
|
75
75
|
overrides: /* @__PURE__ */ new Map(),
|
|
76
76
|
explicit: null,
|
|
77
|
-
path: []
|
|
77
|
+
path: [],
|
|
78
|
+
initialized: false
|
|
78
79
|
};
|
|
79
80
|
}
|
|
80
81
|
var state = globalThis[/* @__PURE__ */ Symbol.for("fluixi.di")] ??= freshState();
|
|
@@ -270,14 +271,14 @@ function platformInjector() {
|
|
|
270
271
|
return state.platform ??= new Injector(null, "platform").register(state.platformProviders);
|
|
271
272
|
}
|
|
272
273
|
function rootInjector() {
|
|
273
|
-
const
|
|
274
|
-
if (
|
|
275
|
-
let inj = state.requestRoots.get(
|
|
274
|
+
const scope = (0, import_dom.requestScope)();
|
|
275
|
+
if (scope) {
|
|
276
|
+
let inj = state.requestRoots.get(scope);
|
|
276
277
|
if (!inj) {
|
|
277
278
|
const root = new Injector(platformInjector(), "root").register(state.rootProviders);
|
|
278
|
-
state.requestRoots.set(
|
|
279
|
+
state.requestRoots.set(scope, root);
|
|
279
280
|
(0, import_dom.onRequestEnd)(() => {
|
|
280
|
-
state.requestRoots.delete(
|
|
281
|
+
state.requestRoots.delete(scope);
|
|
281
282
|
root.dispose();
|
|
282
283
|
});
|
|
283
284
|
inj = root;
|
|
@@ -296,7 +297,39 @@ function nearestOwnerInjector(owner) {
|
|
|
296
297
|
return null;
|
|
297
298
|
}
|
|
298
299
|
function currentInjector() {
|
|
299
|
-
|
|
300
|
+
if (state.explicit) return state.explicit;
|
|
301
|
+
const owner = (0, import_signal2.getOwner)();
|
|
302
|
+
if (owner) return nearestOwnerInjector(owner) ?? rootInjector();
|
|
303
|
+
if ((0, import_dom.requestScope)()) return rootInjector();
|
|
304
|
+
throw new Error(
|
|
305
|
+
"[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()."
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// src/initializers.ts
|
|
310
|
+
var import_dom2 = require("@fluixi/dom");
|
|
311
|
+
var APP_INITIALIZER = injectionToken("AppInitializer");
|
|
312
|
+
async function runAppInitializers(injector = rootInjector()) {
|
|
313
|
+
for (const run of injector.get(APP_INITIALIZER, { optional: true }) ?? []) await runInInjectionContext(injector, run);
|
|
314
|
+
}
|
|
315
|
+
(0, import_dom2.beforeFirstRender)(() => {
|
|
316
|
+
state.initialized = true;
|
|
317
|
+
if (!state.rootProviders.some(isInitializer)) return;
|
|
318
|
+
return runAppInitializers();
|
|
319
|
+
});
|
|
320
|
+
var isInitializer = (p) => p.provide.id === APP_INITIALIZER.id;
|
|
321
|
+
function production() {
|
|
322
|
+
try {
|
|
323
|
+
return true;
|
|
324
|
+
} catch {
|
|
325
|
+
return false;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
function warnLateInitializers(providers) {
|
|
329
|
+
if (!state.initialized || !providers.some(isInitializer) || production()) return;
|
|
330
|
+
console.warn(
|
|
331
|
+
"[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."
|
|
332
|
+
);
|
|
300
333
|
}
|
|
301
334
|
|
|
302
335
|
// src/inject.ts
|
|
@@ -318,9 +351,10 @@ function provide(providers) {
|
|
|
318
351
|
return inj.register(providers);
|
|
319
352
|
}
|
|
320
353
|
function provideRoot(providers) {
|
|
354
|
+
warnLateInitializers(providers);
|
|
321
355
|
state.rootProviders.push(...providers);
|
|
322
356
|
state.clientRoot?.register(providers);
|
|
323
|
-
const ctx = (0,
|
|
357
|
+
const ctx = (0, import_dom3.getRequestContext)();
|
|
324
358
|
if (ctx) state.requestRoots.get(ctx)?.register(providers);
|
|
325
359
|
}
|
|
326
360
|
function providePlatform(providers) {
|
|
@@ -337,7 +371,7 @@ function runInInjectionContext(injector, fn) {
|
|
|
337
371
|
}
|
|
338
372
|
}
|
|
339
373
|
function isInInjectionContext() {
|
|
340
|
-
return state.explicit !== null || (0, import_signal3.getOwner)() !== null || (0,
|
|
374
|
+
return state.explicit !== null || (0, import_signal3.getOwner)() !== null || (0, import_dom3.getRequestContext)() !== void 0;
|
|
341
375
|
}
|
|
342
376
|
function assertInInjectionContext(name) {
|
|
343
377
|
if (!isInInjectionContext()) {
|
package/dist/inject.d.ts
CHANGED
|
@@ -4,17 +4,19 @@ import type { Token } from './token.js';
|
|
|
4
4
|
/**
|
|
5
5
|
* Resolve `token` from the current injection context: the injector set by
|
|
6
6
|
* {@link runInInjectionContext} or building the current service, else the nearest component
|
|
7
|
-
* injector up the reactive owner tree
|
|
8
|
-
*
|
|
7
|
+
* injector up the reactive owner tree (the root when no component provides one), else, during a
|
|
8
|
+
* server request, that request's root.
|
|
9
9
|
*
|
|
10
10
|
* Call it during a component's setup, inside a provider's constructor or factory, or inside
|
|
11
11
|
* {@link runInInjectionContext}. In an event handler or after an `await` there is no component
|
|
12
|
-
* owner any more: keep the value, or the injector (`inject(INJECTOR)`), from setup
|
|
12
|
+
* owner any more and it throws: keep the value, or the injector (`inject(INJECTOR)`), from setup
|
|
13
|
+
* instead.
|
|
13
14
|
*
|
|
14
15
|
* @param token - What to resolve.
|
|
15
16
|
* @param options - Where to look and what to do when nothing is found; see {@link InjectOptions}.
|
|
16
17
|
* @returns The value, or `null` when `optional` is set and nothing provides the token.
|
|
17
18
|
* @throws {@link InjectionError} When nothing provides the token, or on a dependency cycle.
|
|
19
|
+
* @throws Error When there is no injection context (see {@link isInInjectionContext}).
|
|
18
20
|
*
|
|
19
21
|
* @example
|
|
20
22
|
* ```ts
|
package/dist/inject.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"inject.d.ts","sourceRoot":"","sources":["../src/inject.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,KAAK,EAAE,aAAa,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"inject.d.ts","sourceRoot":"","sources":["../src/inject.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,KAAK,EAAE,aAAa,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAG7D,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAExC;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,MAAM,CAAC,CAAC,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,aAAa,GAAG;IAAE,QAAQ,EAAE,IAAI,CAAA;CAAE,GAAG,CAAC,GAAG,IAAI,CAAC;AAClG,wBAAgB,MAAM,CAAC,CAAC,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,CAAC,CAAC;AAKvE;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,OAAO,CAAC,SAAS,EAAE,aAAa,CAAC,QAAQ,CAAC,GAAG,QAAQ,CAapE;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,WAAW,CAAC,SAAS,EAAE,aAAa,CAAC,QAAQ,CAAC,GAAG,IAAI,CAMpE;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,eAAe,CAAC,SAAS,EAAE,aAAa,CAAC,QAAQ,CAAC,GAAG,IAAI,CAGxE;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,qBAAqB,CAAC,CAAC,EAAE,QAAQ,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,CAAC,GAAG,CAAC,CAQ3E;AAED;;;;;GAKG;AACH,wBAAgB,oBAAoB,IAAI,OAAO,CAE9C;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAI3D;AAED;;;;;GAKG;AACH,wBAAgB,WAAW,IAAI,QAAQ,CAEtC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,cAAc,CAAC,SAAS,EAAE,aAAa,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC,EAAE,QAAQ,GAAG,IAAI,GAAG,QAAQ,CAErG"}
|
package/dist/inject.js
CHANGED
|
@@ -9,6 +9,7 @@ import { getOwner, onCleanup } from '@fluixi/reactive/signal';
|
|
|
9
9
|
import { currentInjector, nearestOwnerInjector, rootInjector } from './hierarchy.js';
|
|
10
10
|
import { Injector } from './injector.js';
|
|
11
11
|
import { state } from './state.js';
|
|
12
|
+
import { warnLateInitializers } from './initializers.js';
|
|
12
13
|
export function inject(token, options) {
|
|
13
14
|
return currentInjector().get(token, options ?? {});
|
|
14
15
|
}
|
|
@@ -65,6 +66,7 @@ export function provide(providers) {
|
|
|
65
66
|
* ```
|
|
66
67
|
*/
|
|
67
68
|
export function provideRoot(providers) {
|
|
69
|
+
warnLateInitializers(providers);
|
|
68
70
|
state.rootProviders.push(...providers);
|
|
69
71
|
state.clientRoot?.register(providers);
|
|
70
72
|
const ctx = getRequestContext();
|