@blac/core 2.0.19 → 2.1.0
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/StateContainerRegistry-8zNFMAf7.js +1067 -0
- package/dist/StateContainerRegistry-8zNFMAf7.js.map +1 -0
- package/dist/StateContainerRegistry-Bx2ly-ba.cjs +1162 -0
- package/dist/StateContainerRegistry-Bx2ly-ba.cjs.map +1 -0
- package/dist/StateContainerRegistry-C75QpeaO.cjs +1260 -0
- package/dist/StateContainerRegistry-C75QpeaO.cjs.map +1 -0
- package/dist/StateContainerRegistry-CS18y4au.cjs +1163 -0
- package/dist/StateContainerRegistry-CS18y4au.cjs.map +1 -0
- package/dist/StateContainerRegistry-D8nu7Z0T.js +1068 -0
- package/dist/StateContainerRegistry-D8nu7Z0T.js.map +1 -0
- package/dist/StateContainerRegistry-DL5Ai7wh.js +1153 -0
- package/dist/StateContainerRegistry-DL5Ai7wh.js.map +1 -0
- package/dist/StateContainerRegistry-DWOzx7Gd.js +1154 -0
- package/dist/StateContainerRegistry-DWOzx7Gd.js.map +1 -0
- package/dist/StateContainerRegistry-G81GcCRm.cjs +1261 -0
- package/dist/StateContainerRegistry-G81GcCRm.cjs.map +1 -0
- package/dist/acquire-B1LYXSGj.js +35 -0
- package/dist/acquire-B1LYXSGj.js.map +1 -0
- package/dist/acquire-BJ3xUEmh.cjs +46 -0
- package/dist/acquire-BJ3xUEmh.cjs.map +1 -0
- package/dist/acquire-B_p1fNEh.cjs +46 -0
- package/dist/acquire-B_p1fNEh.cjs.map +1 -0
- package/dist/acquire-BdsVZBSe.js +35 -0
- package/dist/acquire-BdsVZBSe.js.map +1 -0
- package/dist/acquire-CE0uq0Dx.cjs +46 -0
- package/dist/acquire-CE0uq0Dx.cjs.map +1 -0
- package/dist/acquire-CU0u8BMn.js +35 -0
- package/dist/acquire-CU0u8BMn.js.map +1 -0
- package/dist/acquire-CkgLJ8WL.cjs +46 -0
- package/dist/acquire-CkgLJ8WL.cjs.map +1 -0
- package/dist/acquire-DTvfYJ0y.js +35 -0
- package/dist/acquire-DTvfYJ0y.js.map +1 -0
- package/dist/config-C1T6Jw7n.cjs +32 -0
- package/dist/config-C1T6Jw7n.cjs.map +1 -0
- package/dist/config-D2A8ZYRM.js +16 -0
- package/dist/config-D2A8ZYRM.js.map +1 -0
- package/dist/config-D34BkraU.cjs +32 -0
- package/dist/config-D34BkraU.cjs.map +1 -0
- package/dist/config-DOO82lbZ.js +16 -0
- package/dist/config-DOO82lbZ.js.map +1 -0
- package/dist/config-DWUq7zEQ.cjs +32 -0
- package/dist/config-DWUq7zEQ.cjs.map +1 -0
- package/dist/config-DXI3kzmA.js +16 -0
- package/dist/config-DXI3kzmA.js.map +1 -0
- package/dist/config-ZVy_yFZ3.js +16 -0
- package/dist/config-ZVy_yFZ3.js.map +1 -0
- package/dist/config-cwaTn5oy.cjs +32 -0
- package/dist/config-cwaTn5oy.cjs.map +1 -0
- package/dist/config.d.cts +7 -3
- package/dist/config.d.ts +7 -3
- package/dist/constants.d.cts +2 -13
- package/dist/constants.d.ts +2 -13
- package/dist/core/StateContainer.d.ts +28 -1
- package/dist/core/StateContainerRegistry.d.ts +72 -2
- package/dist/core/symbols.d.ts +6 -0
- package/dist/debug.cjs +3 -3
- package/dist/debug.js +3 -3
- package/dist/decorators/blac.d.ts +2 -0
- package/dist/ensure-11-asM1e.js +677 -0
- package/dist/ensure-11-asM1e.js.map +1 -0
- package/dist/ensure-B2DYCbbI.cjs +578 -0
- package/dist/ensure-B2DYCbbI.cjs.map +1 -0
- package/dist/ensure-B59ri8oo.js +555 -0
- package/dist/ensure-B59ri8oo.js.map +1 -0
- package/dist/ensure-BEeY5Ab7.js +673 -0
- package/dist/ensure-BEeY5Ab7.js.map +1 -0
- package/dist/ensure-BKdq5xsY.cjs +578 -0
- package/dist/ensure-BKdq5xsY.cjs.map +1 -0
- package/dist/ensure-BSJHkUNX.js +555 -0
- package/dist/ensure-BSJHkUNX.js.map +1 -0
- package/dist/ensure-D3WWWwMo.cjs +714 -0
- package/dist/ensure-D3WWWwMo.cjs.map +1 -0
- package/dist/ensure-r1C_7K6_.cjs +718 -0
- package/dist/ensure-r1C_7K6_.cjs.map +1 -0
- package/dist/events.d.cts +42 -0
- package/dist/events.d.ts +42 -0
- package/dist/index.cjs +61 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +7 -6
- package/dist/index.d.ts +7 -6
- package/dist/index.js +56 -8
- package/dist/index.js.map +1 -1
- package/dist/management-8x1OVMi6.js +38 -0
- package/dist/management-8x1OVMi6.js.map +1 -0
- package/dist/management-BZgsi29F.js +38 -0
- package/dist/management-BZgsi29F.js.map +1 -0
- package/dist/management-C-WGcwPD.js +38 -0
- package/dist/management-C-WGcwPD.js.map +1 -0
- package/dist/management-Dg2SxUb6.cjs +85 -0
- package/dist/management-Dg2SxUb6.cjs.map +1 -0
- package/dist/management-P_Vi_cvw.js +38 -0
- package/dist/management-P_Vi_cvw.js.map +1 -0
- package/dist/management-QWLpbXlW.cjs +85 -0
- package/dist/management-QWLpbXlW.cjs.map +1 -0
- package/dist/management-hkY13log.cjs +85 -0
- package/dist/management-hkY13log.cjs.map +1 -0
- package/dist/management-iE4x6IQY.cjs +85 -0
- package/dist/management-iE4x6IQY.cjs.map +1 -0
- package/dist/plugin/PluginManager.d.ts +11 -0
- package/dist/plugins.cjs +1 -1
- package/dist/plugins.js +1 -1
- package/dist/registry/index.d.ts +1 -1
- package/dist/registry/release.d.ts +17 -1
- package/dist/testing.cjs +4 -4
- package/dist/testing.js +4 -4
- package/dist/utils/idGenerator.d.ts +0 -36
- package/dist/utils/static-props.d.ts +5 -0
- package/dist/utils/structural-key.d.ts +11 -1
- package/dist/watch/index.d.ts +1 -0
- package/dist/watch/observe.d.ts +13 -0
- package/dist/watch-B0pUfh-v.cjs +119 -0
- package/dist/watch-B0pUfh-v.cjs.map +1 -0
- package/dist/watch-BDiwCPmb.js +108 -0
- package/dist/watch-BDiwCPmb.js.map +1 -0
- package/dist/watch-BW7gYiDf.js +108 -0
- package/dist/watch-BW7gYiDf.js.map +1 -0
- package/dist/watch-Bx0y-Kcc.js +108 -0
- package/dist/watch-Bx0y-Kcc.js.map +1 -0
- package/dist/watch-DttHqefn.js +108 -0
- package/dist/watch-DttHqefn.js.map +1 -0
- package/dist/watch-DxdIZjpt.cjs +119 -0
- package/dist/watch-DxdIZjpt.cjs.map +1 -0
- package/dist/watch-aoNLQAXp.cjs +119 -0
- package/dist/watch-aoNLQAXp.cjs.map +1 -0
- package/dist/watch-entry.cjs +1 -1
- package/dist/watch-entry.js +1 -1
- package/dist/watch-lEco5Tbl.cjs +119 -0
- package/dist/watch-lEco5Tbl.cjs.map +1 -0
- package/package.json +3 -3
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { o as ON_DISPOSE } from "./StateContainerRegistry-D8nu7Z0T.js";
|
|
2
|
+
import { t as getRegistry } from "./config-DXI3kzmA.js";
|
|
3
|
+
import { n as resolveInstanceKey } from "./acquire-B1LYXSGj.js";
|
|
4
|
+
import { ALL_PATHS } from "@dirtytalk/structural";
|
|
5
|
+
//#region src/watch/watch.ts
|
|
6
|
+
const STOP = Symbol("watch.STOP");
|
|
7
|
+
const BLOC_REF_MARKER = Symbol("BlocRef");
|
|
8
|
+
/**
|
|
9
|
+
* Create a reference to a specific bloc instance, identified by its `args`.
|
|
10
|
+
* The key is derived the same way `useBloc`/`acquire` derive it.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* watch(instance(UserBloc, { userId: 'user-123' }), (userBloc) => {
|
|
15
|
+
* console.log(userBloc.state.name);
|
|
16
|
+
* });
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
function instance(BlocClass, args) {
|
|
20
|
+
return {
|
|
21
|
+
[BLOC_REF_MARKER]: true,
|
|
22
|
+
blocClass: BlocClass,
|
|
23
|
+
instanceId: resolveInstanceKey(BlocClass, args),
|
|
24
|
+
args
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function isBlocRef(input) {
|
|
28
|
+
return typeof input === "object" && input !== null && BLOC_REF_MARKER in input;
|
|
29
|
+
}
|
|
30
|
+
function toWatchTarget(input) {
|
|
31
|
+
const registry = getRegistry();
|
|
32
|
+
if (isBlocRef(input)) return {
|
|
33
|
+
blocClass: input.blocClass,
|
|
34
|
+
key: input.instanceId,
|
|
35
|
+
args: input.args
|
|
36
|
+
};
|
|
37
|
+
return {
|
|
38
|
+
blocClass: input,
|
|
39
|
+
key: registry.resolveKey(input, void 0, void 0),
|
|
40
|
+
args: void 0
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
let watchRefSeq = 0;
|
|
44
|
+
/**
|
|
45
|
+
* Acquire a real ref (countRef: true) for the target, carrying its `args`
|
|
46
|
+
* through so a not-yet-created instance is initialized correctly. The
|
|
47
|
+
* caller is responsible for releasing `refId` in cleanup.
|
|
48
|
+
*/
|
|
49
|
+
function resolveBloc(target, refId) {
|
|
50
|
+
return getRegistry().acquire(target.blocClass, target.key, {
|
|
51
|
+
countRef: true,
|
|
52
|
+
refId,
|
|
53
|
+
args: target.args
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
function isArray(input) {
|
|
57
|
+
return Array.isArray(input);
|
|
58
|
+
}
|
|
59
|
+
function watchImpl(blocsOrBloc, callback) {
|
|
60
|
+
const isSingle = !isArray(blocsOrBloc);
|
|
61
|
+
const inputs = isSingle ? [blocsOrBloc] : blocsOrBloc;
|
|
62
|
+
const registry = getRegistry();
|
|
63
|
+
const targets = inputs.map(toWatchTarget);
|
|
64
|
+
const refIds = targets.map(() => `_watch_${watchRefSeq++}`);
|
|
65
|
+
let disposed = false;
|
|
66
|
+
const instances = targets.map((target, i) => resolveBloc(target, refIds[i]));
|
|
67
|
+
const channelUnsubs = [];
|
|
68
|
+
const disposedUnsubs = [];
|
|
69
|
+
const cleanup = () => {
|
|
70
|
+
if (disposed) return;
|
|
71
|
+
disposed = true;
|
|
72
|
+
for (const unsub of channelUnsubs) unsub?.();
|
|
73
|
+
for (const unsub of disposedUnsubs) unsub();
|
|
74
|
+
channelUnsubs.length = 0;
|
|
75
|
+
disposedUnsubs.length = 0;
|
|
76
|
+
for (let i = 0; i < targets.length; i++) registry.release(targets[i].blocClass, targets[i].key, false, refIds[i]);
|
|
77
|
+
};
|
|
78
|
+
const runCallback = () => {
|
|
79
|
+
if (disposed) return;
|
|
80
|
+
if (callback(isSingle ? instances[0] : instances) === STOP) cleanup();
|
|
81
|
+
};
|
|
82
|
+
const subscribeAt = (index) => {
|
|
83
|
+
channelUnsubs[index] = instances[index].channel.subscribe(() => ALL_PATHS, runCallback);
|
|
84
|
+
};
|
|
85
|
+
const resubscribeAt = (index) => {
|
|
86
|
+
if (disposed) return;
|
|
87
|
+
instances[index] = resolveBloc(targets[index], refIds[index]);
|
|
88
|
+
subscribeAt(index);
|
|
89
|
+
subscribeDisposeAt(index);
|
|
90
|
+
runCallback();
|
|
91
|
+
};
|
|
92
|
+
const subscribeDisposeAt = (index) => {
|
|
93
|
+
disposedUnsubs[index] = instances[index][ON_DISPOSE](() => {
|
|
94
|
+
channelUnsubs[index]?.();
|
|
95
|
+
channelUnsubs[index] = void 0;
|
|
96
|
+
queueMicrotask(() => resubscribeAt(index));
|
|
97
|
+
});
|
|
98
|
+
};
|
|
99
|
+
for (let i = 0; i < instances.length; i++) subscribeAt(i);
|
|
100
|
+
for (let i = 0; i < instances.length; i++) subscribeDisposeAt(i);
|
|
101
|
+
runCallback();
|
|
102
|
+
return cleanup;
|
|
103
|
+
}
|
|
104
|
+
const watch = Object.assign(watchImpl, { STOP });
|
|
105
|
+
//#endregion
|
|
106
|
+
export { watch as n, instance as t };
|
|
107
|
+
|
|
108
|
+
//# sourceMappingURL=watch-DttHqefn.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"watch-DttHqefn.js","names":[],"sources":["../src/watch/watch.ts"],"sourcesContent":["import { ALL_PATHS } from '@dirtytalk/structural';\nimport { ON_DISPOSE } from '../core/symbols';\nimport { getRegistry } from '../registry';\nimport { resolveInstanceKey } from '../registry/acquire';\nimport type {\n ExtractArgs,\n StateContainerConstructor,\n StateContainerInstance,\n} from '../types/utilities';\n\nconst STOP: unique symbol = Symbol('watch.STOP');\ntype StopSymbol = typeof STOP;\n\nconst BLOC_REF_MARKER = Symbol('BlocRef');\n\n/**\n * Reference to a specific bloc instance, identified by class + the resolved\n * key derived from its `args`.\n */\nexport interface BlocRef<T extends StateContainerConstructor> {\n [BLOC_REF_MARKER]: true;\n blocClass: T;\n /** @internal The resolved instance key (derived from `args`). */\n instanceId: string;\n /**\n * @internal The `args` used to derive `instanceId`. Forwarded to\n * `registry.acquire` so a not-yet-created instance is initialized with\n * these args instead of `undefined`.\n */\n args?: unknown;\n}\n\n/**\n * Create a reference to a specific bloc instance, identified by its `args`.\n * The key is derived the same way `useBloc`/`acquire` derive it.\n *\n * @example\n * ```ts\n * watch(instance(UserBloc, { userId: 'user-123' }), (userBloc) => {\n * console.log(userBloc.state.name);\n * });\n * ```\n */\nexport function instance<T extends StateContainerConstructor>(\n BlocClass: T,\n args?: ExtractArgs<T>,\n): BlocRef<T> {\n return {\n [BLOC_REF_MARKER]: true,\n blocClass: BlocClass,\n instanceId: resolveInstanceKey(BlocClass, args),\n args,\n };\n}\n\nfunction isBlocRef(\n input: unknown,\n): input is BlocRef<StateContainerConstructor> {\n return (\n typeof input === 'object' && input !== null && BLOC_REF_MARKER in input\n );\n}\n\ntype BlocInput = StateContainerConstructor | BlocRef<StateContainerConstructor>;\n\ntype ExtractInstance<T> =\n T extends BlocRef<infer C>\n ? InstanceType<C>\n : T extends StateContainerConstructor\n ? InstanceType<T>\n : never;\n\ntype ExtractInstances<T extends readonly BlocInput[]> = {\n [K in keyof T]: ExtractInstance<T[K]>;\n};\n\n/**\n * Watch function signature for single bloc.\n */\nexport interface WatchSingleFn {\n <T extends StateContainerConstructor>(\n bloc: T | BlocRef<T>,\n callback: (bloc: InstanceType<T>) => void | StopSymbol,\n ): () => void;\n\n STOP: StopSymbol;\n}\n\n/**\n * Watch function signature for multiple blocs.\n */\nexport interface WatchMultipleFn {\n <T extends readonly BlocInput[]>(\n blocs: T,\n callback: (blocs: ExtractInstances<T>) => void | StopSymbol,\n ): () => void;\n\n STOP: StopSymbol;\n}\n\n/**\n * Combined watch function type.\n */\nexport interface WatchFn extends WatchSingleFn {\n <T extends readonly BlocInput[]>(\n blocs: T,\n callback: (blocs: ExtractInstances<T>) => void | StopSymbol,\n ): () => void;\n}\n\n/**\n * Class + resolved key + args needed to (re-)acquire a specific instance.\n * `watch` keeps this around per input so it can re-acquire the same logical\n * instance if the underlying container is disposed elsewhere.\n */\ninterface WatchTarget {\n blocClass: StateContainerConstructor;\n key: string;\n args: unknown;\n}\n\nfunction toWatchTarget(input: BlocInput): WatchTarget {\n const registry = getRegistry();\n if (isBlocRef(input)) {\n return {\n blocClass: input.blocClass,\n key: input.instanceId,\n args: input.args,\n };\n }\n return {\n blocClass: input,\n key: registry.resolveKey(input, undefined, undefined),\n args: undefined,\n };\n}\n\nlet watchRefSeq = 0;\n\n/**\n * Acquire a real ref (countRef: true) for the target, carrying its `args`\n * through so a not-yet-created instance is initialized correctly. The\n * caller is responsible for releasing `refId` in cleanup.\n */\nfunction resolveBloc(\n target: WatchTarget,\n refId: string,\n): StateContainerInstance {\n const registry = getRegistry();\n return registry.acquire(target.blocClass, target.key, {\n countRef: true,\n refId,\n args: target.args,\n });\n}\n\nfunction isArray(input: unknown): input is readonly BlocInput[] {\n return Array.isArray(input);\n}\n\n/**\n * Watch one or more blocs for state changes.\n *\n * Thin wrapper around `container.channel.subscribe(ALL_PATHS, ...)`. The\n * callback fires once immediately, then on every state change of any of the\n * passed blocs. Returning `watch.STOP` from the callback tears down all\n * subscriptions.\n *\n * Note: subscriptions are microtask-deferred (per the DirtyChannel default\n * scheduler), so callbacks land asynchronously after `emit()`.\n *\n * @example Single bloc\n * ```ts\n * const unwatch = watch(UserBloc, (userBloc) => {\n * console.log(userBloc.state.name);\n * });\n * ```\n *\n * @example Multiple blocs\n * ```ts\n * const unwatch = watch(\n * [UserBloc, SettingsBloc] as const,\n * ([userBloc, settingsBloc]) => {\n * console.log(userBloc.state.name, settingsBloc.state.theme);\n * }\n * );\n * ```\n *\n * @example With specific instance\n * ```ts\n * const unwatch = watch(\n * instance(UserBloc, { userId: 'user-123' }),\n * (userBloc) => {\n * console.log(userBloc.state.name);\n * }\n * );\n * ```\n *\n * @example Stop watching from callback\n * ```ts\n * const unwatch = watch(UserBloc, (userBloc) => {\n * if (userBloc.state.done) {\n * return watch.STOP;\n * }\n * });\n * ```\n */\nfunction watchImpl<T extends StateContainerConstructor>(\n bloc: T | BlocRef<T>,\n callback: (bloc: InstanceType<T>) => void | StopSymbol,\n): () => void;\n\nfunction watchImpl<T extends readonly BlocInput[]>(\n blocs: T,\n callback: (blocs: ExtractInstances<T>) => void | StopSymbol,\n): () => void;\n\nfunction watchImpl(\n blocsOrBloc: BlocInput | readonly BlocInput[],\n callback: (blocsOrBloc: any) => void | StopSymbol,\n): () => void {\n const isSingle = !isArray(blocsOrBloc);\n const inputs = isSingle ? [blocsOrBloc] : blocsOrBloc;\n const registry = getRegistry();\n\n const targets = inputs.map(toWatchTarget);\n const refIds = targets.map(() => `_watch_${watchRefSeq++}`);\n\n let disposed = false;\n const instances: StateContainerInstance[] = targets.map((target, i) =>\n resolveBloc(target, refIds[i]),\n );\n const channelUnsubs: Array<(() => void) | undefined> = [];\n const disposedUnsubs: Array<() => void> = [];\n\n const cleanup = () => {\n if (disposed) return;\n disposed = true;\n for (const unsub of channelUnsubs) unsub?.();\n for (const unsub of disposedUnsubs) unsub();\n channelUnsubs.length = 0;\n disposedUnsubs.length = 0;\n for (let i = 0; i < targets.length; i++) {\n registry.release(targets[i].blocClass, targets[i].key, false, refIds[i]);\n }\n };\n\n const runCallback = () => {\n if (disposed) return;\n const arg = isSingle ? instances[0] : instances;\n const result = callback(arg);\n if (result === STOP) cleanup();\n };\n\n const subscribeAt = (index: number) => {\n channelUnsubs[index] = instances[index].channel.subscribe(\n () => ALL_PATHS,\n runCallback,\n );\n };\n\n // On external dispose, tear down the stale subscription and re-acquire +\n // resubscribe (microtask-deferred, so it never runs mid-mutation of the\n // registry that triggered the dispose, e.g. `clearAll()`), then notify.\n const resubscribeAt = (index: number) => {\n if (disposed) return;\n instances[index] = resolveBloc(targets[index], refIds[index]);\n subscribeAt(index);\n subscribeDisposeAt(index);\n runCallback();\n };\n\n const subscribeDisposeAt = (index: number) => {\n disposedUnsubs[index] = instances[index][ON_DISPOSE](() => {\n channelUnsubs[index]?.();\n channelUnsubs[index] = undefined;\n queueMicrotask(() => resubscribeAt(index));\n });\n };\n\n for (let i = 0; i < instances.length; i++) {\n subscribeAt(i);\n }\n\n for (let i = 0; i < instances.length; i++) {\n subscribeDisposeAt(i);\n }\n\n // Fire once immediately so the consumer sees the current state.\n runCallback();\n\n return cleanup;\n}\n\nexport const watch: WatchFn = Object.assign(watchImpl, { STOP }) as WatchFn;\n"],"mappings":";;;;;AAUA,MAAM,OAAsB,OAAO,YAAY;AAG/C,MAAM,kBAAkB,OAAO,SAAS;;;;;;;;;;;;AA8BxC,SAAgB,SACd,WACA,MACY;CACZ,OAAO;GACJ,kBAAkB;EACnB,WAAW;EACX,YAAY,mBAAmB,WAAW,IAAI;EAC9C;CACF;AACF;AAEA,SAAS,UACP,OAC6C;CAC7C,OACE,OAAO,UAAU,YAAY,UAAU,QAAQ,mBAAmB;AAEtE;AA4DA,SAAS,cAAc,OAA+B;CACpD,MAAM,WAAW,YAAY;CAC7B,IAAI,UAAU,KAAK,GACjB,OAAO;EACL,WAAW,MAAM;EACjB,KAAK,MAAM;EACX,MAAM,MAAM;CACd;CAEF,OAAO;EACL,WAAW;EACX,KAAK,SAAS,WAAW,OAAO,KAAA,GAAW,KAAA,CAAS;EACpD,MAAM,KAAA;CACR;AACF;AAEA,IAAI,cAAc;;;;;;A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,eAAe,SAAS,UAAU,OAAO,kBAAkB;GACzD,cAAc,SAAS;GACvB,cAAc,SAAS,KAAA;GACvB,qBAAqB,cAAc,KAAK,CAAC;EAC3C,CAAC;CACH;CAEA,KAAK,IAAI,IAAI,GAAG,IAAI,UAAU,QAAQ,KACpC,YAAY,CAAC;CAGf,KAAK,IAAI,IAAI,GAAG,IAAI,UAAU,QAAQ,KACpC,mBAAmB,CAAC;CAItB,YAAY;CAEZ,OAAO;AACT;AAEA,MAAa,QAAiB,OAAO,OAAO,WAAW,EAAE,KAAK,CAAC"}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
const require_StateContainerRegistry = require("./StateContainerRegistry-CS18y4au.cjs");
|
|
2
|
+
const require_config = require("./config-DWUq7zEQ.cjs");
|
|
3
|
+
const require_acquire = require("./acquire-BJ3xUEmh.cjs");
|
|
4
|
+
let _dirtytalk_structural = require("@dirtytalk/structural");
|
|
5
|
+
//#region src/watch/watch.ts
|
|
6
|
+
const STOP = Symbol("watch.STOP");
|
|
7
|
+
const BLOC_REF_MARKER = Symbol("BlocRef");
|
|
8
|
+
/**
|
|
9
|
+
* Create a reference to a specific bloc instance, identified by its `args`.
|
|
10
|
+
* The key is derived the same way `useBloc`/`acquire` derive it.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* watch(instance(UserBloc, { userId: 'user-123' }), (userBloc) => {
|
|
15
|
+
* console.log(userBloc.state.name);
|
|
16
|
+
* });
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
function instance(BlocClass, args) {
|
|
20
|
+
return {
|
|
21
|
+
[BLOC_REF_MARKER]: true,
|
|
22
|
+
blocClass: BlocClass,
|
|
23
|
+
instanceId: require_acquire.resolveInstanceKey(BlocClass, args),
|
|
24
|
+
args
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function isBlocRef(input) {
|
|
28
|
+
return typeof input === "object" && input !== null && BLOC_REF_MARKER in input;
|
|
29
|
+
}
|
|
30
|
+
function toWatchTarget(input) {
|
|
31
|
+
const registry = require_config.getRegistry();
|
|
32
|
+
if (isBlocRef(input)) return {
|
|
33
|
+
blocClass: input.blocClass,
|
|
34
|
+
key: input.instanceId,
|
|
35
|
+
args: input.args
|
|
36
|
+
};
|
|
37
|
+
return {
|
|
38
|
+
blocClass: input,
|
|
39
|
+
key: registry.resolveKey(input, void 0, void 0),
|
|
40
|
+
args: void 0
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
let watchRefSeq = 0;
|
|
44
|
+
/**
|
|
45
|
+
* Acquire a real ref (countRef: true) for the target, carrying its `args`
|
|
46
|
+
* through so a not-yet-created instance is initialized correctly. The
|
|
47
|
+
* caller is responsible for releasing `refId` in cleanup.
|
|
48
|
+
*/
|
|
49
|
+
function resolveBloc(target, refId) {
|
|
50
|
+
return require_config.getRegistry().acquire(target.blocClass, target.key, {
|
|
51
|
+
countRef: true,
|
|
52
|
+
refId,
|
|
53
|
+
args: target.args
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
function isArray(input) {
|
|
57
|
+
return Array.isArray(input);
|
|
58
|
+
}
|
|
59
|
+
function watchImpl(blocsOrBloc, callback) {
|
|
60
|
+
const isSingle = !isArray(blocsOrBloc);
|
|
61
|
+
const inputs = isSingle ? [blocsOrBloc] : blocsOrBloc;
|
|
62
|
+
const registry = require_config.getRegistry();
|
|
63
|
+
const targets = inputs.map(toWatchTarget);
|
|
64
|
+
const refIds = targets.map(() => `_watch_${watchRefSeq++}`);
|
|
65
|
+
let disposed = false;
|
|
66
|
+
const instances = targets.map((target, i) => resolveBloc(target, refIds[i]));
|
|
67
|
+
const channelUnsubs = [];
|
|
68
|
+
const disposedUnsubs = [];
|
|
69
|
+
const cleanup = () => {
|
|
70
|
+
if (disposed) return;
|
|
71
|
+
disposed = true;
|
|
72
|
+
for (const unsub of channelUnsubs) unsub?.();
|
|
73
|
+
for (const unsub of disposedUnsubs) unsub();
|
|
74
|
+
channelUnsubs.length = 0;
|
|
75
|
+
disposedUnsubs.length = 0;
|
|
76
|
+
for (let i = 0; i < targets.length; i++) registry.release(targets[i].blocClass, targets[i].key, false, refIds[i]);
|
|
77
|
+
};
|
|
78
|
+
const runCallback = () => {
|
|
79
|
+
if (disposed) return;
|
|
80
|
+
if (callback(isSingle ? instances[0] : instances) === STOP) cleanup();
|
|
81
|
+
};
|
|
82
|
+
const subscribeAt = (index) => {
|
|
83
|
+
channelUnsubs[index] = instances[index].channel.subscribe(() => _dirtytalk_structural.ALL_PATHS, runCallback);
|
|
84
|
+
};
|
|
85
|
+
const resubscribeAt = (index) => {
|
|
86
|
+
if (disposed) return;
|
|
87
|
+
instances[index] = resolveBloc(targets[index], refIds[index]);
|
|
88
|
+
subscribeAt(index);
|
|
89
|
+
subscribeDisposeAt(index);
|
|
90
|
+
runCallback();
|
|
91
|
+
};
|
|
92
|
+
const subscribeDisposeAt = (index) => {
|
|
93
|
+
disposedUnsubs[index] = instances[index][require_StateContainerRegistry.ON_DISPOSE](() => {
|
|
94
|
+
channelUnsubs[index]?.();
|
|
95
|
+
channelUnsubs[index] = void 0;
|
|
96
|
+
queueMicrotask(() => resubscribeAt(index));
|
|
97
|
+
});
|
|
98
|
+
};
|
|
99
|
+
for (let i = 0; i < instances.length; i++) subscribeAt(i);
|
|
100
|
+
for (let i = 0; i < instances.length; i++) subscribeDisposeAt(i);
|
|
101
|
+
runCallback();
|
|
102
|
+
return cleanup;
|
|
103
|
+
}
|
|
104
|
+
const watch = Object.assign(watchImpl, { STOP });
|
|
105
|
+
//#endregion
|
|
106
|
+
Object.defineProperty(exports, "instance", {
|
|
107
|
+
enumerable: true,
|
|
108
|
+
get: function() {
|
|
109
|
+
return instance;
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
Object.defineProperty(exports, "watch", {
|
|
113
|
+
enumerable: true,
|
|
114
|
+
get: function() {
|
|
115
|
+
return watch;
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
//# sourceMappingURL=watch-DxdIZjpt.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"watch-DxdIZjpt.cjs","names":["resolveInstanceKey","getRegistry","ALL_PATHS","ON_DISPOSE"],"sources":["../src/watch/watch.ts"],"sourcesContent":["import { ALL_PATHS } from '@dirtytalk/structural';\nimport { ON_DISPOSE } from '../core/symbols';\nimport { getRegistry } from '../registry';\nimport { resolveInstanceKey } from '../registry/acquire';\nimport type {\n ExtractArgs,\n StateContainerConstructor,\n StateContainerInstance,\n} from '../types/utilities';\n\nconst STOP: unique symbol = Symbol('watch.STOP');\ntype StopSymbol = typeof STOP;\n\nconst BLOC_REF_MARKER = Symbol('BlocRef');\n\n/**\n * Reference to a specific bloc instance, identified by class + the resolved\n * key derived from its `args`.\n */\nexport interface BlocRef<T extends StateContainerConstructor> {\n [BLOC_REF_MARKER]: true;\n blocClass: T;\n /** @internal The resolved instance key (derived from `args`). */\n instanceId: string;\n /**\n * @internal The `args` used to derive `instanceId`. Forwarded to\n * `registry.acquire` so a not-yet-created instance is initialized with\n * these args instead of `undefined`.\n */\n args?: unknown;\n}\n\n/**\n * Create a reference to a specific bloc instance, identified by its `args`.\n * The key is derived the same way `useBloc`/`acquire` derive it.\n *\n * @example\n * ```ts\n * watch(instance(UserBloc, { userId: 'user-123' }), (userBloc) => {\n * console.log(userBloc.state.name);\n * });\n * ```\n */\nexport function instance<T extends StateContainerConstructor>(\n BlocClass: T,\n args?: ExtractArgs<T>,\n): BlocRef<T> {\n return {\n [BLOC_REF_MARKER]: true,\n blocClass: BlocClass,\n instanceId: resolveInstanceKey(BlocClass, args),\n args,\n };\n}\n\nfunction isBlocRef(\n input: unknown,\n): input is BlocRef<StateContainerConstructor> {\n return (\n typeof input === 'object' && input !== null && BLOC_REF_MARKER in input\n );\n}\n\ntype BlocInput = StateContainerConstructor | BlocRef<StateContainerConstructor>;\n\ntype ExtractInstance<T> =\n T extends BlocRef<infer C>\n ? InstanceType<C>\n : T extends StateContainerConstructor\n ? InstanceType<T>\n : never;\n\ntype ExtractInstances<T extends readonly BlocInput[]> = {\n [K in keyof T]: ExtractInstance<T[K]>;\n};\n\n/**\n * Watch function signature for single bloc.\n */\nexport interface WatchSingleFn {\n <T extends StateContainerConstructor>(\n bloc: T | BlocRef<T>,\n callback: (bloc: InstanceType<T>) => void | StopSymbol,\n ): () => void;\n\n STOP: StopSymbol;\n}\n\n/**\n * Watch function signature for multiple blocs.\n */\nexport interface WatchMultipleFn {\n <T extends readonly BlocInput[]>(\n blocs: T,\n callback: (blocs: ExtractInstances<T>) => void | StopSymbol,\n ): () => void;\n\n STOP: StopSymbol;\n}\n\n/**\n * Combined watch function type.\n */\nexport interface WatchFn extends WatchSingleFn {\n <T extends readonly BlocInput[]>(\n blocs: T,\n callback: (blocs: ExtractInstances<T>) => void | StopSymbol,\n ): () => void;\n}\n\n/**\n * Class + resolved key + args needed to (re-)acquire a specific instance.\n * `watch` keeps this around per input so it can re-acquire the same logical\n * instance if the underlying container is disposed elsewhere.\n */\ninterface WatchTarget {\n blocClass: StateContainerConstructor;\n key: string;\n args: unknown;\n}\n\nfunction toWatchTarget(input: BlocInput): WatchTarget {\n const registry = getRegistry();\n if (isBlocRef(input)) {\n return {\n blocClass: input.blocClass,\n key: input.instanceId,\n args: input.args,\n };\n }\n return {\n blocClass: input,\n key: registry.resolveKey(input, undefined, undefined),\n args: undefined,\n };\n}\n\nlet watchRefSeq = 0;\n\n/**\n * Acquire a real ref (countRef: true) for the target, carrying its `args`\n * through so a not-yet-created instance is initialized correctly. The\n * caller is responsible for releasing `refId` in cleanup.\n */\nfunction resolveBloc(\n target: WatchTarget,\n refId: string,\n): StateContainerInstance {\n const registry = getRegistry();\n return registry.acquire(target.blocClass, target.key, {\n countRef: true,\n refId,\n args: target.args,\n });\n}\n\nfunction isArray(input: unknown): input is readonly BlocInput[] {\n return Array.isArray(input);\n}\n\n/**\n * Watch one or more blocs for state changes.\n *\n * Thin wrapper around `container.channel.subscribe(ALL_PATHS, ...)`. The\n * callback fires once immediately, then on every state change of any of the\n * passed blocs. Returning `watch.STOP` from the callback tears down all\n * subscriptions.\n *\n * Note: subscriptions are microtask-deferred (per the DirtyChannel default\n * scheduler), so callbacks land asynchronously after `emit()`.\n *\n * @example Single bloc\n * ```ts\n * const unwatch = watch(UserBloc, (userBloc) => {\n * console.log(userBloc.state.name);\n * });\n * ```\n *\n * @example Multiple blocs\n * ```ts\n * const unwatch = watch(\n * [UserBloc, SettingsBloc] as const,\n * ([userBloc, settingsBloc]) => {\n * console.log(userBloc.state.name, settingsBloc.state.theme);\n * }\n * );\n * ```\n *\n * @example With specific instance\n * ```ts\n * const unwatch = watch(\n * instance(UserBloc, { userId: 'user-123' }),\n * (userBloc) => {\n * console.log(userBloc.state.name);\n * }\n * );\n * ```\n *\n * @example Stop watching from callback\n * ```ts\n * const unwatch = watch(UserBloc, (userBloc) => {\n * if (userBloc.state.done) {\n * return watch.STOP;\n * }\n * });\n * ```\n */\nfunction watchImpl<T extends StateContainerConstructor>(\n bloc: T | BlocRef<T>,\n callback: (bloc: InstanceType<T>) => void | StopSymbol,\n): () => void;\n\nfunction watchImpl<T extends readonly BlocInput[]>(\n blocs: T,\n callback: (blocs: ExtractInstances<T>) => void | StopSymbol,\n): () => void;\n\nfunction watchImpl(\n blocsOrBloc: BlocInput | readonly BlocInput[],\n callback: (blocsOrBloc: any) => void | StopSymbol,\n): () => void {\n const isSingle = !isArray(blocsOrBloc);\n const inputs = isSingle ? [blocsOrBloc] : blocsOrBloc;\n const registry = getRegistry();\n\n const targets = inputs.map(toWatchTarget);\n const refIds = targets.map(() => `_watch_${watchRefSeq++}`);\n\n let disposed = false;\n const instances: StateContainerInstance[] = targets.map((target, i) =>\n resolveBloc(target, refIds[i]),\n );\n const channelUnsubs: Array<(() => void) | undefined> = [];\n const disposedUnsubs: Array<() => void> = [];\n\n const cleanup = () => {\n if (disposed) return;\n disposed = true;\n for (const unsub of channelUnsubs) unsub?.();\n for (const unsub of disposedUnsubs) unsub();\n channelUnsubs.length = 0;\n disposedUnsubs.length = 0;\n for (let i = 0; i < targets.length; i++) {\n registry.release(targets[i].blocClass, targets[i].key, false, refIds[i]);\n }\n };\n\n const runCallback = () => {\n if (disposed) return;\n const arg = isSingle ? instances[0] : instances;\n const result = callback(arg);\n if (result === STOP) cleanup();\n };\n\n const subscribeAt = (index: number) => {\n channelUnsubs[index] = instances[index].channel.subscribe(\n () => ALL_PATHS,\n runCallback,\n );\n };\n\n // On external dispose, tear down the stale subscription and re-acquire +\n // resubscribe (microtask-deferred, so it never runs mid-mutation of the\n // registry that triggered the dispose, e.g. `clearAll()`), then notify.\n const resubscribeAt = (index: number) => {\n if (disposed) return;\n instances[index] = resolveBloc(targets[index], refIds[index]);\n subscribeAt(index);\n subscribeDisposeAt(index);\n runCallback();\n };\n\n const subscribeDisposeAt = (index: number) => {\n disposedUnsubs[index] = instances[index][ON_DISPOSE](() => {\n channelUnsubs[index]?.();\n channelUnsubs[index] = undefined;\n queueMicrotask(() => resubscribeAt(index));\n });\n };\n\n for (let i = 0; i < instances.length; i++) {\n subscribeAt(i);\n }\n\n for (let i = 0; i < instances.length; i++) {\n subscribeDisposeAt(i);\n }\n\n // Fire once immediately so the consumer sees the current state.\n runCallback();\n\n return cleanup;\n}\n\nexport const watch: WatchFn = Object.assign(watchImpl, { STOP }) as WatchFn;\n"],"mappings":";;;;;AAUA,MAAM,OAAsB,OAAO,YAAY;AAG/C,MAAM,kBAAkB,OAAO,SAAS;;;;;;;;;;;;AA8BxC,SAAgB,SACd,WACA,MACY;CACZ,OAAO;GACJ,kBAAkB;EACnB,WAAW;EACX,YAAYA,gBAAAA,mBAAmB,WAAW,IAAI;EAC9C;CACF;AACF;AAEA,SAAS,UACP,OAC6C;CAC7C,OACE,OAAO,UAAU,YAAY,UAAU,QAAQ,mBAAmB;AAEtE;AA4DA,SAAS,cAAc,OAA+B;CACpD,MAAM,WAAWC,eAAAA,YAAY;CAC7B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,SAAS,UAAU,OAAO,QAAQ,gBACxCC,sBAAAA,WACN,WACF;CACF;CAKA,MAAM,iBAAiB,UAAkB;EACvC,IAAI,UAAU;EACd,UAAU,SAAS,YAAY,QAAQ,QAAQ,OAAO,MAAM;EAC5D,YAAY,KAAK;EACjB,mBAAmB,KAAK;EACxB,YAAY;CACd;CAEA,MAAM,sBAAsB,UAAkB;EAC5C,eAAe,SAAS,UAAU,OAAOC,+BAAAA,kBAAkB;GACzD,cAAc,SAAS;GACvB,cAAc,SAAS,KAAA;GACvB,qBAAqB,cAAc,KAAK,CAAC;EAC3C,CAAC;CACH;CAEA,KAAK,IAAI,IAAI,GAAG,IAAI,UAAU,QAAQ,KACpC,YAAY,CAAC;CAGf,KAAK,IAAI,IAAI,GAAG,IAAI,UAAU,QAAQ,KACpC,mBAAmB,CAAC;CAItB,YAAY;CAEZ,OAAO;AACT;AAEA,MAAa,QAAiB,OAAO,OAAO,WAAW,EAAE,KAAK,CAAC"}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
const require_StateContainerRegistry = require("./StateContainerRegistry-C75QpeaO.cjs");
|
|
2
|
+
const require_config = require("./config-D34BkraU.cjs");
|
|
3
|
+
const require_acquire = require("./acquire-CE0uq0Dx.cjs");
|
|
4
|
+
let _dirtytalk_structural = require("@dirtytalk/structural");
|
|
5
|
+
//#region src/watch/watch.ts
|
|
6
|
+
const STOP = Symbol("watch.STOP");
|
|
7
|
+
const BLOC_REF_MARKER = Symbol("BlocRef");
|
|
8
|
+
/**
|
|
9
|
+
* Create a reference to a specific bloc instance, identified by its `args`.
|
|
10
|
+
* The key is derived the same way `useBloc`/`acquire` derive it.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* watch(instance(UserBloc, { userId: 'user-123' }), (userBloc) => {
|
|
15
|
+
* console.log(userBloc.state.name);
|
|
16
|
+
* });
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
function instance(BlocClass, args) {
|
|
20
|
+
return {
|
|
21
|
+
[BLOC_REF_MARKER]: true,
|
|
22
|
+
blocClass: BlocClass,
|
|
23
|
+
instanceId: require_acquire.resolveInstanceKey(BlocClass, args),
|
|
24
|
+
args
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function isBlocRef(input) {
|
|
28
|
+
return typeof input === "object" && input !== null && BLOC_REF_MARKER in input;
|
|
29
|
+
}
|
|
30
|
+
function toWatchTarget(input) {
|
|
31
|
+
const registry = require_config.getRegistry();
|
|
32
|
+
if (isBlocRef(input)) return {
|
|
33
|
+
blocClass: input.blocClass,
|
|
34
|
+
key: input.instanceId,
|
|
35
|
+
args: input.args
|
|
36
|
+
};
|
|
37
|
+
return {
|
|
38
|
+
blocClass: input,
|
|
39
|
+
key: registry.resolveKey(input, void 0, void 0),
|
|
40
|
+
args: void 0
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
let watchRefSeq = 0;
|
|
44
|
+
/**
|
|
45
|
+
* Acquire a real ref (countRef: true) for the target, carrying its `args`
|
|
46
|
+
* through so a not-yet-created instance is initialized correctly. The
|
|
47
|
+
* caller is responsible for releasing `refId` in cleanup.
|
|
48
|
+
*/
|
|
49
|
+
function resolveBloc(target, refId) {
|
|
50
|
+
return require_config.getRegistry().acquire(target.blocClass, target.key, {
|
|
51
|
+
countRef: true,
|
|
52
|
+
refId,
|
|
53
|
+
args: target.args
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
function isArray(input) {
|
|
57
|
+
return Array.isArray(input);
|
|
58
|
+
}
|
|
59
|
+
function watchImpl(blocsOrBloc, callback) {
|
|
60
|
+
const isSingle = !isArray(blocsOrBloc);
|
|
61
|
+
const inputs = isSingle ? [blocsOrBloc] : blocsOrBloc;
|
|
62
|
+
const registry = require_config.getRegistry();
|
|
63
|
+
const targets = inputs.map(toWatchTarget);
|
|
64
|
+
const refIds = targets.map(() => `_watch_${watchRefSeq++}`);
|
|
65
|
+
let disposed = false;
|
|
66
|
+
const instances = targets.map((target, i) => resolveBloc(target, refIds[i]));
|
|
67
|
+
const channelUnsubs = [];
|
|
68
|
+
const disposedUnsubs = [];
|
|
69
|
+
const cleanup = () => {
|
|
70
|
+
if (disposed) return;
|
|
71
|
+
disposed = true;
|
|
72
|
+
for (const unsub of channelUnsubs) unsub?.();
|
|
73
|
+
for (const unsub of disposedUnsubs) unsub();
|
|
74
|
+
channelUnsubs.length = 0;
|
|
75
|
+
disposedUnsubs.length = 0;
|
|
76
|
+
for (let i = 0; i < targets.length; i++) registry.release(targets[i].blocClass, targets[i].key, false, refIds[i]);
|
|
77
|
+
};
|
|
78
|
+
const runCallback = () => {
|
|
79
|
+
if (disposed) return;
|
|
80
|
+
if (callback(isSingle ? instances[0] : instances) === STOP) cleanup();
|
|
81
|
+
};
|
|
82
|
+
const subscribeAt = (index) => {
|
|
83
|
+
channelUnsubs[index] = instances[index].channel.subscribe(() => _dirtytalk_structural.ALL_PATHS, runCallback);
|
|
84
|
+
};
|
|
85
|
+
const resubscribeAt = (index) => {
|
|
86
|
+
if (disposed) return;
|
|
87
|
+
instances[index] = resolveBloc(targets[index], refIds[index]);
|
|
88
|
+
subscribeAt(index);
|
|
89
|
+
subscribeDisposeAt(index);
|
|
90
|
+
runCallback();
|
|
91
|
+
};
|
|
92
|
+
const subscribeDisposeAt = (index) => {
|
|
93
|
+
disposedUnsubs[index] = instances[index][require_StateContainerRegistry.ON_DISPOSE](() => {
|
|
94
|
+
channelUnsubs[index]?.();
|
|
95
|
+
channelUnsubs[index] = void 0;
|
|
96
|
+
queueMicrotask(() => resubscribeAt(index));
|
|
97
|
+
});
|
|
98
|
+
};
|
|
99
|
+
for (let i = 0; i < instances.length; i++) subscribeAt(i);
|
|
100
|
+
for (let i = 0; i < instances.length; i++) subscribeDisposeAt(i);
|
|
101
|
+
runCallback();
|
|
102
|
+
return cleanup;
|
|
103
|
+
}
|
|
104
|
+
const watch = Object.assign(watchImpl, { STOP });
|
|
105
|
+
//#endregion
|
|
106
|
+
Object.defineProperty(exports, "instance", {
|
|
107
|
+
enumerable: true,
|
|
108
|
+
get: function() {
|
|
109
|
+
return instance;
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
Object.defineProperty(exports, "watch", {
|
|
113
|
+
enumerable: true,
|
|
114
|
+
get: function() {
|
|
115
|
+
return watch;
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
//# sourceMappingURL=watch-aoNLQAXp.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"watch-aoNLQAXp.cjs","names":["resolveInstanceKey","getRegistry","ALL_PATHS","ON_DISPOSE"],"sources":["../src/watch/watch.ts"],"sourcesContent":["import { ALL_PATHS } from '@dirtytalk/structural';\nimport { ON_DISPOSE } from '../core/symbols';\nimport { getRegistry } from '../registry';\nimport { resolveInstanceKey } from '../registry/acquire';\nimport type {\n ExtractArgs,\n StateContainerConstructor,\n StateContainerInstance,\n} from '../types/utilities';\n\nconst STOP: unique symbol = Symbol('watch.STOP');\ntype StopSymbol = typeof STOP;\n\nconst BLOC_REF_MARKER = Symbol('BlocRef');\n\n/**\n * Reference to a specific bloc instance, identified by class + the resolved\n * key derived from its `args`.\n */\nexport interface BlocRef<T extends StateContainerConstructor> {\n [BLOC_REF_MARKER]: true;\n blocClass: T;\n /** @internal The resolved instance key (derived from `args`). */\n instanceId: string;\n /**\n * @internal The `args` used to derive `instanceId`. Forwarded to\n * `registry.acquire` so a not-yet-created instance is initialized with\n * these args instead of `undefined`.\n */\n args?: unknown;\n}\n\n/**\n * Create a reference to a specific bloc instance, identified by its `args`.\n * The key is derived the same way `useBloc`/`acquire` derive it.\n *\n * @example\n * ```ts\n * watch(instance(UserBloc, { userId: 'user-123' }), (userBloc) => {\n * console.log(userBloc.state.name);\n * });\n * ```\n */\nexport function instance<T extends StateContainerConstructor>(\n BlocClass: T,\n args?: ExtractArgs<T>,\n): BlocRef<T> {\n return {\n [BLOC_REF_MARKER]: true,\n blocClass: BlocClass,\n instanceId: resolveInstanceKey(BlocClass, args),\n args,\n };\n}\n\nfunction isBlocRef(\n input: unknown,\n): input is BlocRef<StateContainerConstructor> {\n return (\n typeof input === 'object' && input !== null && BLOC_REF_MARKER in input\n );\n}\n\ntype BlocInput = StateContainerConstructor | BlocRef<StateContainerConstructor>;\n\ntype ExtractInstance<T> =\n T extends BlocRef<infer C>\n ? InstanceType<C>\n : T extends StateContainerConstructor\n ? InstanceType<T>\n : never;\n\ntype ExtractInstances<T extends readonly BlocInput[]> = {\n [K in keyof T]: ExtractInstance<T[K]>;\n};\n\n/**\n * Watch function signature for single bloc.\n */\nexport interface WatchSingleFn {\n <T extends StateContainerConstructor>(\n bloc: T | BlocRef<T>,\n callback: (bloc: InstanceType<T>) => void | StopSymbol,\n ): () => void;\n\n STOP: StopSymbol;\n}\n\n/**\n * Watch function signature for multiple blocs.\n */\nexport interface WatchMultipleFn {\n <T extends readonly BlocInput[]>(\n blocs: T,\n callback: (blocs: ExtractInstances<T>) => void | StopSymbol,\n ): () => void;\n\n STOP: StopSymbol;\n}\n\n/**\n * Combined watch function type.\n */\nexport interface WatchFn extends WatchSingleFn {\n <T extends readonly BlocInput[]>(\n blocs: T,\n callback: (blocs: ExtractInstances<T>) => void | StopSymbol,\n ): () => void;\n}\n\n/**\n * Class + resolved key + args needed to (re-)acquire a specific instance.\n * `watch` keeps this around per input so it can re-acquire the same logical\n * instance if the underlying container is disposed elsewhere.\n */\ninterface WatchTarget {\n blocClass: StateContainerConstructor;\n key: string;\n args: unknown;\n}\n\nfunction toWatchTarget(input: BlocInput): WatchTarget {\n const registry = getRegistry();\n if (isBlocRef(input)) {\n return {\n blocClass: input.blocClass,\n key: input.instanceId,\n args: input.args,\n };\n }\n return {\n blocClass: input,\n key: registry.resolveKey(input, undefined, undefined),\n args: undefined,\n };\n}\n\nlet watchRefSeq = 0;\n\n/**\n * Acquire a real ref (countRef: true) for the target, carrying its `args`\n * through so a not-yet-created instance is initialized correctly. The\n * caller is responsible for releasing `refId` in cleanup.\n */\nfunction resolveBloc(\n target: WatchTarget,\n refId: string,\n): StateContainerInstance {\n const registry = getRegistry();\n return registry.acquire(target.blocClass, target.key, {\n countRef: true,\n refId,\n args: target.args,\n });\n}\n\nfunction isArray(input: unknown): input is readonly BlocInput[] {\n return Array.isArray(input);\n}\n\n/**\n * Watch one or more blocs for state changes.\n *\n * Thin wrapper around `container.channel.subscribe(ALL_PATHS, ...)`. The\n * callback fires once immediately, then on every state change of any of the\n * passed blocs. Returning `watch.STOP` from the callback tears down all\n * subscriptions.\n *\n * Note: subscriptions are microtask-deferred (per the DirtyChannel default\n * scheduler), so callbacks land asynchronously after `emit()`.\n *\n * @example Single bloc\n * ```ts\n * const unwatch = watch(UserBloc, (userBloc) => {\n * console.log(userBloc.state.name);\n * });\n * ```\n *\n * @example Multiple blocs\n * ```ts\n * const unwatch = watch(\n * [UserBloc, SettingsBloc] as const,\n * ([userBloc, settingsBloc]) => {\n * console.log(userBloc.state.name, settingsBloc.state.theme);\n * }\n * );\n * ```\n *\n * @example With specific instance\n * ```ts\n * const unwatch = watch(\n * instance(UserBloc, { userId: 'user-123' }),\n * (userBloc) => {\n * console.log(userBloc.state.name);\n * }\n * );\n * ```\n *\n * @example Stop watching from callback\n * ```ts\n * const unwatch = watch(UserBloc, (userBloc) => {\n * if (userBloc.state.done) {\n * return watch.STOP;\n * }\n * });\n * ```\n */\nfunction watchImpl<T extends StateContainerConstructor>(\n bloc: T | BlocRef<T>,\n callback: (bloc: InstanceType<T>) => void | StopSymbol,\n): () => void;\n\nfunction watchImpl<T extends readonly BlocInput[]>(\n blocs: T,\n callback: (blocs: ExtractInstances<T>) => void | StopSymbol,\n): () => void;\n\nfunction watchImpl(\n blocsOrBloc: BlocInput | readonly BlocInput[],\n callback: (blocsOrBloc: any) => void | StopSymbol,\n): () => void {\n const isSingle = !isArray(blocsOrBloc);\n const inputs = isSingle ? [blocsOrBloc] : blocsOrBloc;\n const registry = getRegistry();\n\n const targets = inputs.map(toWatchTarget);\n const refIds = targets.map(() => `_watch_${watchRefSeq++}`);\n\n let disposed = false;\n const instances: StateContainerInstance[] = targets.map((target, i) =>\n resolveBloc(target, refIds[i]),\n );\n const channelUnsubs: Array<(() => void) | undefined> = [];\n const disposedUnsubs: Array<() => void> = [];\n\n const cleanup = () => {\n if (disposed) return;\n disposed = true;\n for (const unsub of channelUnsubs) unsub?.();\n for (const unsub of disposedUnsubs) unsub();\n channelUnsubs.length = 0;\n disposedUnsubs.length = 0;\n for (let i = 0; i < targets.length; i++) {\n registry.release(targets[i].blocClass, targets[i].key, false, refIds[i]);\n }\n };\n\n const runCallback = () => {\n if (disposed) return;\n const arg = isSingle ? instances[0] : instances;\n const result = callback(arg);\n if (result === STOP) cleanup();\n };\n\n const subscribeAt = (index: number) => {\n channelUnsubs[index] = instances[index].channel.subscribe(\n () => ALL_PATHS,\n runCallback,\n );\n };\n\n // On external dispose, tear down the stale subscription and re-acquire +\n // resubscribe (microtask-deferred, so it never runs mid-mutation of the\n // registry that triggered the dispose, e.g. `clearAll()`), then notify.\n const resubscribeAt = (index: number) => {\n if (disposed) return;\n instances[index] = resolveBloc(targets[index], refIds[index]);\n subscribeAt(index);\n subscribeDisposeAt(index);\n runCallback();\n };\n\n const subscribeDisposeAt = (index: number) => {\n disposedUnsubs[index] = instances[index][ON_DISPOSE](() => {\n channelUnsubs[index]?.();\n channelUnsubs[index] = undefined;\n queueMicrotask(() => resubscribeAt(index));\n });\n };\n\n for (let i = 0; i < instances.length; i++) {\n subscribeAt(i);\n }\n\n for (let i = 0; i < instances.length; i++) {\n subscribeDisposeAt(i);\n }\n\n // Fire once immediately so the consumer sees the current state.\n runCallback();\n\n return cleanup;\n}\n\nexport const watch: WatchFn = Object.assign(watchImpl, { STOP }) as WatchFn;\n"],"mappings":";;;;;AAUA,MAAM,OAAsB,OAAO,YAAY;AAG/C,MAAM,kBAAkB,OAAO,SAAS;;;;;;;;;;;;AA8BxC,SAAgB,SACd,WACA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AAG,IAAI,QAAQ,QAAQ,KAClC,SAAS,QAAQ,QAAQ,GAAG,WAAW,QAAQ,GAAG,KAAK,OAAO,OAAO,EAAE;CAE3E;CAEA,MAAM,oBAAoB;EACxB,IAAI,UAAU;EAGd,IADe,SADH,WAAW,UAAU,KAAK,SAE7B,MAAM,MAAM,QAAQ;CAC/B;CAEA,MAAM,eAAe,UAAkB;EACrC,cAAc,SAAS,UAAU,OAAO,QAAQ,gBACxCC,sBAAAA,WACN,WACF;CACF;CAKA,MAAM,iBAAiB,UAAkB;EACvC,IAAI,UAAU;EACd,UAAU,SAAS,YAAY,QAAQ,QAAQ,OAAO,MAAM;EAC5D,YAAY,KAAK;EACjB,mBAAmB,KAAK;EACxB,YAAY;CACd;CAEA,MAAM,sBAAsB,UAAkB;EAC5C,eAAe,SAAS,UAAU,OAAOC,+BAAAA,kBAAkB;GACzD,cAAc,SAAS;GACvB,cAAc,SAAS,KAAA;GACvB,qBAAqB,cAAc,KAAK,CAAC;EAC3C,CAAC;CACH;CAEA,KAAK,IAAI,IAAI,GAAG,IAAI,UAAU,QAAQ,KACpC,YAAY,CAAC;CAGf,KAAK,IAAI,IAAI,GAAG,IAAI,UAAU,QAAQ,KACpC,mBAAmB,CAAC;CAItB,YAAY;CAEZ,OAAO;AACT;AAEA,MAAa,QAAiB,OAAO,OAAO,WAAW,EAAE,KAAK,CAAC"}
|
package/dist/watch-entry.cjs
CHANGED
package/dist/watch-entry.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { n as watch, t as instance } from "./watch-
|
|
1
|
+
import { n as watch, t as instance } from "./watch-BDiwCPmb.js";
|
|
2
2
|
export { instance, watch };
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
const require_StateContainerRegistry = require("./StateContainerRegistry-G81GcCRm.cjs");
|
|
2
|
+
const require_config = require("./config-C1T6Jw7n.cjs");
|
|
3
|
+
const require_acquire = require("./acquire-CkgLJ8WL.cjs");
|
|
4
|
+
let _dirtytalk_structural = require("@dirtytalk/structural");
|
|
5
|
+
//#region src/watch/watch.ts
|
|
6
|
+
const STOP = Symbol("watch.STOP");
|
|
7
|
+
const BLOC_REF_MARKER = Symbol("BlocRef");
|
|
8
|
+
/**
|
|
9
|
+
* Create a reference to a specific bloc instance, identified by its `args`.
|
|
10
|
+
* The key is derived the same way `useBloc`/`acquire` derive it.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* watch(instance(UserBloc, { userId: 'user-123' }), (userBloc) => {
|
|
15
|
+
* console.log(userBloc.state.name);
|
|
16
|
+
* });
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
function instance(BlocClass, args) {
|
|
20
|
+
return {
|
|
21
|
+
[BLOC_REF_MARKER]: true,
|
|
22
|
+
blocClass: BlocClass,
|
|
23
|
+
instanceId: require_acquire.resolveInstanceKey(BlocClass, args),
|
|
24
|
+
args
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function isBlocRef(input) {
|
|
28
|
+
return typeof input === "object" && input !== null && BLOC_REF_MARKER in input;
|
|
29
|
+
}
|
|
30
|
+
function toWatchTarget(input) {
|
|
31
|
+
const registry = require_config.getRegistry();
|
|
32
|
+
if (isBlocRef(input)) return {
|
|
33
|
+
blocClass: input.blocClass,
|
|
34
|
+
key: input.instanceId,
|
|
35
|
+
args: input.args
|
|
36
|
+
};
|
|
37
|
+
return {
|
|
38
|
+
blocClass: input,
|
|
39
|
+
key: registry.resolveKey(input, void 0, void 0),
|
|
40
|
+
args: void 0
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
let watchRefSeq = 0;
|
|
44
|
+
/**
|
|
45
|
+
* Acquire a real ref (countRef: true) for the target, carrying its `args`
|
|
46
|
+
* through so a not-yet-created instance is initialized correctly. The
|
|
47
|
+
* caller is responsible for releasing `refId` in cleanup.
|
|
48
|
+
*/
|
|
49
|
+
function resolveBloc(target, refId) {
|
|
50
|
+
return require_config.getRegistry().acquire(target.blocClass, target.key, {
|
|
51
|
+
countRef: true,
|
|
52
|
+
refId,
|
|
53
|
+
args: target.args
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
function isArray(input) {
|
|
57
|
+
return Array.isArray(input);
|
|
58
|
+
}
|
|
59
|
+
function watchImpl(blocsOrBloc, callback) {
|
|
60
|
+
const isSingle = !isArray(blocsOrBloc);
|
|
61
|
+
const inputs = isSingle ? [blocsOrBloc] : blocsOrBloc;
|
|
62
|
+
const registry = require_config.getRegistry();
|
|
63
|
+
const targets = inputs.map(toWatchTarget);
|
|
64
|
+
const refIds = targets.map(() => `_watch_${watchRefSeq++}`);
|
|
65
|
+
let disposed = false;
|
|
66
|
+
const instances = targets.map((target, i) => resolveBloc(target, refIds[i]));
|
|
67
|
+
const channelUnsubs = [];
|
|
68
|
+
const disposedUnsubs = [];
|
|
69
|
+
const cleanup = () => {
|
|
70
|
+
if (disposed) return;
|
|
71
|
+
disposed = true;
|
|
72
|
+
for (const unsub of channelUnsubs) unsub?.();
|
|
73
|
+
for (const unsub of disposedUnsubs) unsub();
|
|
74
|
+
channelUnsubs.length = 0;
|
|
75
|
+
disposedUnsubs.length = 0;
|
|
76
|
+
for (let i = 0; i < targets.length; i++) registry.release(targets[i].blocClass, targets[i].key, false, refIds[i]);
|
|
77
|
+
};
|
|
78
|
+
const runCallback = () => {
|
|
79
|
+
if (disposed) return;
|
|
80
|
+
if (callback(isSingle ? instances[0] : instances) === STOP) cleanup();
|
|
81
|
+
};
|
|
82
|
+
const subscribeAt = (index) => {
|
|
83
|
+
channelUnsubs[index] = instances[index].channel.subscribe(() => _dirtytalk_structural.ALL_PATHS, runCallback);
|
|
84
|
+
};
|
|
85
|
+
const resubscribeAt = (index) => {
|
|
86
|
+
if (disposed) return;
|
|
87
|
+
instances[index] = resolveBloc(targets[index], refIds[index]);
|
|
88
|
+
subscribeAt(index);
|
|
89
|
+
subscribeDisposeAt(index);
|
|
90
|
+
runCallback();
|
|
91
|
+
};
|
|
92
|
+
const subscribeDisposeAt = (index) => {
|
|
93
|
+
disposedUnsubs[index] = instances[index][require_StateContainerRegistry.ON_DISPOSE](() => {
|
|
94
|
+
channelUnsubs[index]?.();
|
|
95
|
+
channelUnsubs[index] = void 0;
|
|
96
|
+
queueMicrotask(() => resubscribeAt(index));
|
|
97
|
+
});
|
|
98
|
+
};
|
|
99
|
+
for (let i = 0; i < instances.length; i++) subscribeAt(i);
|
|
100
|
+
for (let i = 0; i < instances.length; i++) subscribeDisposeAt(i);
|
|
101
|
+
runCallback();
|
|
102
|
+
return cleanup;
|
|
103
|
+
}
|
|
104
|
+
const watch = Object.assign(watchImpl, { STOP });
|
|
105
|
+
//#endregion
|
|
106
|
+
Object.defineProperty(exports, "instance", {
|
|
107
|
+
enumerable: true,
|
|
108
|
+
get: function() {
|
|
109
|
+
return instance;
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
Object.defineProperty(exports, "watch", {
|
|
113
|
+
enumerable: true,
|
|
114
|
+
get: function() {
|
|
115
|
+
return watch;
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
//# sourceMappingURL=watch-lEco5Tbl.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"watch-lEco5Tbl.cjs","names":["resolveInstanceKey","getRegistry","ALL_PATHS","ON_DISPOSE"],"sources":["../src/watch/watch.ts"],"sourcesContent":["import { ALL_PATHS } from '@dirtytalk/structural';\nimport { ON_DISPOSE } from '../core/symbols';\nimport { getRegistry } from '../registry';\nimport { resolveInstanceKey } from '../registry/acquire';\nimport type {\n ExtractArgs,\n StateContainerConstructor,\n StateContainerInstance,\n} from '../types/utilities';\n\nconst STOP: unique symbol = Symbol('watch.STOP');\ntype StopSymbol = typeof STOP;\n\nconst BLOC_REF_MARKER = Symbol('BlocRef');\n\n/**\n * Reference to a specific bloc instance, identified by class + the resolved\n * key derived from its `args`.\n */\nexport interface BlocRef<T extends StateContainerConstructor> {\n [BLOC_REF_MARKER]: true;\n blocClass: T;\n /** @internal The resolved instance key (derived from `args`). */\n instanceId: string;\n /**\n * @internal The `args` used to derive `instanceId`. Forwarded to\n * `registry.acquire` so a not-yet-created instance is initialized with\n * these args instead of `undefined`.\n */\n args?: unknown;\n}\n\n/**\n * Create a reference to a specific bloc instance, identified by its `args`.\n * The key is derived the same way `useBloc`/`acquire` derive it.\n *\n * @example\n * ```ts\n * watch(instance(UserBloc, { userId: 'user-123' }), (userBloc) => {\n * console.log(userBloc.state.name);\n * });\n * ```\n */\nexport function instance<T extends StateContainerConstructor>(\n BlocClass: T,\n args?: ExtractArgs<T>,\n): BlocRef<T> {\n return {\n [BLOC_REF_MARKER]: true,\n blocClass: BlocClass,\n instanceId: resolveInstanceKey(BlocClass, args),\n args,\n };\n}\n\nfunction isBlocRef(\n input: unknown,\n): input is BlocRef<StateContainerConstructor> {\n return (\n typeof input === 'object' && input !== null && BLOC_REF_MARKER in input\n );\n}\n\ntype BlocInput = StateContainerConstructor | BlocRef<StateContainerConstructor>;\n\ntype ExtractInstance<T> =\n T extends BlocRef<infer C>\n ? InstanceType<C>\n : T extends StateContainerConstructor\n ? InstanceType<T>\n : never;\n\ntype ExtractInstances<T extends readonly BlocInput[]> = {\n [K in keyof T]: ExtractInstance<T[K]>;\n};\n\n/**\n * Watch function signature for single bloc.\n */\nexport interface WatchSingleFn {\n <T extends StateContainerConstructor>(\n bloc: T | BlocRef<T>,\n callback: (bloc: InstanceType<T>) => void | StopSymbol,\n ): () => void;\n\n STOP: StopSymbol;\n}\n\n/**\n * Watch function signature for multiple blocs.\n */\nexport interface WatchMultipleFn {\n <T extends readonly BlocInput[]>(\n blocs: T,\n callback: (blocs: ExtractInstances<T>) => void | StopSymbol,\n ): () => void;\n\n STOP: StopSymbol;\n}\n\n/**\n * Combined watch function type.\n */\nexport interface WatchFn extends WatchSingleFn {\n <T extends readonly BlocInput[]>(\n blocs: T,\n callback: (blocs: ExtractInstances<T>) => void | StopSymbol,\n ): () => void;\n}\n\n/**\n * Class + resolved key + args needed to (re-)acquire a specific instance.\n * `watch` keeps this around per input so it can re-acquire the same logical\n * instance if the underlying container is disposed elsewhere.\n */\ninterface WatchTarget {\n blocClass: StateContainerConstructor;\n key: string;\n args: unknown;\n}\n\nfunction toWatchTarget(input: BlocInput): WatchTarget {\n const registry = getRegistry();\n if (isBlocRef(input)) {\n return {\n blocClass: input.blocClass,\n key: input.instanceId,\n args: input.args,\n };\n }\n return {\n blocClass: input,\n key: registry.resolveKey(input, undefined, undefined),\n args: undefined,\n };\n}\n\nlet watchRefSeq = 0;\n\n/**\n * Acquire a real ref (countRef: true) for the target, carrying its `args`\n * through so a not-yet-created instance is initialized correctly. The\n * caller is responsible for releasing `refId` in cleanup.\n */\nfunction resolveBloc(\n target: WatchTarget,\n refId: string,\n): StateContainerInstance {\n const registry = getRegistry();\n return registry.acquire(target.blocClass, target.key, {\n countRef: true,\n refId,\n args: target.args,\n });\n}\n\nfunction isArray(input: unknown): input is readonly BlocInput[] {\n return Array.isArray(input);\n}\n\n/**\n * Watch one or more blocs for state changes.\n *\n * Thin wrapper around `container.channel.subscribe(ALL_PATHS, ...)`. The\n * callback fires once immediately, then on every state change of any of the\n * passed blocs. Returning `watch.STOP` from the callback tears down all\n * subscriptions.\n *\n * Note: subscriptions are microtask-deferred (per the DirtyChannel default\n * scheduler), so callbacks land asynchronously after `emit()`.\n *\n * @example Single bloc\n * ```ts\n * const unwatch = watch(UserBloc, (userBloc) => {\n * console.log(userBloc.state.name);\n * });\n * ```\n *\n * @example Multiple blocs\n * ```ts\n * const unwatch = watch(\n * [UserBloc, SettingsBloc] as const,\n * ([userBloc, settingsBloc]) => {\n * console.log(userBloc.state.name, settingsBloc.state.theme);\n * }\n * );\n * ```\n *\n * @example With specific instance\n * ```ts\n * const unwatch = watch(\n * instance(UserBloc, { userId: 'user-123' }),\n * (userBloc) => {\n * console.log(userBloc.state.name);\n * }\n * );\n * ```\n *\n * @example Stop watching from callback\n * ```ts\n * const unwatch = watch(UserBloc, (userBloc) => {\n * if (userBloc.state.done) {\n * return watch.STOP;\n * }\n * });\n * ```\n */\nfunction watchImpl<T extends StateContainerConstructor>(\n bloc: T | BlocRef<T>,\n callback: (bloc: InstanceType<T>) => void | StopSymbol,\n): () => void;\n\nfunction watchImpl<T extends readonly BlocInput[]>(\n blocs: T,\n callback: (blocs: ExtractInstances<T>) => void | StopSymbol,\n): () => void;\n\nfunction watchImpl(\n blocsOrBloc: BlocInput | readonly BlocInput[],\n callback: (blocsOrBloc: any) => void | StopSymbol,\n): () => void {\n const isSingle = !isArray(blocsOrBloc);\n const inputs = isSingle ? [blocsOrBloc] : blocsOrBloc;\n const registry = getRegistry();\n\n const targets = inputs.map(toWatchTarget);\n const refIds = targets.map(() => `_watch_${watchRefSeq++}`);\n\n let disposed = false;\n const instances: StateContainerInstance[] = targets.map((target, i) =>\n resolveBloc(target, refIds[i]),\n );\n const channelUnsubs: Array<(() => void) | undefined> = [];\n const disposedUnsubs: Array<() => void> = [];\n\n const cleanup = () => {\n if (disposed) return;\n disposed = true;\n for (const unsub of channelUnsubs) unsub?.();\n for (const unsub of disposedUnsubs) unsub();\n channelUnsubs.length = 0;\n disposedUnsubs.length = 0;\n for (let i = 0; i < targets.length; i++) {\n registry.release(targets[i].blocClass, targets[i].key, false, refIds[i]);\n }\n };\n\n const runCallback = () => {\n if (disposed) return;\n const arg = isSingle ? instances[0] : instances;\n const result = callback(arg);\n if (result === STOP) cleanup();\n };\n\n const subscribeAt = (index: number) => {\n channelUnsubs[index] = instances[index].channel.subscribe(\n () => ALL_PATHS,\n runCallback,\n );\n };\n\n // On external dispose, tear down the stale subscription and re-acquire +\n // resubscribe (microtask-deferred, so it never runs mid-mutation of the\n // registry that triggered the dispose, e.g. `clearAll()`), then notify.\n const resubscribeAt = (index: number) => {\n if (disposed) return;\n instances[index] = resolveBloc(targets[index], refIds[index]);\n subscribeAt(index);\n subscribeDisposeAt(index);\n runCallback();\n };\n\n const subscribeDisposeAt = (index: number) => {\n disposedUnsubs[index] = instances[index][ON_DISPOSE](() => {\n channelUnsubs[index]?.();\n channelUnsubs[index] = undefined;\n queueMicrotask(() => resubscribeAt(index));\n });\n };\n\n for (let i = 0; i < instances.length; i++) {\n subscribeAt(i);\n }\n\n for (let i = 0; i < instances.length; i++) {\n subscribeDisposeAt(i);\n }\n\n // Fire once immediately so the consumer sees the current state.\n runCallback();\n\n return cleanup;\n}\n\nexport const watch: WatchFn = Object.assign(watchImpl, { STOP }) as WatchFn;\n"],"mappings":";;;;;AAUA,MAAM,OAAsB,OAAO,YAAY;AAG/C,MAAM,kBAAkB,OAAO,SAAS;;;;;;;;;;;;AA8BxC,SAAgB,SACd,WACA,MACY;CACZ,OAAO;GACJ,kBAAkB;EACnB,WAAW;EACX,YAAYA,gBAAAA,mBAAmB,WAAW,IAAI;EAC9C;CACF;AACF;AAEA,SAAS,UACP,OAC6C;CAC7C,OACE,OAAO,UAAU,YAAY,UAAU,QAAQ,mBAAmB;AAEtE;AA4DA,SAAS,cAAc,OAA+B;CACpD,MAAM,WAAWC,eAAAA,YAAY;CAC7B,IAAI,UAAU,KAAK,GACjB,OAAO;EACL,WAAW,MAAM;EACjB,KAAK,MAAM;EACX,MAAM,MAAM;CACd;CAEF,OAAO;EACL,WAAW;EACX,KAAK,SAAS,WAAW,OAAO,KAAA,GAAW,KAAA,CAAS;EACpD,MAAM,KAAA;CACR;AACF;AAEA,IAAI,cAAc;;;;;;AAOlB,SAAS,YACP,QACA,OACwB;CAExB,OADiBA,eAAAA,YACH,EAAE,QAAQ,OAAO,WAAW,OAAO,KAAK;EACpD,UAAU;EACV;EACA,MAAM,OAAO;CACf,CAAC;AACH;AAEA,SAAS,QAAQ,OAA+C;CAC9D,OAAO,MAAM,QAAQ,KAAK;AAC5B;AA2DA,SAAS,UACP,aACA,UACY;CACZ,MAAM,WAAW,CAAC,QAAQ,WAAW;CACrC,MAAM,SAAS,WAAW,CAAC,WAAW,IAAI;CAC1C,MAAM,WAAW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AEA,MAAa,QAAiB,OAAO,OAAO,WAAW,EAAE,KAAK,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blac/core",
|
|
3
|
-
"version": "2.0
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Brendan Mullins <jsnanigans@gmail.com>",
|
|
6
6
|
"description": "Type-safe, class-based state management with proxy tracking and plugin system",
|
|
@@ -118,11 +118,11 @@
|
|
|
118
118
|
{
|
|
119
119
|
"name": "@blac/core (ESM, brotli)",
|
|
120
120
|
"path": "dist/index.js",
|
|
121
|
-
"limit": "
|
|
121
|
+
"limit": "9.2 kB"
|
|
122
122
|
}
|
|
123
123
|
],
|
|
124
124
|
"dependencies": {
|
|
125
|
-
"@dirtytalk/structural": "^0.0.
|
|
125
|
+
"@dirtytalk/structural": "^0.0.9"
|
|
126
126
|
},
|
|
127
127
|
"devDependencies": {
|
|
128
128
|
"@size-limit/preset-small-lib": "^11.1.6",
|