@blac/core 2.0.18 → 2.0.20
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-C0dCNlTF.js +1005 -0
- package/dist/StateContainerRegistry-C0dCNlTF.js.map +1 -0
- package/dist/StateContainerRegistry-CS18y4au.cjs +1163 -0
- package/dist/StateContainerRegistry-CS18y4au.cjs.map +1 -0
- package/dist/StateContainerRegistry-CchRjnjX.cjs +1094 -0
- package/dist/StateContainerRegistry-CchRjnjX.cjs.map +1 -0
- package/dist/StateContainerRegistry-D8nu7Z0T.js +1068 -0
- package/dist/StateContainerRegistry-D8nu7Z0T.js.map +1 -0
- package/dist/StateContainerRegistry-D9BIRjwv.cjs +1088 -0
- package/dist/StateContainerRegistry-D9BIRjwv.cjs.map +1 -0
- package/dist/StateContainerRegistry-DVIBTxGZ.js +999 -0
- package/dist/StateContainerRegistry-DVIBTxGZ.js.map +1 -0
- package/dist/StateContainerRegistry-Dj77cok9.js +1005 -0
- package/dist/StateContainerRegistry-Dj77cok9.js.map +1 -0
- package/dist/StateContainerRegistry-lIwo-0m3.cjs +1094 -0
- package/dist/StateContainerRegistry-lIwo-0m3.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-BRIJVJaA.js +35 -0
- package/dist/acquire-BRIJVJaA.js.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-C3nOb3XR.cjs +46 -0
- package/dist/acquire-C3nOb3XR.cjs.map +1 -0
- package/dist/acquire-C4WAS13G.cjs +46 -0
- package/dist/acquire-C4WAS13G.cjs.map +1 -0
- package/dist/acquire-C7beGI55.cjs +46 -0
- package/dist/acquire-C7beGI55.cjs.map +1 -0
- package/dist/acquire-DXEQDZGr.js +35 -0
- package/dist/acquire-DXEQDZGr.js.map +1 -0
- package/dist/acquire-Dg-uXwjF.js +35 -0
- package/dist/acquire-Dg-uXwjF.js.map +1 -0
- package/dist/config-B1l8ZGlU.cjs +32 -0
- package/dist/config-B1l8ZGlU.cjs.map +1 -0
- package/dist/config-CzcoAvBo.js +16 -0
- package/dist/config-CzcoAvBo.js.map +1 -0
- package/dist/config-D6BoWWPv.js +16 -0
- package/dist/config-D6BoWWPv.js.map +1 -0
- package/dist/config-DOO82lbZ.js +16 -0
- package/dist/config-DOO82lbZ.js.map +1 -0
- package/dist/config-DVM_ESQt.js +16 -0
- package/dist/config-DVM_ESQt.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-cwaTn5oy.cjs +32 -0
- package/dist/config-cwaTn5oy.cjs.map +1 -0
- package/dist/config-d9_U58ef.cjs +32 -0
- package/dist/config-d9_U58ef.cjs.map +1 -0
- package/dist/config-hWTayX76.cjs +32 -0
- package/dist/config-hWTayX76.cjs.map +1 -0
- package/dist/core/StateContainer.d.ts +7 -1
- package/dist/core/StateContainerRegistry.d.ts +31 -3
- package/dist/core/symbols.d.ts +6 -0
- package/dist/debug.cjs +3 -3
- package/dist/debug.js +3 -3
- package/dist/ensure-B1dEDsZV.cjs +559 -0
- package/dist/ensure-B1dEDsZV.cjs.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-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-Bm91o37h.js +536 -0
- package/dist/ensure-Bm91o37h.js.map +1 -0
- package/dist/ensure-CQimeD0I.cjs +559 -0
- package/dist/ensure-CQimeD0I.cjs.map +1 -0
- package/dist/ensure-DTTTd2h8.js +536 -0
- package/dist/ensure-DTTTd2h8.js.map +1 -0
- package/dist/ensure-VuiNPccZ.cjs +559 -0
- package/dist/ensure-VuiNPccZ.cjs.map +1 -0
- package/dist/ensure-vxAGa2v3.js +536 -0
- package/dist/ensure-vxAGa2v3.js.map +1 -0
- package/dist/index.cjs +7 -6
- package/dist/index.d.cts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +7 -7
- package/dist/management-AFuqQEwz.js +38 -0
- package/dist/management-AFuqQEwz.js.map +1 -0
- package/dist/management-BodVLAfJ.cjs +85 -0
- package/dist/management-BodVLAfJ.cjs.map +1 -0
- package/dist/management-C-WGcwPD.js +38 -0
- package/dist/management-C-WGcwPD.js.map +1 -0
- package/dist/management-C2wv4vGS.cjs +85 -0
- package/dist/management-C2wv4vGS.cjs.map +1 -0
- package/dist/management-D2pjr38g.js +38 -0
- package/dist/management-D2pjr38g.js.map +1 -0
- package/dist/management-D5WsZoTV.js +38 -0
- package/dist/management-D5WsZoTV.js.map +1 -0
- package/dist/management-DwO0nBzL.cjs +85 -0
- package/dist/management-DwO0nBzL.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-iE4x6IQY.cjs +85 -0
- package/dist/management-iE4x6IQY.cjs.map +1 -0
- package/dist/plugin/PluginManager.d.ts +14 -1
- package/dist/plugins.cjs +1 -1
- package/dist/plugins.js +1 -1
- package/dist/testing.cjs +4 -4
- package/dist/testing.js +4 -4
- package/dist/utils/idGenerator.d.ts +0 -36
- package/dist/watch/watch.d.ts +6 -0
- package/dist/watch-B0pUfh-v.cjs +119 -0
- package/dist/watch-B0pUfh-v.cjs.map +1 -0
- package/dist/watch-BHfCtbn1.cjs +119 -0
- package/dist/watch-BHfCtbn1.cjs.map +1 -0
- package/dist/watch-Bx0y-Kcc.js +108 -0
- package/dist/watch-Bx0y-Kcc.js.map +1 -0
- package/dist/watch-D01MdwKy.js +108 -0
- package/dist/watch-D01MdwKy.js.map +1 -0
- package/dist/watch-DkDGmGAx.cjs +119 -0
- package/dist/watch-DkDGmGAx.cjs.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-DztcJuUy.cjs +119 -0
- package/dist/watch-DztcJuUy.cjs.map +1 -0
- package/dist/watch-IrR8G28N.js +108 -0
- package/dist/watch-IrR8G28N.js.map +1 -0
- package/dist/watch-entry.cjs +1 -1
- package/dist/watch-entry.js +1 -1
- package/dist/watch-hx0r92Zc.js +108 -0
- package/dist/watch-hx0r92Zc.js.map +1 -0
- package/package.json +2 -2
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"management-QWLpbXlW.cjs","names":["getRegistry"],"sources":["../src/registry/queries.ts","../src/registry/management.ts"],"sourcesContent":["import { getRegistry } from './config';\nimport type {\n ExtractArgs,\n StateContainerConstructor,\n InstanceReadonlyState,\n} from '../types/utilities';\n\nexport function hasInstance<T extends StateContainerConstructor>(\n BlocClass: T,\n opts?: { args?: ExtractArgs<T> },\n): boolean {\n const registry = getRegistry();\n const key = registry.resolveKey(BlocClass, undefined, opts?.args);\n return registry.hasInstance(BlocClass, key);\n}\n\nexport function getRefCount<T extends StateContainerConstructor>(\n BlocClass: T,\n opts?: { args?: ExtractArgs<T> },\n): number {\n const registry = getRegistry();\n const key = registry.resolveKey(BlocClass, undefined, opts?.args);\n return registry.getRefCount(BlocClass, key);\n}\n\nexport function getRefIds<T extends StateContainerConstructor>(\n BlocClass: T,\n opts?: { args?: ExtractArgs<T> },\n): string[] {\n const registry = getRegistry();\n const key = registry.resolveKey(BlocClass, undefined, opts?.args);\n return registry.getRefIds(BlocClass, key);\n}\n\nexport function getAll<T extends StateContainerConstructor>(\n BlocClass: T,\n): InstanceReadonlyState<T>[] {\n return getRegistry().getAll(BlocClass);\n}\n\nexport function forEach<T extends StateContainerConstructor>(\n BlocClass: T,\n callback: (instance: InstanceReadonlyState<T>) => void,\n): void {\n getRegistry().forEach(BlocClass, callback);\n}\n","import { getRegistry } from './config';\nimport type { StateContainerConstructor } from '../types/utilities';\n\nexport function clear<T extends StateContainerConstructor>(BlocClass: T): void {\n getRegistry().clear(BlocClass);\n}\n\nexport function clearAll(): void {\n getRegistry().clearAll();\n}\n\nexport function register<T extends StateContainerConstructor>(\n BlocClass: T,\n): void {\n getRegistry().register(BlocClass);\n}\n"],"mappings":";;AAOA,SAAgB,YACd,WACA,MACS;CACT,MAAM,WAAWA,eAAAA,YAAY;CAC7B,MAAM,MAAM,SAAS,WAAW,WAAW,KAAA,GAAW,MAAM,IAAI;CAChE,OAAO,SAAS,YAAY,WAAW,GAAG;AAC5C;AAEA,SAAgB,YACd,WACA,MACQ;CACR,MAAM,WAAWA,eAAAA,YAAY;CAC7B,MAAM,MAAM,SAAS,WAAW,WAAW,KAAA,GAAW,MAAM,IAAI;CAChE,OAAO,SAAS,YAAY,WAAW,GAAG;AAC5C;AAEA,SAAgB,UACd,WACA,MACU;CACV,MAAM,WAAWA,eAAAA,YAAY;CAC7B,MAAM,MAAM,SAAS,WAAW,WAAW,KAAA,GAAW,MAAM,IAAI;CAChE,OAAO,SAAS,UAAU,WAAW,GAAG;AAC1C;AAEA,SAAgB,OACd,WAC4B;CAC5B,OAAOA,eAAAA,YAAY,EAAE,OAAO,SAAS;AACvC;AAEA,SAAgB,QACd,WACA,UACM;CACN,eAAA,YAAY,EAAE,QAAQ,WAAW,QAAQ;AAC3C;;;AC1CA,SAAgB,MAA2C,WAAoB;CAC7E,eAAA,YAAY,EAAE,MAAM,SAAS;AAC/B;AAEA,SAAgB,WAAiB;CAC/B,eAAA,YAAY,EAAE,SAAS;AACzB;AAEA,SAAgB,SACd,WACM;CACN,eAAA,YAAY,EAAE,SAAS,SAAS;AAClC"}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
const require_config = require("./config-cwaTn5oy.cjs");
|
|
2
|
+
//#region src/registry/queries.ts
|
|
3
|
+
function hasInstance(BlocClass, opts) {
|
|
4
|
+
const registry = require_config.getRegistry();
|
|
5
|
+
const key = registry.resolveKey(BlocClass, void 0, opts?.args);
|
|
6
|
+
return registry.hasInstance(BlocClass, key);
|
|
7
|
+
}
|
|
8
|
+
function getRefCount(BlocClass, opts) {
|
|
9
|
+
const registry = require_config.getRegistry();
|
|
10
|
+
const key = registry.resolveKey(BlocClass, void 0, opts?.args);
|
|
11
|
+
return registry.getRefCount(BlocClass, key);
|
|
12
|
+
}
|
|
13
|
+
function getRefIds(BlocClass, opts) {
|
|
14
|
+
const registry = require_config.getRegistry();
|
|
15
|
+
const key = registry.resolveKey(BlocClass, void 0, opts?.args);
|
|
16
|
+
return registry.getRefIds(BlocClass, key);
|
|
17
|
+
}
|
|
18
|
+
function getAll(BlocClass) {
|
|
19
|
+
return require_config.getRegistry().getAll(BlocClass);
|
|
20
|
+
}
|
|
21
|
+
function forEach(BlocClass, callback) {
|
|
22
|
+
require_config.getRegistry().forEach(BlocClass, callback);
|
|
23
|
+
}
|
|
24
|
+
//#endregion
|
|
25
|
+
//#region src/registry/management.ts
|
|
26
|
+
function clear(BlocClass) {
|
|
27
|
+
require_config.getRegistry().clear(BlocClass);
|
|
28
|
+
}
|
|
29
|
+
function clearAll() {
|
|
30
|
+
require_config.getRegistry().clearAll();
|
|
31
|
+
}
|
|
32
|
+
function register(BlocClass) {
|
|
33
|
+
require_config.getRegistry().register(BlocClass);
|
|
34
|
+
}
|
|
35
|
+
//#endregion
|
|
36
|
+
Object.defineProperty(exports, "clear", {
|
|
37
|
+
enumerable: true,
|
|
38
|
+
get: function() {
|
|
39
|
+
return clear;
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
Object.defineProperty(exports, "clearAll", {
|
|
43
|
+
enumerable: true,
|
|
44
|
+
get: function() {
|
|
45
|
+
return clearAll;
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
Object.defineProperty(exports, "forEach", {
|
|
49
|
+
enumerable: true,
|
|
50
|
+
get: function() {
|
|
51
|
+
return forEach;
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
Object.defineProperty(exports, "getAll", {
|
|
55
|
+
enumerable: true,
|
|
56
|
+
get: function() {
|
|
57
|
+
return getAll;
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
Object.defineProperty(exports, "getRefCount", {
|
|
61
|
+
enumerable: true,
|
|
62
|
+
get: function() {
|
|
63
|
+
return getRefCount;
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
Object.defineProperty(exports, "getRefIds", {
|
|
67
|
+
enumerable: true,
|
|
68
|
+
get: function() {
|
|
69
|
+
return getRefIds;
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
Object.defineProperty(exports, "hasInstance", {
|
|
73
|
+
enumerable: true,
|
|
74
|
+
get: function() {
|
|
75
|
+
return hasInstance;
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
Object.defineProperty(exports, "register", {
|
|
79
|
+
enumerable: true,
|
|
80
|
+
get: function() {
|
|
81
|
+
return register;
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
//# sourceMappingURL=management-iE4x6IQY.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"management-iE4x6IQY.cjs","names":["getRegistry"],"sources":["../src/registry/queries.ts","../src/registry/management.ts"],"sourcesContent":["import { getRegistry } from './config';\nimport type {\n ExtractArgs,\n StateContainerConstructor,\n InstanceReadonlyState,\n} from '../types/utilities';\n\nexport function hasInstance<T extends StateContainerConstructor>(\n BlocClass: T,\n opts?: { args?: ExtractArgs<T> },\n): boolean {\n const registry = getRegistry();\n const key = registry.resolveKey(BlocClass, undefined, opts?.args);\n return registry.hasInstance(BlocClass, key);\n}\n\nexport function getRefCount<T extends StateContainerConstructor>(\n BlocClass: T,\n opts?: { args?: ExtractArgs<T> },\n): number {\n const registry = getRegistry();\n const key = registry.resolveKey(BlocClass, undefined, opts?.args);\n return registry.getRefCount(BlocClass, key);\n}\n\nexport function getRefIds<T extends StateContainerConstructor>(\n BlocClass: T,\n opts?: { args?: ExtractArgs<T> },\n): string[] {\n const registry = getRegistry();\n const key = registry.resolveKey(BlocClass, undefined, opts?.args);\n return registry.getRefIds(BlocClass, key);\n}\n\nexport function getAll<T extends StateContainerConstructor>(\n BlocClass: T,\n): InstanceReadonlyState<T>[] {\n return getRegistry().getAll(BlocClass);\n}\n\nexport function forEach<T extends StateContainerConstructor>(\n BlocClass: T,\n callback: (instance: InstanceReadonlyState<T>) => void,\n): void {\n getRegistry().forEach(BlocClass, callback);\n}\n","import { getRegistry } from './config';\nimport type { StateContainerConstructor } from '../types/utilities';\n\nexport function clear<T extends StateContainerConstructor>(BlocClass: T): void {\n getRegistry().clear(BlocClass);\n}\n\nexport function clearAll(): void {\n getRegistry().clearAll();\n}\n\nexport function register<T extends StateContainerConstructor>(\n BlocClass: T,\n): void {\n getRegistry().register(BlocClass);\n}\n"],"mappings":";;AAOA,SAAgB,YACd,WACA,MACS;CACT,MAAM,WAAWA,eAAAA,YAAY;CAC7B,MAAM,MAAM,SAAS,WAAW,WAAW,KAAA,GAAW,MAAM,IAAI;CAChE,OAAO,SAAS,YAAY,WAAW,GAAG;AAC5C;AAEA,SAAgB,YACd,WACA,MACQ;CACR,MAAM,WAAWA,eAAAA,YAAY;CAC7B,MAAM,MAAM,SAAS,WAAW,WAAW,KAAA,GAAW,MAAM,IAAI;CAChE,OAAO,SAAS,YAAY,WAAW,GAAG;AAC5C;AAEA,SAAgB,UACd,WACA,MACU;CACV,MAAM,WAAWA,eAAAA,YAAY;CAC7B,MAAM,MAAM,SAAS,WAAW,WAAW,KAAA,GAAW,MAAM,IAAI;CAChE,OAAO,SAAS,UAAU,WAAW,GAAG;AAC1C;AAEA,SAAgB,OACd,WAC4B;CAC5B,OAAOA,eAAAA,YAAY,EAAE,OAAO,SAAS;AACvC;AAEA,SAAgB,QACd,WACA,UACM;CACN,eAAA,YAAY,EAAE,QAAQ,WAAW,QAAQ;AAC3C;;;AC1CA,SAAgB,MAA2C,WAAoB;CAC7E,eAAA,YAAY,EAAE,MAAM,SAAS;AAC/B;AAEA,SAAgB,WAAiB;CAC/B,eAAA,YAAY,EAAE,SAAS;AACzB;AAEA,SAAgB,SACd,WACM;CACN,eAAA,YAAY,EAAE,SAAS,SAAS;AAClC"}
|
|
@@ -98,6 +98,17 @@ export declare class PluginManager {
|
|
|
98
98
|
*/
|
|
99
99
|
private attachStateBridge;
|
|
100
100
|
private detachStateBridge;
|
|
101
|
+
/**
|
|
102
|
+
* Backfill a newly-installed plugin with the app's existing instances.
|
|
103
|
+
*
|
|
104
|
+
* Iterates every registered `Type` and its live instances (`getAll`
|
|
105
|
+
* already skips disposed), attaching the state bridge (idempotent — see
|
|
106
|
+
* `attachStateBridge`) and, if the plugin implements `onCreated`, invoking
|
|
107
|
+
* it directly for each instance. Scoped to this single plugin only — the
|
|
108
|
+
* broadcast `notifyPlugins` would re-notify *every* plugin of *every*
|
|
109
|
+
* instance, which is wrong here.
|
|
110
|
+
*/
|
|
111
|
+
private backfillPlugin;
|
|
101
112
|
/**
|
|
102
113
|
* Channel-flush callback. Snapshots `next`, hands `(prev, next, paths)`
|
|
103
114
|
* to every enabled plugin's `onStateChange`, then updates `prevState`
|
|
@@ -113,7 +124,9 @@ export declare class PluginManager {
|
|
|
113
124
|
*
|
|
114
125
|
* Builds a fresh `PluginContext` per dispatch (instance becomes
|
|
115
126
|
* `ctx.container`), so plugins can reach the focal bloc through
|
|
116
|
-
* `ctx.container`.
|
|
127
|
+
* `ctx.container`. The context is built lazily on the first matching
|
|
128
|
+
* hook and reused for every other plugin in this dispatch — zero
|
|
129
|
+
* builds if no enabled plugin implements `hookName`.
|
|
117
130
|
*/
|
|
118
131
|
private notifyPlugins;
|
|
119
132
|
/**
|
package/dist/plugins.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_StateContainerRegistry = require("./StateContainerRegistry-
|
|
2
|
+
const require_StateContainerRegistry = require("./StateContainerRegistry-CS18y4au.cjs");
|
|
3
3
|
exports.PluginManager = require_StateContainerRegistry.PluginManager;
|
|
4
4
|
exports.getPluginManager = require_StateContainerRegistry.getPluginManager;
|
package/dist/plugins.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { f as PluginManager, n as getPluginManager } from "./StateContainerRegistry-D8nu7Z0T.js";
|
|
2
2
|
export { PluginManager, getPluginManager };
|
package/dist/testing.cjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_StateContainerRegistry = require("./StateContainerRegistry-
|
|
3
|
-
const require_ensure = require("./ensure-
|
|
4
|
-
const require_config = require("./config-
|
|
5
|
-
const require_acquire = require("./acquire-
|
|
2
|
+
const require_StateContainerRegistry = require("./StateContainerRegistry-CS18y4au.cjs");
|
|
3
|
+
const require_ensure = require("./ensure-B2DYCbbI.cjs");
|
|
4
|
+
const require_config = require("./config-DWUq7zEQ.cjs");
|
|
5
|
+
const require_acquire = require("./acquire-BJ3xUEmh.cjs");
|
|
6
6
|
//#region src/testing.ts
|
|
7
7
|
/** Synthetic owner id used by test helpers for pre-wired deps. */
|
|
8
8
|
const TESTING_DEPS_OWNER = "testing-deps";
|
package/dist/testing.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { a as INIT_CONFIG, i as APPLY_DEPS, t as StateContainerRegistry } from "./StateContainerRegistry-
|
|
2
|
-
import { n as Cubit, t as ensure } from "./ensure-
|
|
3
|
-
import { r as setRegistry, t as getRegistry } from "./config-
|
|
4
|
-
import { n as resolveInstanceKey } from "./acquire-
|
|
1
|
+
import { a as INIT_CONFIG, i as APPLY_DEPS, t as StateContainerRegistry } from "./StateContainerRegistry-D8nu7Z0T.js";
|
|
2
|
+
import { n as Cubit, t as ensure } from "./ensure-BSJHkUNX.js";
|
|
3
|
+
import { r as setRegistry, t as getRegistry } from "./config-DXI3kzmA.js";
|
|
4
|
+
import { n as resolveInstanceKey } from "./acquire-B1LYXSGj.js";
|
|
5
5
|
//#region src/testing.ts
|
|
6
6
|
/** Synthetic owner id used by test helpers for pre-wired deps. */
|
|
7
7
|
const TESTING_DEPS_OWNER = "testing-deps";
|
|
@@ -4,37 +4,6 @@
|
|
|
4
4
|
* Provides consistent, collision-resistant ID generation for all BlaC subsystems.
|
|
5
5
|
* Uses timestamp + counter + random suffix for uniqueness.
|
|
6
6
|
*/
|
|
7
|
-
/**
|
|
8
|
-
* Creates an ID generator with isolated counter state
|
|
9
|
-
*
|
|
10
|
-
* @param prefix - Prefix for generated IDs
|
|
11
|
-
* @returns Object with next(), nextSimple(), and reset() methods
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* ```ts
|
|
15
|
-
* const generator = createIdGenerator('sub');
|
|
16
|
-
* const id1 = generator.next(); // "sub:1698765432100_1_a3k9d7f2q"
|
|
17
|
-
* const id2 = generator.next(); // "sub:1698765432101_2_b4n8e9g3r"
|
|
18
|
-
* ```
|
|
19
|
-
*/
|
|
20
|
-
export declare function createIdGenerator(prefix: string): {
|
|
21
|
-
next: () => string;
|
|
22
|
-
nextSimple: () => string;
|
|
23
|
-
reset: () => void;
|
|
24
|
-
};
|
|
25
|
-
/**
|
|
26
|
-
* Generate ID with timestamp, counter, and random suffix (tree-shakeable)
|
|
27
|
-
*
|
|
28
|
-
* Format: `${prefix}:${timestamp}_${counter}_${random}`
|
|
29
|
-
*
|
|
30
|
-
* @param prefix - Prefix for the ID (e.g., 'sub', 'consumer', 'stage')
|
|
31
|
-
* @returns Branded ID string
|
|
32
|
-
*
|
|
33
|
-
* @example
|
|
34
|
-
* const id = generateId('sub');
|
|
35
|
-
* // Returns: "sub:1698765432100_1_a3k9d7f2q"
|
|
36
|
-
*/
|
|
37
|
-
export declare function generateId(prefix: string): string;
|
|
38
7
|
/**
|
|
39
8
|
* Generate simple ID with timestamp and random (no counter tracking)
|
|
40
9
|
*
|
|
@@ -50,8 +19,3 @@ export declare function generateId(prefix: string): string;
|
|
|
50
19
|
* ```
|
|
51
20
|
*/
|
|
52
21
|
export declare function generateSimpleId(prefix: string, affix?: string): string;
|
|
53
|
-
/**
|
|
54
|
-
* Reset all counters for testing purposes
|
|
55
|
-
* @internal
|
|
56
|
-
*/
|
|
57
|
-
export declare function __resetIdCounters(): void;
|
package/dist/watch/watch.d.ts
CHANGED
|
@@ -11,6 +11,12 @@ export interface BlocRef<T extends StateContainerConstructor> {
|
|
|
11
11
|
blocClass: T;
|
|
12
12
|
/** @internal The resolved instance key (derived from `args`). */
|
|
13
13
|
instanceId: string;
|
|
14
|
+
/**
|
|
15
|
+
* @internal The `args` used to derive `instanceId`. Forwarded to
|
|
16
|
+
* `registry.acquire` so a not-yet-created instance is initialized with
|
|
17
|
+
* these args instead of `undefined`.
|
|
18
|
+
*/
|
|
19
|
+
args?: unknown;
|
|
14
20
|
}
|
|
15
21
|
/**
|
|
16
22
|
* Create a reference to a specific bloc instance, identified by its `args`.
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
const require_StateContainerRegistry = require("./StateContainerRegistry-Bx2ly-ba.cjs");
|
|
2
|
+
const require_config = require("./config-cwaTn5oy.cjs");
|
|
3
|
+
const require_acquire = require("./acquire-B_p1fNEh.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-B0pUfh-v.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"watch-B0pUfh-v.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,WAAWA,eAAAA,YAAY;CAE7B,MAAM,UAAU,OAAO,IAAI,aAAa;CACxC,MAAM,SAAS,QAAQ,UAAU,UAAU,eAAe;CAE1D,IAAI,WAAW;CACf,MAAM,YAAsC,QAAQ,KAAK,QAAQ,MAC/D,YAAY,QAAQ,OAAO,EAAE,CAC/B;CACA,MAAM,gBAAiD,CAAC;CACxD,MAAM,iBAAoC,CAAC;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,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_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.\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,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,WAAWA,eAAAA,YAAY;CAE7B,MAAM,UAAU,OAAO,IAAI,aAAa;CACxC,MAAM,SAAS,QAAQ,UAAU,UAAU,eAAe;CAE1D,IAAI,WAAW;CACf,MAAM,YAAsC,QAAQ,KAAK,QAAQ,MAC/D,YAAY,QAAQ,OAAO,EAAE,CAC/B;CACA,MAAM,gBAAiD,CAAC;CACxD,MAAM,iBAAoC,CAAC;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 { o as ON_DISPOSE } from "./StateContainerRegistry-8zNFMAf7.js";
|
|
2
|
+
import { t as getRegistry } from "./config-DOO82lbZ.js";
|
|
3
|
+
import { n as resolveInstanceKey } from "./acquire-BdsVZBSe.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-Bx0y-Kcc.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"watch-Bx0y-Kcc.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;;;;;;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,UAAU,OAAO,IAAI,aAAa;CACxC,MAAM,SAAS,QAAQ,UAAU,UAAU,eAAe;CAE1D,IAAI,WAAW;CACf,MAAM,YAAsC,QAAQ,KAAK,QAAQ,MAC/D,YAAY,QAAQ,OAAO,EAAE,CAC/B;CACA,MAAM,gBAAiD,CAAC;CACxD,MAAM,iBAAoC,CAAC;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,gBACxC,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,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"}
|