@blac/core 2.0.18 → 2.0.19

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.
Files changed (86) hide show
  1. package/dist/StateContainerRegistry-C0dCNlTF.js +1005 -0
  2. package/dist/StateContainerRegistry-C0dCNlTF.js.map +1 -0
  3. package/dist/StateContainerRegistry-CchRjnjX.cjs +1094 -0
  4. package/dist/StateContainerRegistry-CchRjnjX.cjs.map +1 -0
  5. package/dist/StateContainerRegistry-D9BIRjwv.cjs +1088 -0
  6. package/dist/StateContainerRegistry-D9BIRjwv.cjs.map +1 -0
  7. package/dist/StateContainerRegistry-DVIBTxGZ.js +999 -0
  8. package/dist/StateContainerRegistry-DVIBTxGZ.js.map +1 -0
  9. package/dist/StateContainerRegistry-Dj77cok9.js +1005 -0
  10. package/dist/StateContainerRegistry-Dj77cok9.js.map +1 -0
  11. package/dist/StateContainerRegistry-lIwo-0m3.cjs +1094 -0
  12. package/dist/StateContainerRegistry-lIwo-0m3.cjs.map +1 -0
  13. package/dist/acquire-BRIJVJaA.js +35 -0
  14. package/dist/acquire-BRIJVJaA.js.map +1 -0
  15. package/dist/acquire-C3nOb3XR.cjs +46 -0
  16. package/dist/acquire-C3nOb3XR.cjs.map +1 -0
  17. package/dist/acquire-C4WAS13G.cjs +46 -0
  18. package/dist/acquire-C4WAS13G.cjs.map +1 -0
  19. package/dist/acquire-C7beGI55.cjs +46 -0
  20. package/dist/acquire-C7beGI55.cjs.map +1 -0
  21. package/dist/acquire-DXEQDZGr.js +35 -0
  22. package/dist/acquire-DXEQDZGr.js.map +1 -0
  23. package/dist/acquire-Dg-uXwjF.js +35 -0
  24. package/dist/acquire-Dg-uXwjF.js.map +1 -0
  25. package/dist/config-B1l8ZGlU.cjs +32 -0
  26. package/dist/config-B1l8ZGlU.cjs.map +1 -0
  27. package/dist/config-CzcoAvBo.js +16 -0
  28. package/dist/config-CzcoAvBo.js.map +1 -0
  29. package/dist/config-D6BoWWPv.js +16 -0
  30. package/dist/config-D6BoWWPv.js.map +1 -0
  31. package/dist/config-DVM_ESQt.js +16 -0
  32. package/dist/config-DVM_ESQt.js.map +1 -0
  33. package/dist/config-d9_U58ef.cjs +32 -0
  34. package/dist/config-d9_U58ef.cjs.map +1 -0
  35. package/dist/config-hWTayX76.cjs +32 -0
  36. package/dist/config-hWTayX76.cjs.map +1 -0
  37. package/dist/core/StateContainerRegistry.d.ts +4 -3
  38. package/dist/debug.cjs +3 -3
  39. package/dist/debug.js +3 -3
  40. package/dist/ensure-B1dEDsZV.cjs +559 -0
  41. package/dist/ensure-B1dEDsZV.cjs.map +1 -0
  42. package/dist/ensure-Bm91o37h.js +536 -0
  43. package/dist/ensure-Bm91o37h.js.map +1 -0
  44. package/dist/ensure-CQimeD0I.cjs +559 -0
  45. package/dist/ensure-CQimeD0I.cjs.map +1 -0
  46. package/dist/ensure-DTTTd2h8.js +536 -0
  47. package/dist/ensure-DTTTd2h8.js.map +1 -0
  48. package/dist/ensure-VuiNPccZ.cjs +559 -0
  49. package/dist/ensure-VuiNPccZ.cjs.map +1 -0
  50. package/dist/ensure-vxAGa2v3.js +536 -0
  51. package/dist/ensure-vxAGa2v3.js.map +1 -0
  52. package/dist/index.cjs +6 -6
  53. package/dist/index.js +6 -6
  54. package/dist/management-AFuqQEwz.js +38 -0
  55. package/dist/management-AFuqQEwz.js.map +1 -0
  56. package/dist/management-BodVLAfJ.cjs +85 -0
  57. package/dist/management-BodVLAfJ.cjs.map +1 -0
  58. package/dist/management-C2wv4vGS.cjs +85 -0
  59. package/dist/management-C2wv4vGS.cjs.map +1 -0
  60. package/dist/management-D2pjr38g.js +38 -0
  61. package/dist/management-D2pjr38g.js.map +1 -0
  62. package/dist/management-D5WsZoTV.js +38 -0
  63. package/dist/management-D5WsZoTV.js.map +1 -0
  64. package/dist/management-DwO0nBzL.cjs +85 -0
  65. package/dist/management-DwO0nBzL.cjs.map +1 -0
  66. package/dist/plugin/PluginManager.d.ts +3 -1
  67. package/dist/plugins.cjs +1 -1
  68. package/dist/plugins.js +1 -1
  69. package/dist/testing.cjs +4 -4
  70. package/dist/testing.js +4 -4
  71. package/dist/watch/watch.d.ts +6 -0
  72. package/dist/watch-BHfCtbn1.cjs +119 -0
  73. package/dist/watch-BHfCtbn1.cjs.map +1 -0
  74. package/dist/watch-D01MdwKy.js +108 -0
  75. package/dist/watch-D01MdwKy.js.map +1 -0
  76. package/dist/watch-DkDGmGAx.cjs +119 -0
  77. package/dist/watch-DkDGmGAx.cjs.map +1 -0
  78. package/dist/watch-DztcJuUy.cjs +119 -0
  79. package/dist/watch-DztcJuUy.cjs.map +1 -0
  80. package/dist/watch-IrR8G28N.js +108 -0
  81. package/dist/watch-IrR8G28N.js.map +1 -0
  82. package/dist/watch-entry.cjs +1 -1
  83. package/dist/watch-entry.js +1 -1
  84. package/dist/watch-hx0r92Zc.js +108 -0
  85. package/dist/watch-hx0r92Zc.js.map +1 -0
  86. package/package.json +2 -2
