@blac/core 2.0.22 → 2.1.1
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/StateContainer-BUyuxKb3.cjs +667 -0
- package/dist/StateContainer-BUyuxKb3.cjs.map +1 -0
- package/dist/StateContainer-B_7UHc3I.js +632 -0
- package/dist/StateContainer-B_7UHc3I.js.map +1 -0
- package/dist/StateContainer-Bh2ipNYj.js +632 -0
- package/dist/StateContainer-Bh2ipNYj.js.map +1 -0
- package/dist/StateContainer-ClzwKnQZ.cjs +667 -0
- package/dist/StateContainer-ClzwKnQZ.cjs.map +1 -0
- package/dist/StateContainer-DiQATvZh.js +632 -0
- package/dist/StateContainer-DiQATvZh.js.map +1 -0
- package/dist/StateContainer-j16zciex.cjs +667 -0
- package/dist/StateContainer-j16zciex.cjs.map +1 -0
- package/dist/StateContainerRegistry-BX-DlHXO.js +747 -0
- package/dist/StateContainerRegistry-BX-DlHXO.js.map +1 -0
- package/dist/StateContainerRegistry-Bf5VuVjm.js +747 -0
- package/dist/StateContainerRegistry-Bf5VuVjm.js.map +1 -0
- package/dist/StateContainerRegistry-BvSRfhKv.cjs +866 -0
- package/dist/StateContainerRegistry-BvSRfhKv.cjs.map +1 -0
- package/dist/StateContainerRegistry-C8jxrcqk.cjs +866 -0
- package/dist/StateContainerRegistry-C8jxrcqk.cjs.map +1 -0
- package/dist/StateContainerRegistry-DjQQtlCW.cjs +870 -0
- package/dist/StateContainerRegistry-DjQQtlCW.cjs.map +1 -0
- package/dist/StateContainerRegistry-gQbEutkN.js +751 -0
- package/dist/StateContainerRegistry-gQbEutkN.js.map +1 -0
- package/dist/config.d.cts +10 -6
- package/dist/config.d.ts +10 -6
- package/dist/constants.d.cts +12 -17
- package/dist/constants.d.ts +12 -17
- package/dist/core/StateContainer.d.ts +16 -34
- package/dist/core/StateContainerRegistry.d.ts +57 -174
- package/dist/core/symbols.d.ts +7 -4
- package/dist/decorators/blac.d.ts +5 -2
- package/dist/ensure-Bi_yXk6f.js +46 -0
- package/dist/ensure-Bi_yXk6f.js.map +1 -0
- package/dist/ensure-Bx41d-il.cjs +63 -0
- package/dist/ensure-Bx41d-il.cjs.map +1 -0
- package/dist/ensure-C7UQ_mKM.js +46 -0
- package/dist/ensure-C7UQ_mKM.js.map +1 -0
- package/dist/ensure-D1D4WjYf.cjs +63 -0
- package/dist/ensure-D1D4WjYf.cjs.map +1 -0
- package/dist/ensure-DBnuRJ0R.cjs +63 -0
- package/dist/ensure-DBnuRJ0R.cjs.map +1 -0
- package/dist/ensure-NTB9nnnj.js +46 -0
- package/dist/ensure-NTB9nnnj.js.map +1 -0
- package/dist/index.cjs +70 -49
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -7
- package/dist/index.d.ts +2 -7
- package/dist/index.js +53 -26
- package/dist/index.js.map +1 -1
- package/dist/internal.cjs +10 -0
- package/dist/internal.d.cts +8 -0
- package/dist/internal.d.ts +8 -0
- package/dist/internal.js +3 -0
- package/dist/plugin/BlacPlugin.d.ts +8 -24
- package/dist/plugin/PluginManager.d.ts +41 -64
- package/dist/plugins.cjs +67 -87
- package/dist/plugins.cjs.map +1 -1
- package/dist/plugins.d.cts +7 -6
- package/dist/plugins.d.ts +7 -6
- package/dist/plugins.js +67 -87
- package/dist/plugins.js.map +1 -1
- package/dist/registry/config.d.ts +3 -0
- package/dist/testing.cjs +37 -35
- package/dist/testing.cjs.map +1 -1
- package/dist/testing.d.cts +5 -11
- package/dist/testing.d.ts +5 -11
- package/dist/testing.js +28 -26
- package/dist/testing.js.map +1 -1
- package/dist/types/branded.d.ts +3 -10
- package/dist/types/utilities.d.ts +0 -18
- package/dist/utils/idGenerator.d.ts +6 -13
- package/dist/utils/static-props.d.ts +9 -35
- package/dist/utils/structural-key.d.ts +0 -5
- package/dist/watch/watch.d.ts +4 -9
- package/package.json +15 -2
|
@@ -0,0 +1,667 @@
|
|
|
1
|
+
const require_StateContainerRegistry = require("./StateContainerRegistry-C8jxrcqk.cjs");
|
|
2
|
+
let _dirtytalk_structural = require("@dirtytalk/structural");
|
|
3
|
+
//#region src/utils/idGenerator.ts
|
|
4
|
+
/**
|
|
5
|
+
* Build an instance id: `${prefix}:${affix}` when an affix is given (the
|
|
6
|
+
* registry passes the instance key), otherwise `${prefix}:${timestamp}_${random}`.
|
|
7
|
+
*
|
|
8
|
+
* Ids are not unique: same-named classes share an id for the same affix, and
|
|
9
|
+
* the lazy `$blac.id` of an unregistered instance is always `${prefix}:main`.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* generateSimpleId('CounterBloc', 'default'); // "CounterBloc:default"
|
|
14
|
+
* generateSimpleId('CounterBloc'); // "CounterBloc:1698765432100_a3k9d7f2q"
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
function generateSimpleId(prefix, affix) {
|
|
18
|
+
if (affix) return `${prefix}:${affix}`;
|
|
19
|
+
return `${prefix}:${Date.now()}_${Math.random().toString(36).substring(2, 11)}`;
|
|
20
|
+
}
|
|
21
|
+
//#endregion
|
|
22
|
+
//#region src/registry/config.ts
|
|
23
|
+
let _registry = require_StateContainerRegistry.globalRegistry;
|
|
24
|
+
/**
|
|
25
|
+
* The registry every plain helper (`acquire`, `ensure`, `watch`, …) uses.
|
|
26
|
+
* `RegistryProvider` from `@blac/react` does not change it; components use
|
|
27
|
+
* `useRegistry()` for the provider's registry.
|
|
28
|
+
* @public
|
|
29
|
+
*/
|
|
30
|
+
function getRegistry() {
|
|
31
|
+
return _registry;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* @public
|
|
35
|
+
*/
|
|
36
|
+
function setRegistry(registry) {
|
|
37
|
+
_registry = registry;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* @public
|
|
41
|
+
*/
|
|
42
|
+
function getStats() {
|
|
43
|
+
return _registry.getStats();
|
|
44
|
+
}
|
|
45
|
+
//#endregion
|
|
46
|
+
//#region src/core/meta.ts
|
|
47
|
+
/**
|
|
48
|
+
* `$blac` meta namespace for `StateContainer`.
|
|
49
|
+
*
|
|
50
|
+
* `BlacMeta` and `BlacHydration` are classes whose accessors live on the
|
|
51
|
+
* prototype and read the container through one `#private` back-reference, so
|
|
52
|
+
* a container allocates a single small object here instead of a literal with
|
|
53
|
+
* seven closures. Members are therefore not own-enumerable: `Object.keys` and
|
|
54
|
+
* `JSON.stringify` see an empty object. Read the fields by name.
|
|
55
|
+
*/
|
|
56
|
+
/**
|
|
57
|
+
* Non-enumerable brand carried on every `$blac` object. Used by the clobber
|
|
58
|
+
* guard in `StateContainer` to detect a subclass class-field shadowing the
|
|
59
|
+
* base's own `$blac` property, and by the meta tests. Intentionally NOT
|
|
60
|
+
* re-exported from the package barrel.
|
|
61
|
+
*/
|
|
62
|
+
const META_BRAND = Symbol("blac.meta");
|
|
63
|
+
const EMPTY_DEPS = /* @__PURE__ */ new Map();
|
|
64
|
+
/**
|
|
65
|
+
* Hydration sub-surface of {@link BlacMeta}. Exposes the container's
|
|
66
|
+
* hydration state machine — the `begin`/`apply`/`finish`/`fail`/`wait`
|
|
67
|
+
* methods and the `status`/`error`/`isHydrated`/`changedWhileHydrating`
|
|
68
|
+
* getters — namespaced under `$blac`.
|
|
69
|
+
*/
|
|
70
|
+
var BlacHydration = class {
|
|
71
|
+
#c;
|
|
72
|
+
/** @internal */
|
|
73
|
+
constructor(container) {
|
|
74
|
+
this.#c = container;
|
|
75
|
+
Object.freeze(this);
|
|
76
|
+
}
|
|
77
|
+
get status() {
|
|
78
|
+
return this.#c._hydrationStatus;
|
|
79
|
+
}
|
|
80
|
+
get error() {
|
|
81
|
+
return this.#c._hydrationError;
|
|
82
|
+
}
|
|
83
|
+
get isHydrated() {
|
|
84
|
+
return this.#c._hydrationStatus === "hydrated";
|
|
85
|
+
}
|
|
86
|
+
get changedWhileHydrating() {
|
|
87
|
+
return this.#c._changedWhileHydrating;
|
|
88
|
+
}
|
|
89
|
+
begin() {
|
|
90
|
+
this.#c._beginHydration();
|
|
91
|
+
}
|
|
92
|
+
apply(next) {
|
|
93
|
+
return this.#c._applyHydratedState(next);
|
|
94
|
+
}
|
|
95
|
+
finish() {
|
|
96
|
+
this.#c._finishHydration();
|
|
97
|
+
}
|
|
98
|
+
fail(error) {
|
|
99
|
+
this.#c._failHydration(error);
|
|
100
|
+
}
|
|
101
|
+
wait() {
|
|
102
|
+
return this.#c._waitForHydration();
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
/**
|
|
106
|
+
* The reserved `$blac` instance member on every `StateContainer`. Gathers the
|
|
107
|
+
* container's identity/lifecycle/hydration surface (`name`, `id`, `debug`,
|
|
108
|
+
* `createdAt`, `disposed`, `dependencies`, and the hydration methods) under one
|
|
109
|
+
* namespace, keeping the generic names free for userland blocs.
|
|
110
|
+
*
|
|
111
|
+
* Allocated once per container and frozen. Every accessor reads the
|
|
112
|
+
* container's current `_`-private fields live — values are never snapshotted.
|
|
113
|
+
*/
|
|
114
|
+
var BlacMeta = class {
|
|
115
|
+
#c;
|
|
116
|
+
#hydration;
|
|
117
|
+
/** @internal Called from the `StateContainer` field initializer. */
|
|
118
|
+
constructor(container) {
|
|
119
|
+
this.#c = container;
|
|
120
|
+
Object.freeze(this);
|
|
121
|
+
}
|
|
122
|
+
get name() {
|
|
123
|
+
return this.#c._name;
|
|
124
|
+
}
|
|
125
|
+
get id() {
|
|
126
|
+
const c = this.#c;
|
|
127
|
+
return c._instanceId ??= generateSimpleId(require_StateContainerRegistry.getBlacName(c.constructor), "main");
|
|
128
|
+
}
|
|
129
|
+
get debug() {
|
|
130
|
+
return this.#c._debug;
|
|
131
|
+
}
|
|
132
|
+
get createdAt() {
|
|
133
|
+
return this.#c._createdAt;
|
|
134
|
+
}
|
|
135
|
+
get disposed() {
|
|
136
|
+
return this.#c._disposed;
|
|
137
|
+
}
|
|
138
|
+
get dependencies() {
|
|
139
|
+
return this.#c._dependencies ?? EMPTY_DEPS;
|
|
140
|
+
}
|
|
141
|
+
/** Built on first access; identity-stable afterwards. */
|
|
142
|
+
get hydration() {
|
|
143
|
+
return this.#hydration ??= new BlacHydration(this.#c);
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
Object.defineProperty(BlacMeta.prototype, META_BRAND, {
|
|
147
|
+
value: true,
|
|
148
|
+
enumerable: false,
|
|
149
|
+
writable: false,
|
|
150
|
+
configurable: false
|
|
151
|
+
});
|
|
152
|
+
//#endregion
|
|
153
|
+
//#region src/core/StateContainer.ts
|
|
154
|
+
/**
|
|
155
|
+
* Brand symbol carried (non-enumerable) on every handle returned by
|
|
156
|
+
* `depend()`. Framework adapters (`@blac/react`) use this to detect handles
|
|
157
|
+
* inside a tracked-proxy and swap in a session-bound wrapper.
|
|
158
|
+
*
|
|
159
|
+
* @internal — exported from `@blac/core/internal` for `@blac/react`.
|
|
160
|
+
*/
|
|
161
|
+
const DEP_BRAND = Symbol("blac.depHandle");
|
|
162
|
+
const EMPTY_RECORD = Object.freeze({});
|
|
163
|
+
let deactivatedReason;
|
|
164
|
+
let stateChangeCount = 0;
|
|
165
|
+
/** @internal */
|
|
166
|
+
function getStateChangeCount() {
|
|
167
|
+
return stateChangeCount;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Shallow per-key `Object.is` comparison of two plain records. Keys are
|
|
171
|
+
* considered: a key present in one but not the other (regardless of value)
|
|
172
|
+
* makes the records unequal. Used to detect real deps changes.
|
|
173
|
+
*/
|
|
174
|
+
function shallowEqualRecord(a, b) {
|
|
175
|
+
if (a === b) return true;
|
|
176
|
+
const aKeys = Object.keys(a);
|
|
177
|
+
if (aKeys.length !== Object.keys(b).length) return false;
|
|
178
|
+
for (const key of aKeys) {
|
|
179
|
+
if (!(key in b)) return false;
|
|
180
|
+
if (!Object.is(a[key], b[key])) return false;
|
|
181
|
+
}
|
|
182
|
+
return true;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* BlaC's lifecycle/identity/dependency layer on top of `StructuralContainer`
|
|
186
|
+
* (which provides `state`, `emit`/`patch`/`update`, and `channel`).
|
|
187
|
+
* StateContainer adds identity/lifecycle/hydration (`$blac: BlacMeta`),
|
|
188
|
+
* cross-bloc deps (`depend()`), per-consumer deps slices, and registry
|
|
189
|
+
* integration. Subscribe via `onSystemEvent(...)` for coarse lifecycle
|
|
190
|
+
* events or `this.channel.subscribe(...)` for path-scoped ones.
|
|
191
|
+
*/
|
|
192
|
+
var StateContainer = class extends _dirtytalk_structural.StructuralContainer {
|
|
193
|
+
static {
|
|
194
|
+
this.__excludeFromDevTools = false;
|
|
195
|
+
}
|
|
196
|
+
get deps() {
|
|
197
|
+
return this._deps;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* @internal Apply one owner's (consumer's) deps slice. Shallow-merges the
|
|
201
|
+
* slice, reconciles keys the owner dropped since its last apply (other
|
|
202
|
+
* owners' keys untouched), dev-warns on cross-owner key collisions, then
|
|
203
|
+
* recomputes the merged view and fires onDepsChanged if it changed.
|
|
204
|
+
*
|
|
205
|
+
* Idempotent: re-applying an identical slice for the same owner is a no-op.
|
|
206
|
+
*/
|
|
207
|
+
[require_StateContainerRegistry.APPLY_DEPS](ownerId, slice) {
|
|
208
|
+
if (this._disposed) return;
|
|
209
|
+
const owners = this._depsByOwner ??= /* @__PURE__ */ new Map();
|
|
210
|
+
const prevSlice = owners.get(ownerId);
|
|
211
|
+
if (prevSlice && shallowEqualRecord(prevSlice, slice)) return;
|
|
212
|
+
if (require_StateContainerRegistry.IS_DEV) for (const key of Object.keys(slice)) for (const [otherOwner, otherSlice] of owners) {
|
|
213
|
+
if (otherOwner === ownerId) continue;
|
|
214
|
+
if (key in otherSlice && otherSlice[key] !== slice[key]) console.warn(`[${this._name}] multiple owners writing dep \`${String(key)}\`; last write wins`);
|
|
215
|
+
}
|
|
216
|
+
owners.set(ownerId, { ...slice });
|
|
217
|
+
this.reconcileDeps();
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* @internal Withdraw an owner's entire deps slice (consumer unmounted).
|
|
221
|
+
*/
|
|
222
|
+
[require_StateContainerRegistry.REMOVE_DEPS_OWNER](ownerId) {
|
|
223
|
+
if (this._disposed) return;
|
|
224
|
+
if (!this._depsByOwner?.delete(ownerId)) return;
|
|
225
|
+
this.reconcileDeps();
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* @internal Subscribe to this instance's own disposal. Delegates to the
|
|
229
|
+
* per-instance `'dispose'` system event — used by `watch()` in place of
|
|
230
|
+
* filtering the global registry `'disposed'` event.
|
|
231
|
+
*/
|
|
232
|
+
[require_StateContainerRegistry.ON_DISPOSE](handler) {
|
|
233
|
+
return this.onSystemEvent("dispose", handler);
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* @internal Drive the 0↔1 ownership transition. Idempotent: repeated calls
|
|
237
|
+
* with the same value are no-ops, so the registry can call it from every
|
|
238
|
+
* acquire/release without tracking edges itself.
|
|
239
|
+
*
|
|
240
|
+
* Returns which real transition (if any) just happened, so the registry can
|
|
241
|
+
* emit the matching `activated`/`deactivated` plugin event without exposing
|
|
242
|
+
* `_activation` itself — `'none'` for an idempotent no-op call.
|
|
243
|
+
*/
|
|
244
|
+
[require_StateContainerRegistry.SET_ACTIVE](active) {
|
|
245
|
+
if (this._disposed) return "none";
|
|
246
|
+
if (active === (this._activation !== null)) return "none";
|
|
247
|
+
if (active) {
|
|
248
|
+
this._activation = new AbortController();
|
|
249
|
+
this.onActivate(this._activation.signal);
|
|
250
|
+
return {
|
|
251
|
+
kind: "activated",
|
|
252
|
+
signal: this._activation.signal
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
this._abortActivation();
|
|
256
|
+
this.onDeactivate();
|
|
257
|
+
return "deactivated";
|
|
258
|
+
}
|
|
259
|
+
_abortActivation() {
|
|
260
|
+
this._activation?.abort(deactivatedReason ??= new DOMException("The bloc was deactivated", "AbortError"));
|
|
261
|
+
this._activation = null;
|
|
262
|
+
}
|
|
263
|
+
reconcileDeps() {
|
|
264
|
+
const prev = this._deps;
|
|
265
|
+
const next = {};
|
|
266
|
+
if (this._depsByOwner) for (const slice of this._depsByOwner.values()) Object.assign(next, slice);
|
|
267
|
+
for (const key of Object.keys(prev)) if (!(key in next)) next[key] = void 0;
|
|
268
|
+
if (shallowEqualRecord(prev, next)) return;
|
|
269
|
+
this._deps = next;
|
|
270
|
+
this.onDepsChanged(next, prev);
|
|
271
|
+
this._registry.emit("depsChanged", this, prev, next);
|
|
272
|
+
}
|
|
273
|
+
onDepsChanged(_next, _prev) {}
|
|
274
|
+
get state() {
|
|
275
|
+
return this._stateOverride ?? super.state;
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* @internal Run `fn` (with `this` = this instance, so a prototype getter can
|
|
279
|
+
* be passed directly) while `state` reports `tracked` and dep handles read
|
|
280
|
+
* off `this` route through `onDepHandle`. Restored in `finally`, and
|
|
281
|
+
* nestable — getters reading other getters stay in tracked context.
|
|
282
|
+
*
|
|
283
|
+
* The dep hook lives here rather than in the adapter's proxy because getters
|
|
284
|
+
* now run with `this` = the real instance (so ES `#private` works), which
|
|
285
|
+
* means a `this.someDep` read never passes through the proxy's trap.
|
|
286
|
+
*/
|
|
287
|
+
[require_StateContainerRegistry.WITH_TRACKED_STATE](tracked, fn, onDepHandle) {
|
|
288
|
+
const prevState = this._stateOverride;
|
|
289
|
+
const prevHook = this._depHandleHook;
|
|
290
|
+
this._stateOverride = tracked;
|
|
291
|
+
this._depHandleHook = onDepHandle;
|
|
292
|
+
try {
|
|
293
|
+
return fn.call(this);
|
|
294
|
+
} finally {
|
|
295
|
+
this._stateOverride = prevState;
|
|
296
|
+
this._depHandleHook = prevHook;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
get args() {
|
|
300
|
+
return this._config.args;
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Declare a cross-bloc dependency. Returns a branded handle with two
|
|
304
|
+
* accessors — the dep instance is resolved against the registry on each call,
|
|
305
|
+
* which keeps the surface immune to dep-instance churn:
|
|
306
|
+
*
|
|
307
|
+
* - `handle.track(options?)` — reactive read. Returns `[state, instance]`.
|
|
308
|
+
* Inside a getter reached through the React proxy this subscribes the
|
|
309
|
+
* reading component to the dep's changes (base impl: live, no subscription).
|
|
310
|
+
* - `handle.untracked(options?)` — returns the live instance with no tracking,
|
|
311
|
+
* for imperative method calls and one-off reads.
|
|
312
|
+
*
|
|
313
|
+
* `defaultArgs` resolves the dep instance when an accessor is called without
|
|
314
|
+
* its own `args`; per-call `options.args` overrides it and can derive from
|
|
315
|
+
* current state. This does NOT auto-resubscribe outside the React proxy;
|
|
316
|
+
* non-React consumers needing updates should subscribe explicitly.
|
|
317
|
+
*/
|
|
318
|
+
depend(Type, defaultArgs) {
|
|
319
|
+
if (!this._dependencies) this._dependencies = /* @__PURE__ */ new Map();
|
|
320
|
+
this._dependencies.set(Type, this._registry.resolveKey(Type, void 0, defaultArgs));
|
|
321
|
+
const resolve = (args) => {
|
|
322
|
+
const effectiveArgs = args ?? defaultArgs;
|
|
323
|
+
const key = this._registry.resolveKey(Type, void 0, effectiveArgs);
|
|
324
|
+
if (this._disposed && require_StateContainerRegistry.IS_DEV) console.warn(`[blac] ${this._name}: dependency accessed after dispose. Guard with \`if (this.$blac.disposed) return\` after each \`await\`.`);
|
|
325
|
+
return this._registry.acquire(Type, key, {
|
|
326
|
+
canCreate: true,
|
|
327
|
+
countRef: false,
|
|
328
|
+
args: effectiveArgs,
|
|
329
|
+
dependent: this._disposed ? void 0 : this,
|
|
330
|
+
sweepIfUnowned: this._disposed
|
|
331
|
+
});
|
|
332
|
+
};
|
|
333
|
+
const handle = {
|
|
334
|
+
track: (options) => {
|
|
335
|
+
const wrapped = this._depHandleHook?.(handle);
|
|
336
|
+
if (wrapped !== void 0 && wrapped !== handle) return wrapped.track(options);
|
|
337
|
+
const instance = resolve(options?.args);
|
|
338
|
+
return [instance.state, instance];
|
|
339
|
+
},
|
|
340
|
+
untracked: (options) => resolve(options?.args)
|
|
341
|
+
};
|
|
342
|
+
Object.defineProperty(handle, DEP_BRAND, {
|
|
343
|
+
value: {
|
|
344
|
+
Type,
|
|
345
|
+
defaultArgs
|
|
346
|
+
},
|
|
347
|
+
enumerable: false,
|
|
348
|
+
writable: false,
|
|
349
|
+
configurable: false
|
|
350
|
+
});
|
|
351
|
+
return handle;
|
|
352
|
+
}
|
|
353
|
+
constructor(initialState, options) {
|
|
354
|
+
super(initialState, options);
|
|
355
|
+
this._depsByOwner = null;
|
|
356
|
+
this._deps = EMPTY_RECORD;
|
|
357
|
+
this._disposed = false;
|
|
358
|
+
this._hydrationStatus = "idle";
|
|
359
|
+
this._changedWhileHydrating = false;
|
|
360
|
+
this._hydrationPromise = null;
|
|
361
|
+
this._hydrationPromiseSettled = false;
|
|
362
|
+
this._config = EMPTY_RECORD;
|
|
363
|
+
this._initCalled = false;
|
|
364
|
+
this._activation = null;
|
|
365
|
+
this._emitWindowStart = 0;
|
|
366
|
+
this._emitCount = 0;
|
|
367
|
+
this._emitRateWarned = false;
|
|
368
|
+
this._systemEventHandlers = null;
|
|
369
|
+
this._dependencies = null;
|
|
370
|
+
this._pendingChange = null;
|
|
371
|
+
this._bridgeUnsub = null;
|
|
372
|
+
this._registry = getRegistry();
|
|
373
|
+
this._equalityFn = require_StateContainerRegistry.getClassEquality(this.constructor) ?? require_StateContainerRegistry.getBlacConfig().equality;
|
|
374
|
+
this._name = require_StateContainerRegistry.getBlacName(this.constructor);
|
|
375
|
+
this._debug = false;
|
|
376
|
+
this._createdAt = Date.now();
|
|
377
|
+
this.$blac = new BlacMeta(this);
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* Called once after construction with the args passed at acquire time, before the first
|
|
381
|
+
* state snapshot is read by any consumer. Override to seed args-derived state (via
|
|
382
|
+
* this.emit(...)) or kick off loads.
|
|
383
|
+
*/
|
|
384
|
+
init(_args) {}
|
|
385
|
+
/**
|
|
386
|
+
* Called when this instance goes from unowned to owned (first ref or first
|
|
387
|
+
* `depend()` dependent). Override to start work that should only run while
|
|
388
|
+
* something is actually using the instance — fetches, timers, listeners.
|
|
389
|
+
*
|
|
390
|
+
* `signal` aborts on the matching `onDeactivate` and on dispose, so
|
|
391
|
+
* async work started here can be cancelled without extra bookkeeping.
|
|
392
|
+
*
|
|
393
|
+
* Runs before the first `useBlocDeps` slice is applied, so `this.deps` is
|
|
394
|
+
* empty on first activation — use `onDepsChanged` for deps-driven work.
|
|
395
|
+
*/
|
|
396
|
+
onActivate(_signal) {}
|
|
397
|
+
/**
|
|
398
|
+
* Called when this instance goes from owned back to unowned while surviving
|
|
399
|
+
* in the registry (i.e. `keepAlive`). A disposed instance does NOT get this
|
|
400
|
+
* callback — `dispose()` is the teardown path for that case.
|
|
401
|
+
*/
|
|
402
|
+
onDeactivate() {}
|
|
403
|
+
/**
|
|
404
|
+
* @internal Framework-only configuration entry point (registry + testing
|
|
405
|
+
* helpers). Writes the `_`-private identity fields directly, emits the
|
|
406
|
+
* registry `created` event, and runs `init()` once. See `INIT_CONFIG`.
|
|
407
|
+
*/
|
|
408
|
+
[require_StateContainerRegistry.INIT_CONFIG](config) {
|
|
409
|
+
this._config = { ...config };
|
|
410
|
+
const className = require_StateContainerRegistry.getBlacName(this.constructor);
|
|
411
|
+
this._name = this._config.name || className;
|
|
412
|
+
this._debug = this._config.debug ?? false;
|
|
413
|
+
this._instanceId = generateSimpleId(className, this._config.instanceId);
|
|
414
|
+
this._registry = this._config.registry ?? this._registry;
|
|
415
|
+
if (!this._initCalled) {
|
|
416
|
+
this._initCalled = true;
|
|
417
|
+
this.init(this._config.args);
|
|
418
|
+
}
|
|
419
|
+
this._registry.emit("created", this);
|
|
420
|
+
if (require_StateContainerRegistry.IS_DEV) {
|
|
421
|
+
if (!this.$blac?.[META_BRAND]) console.warn(`[blac] ${this.constructor.name} appears to declare its own \`$blac\` member, which shadows the reserved meta namespace. \`$blac\` is reserved by StateContainer — remove the subclass field.`);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
dispose() {
|
|
425
|
+
if (this._disposed) return;
|
|
426
|
+
if (this._debug) console.log(`[${this._name}] Disposing...`);
|
|
427
|
+
if (this._depsByOwner) {
|
|
428
|
+
this._depsByOwner.clear();
|
|
429
|
+
this.reconcileDeps();
|
|
430
|
+
}
|
|
431
|
+
this._disposed = true;
|
|
432
|
+
if (this._hydrationStatus === "hydrating") this._failHydration(/* @__PURE__ */ new Error(`Hydration cancelled because ${this._name} was disposed`));
|
|
433
|
+
this.emitSystemEvent("dispose", void 0);
|
|
434
|
+
this._bridgeUnsub?.();
|
|
435
|
+
this._bridgeUnsub = null;
|
|
436
|
+
this._abortActivation();
|
|
437
|
+
this._systemEventHandlers = null;
|
|
438
|
+
this._pendingChange = null;
|
|
439
|
+
this._registry.emit("disposed", this);
|
|
440
|
+
super.dispose();
|
|
441
|
+
if (this._debug) console.log(`[${this._name}] Disposed successfully`);
|
|
442
|
+
}
|
|
443
|
+
emit(next) {
|
|
444
|
+
this.applyState(next, "default");
|
|
445
|
+
}
|
|
446
|
+
/** @see emit for the StateContainer concerns layered onto `super.patch`. */
|
|
447
|
+
patch(partial) {
|
|
448
|
+
if (this._disposed) {
|
|
449
|
+
this._warnDisposedMutation("patch");
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
const prev = super.state;
|
|
453
|
+
super.patch(partial);
|
|
454
|
+
const next = super.state;
|
|
455
|
+
if (Object.is(prev, next)) return;
|
|
456
|
+
this._recordChange(prev, next, "default");
|
|
457
|
+
}
|
|
458
|
+
applyState(next, source) {
|
|
459
|
+
if (this._disposed) {
|
|
460
|
+
this._warnDisposedMutation("emit");
|
|
461
|
+
return;
|
|
462
|
+
}
|
|
463
|
+
const prev = super.state;
|
|
464
|
+
if (prev === next) return;
|
|
465
|
+
if (this._equalityFn(prev, next)) return;
|
|
466
|
+
this._recordChange(prev, next, source);
|
|
467
|
+
super.emit(next);
|
|
468
|
+
}
|
|
469
|
+
/** Bookkeeping shared by every real state change (`emit`, `patch`, hydration). */
|
|
470
|
+
_recordChange(prev, next, source) {
|
|
471
|
+
stateChangeCount++;
|
|
472
|
+
if (require_StateContainerRegistry.IS_DEV && !this._emitRateWarned) this._checkEmitRate();
|
|
473
|
+
if (this._hydrationStatus === "hydrating" && source !== "hydration") this._changedWhileHydrating = true;
|
|
474
|
+
if (this._bridgeUnsub !== null) if (this._pendingChange) this._pendingChange.next = next;
|
|
475
|
+
else this._pendingChange = {
|
|
476
|
+
prev,
|
|
477
|
+
next
|
|
478
|
+
};
|
|
479
|
+
this._registry.notifyStateChanged(this, prev, next);
|
|
480
|
+
}
|
|
481
|
+
/**
|
|
482
|
+
* Called by the channel-bridge subscriber on each flush. Drains the
|
|
483
|
+
* pending change (if any) into the 'stateChanged' system event.
|
|
484
|
+
* No pending change == flush from a no-op patch == skip.
|
|
485
|
+
*/
|
|
486
|
+
_drainPending() {
|
|
487
|
+
const pending = this._pendingChange;
|
|
488
|
+
if (!pending) return;
|
|
489
|
+
this._pendingChange = null;
|
|
490
|
+
this.emitSystemEvent("stateChanged", {
|
|
491
|
+
state: pending.next,
|
|
492
|
+
previousState: pending.prev
|
|
493
|
+
});
|
|
494
|
+
}
|
|
495
|
+
_beginHydration() {
|
|
496
|
+
if (this._disposed) throw new Error(`Cannot begin hydration for disposed container ${this._name}`);
|
|
497
|
+
this._changedWhileHydrating = false;
|
|
498
|
+
this._hydrationError = void 0;
|
|
499
|
+
this._hydrationPromise = null;
|
|
500
|
+
this._resolveHydrationPromise = void 0;
|
|
501
|
+
this._rejectHydrationPromise = void 0;
|
|
502
|
+
this._hydrationPromiseSettled = false;
|
|
503
|
+
this.ensureHydrationPromise();
|
|
504
|
+
this.setHydrationStatus("hydrating");
|
|
505
|
+
}
|
|
506
|
+
_warnDisposedMutation(op) {
|
|
507
|
+
if (require_StateContainerRegistry.IS_DEV) console.warn(`[blac] ${this._name}: ${op}() after dispose ignored. This usually means an async method resolved after the last consumer unmounted. Guard with \`if (this.$blac.disposed) return\` after each \`await\`, or abort the work from \`onSystemEvent('dispose', ...)\`.`);
|
|
508
|
+
}
|
|
509
|
+
_applyHydratedState(next) {
|
|
510
|
+
if (this._disposed) return false;
|
|
511
|
+
if (this._hydrationStatus !== "hydrating" || this._changedWhileHydrating) {
|
|
512
|
+
if (require_StateContainerRegistry.IS_DEV && this._changedWhileHydrating) console.warn(`[blac] ${this._name}: persisted state discarded because the container emitted while hydrating.`);
|
|
513
|
+
return false;
|
|
514
|
+
}
|
|
515
|
+
this.applyState(next, "hydration");
|
|
516
|
+
return true;
|
|
517
|
+
}
|
|
518
|
+
_finishHydration() {
|
|
519
|
+
if (this._hydrationStatus !== "hydrating") {
|
|
520
|
+
if (this._hydrationStatus === "hydrated") return;
|
|
521
|
+
if (this._hydrationStatus === "error") this.ensureHydrationPromise();
|
|
522
|
+
}
|
|
523
|
+
this.setHydrationStatus("hydrated");
|
|
524
|
+
this.resolveHydration();
|
|
525
|
+
}
|
|
526
|
+
_failHydration(error) {
|
|
527
|
+
const err = error instanceof Error ? error : /* @__PURE__ */ new Error(`Hydration failed: ${String(error)}`);
|
|
528
|
+
this._hydrationError = err;
|
|
529
|
+
this.setHydrationStatus("error", err);
|
|
530
|
+
this.rejectHydration(err);
|
|
531
|
+
}
|
|
532
|
+
_waitForHydration() {
|
|
533
|
+
if (this._hydrationStatus === "idle" || this._hydrationStatus === "hydrated") return Promise.resolve();
|
|
534
|
+
if (this._hydrationStatus === "error") return Promise.reject(this._hydrationError ?? /* @__PURE__ */ new Error(`Hydration failed for container ${this._name}`));
|
|
535
|
+
return this.ensureHydrationPromise();
|
|
536
|
+
}
|
|
537
|
+
setHydrationStatus(status, error) {
|
|
538
|
+
const previousStatus = this._hydrationStatus;
|
|
539
|
+
this._hydrationStatus = status;
|
|
540
|
+
this._hydrationError = error;
|
|
541
|
+
this.emitSystemEvent("hydrationChanged", {
|
|
542
|
+
status,
|
|
543
|
+
previousStatus,
|
|
544
|
+
error,
|
|
545
|
+
changedWhileHydrating: this._changedWhileHydrating
|
|
546
|
+
});
|
|
547
|
+
this._registry.emit("hydrationChanged", this, status, previousStatus);
|
|
548
|
+
}
|
|
549
|
+
ensureHydrationPromise() {
|
|
550
|
+
if (!this._hydrationPromise || this._hydrationPromiseSettled) {
|
|
551
|
+
this._hydrationPromiseSettled = false;
|
|
552
|
+
this._hydrationPromise = new Promise((resolve, reject) => {
|
|
553
|
+
this._resolveHydrationPromise = () => {
|
|
554
|
+
if (this._hydrationPromiseSettled) return;
|
|
555
|
+
this._hydrationPromiseSettled = true;
|
|
556
|
+
resolve();
|
|
557
|
+
};
|
|
558
|
+
this._rejectHydrationPromise = (error) => {
|
|
559
|
+
if (this._hydrationPromiseSettled) return;
|
|
560
|
+
this._hydrationPromiseSettled = true;
|
|
561
|
+
reject(error);
|
|
562
|
+
};
|
|
563
|
+
});
|
|
564
|
+
this._hydrationPromise.catch(() => {});
|
|
565
|
+
}
|
|
566
|
+
return this._hydrationPromise;
|
|
567
|
+
}
|
|
568
|
+
resolveHydration() {
|
|
569
|
+
this._resolveHydrationPromise?.();
|
|
570
|
+
}
|
|
571
|
+
rejectHydration(error) {
|
|
572
|
+
this.ensureHydrationPromise();
|
|
573
|
+
this._rejectHydrationPromise?.(error);
|
|
574
|
+
}
|
|
575
|
+
onSystemEvent(event, handler) {
|
|
576
|
+
if (this._disposed) return () => {};
|
|
577
|
+
const all = this._systemEventHandlers ??= /* @__PURE__ */ new Map();
|
|
578
|
+
let handlers = all.get(event);
|
|
579
|
+
if (!handlers) {
|
|
580
|
+
handlers = /* @__PURE__ */ new Set();
|
|
581
|
+
all.set(event, handlers);
|
|
582
|
+
}
|
|
583
|
+
handlers.add(handler);
|
|
584
|
+
if (event === "stateChanged" && this._bridgeUnsub === null) this._bridgeUnsub = this.channel.subscribe(() => _dirtytalk_structural.ALL_PATHS, () => this._drainPending());
|
|
585
|
+
return () => {
|
|
586
|
+
handlers?.delete(handler);
|
|
587
|
+
if (event === "stateChanged" && handlers?.size === 0) {
|
|
588
|
+
this._bridgeUnsub?.();
|
|
589
|
+
this._bridgeUnsub = null;
|
|
590
|
+
this._pendingChange = null;
|
|
591
|
+
}
|
|
592
|
+
};
|
|
593
|
+
}
|
|
594
|
+
emitSystemEvent(event, payload) {
|
|
595
|
+
const handlers = this._systemEventHandlers?.get(event);
|
|
596
|
+
if (!handlers) return;
|
|
597
|
+
let remaining = handlers.size;
|
|
598
|
+
for (const handler of handlers) {
|
|
599
|
+
if (remaining-- === 0) break;
|
|
600
|
+
try {
|
|
601
|
+
handler(payload);
|
|
602
|
+
} catch (error) {
|
|
603
|
+
console.error(`[${this._name}] Error in system event handler:`, error);
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
/**
|
|
608
|
+
* Dev-only soft circuit breaker. Counts real state changes in a rolling 1s
|
|
609
|
+
* window and warns once if the rate exceeds `maxEmitsPerSecond` — the
|
|
610
|
+
* signature of a runaway loop (RAF/animation or emit-on-every-commit) pushing
|
|
611
|
+
* high-frequency data through state, which freezes subscribers/plugins.
|
|
612
|
+
*/
|
|
613
|
+
_checkEmitRate() {
|
|
614
|
+
const limit = require_StateContainerRegistry.getBlacConfig().maxEmitsPerSecond;
|
|
615
|
+
if (!(limit > 0) || !Number.isFinite(limit)) return;
|
|
616
|
+
const now = Date.now();
|
|
617
|
+
if (now - this._emitWindowStart >= 1e3) {
|
|
618
|
+
this._emitWindowStart = now;
|
|
619
|
+
this._emitCount = 1;
|
|
620
|
+
return;
|
|
621
|
+
}
|
|
622
|
+
this._emitCount++;
|
|
623
|
+
if (this._emitCount > limit) {
|
|
624
|
+
this._emitRateWarned = true;
|
|
625
|
+
console.warn(`[${this._name}] emitted more than ${limit} state changes in under a second. This is usually a runaway loop pushing high-frequency data through state (e.g. \`emit\`/\`patch\` inside a requestAnimationFrame loop, or an effect that emits on every render). It can freeze the app by saturating subscribers and plugins (logging/devtools). Keep high-frequency work imperative and emit only coarse/throttled state, or raise \`configureBlac({ maxEmitsPerSecond })\`. (This warning fires once.)`);
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
};
|
|
629
|
+
//#endregion
|
|
630
|
+
Object.defineProperty(exports, "DEP_BRAND", {
|
|
631
|
+
enumerable: true,
|
|
632
|
+
get: function() {
|
|
633
|
+
return DEP_BRAND;
|
|
634
|
+
}
|
|
635
|
+
});
|
|
636
|
+
Object.defineProperty(exports, "StateContainer", {
|
|
637
|
+
enumerable: true,
|
|
638
|
+
get: function() {
|
|
639
|
+
return StateContainer;
|
|
640
|
+
}
|
|
641
|
+
});
|
|
642
|
+
Object.defineProperty(exports, "getRegistry", {
|
|
643
|
+
enumerable: true,
|
|
644
|
+
get: function() {
|
|
645
|
+
return getRegistry;
|
|
646
|
+
}
|
|
647
|
+
});
|
|
648
|
+
Object.defineProperty(exports, "getStateChangeCount", {
|
|
649
|
+
enumerable: true,
|
|
650
|
+
get: function() {
|
|
651
|
+
return getStateChangeCount;
|
|
652
|
+
}
|
|
653
|
+
});
|
|
654
|
+
Object.defineProperty(exports, "getStats", {
|
|
655
|
+
enumerable: true,
|
|
656
|
+
get: function() {
|
|
657
|
+
return getStats;
|
|
658
|
+
}
|
|
659
|
+
});
|
|
660
|
+
Object.defineProperty(exports, "setRegistry", {
|
|
661
|
+
enumerable: true,
|
|
662
|
+
get: function() {
|
|
663
|
+
return setRegistry;
|
|
664
|
+
}
|
|
665
|
+
});
|
|
666
|
+
|
|
667
|
+
//# sourceMappingURL=StateContainer-j16zciex.cjs.map
|