bunja 3.0.2 → 4.0.0-alpha.2
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/bunja.d.ts +214 -2
- package/dist/bunja.js +780 -3
- package/dist/react.d.ts +7 -13
- package/dist/react.js +7 -10
- package/dist/solid/v1-compat.d.ts +11 -0
- package/dist/solid/v1-compat.js +69 -0
- package/dist/solid.d.ts +9 -12
- package/dist/solid.js +28 -22
- package/package.json +31 -45
- package/bunja.ts +0 -1199
- package/deno.json +0 -17
- package/dist/bunja-B81fiP3K.d.cts +0 -192
- package/dist/bunja-BmPnlAec.js +0 -636
- package/dist/bunja-DJPGfe5j.cjs +0 -684
- package/dist/bunja-pdetyCM3.d.ts +0 -192
- package/dist/bunja.cjs +0 -10
- package/dist/bunja.d.cts +0 -2
- package/dist/chunk-CUT6urMc.cjs +0 -30
- package/dist/react.cjs +0 -74
- package/dist/react.d.cts +0 -15
- package/dist/solid.cjs +0 -68
- package/dist/solid.d.cts +0 -14
- package/react.ts +0 -113
- package/solid.ts +0 -115
- package/solid_test.ts +0 -166
- package/test.ts +0 -868
- package/tsconfig.json +0 -13
- package/tsdown.config.ts +0 -10
package/dist/bunja.js
CHANGED
|
@@ -1,3 +1,780 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
// bunja.ts
|
|
2
|
+
const __DEV__ = process.env.NODE_ENV !== "production";
|
|
3
|
+
const bunja = bunjaFn;
|
|
4
|
+
function bunjaFn(init) {
|
|
5
|
+
return new Bunja(() => init(), void 0);
|
|
6
|
+
}
|
|
7
|
+
bunjaFn.withSeed = function withSeed(defaultSeed, init) {
|
|
8
|
+
return new Bunja(init, defaultSeed);
|
|
9
|
+
};
|
|
10
|
+
bunjaFn.use = ((dep, scopeValuePairs) => getCurrentFrame("`bunja.use`").use(dep, scopeValuePairs));
|
|
11
|
+
bunjaFn.will = ((dep, scopeValuePairs) => getCurrentFrame("`bunja.will`").will(dep, scopeValuePairs));
|
|
12
|
+
bunjaFn.effect = ((callback) => getCurrentFrame("`bunja.effect`").effect(callback));
|
|
13
|
+
function createScope(hash) {
|
|
14
|
+
return new Scope(hash);
|
|
15
|
+
}
|
|
16
|
+
function override(target, init) {
|
|
17
|
+
return {
|
|
18
|
+
target,
|
|
19
|
+
init
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
function createBunjaStore(config) {
|
|
23
|
+
return new BunjaStore(config);
|
|
24
|
+
}
|
|
25
|
+
const frameStack = [];
|
|
26
|
+
function getCurrentFrame(api) {
|
|
27
|
+
const frame = frameStack[frameStack.length - 1];
|
|
28
|
+
if (!frame) throw new Error(`${api} can only be used inside a bunja init function.`);
|
|
29
|
+
return frame;
|
|
30
|
+
}
|
|
31
|
+
function runWithFrame(frame, fn) {
|
|
32
|
+
frameStack.push(frame);
|
|
33
|
+
try {
|
|
34
|
+
return fn();
|
|
35
|
+
} finally {
|
|
36
|
+
frameStack.pop();
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
const defaultWrapInstanceFn = (fn) => fn(noop);
|
|
40
|
+
function normalizeBunjaRuntimeRef(bunjaOrRef, scopeValuePairs = []) {
|
|
41
|
+
if (bunjaOrRef instanceof Bunja) return {
|
|
42
|
+
bunja: bunjaOrRef,
|
|
43
|
+
scopeValuePairs,
|
|
44
|
+
seed: bunjaOrRef.defaultSeed
|
|
45
|
+
};
|
|
46
|
+
const { bunja, with: refScopeValuePairs = [] } = bunjaOrRef;
|
|
47
|
+
return {
|
|
48
|
+
bunja,
|
|
49
|
+
scopeValuePairs: refScopeValuePairs,
|
|
50
|
+
seed: "seed" in bunjaOrRef ? bunjaOrRef.seed : bunja.defaultSeed
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
function normalizeBunjaPrebakeRef(bunjaOrRef) {
|
|
54
|
+
if (bunjaOrRef instanceof Bunja) return {
|
|
55
|
+
bunja: bunjaOrRef,
|
|
56
|
+
scopeValuePairs: []
|
|
57
|
+
};
|
|
58
|
+
if ("seed" in bunjaOrRef) throw new Error("A bunja seed cannot be provided to `store.prebake`.");
|
|
59
|
+
const { bunja, with: refScopeValuePairs = [] } = bunjaOrRef;
|
|
60
|
+
return {
|
|
61
|
+
bunja,
|
|
62
|
+
scopeValuePairs: refScopeValuePairs
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
function toBunjaGraphRef(bunjaRef) {
|
|
66
|
+
return {
|
|
67
|
+
bunja: bunjaRef.bunja,
|
|
68
|
+
scopeValuePairs: bunjaRef.scopeValuePairs.map(([scope, value]) => [scope, value])
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
function isBunjaRef(value) {
|
|
72
|
+
return typeof value === "object" && value !== null && "bunja" in value && value.bunja instanceof Bunja;
|
|
73
|
+
}
|
|
74
|
+
function getBoundScopeSet(scopeValuePairs) {
|
|
75
|
+
return new Set(scopeValuePairs.map(([scope]) => scope));
|
|
76
|
+
}
|
|
77
|
+
function getScopeInstances(scopes, scopeInstanceMap) {
|
|
78
|
+
return scopes.map((scope) => scopeInstanceMap.get(scope));
|
|
79
|
+
}
|
|
80
|
+
function dedupeScopeInstances(scopeInstances) {
|
|
81
|
+
const seen = /* @__PURE__ */ new Set();
|
|
82
|
+
const result = [];
|
|
83
|
+
for (const scopeInstance of scopeInstances) {
|
|
84
|
+
if (seen.has(scopeInstance.id)) continue;
|
|
85
|
+
seen.add(scopeInstance.id);
|
|
86
|
+
result.push(scopeInstance);
|
|
87
|
+
}
|
|
88
|
+
return result;
|
|
89
|
+
}
|
|
90
|
+
function dedupeBunjas(bunjas) {
|
|
91
|
+
return Array.from(new Set(bunjas));
|
|
92
|
+
}
|
|
93
|
+
function addUniqueBunjaRef(refs, ref) {
|
|
94
|
+
if (!refs.includes(ref)) refs.push(ref);
|
|
95
|
+
}
|
|
96
|
+
function createBunjaPrebakeContext() {
|
|
97
|
+
return {
|
|
98
|
+
inProgressBunjas: /* @__PURE__ */ new Set(),
|
|
99
|
+
values: /* @__PURE__ */ new Map()
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
var BunjaStore = class BunjaStore {
|
|
103
|
+
static counter = 0;
|
|
104
|
+
id = String(BunjaStore.counter++);
|
|
105
|
+
#bunjas = {};
|
|
106
|
+
#bunjaBuckets = /* @__PURE__ */ new Map();
|
|
107
|
+
#scopes = /* @__PURE__ */ new Map();
|
|
108
|
+
#graphs = /* @__PURE__ */ new Map();
|
|
109
|
+
#overrides = /* @__PURE__ */ new Map();
|
|
110
|
+
#graph = (bunja) => {
|
|
111
|
+
let graph = this.#graphs.get(bunja);
|
|
112
|
+
if (!graph) {
|
|
113
|
+
graph = new BunjaGraph(bunja, (dependency) => this.#graphs.get(dependency));
|
|
114
|
+
this.#graphs.set(bunja, graph);
|
|
115
|
+
}
|
|
116
|
+
return graph;
|
|
117
|
+
};
|
|
118
|
+
#initializer(bunja) {
|
|
119
|
+
return this.#overrides.get(bunja) ?? bunja.init;
|
|
120
|
+
}
|
|
121
|
+
inspect(bunja) {
|
|
122
|
+
const graph = this.#graphs.get(bunja);
|
|
123
|
+
const refs = (items) => Object.freeze(items.map((ref) => Object.freeze({
|
|
124
|
+
bunja: ref.bunja,
|
|
125
|
+
scopeValuePairs: Object.freeze(ref.scopeValuePairs.map((pair) => Object.freeze([...pair])))
|
|
126
|
+
})));
|
|
127
|
+
return Object.freeze({
|
|
128
|
+
storeId: this.id,
|
|
129
|
+
bunjaId: bunja.id,
|
|
130
|
+
baked: graph?.baked ?? false,
|
|
131
|
+
scopes: Object.freeze(graph?.scopes ?? []),
|
|
132
|
+
requiredBunjaRefs: refs(graph?.requiredBunjaRefs ?? []),
|
|
133
|
+
optionalBunjaRefs: refs(graph?.optionalBunjaRefs ?? []),
|
|
134
|
+
expandedRequiredBunjas: Object.freeze(graph?.baked ? [...graph.expandedRequiredBunjas] : []),
|
|
135
|
+
relatedBunjas: Object.freeze(graph?.baked ? graph.relatedBunjas : []),
|
|
136
|
+
requiredScopes: Object.freeze(graph?.baked ? [...graph.requiredScopes] : [])
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
snapshot() {
|
|
140
|
+
return Object.freeze({
|
|
141
|
+
storeId: this.id,
|
|
142
|
+
graphs: Object.freeze(Array.from(this.#graphs.keys(), (bunja) => this.inspect(bunja))),
|
|
143
|
+
instances: Object.freeze(Object.values(this.#bunjas).map((instance) => Object.freeze({
|
|
144
|
+
id: instance.id,
|
|
145
|
+
bunjaId: instance.baseId.split(":")[0],
|
|
146
|
+
activeDependencyIds: Object.freeze([...instance.recipe.activeDependencyIds])
|
|
147
|
+
})))
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
#register() {
|
|
151
|
+
if (!__DEV__ || devtoolsGlobalHook.stores.has(this.id)) return;
|
|
152
|
+
devtoolsGlobalHook.stores.set(this.id, new WeakRef(this));
|
|
153
|
+
storeFinalizer?.register(this, this.id, this);
|
|
154
|
+
devtoolsGlobalHook.emit("storeCreated", { storeId: this.id });
|
|
155
|
+
}
|
|
156
|
+
wrapInstance;
|
|
157
|
+
constructor({ wrapInstance = defaultWrapInstanceFn, overrides = [] } = {}) {
|
|
158
|
+
this.wrapInstance = wrapInstance;
|
|
159
|
+
for (const { target, init } of overrides) {
|
|
160
|
+
if (this.#overrides.has(target)) throw new Error("Duplicate bunja override target.");
|
|
161
|
+
this.#overrides.set(target, init);
|
|
162
|
+
}
|
|
163
|
+
this.#register();
|
|
164
|
+
}
|
|
165
|
+
get _internalState() {
|
|
166
|
+
if (__DEV__) return {
|
|
167
|
+
bunjas: this.#bunjas,
|
|
168
|
+
scopes: this.#scopes,
|
|
169
|
+
get instantiating() {
|
|
170
|
+
return frameStack.length > 0;
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
dispose() {
|
|
175
|
+
for (const instance of Object.values(this.#bunjas)) instance.dispose();
|
|
176
|
+
for (const instanceMap of this.#scopes.values()) for (const instance of instanceMap.values()) instance.dispose();
|
|
177
|
+
this.#bunjas = {};
|
|
178
|
+
this.#bunjaBuckets = /* @__PURE__ */ new Map();
|
|
179
|
+
this.#scopes = /* @__PURE__ */ new Map();
|
|
180
|
+
this.#graphs.clear();
|
|
181
|
+
storeFinalizer?.unregister(this);
|
|
182
|
+
if (__DEV__) {
|
|
183
|
+
devtoolsGlobalHook.stores.delete(this.id);
|
|
184
|
+
devtoolsGlobalHook.emit("storeDisposed", { storeId: this.id });
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
get(bunjaOrRef, readScope) {
|
|
188
|
+
this.#register();
|
|
189
|
+
const bunjaRef = normalizeBunjaRuntimeRef(bunjaOrRef);
|
|
190
|
+
const resolved = this.#resolveBunjaRef(toBunjaGraphRef(bunjaRef), readScope, /* @__PURE__ */ new Set(), bunjaRef.seed, true);
|
|
191
|
+
const result = {
|
|
192
|
+
value: resolved.value,
|
|
193
|
+
mount: resolved.mount,
|
|
194
|
+
deps: resolved.deps.map(({ value }) => value)
|
|
195
|
+
};
|
|
196
|
+
if (__DEV__) {
|
|
197
|
+
result.bunjaInstance = resolved.instance;
|
|
198
|
+
devtoolsGlobalHook.emit("getCalled", {
|
|
199
|
+
storeId: this.id,
|
|
200
|
+
bunjaInstanceId: resolved.instance.id
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
return result;
|
|
204
|
+
}
|
|
205
|
+
prebake(bunjaOrRef, readScope) {
|
|
206
|
+
this.#register();
|
|
207
|
+
const bunjaRef = normalizeBunjaPrebakeRef(bunjaOrRef);
|
|
208
|
+
this.#prebakeBunjaRef(bunjaRef, readScope, createBunjaPrebakeContext());
|
|
209
|
+
const graph = this.#graph(bunjaRef.bunja);
|
|
210
|
+
return {
|
|
211
|
+
relatedBunjas: graph.relatedBunjas,
|
|
212
|
+
requiredScopes: [...graph.requiredScopes]
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
#resolveBunjaRef(bunjaRef, readScope, inProgressBunjas, seed = bunjaRef.bunja.defaultSeed, includeBoundScopeDeps = false) {
|
|
216
|
+
const { bunja } = bunjaRef;
|
|
217
|
+
if (inProgressBunjas.has(bunja)) throw new Error("Circular bunja dependency detected.");
|
|
218
|
+
const resolvedReadScope = bunjaRef.scopeValuePairs.length > 0 ? createReadScopeFn(bunjaRef.scopeValuePairs, readScope) : readScope;
|
|
219
|
+
const wasBaked = this.#graph(bunja).baked;
|
|
220
|
+
inProgressBunjas.add(bunja);
|
|
221
|
+
try {
|
|
222
|
+
if (!wasBaked) return this.#createResolvedBunja(bunjaRef, resolvedReadScope, inProgressBunjas, seed, includeBoundScopeDeps);
|
|
223
|
+
const scopeInstanceMap = this.#resolveScopeInstanceMap(bunja, resolvedReadScope);
|
|
224
|
+
const graph = this.#graph(bunja);
|
|
225
|
+
const boundScopes = getBoundScopeSet(bunjaRef.scopeValuePairs);
|
|
226
|
+
const scopeInstances = getScopeInstances(graph.requiredScopes, scopeInstanceMap);
|
|
227
|
+
const directDeps = getScopeInstances(includeBoundScopeDeps ? graph.requiredScopes : graph.requiredScopes.filter((scope) => !boundScopes.has(scope)), scopeInstanceMap);
|
|
228
|
+
const baseId = graph.calcBaseInstanceId(scopeInstanceMap);
|
|
229
|
+
const bucket = this.#bunjaBuckets.get(baseId);
|
|
230
|
+
if (bucket) for (const candidateId of Array.from(bucket)) {
|
|
231
|
+
const candidate = this.#bunjas[candidateId];
|
|
232
|
+
if (!candidate) {
|
|
233
|
+
bucket.delete(candidateId);
|
|
234
|
+
continue;
|
|
235
|
+
}
|
|
236
|
+
const activeDeps = this.#resolveActiveDependencyRecipe(candidate.recipe, resolvedReadScope, inProgressBunjas);
|
|
237
|
+
const currentId = this.#graph(bunja).calcInstanceId(scopeInstanceMap, activeDeps.activeDependencyIds);
|
|
238
|
+
const instance = currentId === candidate.id ? candidate : this.#bunjas[currentId];
|
|
239
|
+
if (!instance) continue;
|
|
240
|
+
return this.#toResolvedBunja(instance, scopeInstances, directDeps, activeDeps.deps);
|
|
241
|
+
}
|
|
242
|
+
return this.#createResolvedBunja(bunjaRef, resolvedReadScope, inProgressBunjas, seed, includeBoundScopeDeps, scopeInstanceMap);
|
|
243
|
+
} catch (error) {
|
|
244
|
+
if (!wasBaked && !this.#graphs.get(bunja)?.baked) {
|
|
245
|
+
this.#graphs.delete(bunja);
|
|
246
|
+
if (__DEV__) devtoolsGlobalHook.emit("graphChanged", {
|
|
247
|
+
storeId: this.id,
|
|
248
|
+
bunjaId: bunja.id
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
throw error;
|
|
252
|
+
} finally {
|
|
253
|
+
inProgressBunjas.delete(bunja);
|
|
254
|
+
if (__DEV__ && !wasBaked && this.#graphs.get(bunja)?.baked) devtoolsGlobalHook.emit("graphChanged", {
|
|
255
|
+
storeId: this.id,
|
|
256
|
+
bunjaId: bunja.id
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
#createResolvedBunja(bunjaRef, readScope, inProgressBunjas, seed, includeBoundScopeDeps, initialScopeInstanceMap = /* @__PURE__ */ new Map()) {
|
|
261
|
+
const { bunja } = bunjaRef;
|
|
262
|
+
return this.wrapInstance((dispose) => {
|
|
263
|
+
let instanceCreated = false;
|
|
264
|
+
let disposed = false;
|
|
265
|
+
const disposeOnce = () => {
|
|
266
|
+
if (disposed) return;
|
|
267
|
+
disposed = true;
|
|
268
|
+
dispose();
|
|
269
|
+
};
|
|
270
|
+
try {
|
|
271
|
+
const frame = this.#createInitFrame(bunja, readScope, initialScopeInstanceMap, inProgressBunjas);
|
|
272
|
+
const value = runWithFrame(frame, () => this.#initializer(bunja)(seed));
|
|
273
|
+
let graph = this.#graph(bunja);
|
|
274
|
+
if (!graph.baked) graph.bake();
|
|
275
|
+
this.#ensureRequiredScopeInstances(bunja, frame.scopeInstanceMap, readScope);
|
|
276
|
+
graph = this.#graph(bunja);
|
|
277
|
+
const boundScopes = getBoundScopeSet(bunjaRef.scopeValuePairs);
|
|
278
|
+
const scopeInstances = getScopeInstances(graph.requiredScopes, frame.scopeInstanceMap);
|
|
279
|
+
const directDeps = getScopeInstances(includeBoundScopeDeps ? graph.requiredScopes : graph.requiredScopes.filter((scope) => !boundScopes.has(scope)), frame.scopeInstanceMap);
|
|
280
|
+
const baseId = graph.calcBaseInstanceId(frame.scopeInstanceMap);
|
|
281
|
+
const id = graph.calcInstanceId(frame.scopeInstanceMap, frame.activeDependencyIds);
|
|
282
|
+
const existing = this.#bunjas[id];
|
|
283
|
+
if (existing) {
|
|
284
|
+
disposeOnce();
|
|
285
|
+
return this.#toResolvedBunja(existing, scopeInstances, directDeps, frame.activeDependencyDeps);
|
|
286
|
+
}
|
|
287
|
+
const instance = this.#createBunjaInstance(id, baseId, value, Array.from(frame.activeDependencyMounts.values()), frame.effects, {
|
|
288
|
+
activeDependencies: frame.activeDependencyRecipes,
|
|
289
|
+
activeDependencyIds: [...frame.activeDependencyIds]
|
|
290
|
+
}, dispose);
|
|
291
|
+
instanceCreated = true;
|
|
292
|
+
return this.#toResolvedBunja(instance, scopeInstances, directDeps, frame.activeDependencyDeps);
|
|
293
|
+
} finally {
|
|
294
|
+
if (!instanceCreated) disposeOnce();
|
|
295
|
+
}
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
#toResolvedBunja(instance, scopeInstances, directDeps, activeDependencyDeps) {
|
|
299
|
+
const deps = dedupeScopeInstances([...directDeps, ...activeDependencyDeps]);
|
|
300
|
+
return {
|
|
301
|
+
value: instance.value,
|
|
302
|
+
instance,
|
|
303
|
+
deps,
|
|
304
|
+
mount: () => {
|
|
305
|
+
for (const scopeInstance of scopeInstances) scopeInstance.add();
|
|
306
|
+
instance.add();
|
|
307
|
+
return () => {
|
|
308
|
+
instance.sub();
|
|
309
|
+
for (const scopeInstance of scopeInstances) scopeInstance.sub();
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
#createInitFrame(currentBunja, readScope, scopeInstanceMap, inProgressBunjas) {
|
|
315
|
+
const frame = {
|
|
316
|
+
currentBunja,
|
|
317
|
+
readScope,
|
|
318
|
+
scopeInstanceMap,
|
|
319
|
+
inProgressBunjas,
|
|
320
|
+
effects: [],
|
|
321
|
+
activeDependencyIds: /* @__PURE__ */ new Set(),
|
|
322
|
+
activeDependencyRecipes: [],
|
|
323
|
+
activeDependencyMounts: /* @__PURE__ */ new Map(),
|
|
324
|
+
activeDependencyDeps: [],
|
|
325
|
+
use: ((dep, scopeValuePairs) => {
|
|
326
|
+
if (dep instanceof Scope) return this.#useScopeInFrame(frame, dep);
|
|
327
|
+
if (dep instanceof Bunja || isBunjaRef(dep)) {
|
|
328
|
+
const bunjaRef = normalizeBunjaRuntimeRef(dep, scopeValuePairs);
|
|
329
|
+
const graphRef = toBunjaGraphRef(bunjaRef);
|
|
330
|
+
this.#graph(currentBunja).addRequiredBunjaRef(graphRef);
|
|
331
|
+
return this.#useBunjaDependencyInFrame(frame, bunjaRef);
|
|
332
|
+
}
|
|
333
|
+
throw new Error("`bunja.use` can only be used with Bunja or Scope.");
|
|
334
|
+
}),
|
|
335
|
+
will: ((dep, scopeValuePairs) => {
|
|
336
|
+
if (!(dep instanceof Bunja || isBunjaRef(dep))) throw new Error("`bunja.will` can only be used with Bunja.");
|
|
337
|
+
const bunjaRef = normalizeBunjaRuntimeRef(dep, scopeValuePairs);
|
|
338
|
+
const graphRef = toBunjaGraphRef(bunjaRef);
|
|
339
|
+
this.#graph(currentBunja).addOptionalBunjaRef(graphRef);
|
|
340
|
+
return () => {
|
|
341
|
+
if (frameStack[frameStack.length - 1] !== frame) throw new Error("A thunk returned by `bunja.will` can only be called inside the same bunja init function.");
|
|
342
|
+
return this.#useBunjaDependencyInFrame(frame, bunjaRef);
|
|
343
|
+
};
|
|
344
|
+
}),
|
|
345
|
+
effect: ((callback) => {
|
|
346
|
+
frame.effects.push(callback);
|
|
347
|
+
})
|
|
348
|
+
};
|
|
349
|
+
return frame;
|
|
350
|
+
}
|
|
351
|
+
#useScopeInFrame(frame, scope) {
|
|
352
|
+
const graph = this.#graph(frame.currentBunja);
|
|
353
|
+
graph.addScope(scope);
|
|
354
|
+
let scopeInstance = frame.scopeInstanceMap.get(scope);
|
|
355
|
+
if (!scopeInstance) {
|
|
356
|
+
if (graph.baked) throw new Error("`bunja.use(scope)` cannot introduce a new scope after the bunja is baked.");
|
|
357
|
+
scopeInstance = this.#getScopeInstance(scope, frame.readScope(scope));
|
|
358
|
+
frame.scopeInstanceMap.set(scope, scopeInstance);
|
|
359
|
+
}
|
|
360
|
+
return scopeInstance.value;
|
|
361
|
+
}
|
|
362
|
+
#useBunjaDependencyInFrame(frame, bunjaRef) {
|
|
363
|
+
const graphRef = toBunjaGraphRef(bunjaRef);
|
|
364
|
+
const resolved = this.#resolveBunjaRef(graphRef, frame.readScope, frame.inProgressBunjas, bunjaRef.seed);
|
|
365
|
+
frame.activeDependencyIds.add(resolved.instance.id);
|
|
366
|
+
frame.activeDependencyRecipes.push({
|
|
367
|
+
ref: graphRef,
|
|
368
|
+
seed: bunjaRef.seed
|
|
369
|
+
});
|
|
370
|
+
if (!frame.activeDependencyMounts.has(resolved.instance.id)) frame.activeDependencyMounts.set(resolved.instance.id, resolved.mount);
|
|
371
|
+
frame.activeDependencyDeps.push(...resolved.deps);
|
|
372
|
+
return resolved.value;
|
|
373
|
+
}
|
|
374
|
+
#resolveActiveDependencyRecipe(recipe, readScope, inProgressBunjas) {
|
|
375
|
+
const activeDependencyIds = /* @__PURE__ */ new Set();
|
|
376
|
+
const deps = [];
|
|
377
|
+
for (const { ref, seed } of recipe.activeDependencies) {
|
|
378
|
+
const resolved = this.#resolveBunjaRef(ref, readScope, inProgressBunjas, seed);
|
|
379
|
+
activeDependencyIds.add(resolved.instance.id);
|
|
380
|
+
deps.push(...resolved.deps);
|
|
381
|
+
}
|
|
382
|
+
return {
|
|
383
|
+
activeDependencyIds,
|
|
384
|
+
deps: dedupeScopeInstances(deps)
|
|
385
|
+
};
|
|
386
|
+
}
|
|
387
|
+
#prebakeBunjaRef(bunjaRef, readScope, prebakeContext) {
|
|
388
|
+
const { bunja } = bunjaRef;
|
|
389
|
+
if (prebakeContext.values.has(bunja)) return prebakeContext.values.get(bunja);
|
|
390
|
+
const { inProgressBunjas } = prebakeContext;
|
|
391
|
+
if (inProgressBunjas.has(bunja)) throw new Error("Circular bunja dependency detected.");
|
|
392
|
+
const resolvedReadScope = bunjaRef.scopeValuePairs.length > 0 ? createReadScopeFn(bunjaRef.scopeValuePairs, readScope) : readScope;
|
|
393
|
+
const wasBaked = this.#graph(bunja).baked;
|
|
394
|
+
inProgressBunjas.add(bunja);
|
|
395
|
+
try {
|
|
396
|
+
return this.wrapInstance((dispose) => {
|
|
397
|
+
try {
|
|
398
|
+
const value = runWithFrame(this.#createPrebakeFrame(bunja, resolvedReadScope, prebakeContext), () => this.#initializer(bunja)(bunja.defaultSeed));
|
|
399
|
+
const graph = this.#graph(bunja);
|
|
400
|
+
if (!graph.baked) graph.bake();
|
|
401
|
+
for (const ref of [...graph.requiredBunjaRefs, ...graph.optionalBunjaRefs]) this.#prebakeBunjaRef(ref, resolvedReadScope, prebakeContext);
|
|
402
|
+
prebakeContext.values.set(bunja, value);
|
|
403
|
+
return value;
|
|
404
|
+
} finally {
|
|
405
|
+
dispose();
|
|
406
|
+
}
|
|
407
|
+
});
|
|
408
|
+
} catch (error) {
|
|
409
|
+
if (!wasBaked && !this.#graphs.get(bunja)?.baked) {
|
|
410
|
+
this.#graphs.delete(bunja);
|
|
411
|
+
if (__DEV__) devtoolsGlobalHook.emit("graphChanged", {
|
|
412
|
+
storeId: this.id,
|
|
413
|
+
bunjaId: bunja.id
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
throw error;
|
|
417
|
+
} finally {
|
|
418
|
+
inProgressBunjas.delete(bunja);
|
|
419
|
+
if (__DEV__ && !wasBaked && this.#graphs.get(bunja)?.baked) devtoolsGlobalHook.emit("graphChanged", {
|
|
420
|
+
storeId: this.id,
|
|
421
|
+
bunjaId: bunja.id
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
#createPrebakeFrame(currentBunja, readScope, prebakeContext) {
|
|
426
|
+
const frame = {
|
|
427
|
+
currentBunja,
|
|
428
|
+
readScope,
|
|
429
|
+
prebakeContext,
|
|
430
|
+
use: ((dep, scopeValuePairs) => {
|
|
431
|
+
if (dep instanceof Scope) {
|
|
432
|
+
this.#graph(currentBunja).addScope(dep);
|
|
433
|
+
return readScope(dep);
|
|
434
|
+
}
|
|
435
|
+
if (dep instanceof Bunja || isBunjaRef(dep)) {
|
|
436
|
+
const bunjaRef = toBunjaGraphRef(normalizeBunjaRuntimeRef(dep, scopeValuePairs));
|
|
437
|
+
this.#graph(currentBunja).addRequiredBunjaRef(bunjaRef);
|
|
438
|
+
return this.#prebakeBunjaRef(bunjaRef, readScope, prebakeContext);
|
|
439
|
+
}
|
|
440
|
+
throw new Error("`bunja.use` can only be used with Bunja or Scope.");
|
|
441
|
+
}),
|
|
442
|
+
will: ((dep, scopeValuePairs) => {
|
|
443
|
+
if (!(dep instanceof Bunja || isBunjaRef(dep))) throw new Error("`bunja.will` can only be used with Bunja.");
|
|
444
|
+
const bunjaRef = toBunjaGraphRef(normalizeBunjaRuntimeRef(dep, scopeValuePairs));
|
|
445
|
+
this.#graph(currentBunja).addOptionalBunjaRef(bunjaRef);
|
|
446
|
+
const value = this.#prebakeBunjaRef(bunjaRef, readScope, prebakeContext);
|
|
447
|
+
return () => {
|
|
448
|
+
if (frameStack[frameStack.length - 1] !== frame) throw new Error("A thunk returned by `bunja.will` can only be called inside the same bunja init function.");
|
|
449
|
+
return value;
|
|
450
|
+
};
|
|
451
|
+
}),
|
|
452
|
+
effect: noop
|
|
453
|
+
};
|
|
454
|
+
return frame;
|
|
455
|
+
}
|
|
456
|
+
#resolveScopeInstanceMap(bunja, readScope) {
|
|
457
|
+
const scopeInstanceMap = /* @__PURE__ */ new Map();
|
|
458
|
+
this.#ensureRequiredScopeInstances(bunja, scopeInstanceMap, readScope);
|
|
459
|
+
return scopeInstanceMap;
|
|
460
|
+
}
|
|
461
|
+
#ensureRequiredScopeInstances(bunja, scopeInstanceMap, readScope) {
|
|
462
|
+
for (const scope of this.#graph(bunja).requiredScopes) {
|
|
463
|
+
if (scopeInstanceMap.has(scope)) continue;
|
|
464
|
+
scopeInstanceMap.set(scope, this.#getScopeInstance(scope, readScope(scope)));
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
#getScopeInstance(scope, value) {
|
|
468
|
+
const key = scope.hash(value);
|
|
469
|
+
const instanceMap = this.#scopes.get(scope) ?? this.#scopes.set(scope, /* @__PURE__ */ new Map()).get(scope);
|
|
470
|
+
return instanceMap.get(key) ?? instanceMap.set(key, this.#createScopeInstance(scope, key, value, () => {
|
|
471
|
+
instanceMap.delete(key);
|
|
472
|
+
if (__DEV__) devtoolsGlobalHook.emit("scopeInstanceUnmounted", {
|
|
473
|
+
storeId: this.id,
|
|
474
|
+
scope,
|
|
475
|
+
key
|
|
476
|
+
});
|
|
477
|
+
})).get(key);
|
|
478
|
+
}
|
|
479
|
+
#createBunjaInstance(id, baseId, value, dependencyMounts, effects, recipe, dispose) {
|
|
480
|
+
const bunjaInstance = new BunjaInstance(id, baseId, value, dependencyMounts, effects, recipe, () => {
|
|
481
|
+
if (__DEV__) devtoolsGlobalHook.emit("bunjaInstanceUnmounted", {
|
|
482
|
+
storeId: this.id,
|
|
483
|
+
bunjaInstanceId: id
|
|
484
|
+
});
|
|
485
|
+
dispose();
|
|
486
|
+
delete this.#bunjas[id];
|
|
487
|
+
const bucket = this.#bunjaBuckets.get(baseId);
|
|
488
|
+
bucket?.delete(id);
|
|
489
|
+
if (bucket?.size === 0) this.#bunjaBuckets.delete(baseId);
|
|
490
|
+
});
|
|
491
|
+
this.#bunjas[id] = bunjaInstance;
|
|
492
|
+
(this.#bunjaBuckets.get(baseId) ?? this.#bunjaBuckets.set(baseId, /* @__PURE__ */ new Set()).get(baseId)).add(id);
|
|
493
|
+
if (__DEV__) devtoolsGlobalHook.emit("bunjaInstanceMounted", {
|
|
494
|
+
storeId: this.id,
|
|
495
|
+
bunjaInstanceId: id
|
|
496
|
+
});
|
|
497
|
+
return bunjaInstance;
|
|
498
|
+
}
|
|
499
|
+
#createScopeInstance(scope, key, value, dispose) {
|
|
500
|
+
if (__DEV__) devtoolsGlobalHook.emit("scopeInstanceMounted", {
|
|
501
|
+
storeId: this.id,
|
|
502
|
+
scope,
|
|
503
|
+
key
|
|
504
|
+
});
|
|
505
|
+
return new ScopeInstance(value, dispose);
|
|
506
|
+
}
|
|
507
|
+
};
|
|
508
|
+
function createReadScopeFn(scopeValuePairs, readScope) {
|
|
509
|
+
const map = new Map(scopeValuePairs);
|
|
510
|
+
return (scope) => {
|
|
511
|
+
if (map.has(scope)) return map.get(scope);
|
|
512
|
+
return readScope(scope);
|
|
513
|
+
};
|
|
514
|
+
}
|
|
515
|
+
function delayUnmount(mount, ms = 0) {
|
|
516
|
+
return () => {
|
|
517
|
+
const unmount = mount();
|
|
518
|
+
return () => setTimeout(unmount, ms);
|
|
519
|
+
};
|
|
520
|
+
}
|
|
521
|
+
var Bunja = class Bunja {
|
|
522
|
+
init;
|
|
523
|
+
defaultSeed;
|
|
524
|
+
static counter = 0;
|
|
525
|
+
id = String(Bunja.counter++);
|
|
526
|
+
debugLabel = "";
|
|
527
|
+
constructor(init, defaultSeed) {
|
|
528
|
+
this.init = init;
|
|
529
|
+
this.defaultSeed = defaultSeed;
|
|
530
|
+
if (__DEV__) {
|
|
531
|
+
devtoolsGlobalHook.bunjas[this.id] = this;
|
|
532
|
+
devtoolsGlobalHook.emit("bunjaCreated", { bunjaId: this.id });
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
toString() {
|
|
536
|
+
const { id, debugLabel } = this;
|
|
537
|
+
return `[Bunja:${id}${debugLabel && ` - ${debugLabel}`}]`;
|
|
538
|
+
}
|
|
539
|
+
};
|
|
540
|
+
var BunjaGraph = class {
|
|
541
|
+
bunja;
|
|
542
|
+
graph;
|
|
543
|
+
#phase = {
|
|
544
|
+
baked: false,
|
|
545
|
+
requiredBunjaRefs: [],
|
|
546
|
+
optionalBunjaRefs: [],
|
|
547
|
+
scopes: /* @__PURE__ */ new Set()
|
|
548
|
+
};
|
|
549
|
+
constructor(bunja, graph) {
|
|
550
|
+
this.bunja = bunja;
|
|
551
|
+
this.graph = graph;
|
|
552
|
+
}
|
|
553
|
+
get scopes() {
|
|
554
|
+
return Array.from(this.#phase.scopes);
|
|
555
|
+
}
|
|
556
|
+
get baked() {
|
|
557
|
+
return this.#phase.baked;
|
|
558
|
+
}
|
|
559
|
+
get requiredBunjas() {
|
|
560
|
+
return dedupeBunjas(this.#phase.requiredBunjaRefs.map(({ bunja }) => bunja));
|
|
561
|
+
}
|
|
562
|
+
get optionalBunjas() {
|
|
563
|
+
return dedupeBunjas(this.#phase.optionalBunjaRefs.map(({ bunja }) => bunja));
|
|
564
|
+
}
|
|
565
|
+
get requiredBunjaRefs() {
|
|
566
|
+
return this.#phase.requiredBunjaRefs;
|
|
567
|
+
}
|
|
568
|
+
get optionalBunjaRefs() {
|
|
569
|
+
return this.#phase.optionalBunjaRefs;
|
|
570
|
+
}
|
|
571
|
+
get expandedRequiredBunjas() {
|
|
572
|
+
if (!this.#phase.baked) throw new Error("Bunja is not baked yet.");
|
|
573
|
+
return this.#phase.expandedRequiredBunjas;
|
|
574
|
+
}
|
|
575
|
+
get relatedBunjas() {
|
|
576
|
+
if (!this.#phase.baked) throw new Error("Bunja is not baked yet.");
|
|
577
|
+
return toposort([...this.requiredBunjas, ...this.optionalBunjas], (bunja) => [...this.graph(bunja)?.requiredBunjas ?? [], ...this.graph(bunja)?.optionalBunjas ?? []]);
|
|
578
|
+
}
|
|
579
|
+
get requiredScopes() {
|
|
580
|
+
if (!this.#phase.baked) throw new Error("Bunja is not baked yet.");
|
|
581
|
+
return this.#phase.requiredScopes;
|
|
582
|
+
}
|
|
583
|
+
addRequiredBunjaRef(ref) {
|
|
584
|
+
if (this.#phase.baked) {
|
|
585
|
+
this.#checkRef(this.requiredBunjaRefs, ref);
|
|
586
|
+
return;
|
|
587
|
+
}
|
|
588
|
+
addUniqueBunjaRef(this.#phase.requiredBunjaRefs, ref);
|
|
589
|
+
}
|
|
590
|
+
addOptionalBunjaRef(ref) {
|
|
591
|
+
if (this.#phase.baked) {
|
|
592
|
+
this.#checkRef(this.optionalBunjaRefs, ref);
|
|
593
|
+
return;
|
|
594
|
+
}
|
|
595
|
+
addUniqueBunjaRef(this.#phase.optionalBunjaRefs, ref);
|
|
596
|
+
}
|
|
597
|
+
#checkRef(refs, ref) {
|
|
598
|
+
if (!refs.some((known) => known.bunja === ref.bunja && known.scopeValuePairs.length === ref.scopeValuePairs.length && known.scopeValuePairs.every(([scope], i) => scope === ref.scopeValuePairs[i][0]))) throw new Error("Cannot introduce a new dependency after the bunja is baked.");
|
|
599
|
+
}
|
|
600
|
+
addScope(scope) {
|
|
601
|
+
if (this.#phase.baked) {
|
|
602
|
+
if (!this.#phase.scopes.has(scope)) throw new Error("Cannot introduce a new scope after the bunja is baked.");
|
|
603
|
+
return;
|
|
604
|
+
}
|
|
605
|
+
this.#phase.scopes.add(scope);
|
|
606
|
+
}
|
|
607
|
+
bake() {
|
|
608
|
+
if (this.#phase.baked) throw new Error("Bunja is already baked.");
|
|
609
|
+
const scopes = this.#phase.scopes;
|
|
610
|
+
const requiredBunjaRefs = this.#phase.requiredBunjaRefs;
|
|
611
|
+
const optionalBunjaRefs = this.#phase.optionalBunjaRefs;
|
|
612
|
+
const requiredBunjas = this.requiredBunjas;
|
|
613
|
+
const expandedRequiredBunjas = toposort(requiredBunjas, (bunja) => this.graph(bunja)?.requiredBunjas ?? []);
|
|
614
|
+
const requiredScopeSet = /* @__PURE__ */ new Set();
|
|
615
|
+
for (const ref of requiredBunjaRefs) {
|
|
616
|
+
const boundScopes = getBoundScopeSet(ref.scopeValuePairs);
|
|
617
|
+
for (const scope of this.graph(ref.bunja).requiredScopes) if (!boundScopes.has(scope)) requiredScopeSet.add(scope);
|
|
618
|
+
}
|
|
619
|
+
for (const scope of scopes) requiredScopeSet.add(scope);
|
|
620
|
+
const requiredScopes = Array.from(requiredScopeSet);
|
|
621
|
+
this.#phase = {
|
|
622
|
+
baked: true,
|
|
623
|
+
requiredBunjaRefs,
|
|
624
|
+
optionalBunjaRefs,
|
|
625
|
+
expandedRequiredBunjas,
|
|
626
|
+
requiredScopes,
|
|
627
|
+
scopes
|
|
628
|
+
};
|
|
629
|
+
}
|
|
630
|
+
calcBaseInstanceId(scopeInstanceMap) {
|
|
631
|
+
const scopeInstanceIds = this.requiredScopes.map((scope) => scopeInstanceMap.get(scope).id);
|
|
632
|
+
return `${this.bunja.id}:${scopeInstanceIds.join(",")}`;
|
|
633
|
+
}
|
|
634
|
+
calcInstanceId(scopeInstanceMap, activeDependencyIds = []) {
|
|
635
|
+
return `${this.calcBaseInstanceId(scopeInstanceMap)}:${Array.from(activeDependencyIds).join(",")}`;
|
|
636
|
+
}
|
|
637
|
+
};
|
|
638
|
+
var Scope = class Scope {
|
|
639
|
+
hash;
|
|
640
|
+
static counter = 0;
|
|
641
|
+
id = String(Scope.counter++);
|
|
642
|
+
debugLabel = "";
|
|
643
|
+
constructor(hash = Scope.identity) {
|
|
644
|
+
this.hash = hash;
|
|
645
|
+
if (__DEV__) {
|
|
646
|
+
devtoolsGlobalHook.scopes[this.id] = this;
|
|
647
|
+
devtoolsGlobalHook.emit("scopeCreated", { scopeId: this.id });
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
static identity(x) {
|
|
651
|
+
return x;
|
|
652
|
+
}
|
|
653
|
+
bind(value) {
|
|
654
|
+
return [this, value];
|
|
655
|
+
}
|
|
656
|
+
toString() {
|
|
657
|
+
const { id, debugLabel } = this;
|
|
658
|
+
return `[Scope:${id}${debugLabel && ` - ${debugLabel}`}]`;
|
|
659
|
+
}
|
|
660
|
+
};
|
|
661
|
+
var RefCounter = class {
|
|
662
|
+
#count = 0;
|
|
663
|
+
add() {
|
|
664
|
+
++this.#count;
|
|
665
|
+
}
|
|
666
|
+
sub() {
|
|
667
|
+
--this.#count;
|
|
668
|
+
if (this.#count < 1) {
|
|
669
|
+
this.dispose();
|
|
670
|
+
this.dispose = noop;
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
};
|
|
674
|
+
var BunjaInstance = class extends RefCounter {
|
|
675
|
+
id;
|
|
676
|
+
baseId;
|
|
677
|
+
value;
|
|
678
|
+
dependencyMounts;
|
|
679
|
+
effects;
|
|
680
|
+
recipe;
|
|
681
|
+
_dispose;
|
|
682
|
+
#cleanup;
|
|
683
|
+
#disposed = false;
|
|
684
|
+
constructor(id, baseId, value, dependencyMounts, effects, recipe, _dispose) {
|
|
685
|
+
super();
|
|
686
|
+
this.id = id;
|
|
687
|
+
this.baseId = baseId;
|
|
688
|
+
this.value = value;
|
|
689
|
+
this.dependencyMounts = dependencyMounts;
|
|
690
|
+
this.effects = effects;
|
|
691
|
+
this.recipe = recipe;
|
|
692
|
+
this._dispose = _dispose;
|
|
693
|
+
}
|
|
694
|
+
dispose() {
|
|
695
|
+
if (this.#disposed) return;
|
|
696
|
+
this.#disposed = true;
|
|
697
|
+
this.#cleanup?.();
|
|
698
|
+
this.#cleanup = void 0;
|
|
699
|
+
this._dispose();
|
|
700
|
+
}
|
|
701
|
+
add() {
|
|
702
|
+
this.#cleanup ??= this.#mount();
|
|
703
|
+
super.add();
|
|
704
|
+
}
|
|
705
|
+
#mount() {
|
|
706
|
+
const dependencyCleanups = this.dependencyMounts.map((mount) => mount());
|
|
707
|
+
const effectCleanups = this.effects.map((effect) => effect()).filter(Boolean);
|
|
708
|
+
return () => {
|
|
709
|
+
for (const cleanup of dependencyCleanups) cleanup();
|
|
710
|
+
for (const cleanup of effectCleanups) cleanup();
|
|
711
|
+
};
|
|
712
|
+
}
|
|
713
|
+
};
|
|
714
|
+
var ScopeInstance = class ScopeInstance extends RefCounter {
|
|
715
|
+
value;
|
|
716
|
+
dispose;
|
|
717
|
+
static counter = 0;
|
|
718
|
+
id = String(ScopeInstance.counter++);
|
|
719
|
+
constructor(value, dispose) {
|
|
720
|
+
super();
|
|
721
|
+
this.value = value;
|
|
722
|
+
this.dispose = dispose;
|
|
723
|
+
}
|
|
724
|
+
};
|
|
725
|
+
function toposort(nodes, getDependencies) {
|
|
726
|
+
const visited = /* @__PURE__ */ new Set();
|
|
727
|
+
const result = [];
|
|
728
|
+
function visit(current) {
|
|
729
|
+
if (visited.has(current)) return;
|
|
730
|
+
visited.add(current);
|
|
731
|
+
for (const dependency of getDependencies(current)) visit(dependency);
|
|
732
|
+
result.push(current);
|
|
733
|
+
}
|
|
734
|
+
for (const node of nodes) visit(node);
|
|
735
|
+
return result;
|
|
736
|
+
}
|
|
737
|
+
const noop = () => {};
|
|
738
|
+
let devtoolsGlobalHook;
|
|
739
|
+
const storeFinalizer = __DEV__ ? new FinalizationRegistry((id) => {
|
|
740
|
+
devtoolsGlobalHook.stores.delete(id);
|
|
741
|
+
}) : void 0;
|
|
742
|
+
if (__DEV__) {
|
|
743
|
+
if (globalThis.__BUNJA_DEVTOOLS_GLOBAL_HOOK__) devtoolsGlobalHook = globalThis.__BUNJA_DEVTOOLS_GLOBAL_HOOK__;
|
|
744
|
+
else {
|
|
745
|
+
devtoolsGlobalHook = {
|
|
746
|
+
stores: /* @__PURE__ */ new Map(),
|
|
747
|
+
snapshot: () => [],
|
|
748
|
+
bunjas: {},
|
|
749
|
+
scopes: {},
|
|
750
|
+
listeners: {
|
|
751
|
+
graphChanged: /* @__PURE__ */ new Set(),
|
|
752
|
+
bunjaCreated: /* @__PURE__ */ new Set(),
|
|
753
|
+
scopeCreated: /* @__PURE__ */ new Set(),
|
|
754
|
+
storeCreated: /* @__PURE__ */ new Set(),
|
|
755
|
+
storeDisposed: /* @__PURE__ */ new Set(),
|
|
756
|
+
getCalled: /* @__PURE__ */ new Set(),
|
|
757
|
+
bunjaInstanceMounted: /* @__PURE__ */ new Set(),
|
|
758
|
+
bunjaInstanceUnmounted: /* @__PURE__ */ new Set(),
|
|
759
|
+
scopeInstanceMounted: /* @__PURE__ */ new Set(),
|
|
760
|
+
scopeInstanceUnmounted: /* @__PURE__ */ new Set()
|
|
761
|
+
},
|
|
762
|
+
emit: (type, event) => {
|
|
763
|
+
for (const fn of devtoolsGlobalHook.listeners[type]) fn(event);
|
|
764
|
+
},
|
|
765
|
+
on: (type, listener) => {
|
|
766
|
+
devtoolsGlobalHook.listeners[type].add(listener);
|
|
767
|
+
return () => devtoolsGlobalHook.listeners[type].delete(listener);
|
|
768
|
+
}
|
|
769
|
+
};
|
|
770
|
+
globalThis.__BUNJA_DEVTOOLS_GLOBAL_HOOK__ = devtoolsGlobalHook;
|
|
771
|
+
}
|
|
772
|
+
devtoolsGlobalHook.stores ??= /* @__PURE__ */ new Map();
|
|
773
|
+
devtoolsGlobalHook.listeners.graphChanged ??= /* @__PURE__ */ new Set();
|
|
774
|
+
devtoolsGlobalHook.snapshot = () => Object.freeze(Array.from(devtoolsGlobalHook.stores, ([id, ref]) => {
|
|
775
|
+
const store = ref.deref();
|
|
776
|
+
if (!store) devtoolsGlobalHook.stores.delete(id);
|
|
777
|
+
return store?.snapshot();
|
|
778
|
+
}).filter((snapshot) => snapshot !== void 0));
|
|
779
|
+
}
|
|
780
|
+
export { Bunja, BunjaStore, Scope, bunja, createBunjaStore, createReadScopeFn, createScope, delayUnmount, override };
|