@@ -0,0 +1,119 @@
1
+ const require_config = require("./config-B1l8ZGlU.cjs");
2
+ const require_acquire = require("./acquire-C4WAS13G.cjs");
3
+ let _dirtytalk_structural = require("@dirtytalk/structural");
4
+ //#region src/watch/watch.ts
5
+ const STOP = Symbol("watch.STOP");
6
+ const BLOC_REF_MARKER = Symbol("BlocRef");
7
+ /**
8
+ * Create a reference to a specific bloc instance, identified by its `args`.
9
+ * The key is derived the same way `useBloc`/`acquire` derive it.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * watch(instance(UserBloc, { userId: 'user-123' }), (userBloc) => {
14
+ * console.log(userBloc.state.name);
15
+ * });
16
+ * ```
17
+ */
18
+ function instance(BlocClass, args) {
19
+ return {
20
+ [BLOC_REF_MARKER]: true,
21
+ blocClass: BlocClass,
22
+ instanceId: require_acquire.resolveInstanceKey(BlocClass, args),
23
+ args
24
+ };
25
+ }
26
+ function isBlocRef(input) {
27
+ return typeof input === "object" && input !== null && BLOC_REF_MARKER in input;
28
+ }
29
+ function toWatchTarget(input) {
30
+ const registry = require_config.getRegistry();
31
+ if (isBlocRef(input)) return {
32
+ blocClass: input.blocClass,
33
+ key: input.instanceId,
34
+ args: input.args
35
+ };
36
+ return {
37
+ blocClass: input,
38
+ key: registry.resolveKey(input, void 0, void 0),
39
+ args: void 0
40
+ };
41
+ }
42
+ let watchRefSeq = 0;
43
+ /**
44
+ * Acquire a real ref (countRef: true) for the target, carrying its `args`
45
+ * through so a not-yet-created instance is initialized correctly. The
46
+ * caller is responsible for releasing `refId` in cleanup.
47
+ */
48
+ function resolveBloc(target, refId) {
49
+ return require_config.getRegistry().acquire(target.blocClass, target.key, {
50
+ countRef: true,
51
+ refId,
52
+ args: target.args
53
+ });
54
+ }
55
+ function isArray(input) {
56
+ return Array.isArray(input);
57
+ }
58
+ function watchImpl(blocsOrBloc, callback) {
59
+ const isSingle = !isArray(blocsOrBloc);
60
+ const inputs = isSingle ? [blocsOrBloc] : blocsOrBloc;
61
+ const registry = require_config.getRegistry();
62
+ const targets = inputs.map(toWatchTarget);
63
+ const refIds = targets.map(() => `_watch_${watchRefSeq++}`);
64
+ let disposed = false;
65
+ const instances = targets.map((target, i) => resolveBloc(target, refIds[i]));
66
+ const channelUnsubs = [];
67
+ const disposedUnsubs = [];
68
+ const cleanup = () => {
69
+ if (disposed) return;
70
+ disposed = true;
71
+ for (const unsub of channelUnsubs) unsub?.();
72
+ for (const unsub of disposedUnsubs) unsub();
73
+ channelUnsubs.length = 0;
74
+ disposedUnsubs.length = 0;
75
+ for (let i = 0; i < targets.length; i++) registry.release(targets[i].blocClass, targets[i].key, false, refIds[i]);
76
+ };
77
+ const runCallback = () => {
78
+ if (disposed) return;
79
+ if (callback(isSingle ? instances[0] : instances) === STOP) cleanup();
80
+ };
81
+ const subscribeAt = (index) => {
82
+ channelUnsubs[index] = instances[index].channel.subscribe(() => _dirtytalk_structural.ALL_PATHS, runCallback);
83
+ };
84
+ const resubscribeAt = (index) => {
85
+ if (disposed) return;
86
+ instances[index] = resolveBloc(targets[index], refIds[index]);
87
+ subscribeAt(index);
88
+ runCallback();
89
+ };
90
+ for (let i = 0; i < instances.length; i++) subscribeAt(i);
91
+ for (let i = 0; i < instances.length; i++) {
92
+ const index = i;
93
+ disposedUnsubs.push(registry.on("disposed", (container) => {
94
+ if (disposed) return;
95
+ if (container !== instances[index]) return;
96
+ channelUnsubs[index]?.();
97
+ channelUnsubs[index] = void 0;
98
+ queueMicrotask(() => resubscribeAt(index));
99
+ }));
100
+ }
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-BHfCtbn1.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"watch-BHfCtbn1.cjs","names":["resolveInstanceKey","getRegistry","ALL_PATHS"],"sources":["../src/watch/watch.ts"],"sourcesContent":["import { ALL_PATHS } from '@dirtytalk/structural';\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 runCallback();\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 const index = i;\n disposedUnsubs.push(\n registry.on('disposed', (container) => {\n if (disposed) return;\n if (container !== instances[index]) return;\n channelUnsubs[index]?.();\n channelUnsubs[index] = undefined;\n queueMicrotask(() => resubscribeAt(index));\n }),\n );\n }\n\n // Fire once immediately so the consumer sees the current state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C;CAE3C,MAAM,gBAAgB;EACpB,IAAI,UAAU;EACd,WAAW;EACX,KAAK,MAAM,SAAS,eAAe,QAAQ;EAC3C,KAAK,MAAM,SAAS,gBAAgB,MAAM;EAC1C,cAAc,SAAS;EACvB,eAAe,SAAS;EACxB,KAAK,IAAI,IAAI,GAAG,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,YAAY;CACd;CAEA,KAAK,IAAI,IAAI,GAAG,IAAI,UAAU,QAAQ,KACpC,YAAY,CAAC;CAGf,KAAK,IAAI,IAAI,GAAG,IAAI,UAAU,QAAQ,KAAK;EACzC,MAAM,QAAQ;EACd,eAAe,KACb,SAAS,GAAG,aAAa,cAAc;GACrC,IAAI,UAAU;GACd,IAAI,cAAc,UAAU,QAAQ;GACpC,cAAc,SAAS;GACvB,cAAc,SAAS,KAAA;GACvB,qBAAqB,cAAc,KAAK,CAAC;EAC3C,CAAC,CACH;CACF;CAGA,YAAY;CAEZ,OAAO;AACT;AAEA,MAAa,QAAiB,OAAO,OAAO,WAAW,EAAE,KAAK,CAAC"}
@@ -0,0 +1,108 @@
1
+ import { t as getRegistry } from "./config-DVM_ESQt.js";
2
+ import { n as resolveInstanceKey } from "./acquire-Dg-uXwjF.js";
3
+ import { ALL_PATHS } from "@dirtytalk/structural";
4
+ //#region src/watch/watch.ts
5
+ const STOP = Symbol("watch.STOP");
6
+ const BLOC_REF_MARKER = Symbol("BlocRef");
7
+ /**
8
+ * Create a reference to a specific bloc instance, identified by its `args`.
9
+ * The key is derived the same way `useBloc`/`acquire` derive it.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * watch(instance(UserBloc, { userId: 'user-123' }), (userBloc) => {
14
+ * console.log(userBloc.state.name);
15
+ * });
16
+ * ```
17
+ */
18
+ function instance(BlocClass, args) {
19
+ return {
20
+ [BLOC_REF_MARKER]: true,
21
+ blocClass: BlocClass,
22
+ instanceId: resolveInstanceKey(BlocClass, args),
23
+ args
24
+ };
25
+ }
26
+ function isBlocRef(input) {
27
+ return typeof input === "object" && input !== null && BLOC_REF_MARKER in input;
28
+ }
29
+ function toWatchTarget(input) {
30
+ const registry = getRegistry();
31
+ if (isBlocRef(input)) return {
32
+ blocClass: input.blocClass,
33
+ key: input.instanceId,
34
+ args: input.args
35
+ };
36
+ return {
37
+ blocClass: input,
38
+ key: registry.resolveKey(input, void 0, void 0),
39
+ args: void 0
40
+ };
41
+ }
42
+ let watchRefSeq = 0;
43
+ /**
44
+ * Acquire a real ref (countRef: true) for the target, carrying its `args`
45
+ * through so a not-yet-created instance is initialized correctly. The
46
+ * caller is responsible for releasing `refId` in cleanup.
47
+ */
48
+ function resolveBloc(target, refId) {
49
+ return getRegistry().acquire(target.blocClass, target.key, {
50
+ countRef: true,
51
+ refId,
52
+ args: target.args
53
+ });
54
+ }
55
+ function isArray(input) {
56
+ return Array.isArray(input);
57
+ }
58
+ function watchImpl(blocsOrBloc, callback) {
59
+ const isSingle = !isArray(blocsOrBloc);
60
+ const inputs = isSingle ? [blocsOrBloc] : blocsOrBloc;
61
+ const registry = getRegistry();
62
+ const targets = inputs.map(toWatchTarget);
63
+ const refIds = targets.map(() => `_watch_${watchRefSeq++}`);
64
+ let disposed = false;
65
+ const instances = targets.map((target, i) => resolveBloc(target, refIds[i]));
66
+ const channelUnsubs = [];
67
+ const disposedUnsubs = [];
68
+ const cleanup = () => {
69
+ if (disposed) return;
70
+ disposed = true;
71
+ for (const unsub of channelUnsubs) unsub?.();
72
+ for (const unsub of disposedUnsubs) unsub();
73
+ channelUnsubs.length = 0;
74
+ disposedUnsubs.length = 0;
75
+ for (let i = 0; i < targets.length; i++) registry.release(targets[i].blocClass, targets[i].key, false, refIds[i]);
76
+ };
77
+ const runCallback = () => {
78
+ if (disposed) return;
79
+ if (callback(isSingle ? instances[0] : instances) === STOP) cleanup();
80
+ };
81
+ const subscribeAt = (index) => {
82
+ channelUnsubs[index] = instances[index].channel.subscribe(() => ALL_PATHS, runCallback);
83
+ };
84
+ const resubscribeAt = (index) => {
85
+ if (disposed) return;
86
+ instances[index] = resolveBloc(targets[index], refIds[index]);
87
+ subscribeAt(index);
88
+ runCallback();
89
+ };
90
+ for (let i = 0; i < instances.length; i++) subscribeAt(i);
91
+ for (let i = 0; i < instances.length; i++) {
92
+ const index = i;
93
+ disposedUnsubs.push(registry.on("disposed", (container) => {
94
+ if (disposed) return;
95
+ if (container !== instances[index]) return;
96
+ channelUnsubs[index]?.();
97
+ channelUnsubs[index] = void 0;
98
+ queueMicrotask(() => resubscribeAt(index));
99
+ }));
100
+ }
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-D01MdwKy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"watch-D01MdwKy.js","names":[],"sources":["../src/watch/watch.ts"],"sourcesContent":["import { ALL_PATHS } from '@dirtytalk/structural';\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 runCallback();\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 const index = i;\n disposedUnsubs.push(\n registry.on('disposed', (container) => {\n if (disposed) return;\n if (container !== instances[index]) return;\n channelUnsubs[index]?.();\n channelUnsubs[index] = undefined;\n queueMicrotask(() => resubscribeAt(index));\n }),\n );\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":";;;;AASA,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;;;;;;AAOlB,SAAS,YACP,QACA,OACwB;CAExB,OADiB,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,YAAY;CAE7B,MAAM,UA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,KAAK,CAAC"}
@@ -0,0 +1,119 @@
1
+ const require_config = require("./config-d9_U58ef.cjs");
2
+ const require_acquire = require("./acquire-C3nOb3XR.cjs");
3
+ let _dirtytalk_structural = require("@dirtytalk/structural");
4
+ //#region src/watch/watch.ts
5
+ const STOP = Symbol("watch.STOP");
6
+ const BLOC_REF_MARKER = Symbol("BlocRef");
7
+ /**
8
+ * Create a reference to a specific bloc instance, identified by its `args`.
9
+ * The key is derived the same way `useBloc`/`acquire` derive it.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * watch(instance(UserBloc, { userId: 'user-123' }), (userBloc) => {
14
+ * console.log(userBloc.state.name);
15
+ * });
16
+ * ```
17
+ */
18
+ function instance(BlocClass, args) {
19
+ return {
20
+ [BLOC_REF_MARKER]: true,
21
+ blocClass: BlocClass,
22
+ instanceId: require_acquire.resolveInstanceKey(BlocClass, args),
23
+ args
24
+ };
25
+ }
26
+ function isBlocRef(input) {
27
+ return typeof input === "object" && input !== null && BLOC_REF_MARKER in input;
28
+ }
29
+ function toWatchTarget(input) {
30
+ const registry = require_config.getRegistry();
31
+ if (isBlocRef(input)) return {
32
+ blocClass: input.blocClass,
33
+ key: input.instanceId,
34
+ args: input.args
35
+ };
36
+ return {
37
+ blocClass: input,
38
+ key: registry.resolveKey(input, void 0, void 0),
39
+ args: void 0
40
+ };
41
+ }
42
+ let watchRefSeq = 0;
43
+ /**
44
+ * Acquire a real ref (countRef: true) for the target, carrying its `args`
45
+ * through so a not-yet-created instance is initialized correctly. The
46
+ * caller is responsible for releasing `refId` in cleanup.
47
+ */
48
+ function resolveBloc(target, refId) {
49
+ return require_config.getRegistry().acquire(target.blocClass, target.key, {
50
+ countRef: true,
51
+ refId,
52
+ args: target.args
53
+ });
54
+ }
55
+ function isArray(input) {
56
+ return Array.isArray(input);
57
+ }
58
+ function watchImpl(blocsOrBloc, callback) {
59
+ const isSingle = !isArray(blocsOrBloc);
60
+ const inputs = isSingle ? [blocsOrBloc] : blocsOrBloc;
61
+ const registry = require_config.getRegistry();
62
+ const targets = inputs.map(toWatchTarget);
63
+ const refIds = targets.map(() => `_watch_${watchRefSeq++}`);
64
+ let disposed = false;
65
+ const instances = targets.map((target, i) => resolveBloc(target, refIds[i]));
66
+ const channelUnsubs = [];
67
+ const disposedUnsubs = [];
68
+ const cleanup = () => {
69
+ if (disposed) return;
70
+ disposed = true;
71
+ for (const unsub of channelUnsubs) unsub?.();
72
+ for (const unsub of disposedUnsubs) unsub();
73
+ channelUnsubs.length = 0;
74
+ disposedUnsubs.length = 0;
75
+ for (let i = 0; i < targets.length; i++) registry.release(targets[i].blocClass, targets[i].key, false, refIds[i]);
76
+ };
77
+ const runCallback = () => {
78
+ if (disposed) return;
79
+ if (callback(isSingle ? instances[0] : instances) === STOP) cleanup();
80
+ };
81
+ const subscribeAt = (index) => {
82
+ channelUnsubs[index] = instances[index].channel.subscribe(() => _dirtytalk_structural.ALL_PATHS, runCallback);
83
+ };
84
+ const resubscribeAt = (index) => {
85
+ if (disposed) return;
86
+ instances[index] = resolveBloc(targets[index], refIds[index]);
87
+ subscribeAt(index);
88
+ runCallback();
89
+ };
90
+ for (let i = 0; i < instances.length; i++) subscribeAt(i);
91
+ for (let i = 0; i < instances.length; i++) {
92
+ const index = i;
93
+ disposedUnsubs.push(registry.on("disposed", (container) => {
94
+ if (disposed) return;
95
+ if (container !== instances[index]) return;
96
+ channelUnsubs[index]?.();
97
+ channelUnsubs[index] = void 0;
98
+ queueMicrotask(() => resubscribeAt(index));
99
+ }));
100
+ }
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-DkDGmGAx.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"watch-DkDGmGAx.cjs","names":["resolveInstanceKey","getRegistry","ALL_PATHS"],"sources":["../src/watch/watch.ts"],"sourcesContent":["import { ALL_PATHS } from '@dirtytalk/structural';\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 runCallback();\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 const index = i;\n disposedUnsubs.push(\n registry.on('disposed', (container) => {\n if (disposed) return;\n if (container !== instances[index]) return;\n channelUnsubs[index]?.();\n channelUnsubs[index] = undefined;\n queueMicrotask(() => resubscribeAt(index));\n }),\n );\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":";;;;AASA,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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O,MAAM;EAC5D,YAAY,KAAK;EACjB,YAAY;CACd;CAEA,KAAK,IAAI,IAAI,GAAG,IAAI,UAAU,QAAQ,KACpC,YAAY,CAAC;CAGf,KAAK,IAAI,IAAI,GAAG,IAAI,UAAU,QAAQ,KAAK;EACzC,MAAM,QAAQ;EACd,eAAe,KACb,SAAS,GAAG,aAAa,cAAc;GACrC,IAAI,UAAU;GACd,IAAI,cAAc,UAAU,QAAQ;GACpC,cAAc,SAAS;GACvB,cAAc,SAAS,KAAA;GACvB,qBAAqB,cAAc,KAAK,CAAC;EAC3C,CAAC,CACH;CACF;CAGA,YAAY;CAEZ,OAAO;AACT;AAEA,MAAa,QAAiB,OAAO,OAAO,WAAW,EAAE,KAAK,CAAC"}
@@ -0,0 +1,119 @@
1
+ const require_config = require("./config-hWTayX76.cjs");
2
+ const require_acquire = require("./acquire-C7beGI55.cjs");
3
+ let _dirtytalk_structural = require("@dirtytalk/structural");
4
+ //#region src/watch/watch.ts
5
+ const STOP = Symbol("watch.STOP");
6
+ const BLOC_REF_MARKER = Symbol("BlocRef");
7
+ /**
8
+ * Create a reference to a specific bloc instance, identified by its `args`.
9
+ * The key is derived the same way `useBloc`/`acquire` derive it.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * watch(instance(UserBloc, { userId: 'user-123' }), (userBloc) => {
14
+ * console.log(userBloc.state.name);
15
+ * });
16
+ * ```
17
+ */
18
+ function instance(BlocClass, args) {
19
+ return {
20
+ [BLOC_REF_MARKER]: true,
21
+ blocClass: BlocClass,
22
+ instanceId: require_acquire.resolveInstanceKey(BlocClass, args),
23
+ args
24
+ };
25
+ }
26
+ function isBlocRef(input) {
27
+ return typeof input === "object" && input !== null && BLOC_REF_MARKER in input;
28
+ }
29
+ function toWatchTarget(input) {
30
+ const registry = require_config.getRegistry();
31
+ if (isBlocRef(input)) return {
32
+ blocClass: input.blocClass,
33
+ key: input.instanceId,
34
+ args: input.args
35
+ };
36
+ return {
37
+ blocClass: input,
38
+ key: registry.resolveKey(input, void 0, void 0),
39
+ args: void 0
40
+ };
41
+ }
42
+ let watchRefSeq = 0;
43
+ /**
44
+ * Acquire a real ref (countRef: true) for the target, carrying its `args`
45
+ * through so a not-yet-created instance is initialized correctly. The
46
+ * caller is responsible for releasing `refId` in cleanup.
47
+ */
48
+ function resolveBloc(target, refId) {
49
+ return require_config.getRegistry().acquire(target.blocClass, target.key, {
50
+ countRef: true,
51
+ refId,
52
+ args: target.args
53
+ });
54
+ }
55
+ function isArray(input) {
56
+ return Array.isArray(input);
57
+ }
58
+ function watchImpl(blocsOrBloc, callback) {
59
+ const isSingle = !isArray(blocsOrBloc);
60
+ const inputs = isSingle ? [blocsOrBloc] : blocsOrBloc;
61
+ const registry = require_config.getRegistry();
62
+ const targets = inputs.map(toWatchTarget);
63
+ const refIds = targets.map(() => `_watch_${watchRefSeq++}`);
64
+ let disposed = false;
65
+ const instances = targets.map((target, i) => resolveBloc(target, refIds[i]));
66
+ const channelUnsubs = [];
67
+ const disposedUnsubs = [];
68
+ const cleanup = () => {
69
+ if (disposed) return;
70
+ disposed = true;
71
+ for (const unsub of channelUnsubs) unsub?.();
72
+ for (const unsub of disposedUnsubs) unsub();
73
+ channelUnsubs.length = 0;
74
+ disposedUnsubs.length = 0;
75
+ for (let i = 0; i < targets.length; i++) registry.release(targets[i].blocClass, targets[i].key, false, refIds[i]);
76
+ };
77
+ const runCallback = () => {
78
+ if (disposed) return;
79
+ if (callback(isSingle ? instances[0] : instances) === STOP) cleanup();
80
+ };
81
+ const subscribeAt = (index) => {
82
+ channelUnsubs[index] = instances[index].channel.subscribe(() => _dirtytalk_structural.ALL_PATHS, runCallback);
83
+ };
84
+ const resubscribeAt = (index) => {
85
+ if (disposed) return;
86
+ instances[index] = resolveBloc(targets[index], refIds[index]);
87
+ subscribeAt(index);
88
+ runCallback();
89
+ };
90
+ for (let i = 0; i < instances.length; i++) subscribeAt(i);
91
+ for (let i = 0; i < instances.length; i++) {
92
+ const index = i;
93
+ disposedUnsubs.push(registry.on("disposed", (container) => {
94
+ if (disposed) return;
95
+ if (container !== instances[index]) return;
96
+ channelUnsubs[index]?.();
97
+ channelUnsubs[index] = void 0;
98
+ queueMicrotask(() => resubscribeAt(index));
99
+ }));
100
+ }
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-DztcJuUy.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"watch-DztcJuUy.cjs","names":["resolveInstanceKey","getRegistry","ALL_PATHS"],"sources":["../src/watch/watch.ts"],"sourcesContent":["import { ALL_PATHS } from '@dirtytalk/structural';\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 runCallback();\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 const index = i;\n disposedUnsubs.push(\n registry.on('disposed', (container) => {\n if (disposed) return;\n if (container !== instances[index]) return;\n channelUnsubs[index]?.();\n channelUnsubs[index] = undefined;\n queueMicrotask(() => resubscribeAt(index));\n }),\n );\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":";;;;AASA,MAAM,OAAsB,OAAO,YAAY;AAG/C,MAAM,kBAAkB,OAAO,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EACvB,eAAe,SAAS;EACxB,KAAK,IAAI,IAAI,GAAG,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,YAAY;CACd;CAEA,KAAK,IAAI,IAAI,GAAG,IAAI,UAAU,QAAQ,KACpC,YAAY,CAAC;CAGf,KAAK,IAAI,IAAI,GAAG,IAAI,UAAU,QAAQ,KAAK;EACzC,MAAM,QAAQ;EACd,eAAe,KACb,SAAS,GAAG,aAAa,cAAc;GACrC,IAAI,UAAU;GACd,IAAI,cAAc,UAAU,QAAQ;GACpC,cAAc,SAAS;GACvB,cAAc,SAAS,KAAA;GACvB,qBAAqB,cAAc,KAAK,CAAC;EAC3C,CAAC,CACH;CACF;CAGA,YAAY;CAEZ,OAAO;AACT;AAEA,MAAa,QAAiB,OAAO,OAAO,WAAW,EAAE,KAAK,CAAC"}
@@ -0,0 +1,108 @@
1
+ import { t as getRegistry } from "./config-D6BoWWPv.js";
2
+ import { n as resolveInstanceKey } from "./acquire-DXEQDZGr.js";
3
+ import { ALL_PATHS } from "@dirtytalk/structural";
4
+ //#region src/watch/watch.ts
5
+ const STOP = Symbol("watch.STOP");
6
+ const BLOC_REF_MARKER = Symbol("BlocRef");
7
+ /**
8
+ * Create a reference to a specific bloc instance, identified by its `args`.
9
+ * The key is derived the same way `useBloc`/`acquire` derive it.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * watch(instance(UserBloc, { userId: 'user-123' }), (userBloc) => {
14
+ * console.log(userBloc.state.name);
15
+ * });
16
+ * ```
17
+ */
18
+ function instance(BlocClass, args) {
19
+ return {
20
+ [BLOC_REF_MARKER]: true,
21
+ blocClass: BlocClass,
22
+ instanceId: resolveInstanceKey(BlocClass, args),
23
+ args
24
+ };
25
+ }
26
+ function isBlocRef(input) {
27
+ return typeof input === "object" && input !== null && BLOC_REF_MARKER in input;
28
+ }
29
+ function toWatchTarget(input) {
30
+ const registry = getRegistry();
31
+ if (isBlocRef(input)) return {
32
+ blocClass: input.blocClass,
33
+ key: input.instanceId,
34
+ args: input.args
35
+ };
36
+ return {
37
+ blocClass: input,
38
+ key: registry.resolveKey(input, void 0, void 0),
39
+ args: void 0
40
+ };
41
+ }
42
+ let watchRefSeq = 0;
43
+ /**
44
+ * Acquire a real ref (countRef: true) for the target, carrying its `args`
45
+ * through so a not-yet-created instance is initialized correctly. The
46
+ * caller is responsible for releasing `refId` in cleanup.
47
+ */
48
+ function resolveBloc(target, refId) {
49
+ return getRegistry().acquire(target.blocClass, target.key, {
50
+ countRef: true,
51
+ refId,
52
+ args: target.args
53
+ });
54
+ }
55
+ function isArray(input) {
56
+ return Array.isArray(input);
57
+ }
58
+ function watchImpl(blocsOrBloc, callback) {
59
+ const isSingle = !isArray(blocsOrBloc);
60
+ const inputs = isSingle ? [blocsOrBloc] : blocsOrBloc;
61
+ const registry = getRegistry();
62
+ const targets = inputs.map(toWatchTarget);
63
+ const refIds = targets.map(() => `_watch_${watchRefSeq++}`);
64
+ let disposed = false;
65
+ const instances = targets.map((target, i) => resolveBloc(target, refIds[i]));
66
+ const channelUnsubs = [];
67
+ const disposedUnsubs = [];
68
+ const cleanup = () => {
69
+ if (disposed) return;
70
+ disposed = true;
71
+ for (const unsub of channelUnsubs) unsub?.();
72
+ for (const unsub of disposedUnsubs) unsub();
73
+ channelUnsubs.length = 0;
74
+ disposedUnsubs.length = 0;
75
+ for (let i = 0; i < targets.length; i++) registry.release(targets[i].blocClass, targets[i].key, false, refIds[i]);
76
+ };
77
+ const runCallback = () => {
78
+ if (disposed) return;
79
+ if (callback(isSingle ? instances[0] : instances) === STOP) cleanup();
80
+ };
81
+ const subscribeAt = (index) => {
82
+ channelUnsubs[index] = instances[index].channel.subscribe(() => ALL_PATHS, runCallback);
83
+ };
84
+ const resubscribeAt = (index) => {
85
+ if (disposed) return;
86
+ instances[index] = resolveBloc(targets[index], refIds[index]);
87
+ subscribeAt(index);
88
+ runCallback();
89
+ };
90
+ for (let i = 0; i < instances.length; i++) subscribeAt(i);
91
+ for (let i = 0; i < instances.length; i++) {
92
+ const index = i;
93
+ disposedUnsubs.push(registry.on("disposed", (container) => {
94
+ if (disposed) return;
95
+ if (container !== instances[index]) return;
96
+ channelUnsubs[index]?.();
97
+ channelUnsubs[index] = void 0;
98
+ queueMicrotask(() => resubscribeAt(index));
99
+ }));
100
+ }
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-IrR8G28N.js.map