@fluixi/di 0.0.0-stage → 1.0.0-alpha.99
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/LICENSE +21 -0
- package/README.md +43 -2
- package/dist/errors.cjs +36 -0
- package/dist/errors.d.ts +31 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +34 -0
- package/dist/errors.mjs +15 -0
- package/dist/hierarchy.cjs +291 -0
- package/dist/hierarchy.d.ts +11 -0
- package/dist/hierarchy.d.ts.map +1 -0
- package/dist/hierarchy.js +47 -0
- package/dist/hierarchy.mjs +271 -0
- package/dist/index.cjs +374 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +15 -0
- package/dist/index.mjs +352 -0
- package/dist/initializers.cjs +289 -0
- package/dist/initializers.d.ts +36 -0
- package/dist/initializers.d.ts.map +1 -0
- package/dist/initializers.js +45 -0
- package/dist/initializers.mjs +267 -0
- package/dist/inject.cjs +352 -0
- package/dist/inject.d.ts +151 -0
- package/dist/inject.d.ts.map +1 -0
- package/dist/inject.js +174 -0
- package/dist/inject.mjs +332 -0
- package/dist/injector.cjs +259 -0
- package/dist/injector.d.ts +73 -0
- package/dist/injector.d.ts.map +1 -0
- package/dist/injector.js +213 -0
- package/dist/injector.mjs +239 -0
- package/dist/provider.cjs +18 -0
- package/dist/provider.d.ts +97 -0
- package/dist/provider.d.ts.map +1 -0
- package/dist/provider.js +1 -0
- package/dist/provider.mjs +0 -0
- package/dist/state.cjs +45 -0
- package/dist/state.d.ts +35 -0
- package/dist/state.d.ts.map +1 -0
- package/dist/state.js +26 -0
- package/dist/state.mjs +24 -0
- package/dist/testing.cjs +275 -0
- package/dist/testing.d.ts +50 -0
- package/dist/testing.d.ts.map +1 -0
- package/dist/testing.js +48 -0
- package/dist/testing.mjs +253 -0
- package/dist/token.cjs +28 -0
- package/dist/token.d.ts +67 -0
- package/dist/token.d.ts.map +1 -0
- package/dist/token.js +33 -0
- package/dist/token.mjs +7 -0
- package/dist/tokens.cjs +35 -0
- package/dist/tokens.d.ts +53 -0
- package/dist/tokens.d.ts.map +1 -0
- package/dist/tokens.js +26 -0
- package/dist/tokens.mjs +12 -0
- package/dist/tsconfig.lib.tsbuildinfo +1 -0
- package/package.json +55 -3
|
@@ -0,0 +1,271 @@
|
|
|
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
|
+
}
|
|
266
|
+
export {
|
|
267
|
+
currentInjector,
|
|
268
|
+
nearestOwnerInjector,
|
|
269
|
+
platformInjector,
|
|
270
|
+
rootInjector
|
|
271
|
+
};
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
/*! @fluixi/di v1.0.0-alpha.99 | (c) 2026 Ibrahima Touré and Fluixi contributors | MIT */
|
|
2
|
+
"use strict";
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __typeError = (msg) => {
|
|
8
|
+
throw TypeError(msg);
|
|
9
|
+
};
|
|
10
|
+
var __export = (target, all) => {
|
|
11
|
+
for (var name in all)
|
|
12
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
13
|
+
};
|
|
14
|
+
var __copyProps = (to, from, except, desc) => {
|
|
15
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
16
|
+
for (let key of __getOwnPropNames(from))
|
|
17
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
18
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
22
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
23
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
24
|
+
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
25
|
+
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);
|
|
26
|
+
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
|
|
27
|
+
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
|
|
28
|
+
|
|
29
|
+
// src/index.ts
|
|
30
|
+
var index_exports = {};
|
|
31
|
+
__export(index_exports, {
|
|
32
|
+
APP_INITIALIZER: () => APP_INITIALIZER,
|
|
33
|
+
DestroyRef: () => DestroyRef,
|
|
34
|
+
INJECTOR: () => INJECTOR,
|
|
35
|
+
InjectionError: () => InjectionError,
|
|
36
|
+
Injector: () => Injector,
|
|
37
|
+
assertInInjectionContext: () => assertInInjectionContext,
|
|
38
|
+
createInjector: () => createInjector,
|
|
39
|
+
getInjector: () => getInjector,
|
|
40
|
+
inject: () => inject,
|
|
41
|
+
injectionToken: () => injectionToken,
|
|
42
|
+
isInInjectionContext: () => isInInjectionContext,
|
|
43
|
+
provide: () => provide,
|
|
44
|
+
provideAppInitializer: () => provideAppInitializer,
|
|
45
|
+
providePlatform: () => providePlatform,
|
|
46
|
+
provideRoot: () => provideRoot,
|
|
47
|
+
runAppInitializers: () => runAppInitializers,
|
|
48
|
+
runInInjectionContext: () => runInInjectionContext,
|
|
49
|
+
setModuleScope: () => setModuleScope
|
|
50
|
+
});
|
|
51
|
+
module.exports = __toCommonJS(index_exports);
|
|
52
|
+
|
|
53
|
+
// src/token.ts
|
|
54
|
+
function injectionToken(name, options) {
|
|
55
|
+
return { id: Symbol(name), name, factory: options?.factory, scope: options?.scope ?? "root" };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// src/errors.ts
|
|
59
|
+
var InjectionError = class extends Error {
|
|
60
|
+
/**
|
|
61
|
+
* @param message - What went wrong, with the path rendered into it.
|
|
62
|
+
* @param path - The tokens being resolved, outermost first; the last one is the one that failed.
|
|
63
|
+
*/
|
|
64
|
+
constructor(message, path) {
|
|
65
|
+
super(message);
|
|
66
|
+
this.path = path;
|
|
67
|
+
this.name = "InjectionError";
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
// src/tokens.ts
|
|
72
|
+
var DestroyRef = injectionToken("DestroyRef");
|
|
73
|
+
var INJECTOR = injectionToken("Injector");
|
|
74
|
+
|
|
75
|
+
// src/injector.ts
|
|
76
|
+
var import_signal2 = require("@fluixi/reactive/signal");
|
|
77
|
+
|
|
78
|
+
// src/hierarchy.ts
|
|
79
|
+
var import_dom = require("@fluixi/dom");
|
|
80
|
+
var import_signal = require("@fluixi/reactive/signal");
|
|
81
|
+
|
|
82
|
+
// src/state.ts
|
|
83
|
+
function freshState() {
|
|
84
|
+
return {
|
|
85
|
+
platformProviders: [],
|
|
86
|
+
rootProviders: [],
|
|
87
|
+
platform: null,
|
|
88
|
+
clientRoot: null,
|
|
89
|
+
requestRoots: /* @__PURE__ */ new WeakMap(),
|
|
90
|
+
ownerInjectors: /* @__PURE__ */ new WeakMap(),
|
|
91
|
+
moduleScopes: /* @__PURE__ */ new WeakMap(),
|
|
92
|
+
overrides: /* @__PURE__ */ new Map(),
|
|
93
|
+
explicit: null,
|
|
94
|
+
path: []
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
var state = globalThis[/* @__PURE__ */ Symbol.for("fluixi.di")] ??= freshState();
|
|
98
|
+
function setModuleScope(provider, scope) {
|
|
99
|
+
state.moduleScopes.set(provider, scope);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// src/hierarchy.ts
|
|
103
|
+
function platformInjector() {
|
|
104
|
+
return state.platform ??= new Injector(null, "platform").register(state.platformProviders);
|
|
105
|
+
}
|
|
106
|
+
function rootInjector() {
|
|
107
|
+
const ctx = (0, import_dom.getRequestContext)();
|
|
108
|
+
if (ctx) {
|
|
109
|
+
let inj = state.requestRoots.get(ctx);
|
|
110
|
+
if (!inj) {
|
|
111
|
+
const root = new Injector(platformInjector(), "root").register(state.rootProviders);
|
|
112
|
+
state.requestRoots.set(ctx, root);
|
|
113
|
+
(0, import_dom.onRequestEnd)(() => {
|
|
114
|
+
state.requestRoots.delete(ctx);
|
|
115
|
+
root.dispose();
|
|
116
|
+
});
|
|
117
|
+
inj = root;
|
|
118
|
+
}
|
|
119
|
+
return inj;
|
|
120
|
+
}
|
|
121
|
+
return state.clientRoot ??= new Injector(platformInjector(), "root").register(state.rootProviders);
|
|
122
|
+
}
|
|
123
|
+
function nearestOwnerInjector(owner) {
|
|
124
|
+
let o = owner;
|
|
125
|
+
while (o) {
|
|
126
|
+
const inj = state.ownerInjectors.get(o);
|
|
127
|
+
if (inj) return inj;
|
|
128
|
+
o = o.parent;
|
|
129
|
+
}
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
function currentInjector() {
|
|
133
|
+
return state.explicit ?? nearestOwnerInjector((0, import_signal.getOwner)()) ?? rootInjector();
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// src/injector.ts
|
|
137
|
+
var _records, _created, _hooks, _destroyed, _Injector_instances, home_fn, fromProvider_fn, build_fn, make_fn;
|
|
138
|
+
var Injector = class {
|
|
139
|
+
/**
|
|
140
|
+
* @param parent - Where a token this injector does not hold is looked for next; `null` for the
|
|
141
|
+
* top of a chain.
|
|
142
|
+
* @param kind - Where the injector sits in the hierarchy; see {@link InjectorKind}.
|
|
143
|
+
*/
|
|
144
|
+
constructor(parent = null, kind = "standalone") {
|
|
145
|
+
this.parent = parent;
|
|
146
|
+
this.kind = kind;
|
|
147
|
+
__privateAdd(this, _Injector_instances);
|
|
148
|
+
__privateAdd(this, _records, /* @__PURE__ */ new Map());
|
|
149
|
+
__privateAdd(this, _created, []);
|
|
150
|
+
__privateAdd(this, _hooks, []);
|
|
151
|
+
__privateAdd(this, _destroyed, false);
|
|
152
|
+
const self = this;
|
|
153
|
+
this.destroyRef = {
|
|
154
|
+
onDestroy(fn) {
|
|
155
|
+
__privateGet(self, _hooks).push(fn);
|
|
156
|
+
return () => {
|
|
157
|
+
const i = __privateGet(self, _hooks).indexOf(fn);
|
|
158
|
+
if (i >= 0) __privateGet(self, _hooks).splice(i, 1);
|
|
159
|
+
};
|
|
160
|
+
},
|
|
161
|
+
get destroyed() {
|
|
162
|
+
return __privateGet(self, _destroyed);
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
/** Whether {@link Injector.dispose} has run. A disposed injector refuses every `get`. */
|
|
167
|
+
get destroyed() {
|
|
168
|
+
return __privateGet(this, _destroyed);
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Add providers to this injector. A provider for a token already held replaces it, except
|
|
172
|
+
* `multi` providers, which extend the token's list. A provider whose token has a test override
|
|
173
|
+
* is replaced by the override.
|
|
174
|
+
*
|
|
175
|
+
* @param providers - The providers to add, in order.
|
|
176
|
+
* @returns This injector, for chaining.
|
|
177
|
+
*/
|
|
178
|
+
register(providers) {
|
|
179
|
+
for (const given of providers) {
|
|
180
|
+
const p = state.overrides.get(given.provide.id) ?? given;
|
|
181
|
+
if (p.multi) {
|
|
182
|
+
const rec = __privateGet(this, _records).get(p.provide.id);
|
|
183
|
+
if (rec?.multi) {
|
|
184
|
+
rec.providers.push(p);
|
|
185
|
+
rec.built = false;
|
|
186
|
+
} else __privateGet(this, _records).set(p.provide.id, { multi: true, providers: [p], built: false, building: false, value: [] });
|
|
187
|
+
} else {
|
|
188
|
+
__privateGet(this, _records).set(p.provide.id, { multi: false, provider: p, built: false, building: false, value: void 0 });
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
return this;
|
|
192
|
+
}
|
|
193
|
+
get(token, options = {}) {
|
|
194
|
+
var _a, _b, _c;
|
|
195
|
+
if (__privateGet(this, _destroyed)) throw new InjectionError(`[fluixi/di] "${token.name}" requested from a disposed injector.`, [...state.path, token.name]);
|
|
196
|
+
if (token.id === INJECTOR.id) return this;
|
|
197
|
+
if (token.id === DestroyRef.id) return this.destroyRef;
|
|
198
|
+
let inj = options.skipSelf ? this.parent : this;
|
|
199
|
+
while (inj) {
|
|
200
|
+
const rec = __privateGet(inj, _records).get(token.id);
|
|
201
|
+
if (rec) return __privateMethod(_a = inj, _Injector_instances, build_fn).call(_a, token, rec);
|
|
202
|
+
if (options.self || options.host && inj.kind === "owner") break;
|
|
203
|
+
inj = inj.parent;
|
|
204
|
+
}
|
|
205
|
+
const override = state.overrides.get(token.id);
|
|
206
|
+
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);
|
|
207
|
+
if (token.factory && !options.self && !options.host) {
|
|
208
|
+
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 });
|
|
209
|
+
}
|
|
210
|
+
if (options.optional) return null;
|
|
211
|
+
const path = [...state.path, token.name];
|
|
212
|
+
throw new InjectionError(
|
|
213
|
+
`[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 }.`,
|
|
214
|
+
path
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Release this injector: run the hooks registered on its `DestroyRef`, latest first, then the
|
|
219
|
+
* `onDestroy()` method of every service it built, latest built first, then forget its
|
|
220
|
+
* providers. A hook or `onDestroy()` that throws is logged and the rest still run. Running it
|
|
221
|
+
* again does nothing.
|
|
222
|
+
*
|
|
223
|
+
* Component injectors are disposed when the component unmounts, request roots when the request
|
|
224
|
+
* ends; call it yourself only for injectors you created.
|
|
225
|
+
*/
|
|
226
|
+
dispose() {
|
|
227
|
+
if (__privateGet(this, _destroyed)) return;
|
|
228
|
+
__privateSet(this, _destroyed, true);
|
|
229
|
+
const fail = (error) => console.error("[fluixi/di] a destroy hook threw:", error);
|
|
230
|
+
for (const hook of __privateGet(this, _hooks).splice(0).reverse()) {
|
|
231
|
+
try {
|
|
232
|
+
hook();
|
|
233
|
+
} catch (error) {
|
|
234
|
+
fail(error);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
for (const value of __privateGet(this, _created).splice(0).reverse()) {
|
|
238
|
+
const service = value;
|
|
239
|
+
if (service && typeof service.onDestroy === "function") {
|
|
240
|
+
try {
|
|
241
|
+
service.onDestroy();
|
|
242
|
+
} catch (error) {
|
|
243
|
+
fail(error);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
__privateGet(this, _records).clear();
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
_records = new WeakMap();
|
|
251
|
+
_created = new WeakMap();
|
|
252
|
+
_hooks = new WeakMap();
|
|
253
|
+
_destroyed = new WeakMap();
|
|
254
|
+
_Injector_instances = new WeakSet();
|
|
255
|
+
/** The injector a self-providing token builds in: the nearest root, or the platform. */
|
|
256
|
+
home_fn = function(scope) {
|
|
257
|
+
let inj = this;
|
|
258
|
+
if (scope === "platform") {
|
|
259
|
+
while (inj.parent) inj = inj.parent;
|
|
260
|
+
return inj.kind === "platform" ? inj : platformInjector();
|
|
261
|
+
}
|
|
262
|
+
let probe = this;
|
|
263
|
+
while (probe) {
|
|
264
|
+
if (probe.kind === "root") return probe;
|
|
265
|
+
probe = probe.parent;
|
|
266
|
+
}
|
|
267
|
+
while (inj.parent && inj.parent.kind !== "platform") inj = inj.parent;
|
|
268
|
+
return inj;
|
|
269
|
+
};
|
|
270
|
+
fromProvider_fn = function(token, provider) {
|
|
271
|
+
let rec = __privateGet(this, _records).get(token.id);
|
|
272
|
+
if (!rec) {
|
|
273
|
+
rec = { multi: false, provider, built: false, building: false, value: void 0 };
|
|
274
|
+
__privateGet(this, _records).set(token.id, rec);
|
|
275
|
+
}
|
|
276
|
+
return __privateMethod(this, _Injector_instances, build_fn).call(this, token, rec);
|
|
277
|
+
};
|
|
278
|
+
build_fn = function(token, rec) {
|
|
279
|
+
if (rec.built) return rec.value;
|
|
280
|
+
if (rec.building) {
|
|
281
|
+
const path = [...state.path, token.name];
|
|
282
|
+
throw new InjectionError(`[fluixi/di] Circular dependency: ${path.join(" → ")}.`, path);
|
|
283
|
+
}
|
|
284
|
+
rec.building = true;
|
|
285
|
+
state.path.push(token.name);
|
|
286
|
+
const previous = state.explicit;
|
|
287
|
+
state.explicit = this;
|
|
288
|
+
try {
|
|
289
|
+
if (rec.multi) {
|
|
290
|
+
rec.value = rec.providers.map((p) => __privateMethod(this, _Injector_instances, make_fn).call(this, token, p));
|
|
291
|
+
} else {
|
|
292
|
+
const module2 = state.moduleScopes.get(rec.provider);
|
|
293
|
+
rec.value = module2 ? (0, import_signal2.withReactiveScope)(module2, () => __privateMethod(this, _Injector_instances, make_fn).call(this, token, rec.provider)) : __privateMethod(this, _Injector_instances, make_fn).call(this, token, rec.provider);
|
|
294
|
+
}
|
|
295
|
+
rec.built = true;
|
|
296
|
+
return rec.value;
|
|
297
|
+
} finally {
|
|
298
|
+
state.explicit = previous;
|
|
299
|
+
state.path.pop();
|
|
300
|
+
rec.building = false;
|
|
301
|
+
}
|
|
302
|
+
};
|
|
303
|
+
make_fn = function(token, p) {
|
|
304
|
+
if ("useValue" in p) return p.useValue;
|
|
305
|
+
if ("useExisting" in p) return this.get(p.useExisting);
|
|
306
|
+
const deps = (p.deps ?? []).map((d) => "token" in d ? this.get(d.token, d) : this.get(d));
|
|
307
|
+
const scope = (0, import_signal2.openReactiveScope)({ kind: "service", name: token.name, detail: "useClass" in p ? "useClass" : "useFactory" });
|
|
308
|
+
const value = (0, import_signal2.withReactiveScope)(scope, () => "useClass" in p ? new p.useClass(...deps) : p.useFactory(...deps));
|
|
309
|
+
__privateGet(this, _created).push(value);
|
|
310
|
+
return value;
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
// src/inject.ts
|
|
314
|
+
var import_dom2 = require("@fluixi/dom");
|
|
315
|
+
var import_signal3 = require("@fluixi/reactive/signal");
|
|
316
|
+
function inject(token, options) {
|
|
317
|
+
return currentInjector().get(token, options ?? {});
|
|
318
|
+
}
|
|
319
|
+
function provide(providers) {
|
|
320
|
+
const owner = (0, import_signal3.getOwner)();
|
|
321
|
+
if (!owner) {
|
|
322
|
+
throw new Error("[fluixi/di] provide() runs inside a component. Use provideRoot() for services of the whole app.");
|
|
323
|
+
}
|
|
324
|
+
let inj = state.ownerInjectors.get(owner);
|
|
325
|
+
if (!inj) {
|
|
326
|
+
const created = new Injector(nearestOwnerInjector(owner.parent) ?? rootInjector(), "owner");
|
|
327
|
+
state.ownerInjectors.set(owner, created);
|
|
328
|
+
(0, import_signal3.onCleanup)(() => created.dispose());
|
|
329
|
+
inj = created;
|
|
330
|
+
}
|
|
331
|
+
return inj.register(providers);
|
|
332
|
+
}
|
|
333
|
+
function provideRoot(providers) {
|
|
334
|
+
state.rootProviders.push(...providers);
|
|
335
|
+
state.clientRoot?.register(providers);
|
|
336
|
+
const ctx = (0, import_dom2.getRequestContext)();
|
|
337
|
+
if (ctx) state.requestRoots.get(ctx)?.register(providers);
|
|
338
|
+
}
|
|
339
|
+
function providePlatform(providers) {
|
|
340
|
+
state.platformProviders.push(...providers);
|
|
341
|
+
state.platform?.register(providers);
|
|
342
|
+
}
|
|
343
|
+
function runInInjectionContext(injector, fn) {
|
|
344
|
+
const previous = state.explicit;
|
|
345
|
+
state.explicit = injector;
|
|
346
|
+
try {
|
|
347
|
+
return fn();
|
|
348
|
+
} finally {
|
|
349
|
+
state.explicit = previous;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
function isInInjectionContext() {
|
|
353
|
+
return state.explicit !== null || (0, import_signal3.getOwner)() !== null || (0, import_dom2.getRequestContext)() !== void 0;
|
|
354
|
+
}
|
|
355
|
+
function assertInInjectionContext(name) {
|
|
356
|
+
if (!isInInjectionContext()) {
|
|
357
|
+
throw new Error(`[fluixi/di] ${name}() runs inside a component, a request or runInInjectionContext().`);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
function getInjector() {
|
|
361
|
+
return currentInjector();
|
|
362
|
+
}
|
|
363
|
+
function createInjector(providers, parent) {
|
|
364
|
+
return new Injector(parent === void 0 ? rootInjector() : parent, "standalone").register(providers);
|
|
365
|
+
}
|
|
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
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fluixi/di: dependency injection for Fluixi.
|
|
3
|
+
*
|
|
4
|
+
* Typed tokens, providers (value, class, factory, alias, multi), and a hierarchy of injectors:
|
|
5
|
+
* the platform (one per process), the root (one per request on the server, one per app in the
|
|
6
|
+
* browser) and component injectors on the reactive owner tree. `inject(token, options)` resolves
|
|
7
|
+
* from the current injection context. Tests use `@fluixi/di/testing`.
|
|
8
|
+
*/
|
|
9
|
+
export { injectionToken, type Scope, type Token } from './token.js';
|
|
10
|
+
export type { Dependency, InjectOptions, Provider } from './provider.js';
|
|
11
|
+
export { InjectionError } from './errors.js';
|
|
12
|
+
export { DestroyRef, INJECTOR } from './tokens.js';
|
|
13
|
+
export { Injector, type InjectorKind } from './injector.js';
|
|
14
|
+
export { inject, provide, provideRoot, providePlatform, runInInjectionContext, isInInjectionContext, assertInInjectionContext, getInjector, createInjector, } from './inject.js';
|
|
15
|
+
export { APP_INITIALIZER, provideAppInitializer, runAppInitializers } from './initializers.js';
|
|
16
|
+
export { setModuleScope } from './state.js';
|
|
17
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EAAE,cAAc,EAAE,KAAK,KAAK,EAAE,KAAK,KAAK,EAAE,MAAM,YAAY,CAAC;AACpE,YAAY,EAAE,UAAU,EAAE,aAAa,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzE,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,EAAE,QAAQ,EAAE,KAAK,YAAY,EAAE,MAAM,eAAe,CAAC;AAC5D,OAAO,EACL,MAAM,EACN,OAAO,EACP,WAAW,EACX,eAAe,EACf,qBAAqB,EACrB,oBAAoB,EACpB,wBAAwB,EACxB,WAAW,EACX,cAAc,GACf,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,eAAe,EAAE,qBAAqB,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAC/F,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fluixi/di: dependency injection for Fluixi.
|
|
3
|
+
*
|
|
4
|
+
* Typed tokens, providers (value, class, factory, alias, multi), and a hierarchy of injectors:
|
|
5
|
+
* the platform (one per process), the root (one per request on the server, one per app in the
|
|
6
|
+
* browser) and component injectors on the reactive owner tree. `inject(token, options)` resolves
|
|
7
|
+
* from the current injection context. Tests use `@fluixi/di/testing`.
|
|
8
|
+
*/
|
|
9
|
+
export { injectionToken } from './token.js';
|
|
10
|
+
export { InjectionError } from './errors.js';
|
|
11
|
+
export { DestroyRef, INJECTOR } from './tokens.js';
|
|
12
|
+
export { Injector } from './injector.js';
|
|
13
|
+
export { inject, provide, provideRoot, providePlatform, runInInjectionContext, isInInjectionContext, assertInInjectionContext, getInjector, createInjector, } from './inject.js';
|
|
14
|
+
export { APP_INITIALIZER, provideAppInitializer, runAppInitializers } from './initializers.js';
|
|
15
|
+
export { setModuleScope } from './state.js';
|