cross-tab-worker-databus 0.20.71 → 0.20.86
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/CHANGELOG.md +216 -2
- package/README.md +8 -0
- package/README.zh.md +4 -0
- package/dist/centrifuge-protocol.d.ts +59 -26
- package/dist/centrifuge-protocol.d.ts.map +1 -1
- package/dist/centrifuge-session.d.ts +9 -0
- package/dist/centrifuge-session.d.ts.map +1 -1
- package/dist/centrifuge.d.ts +12 -2
- package/dist/centrifuge.d.ts.map +1 -1
- package/dist/centrifuge.js +168 -88
- package/dist/centrifuge.js.map +3 -3
- package/dist/centrifuge.shared.worker.js +146 -38
- package/dist/centrifuge.shared.worker.js.map +3 -3
- package/dist/centrifuge.worker.js +140 -33
- package/dist/centrifuge.worker.js.map +3 -3
- package/dist/{chunk-D2SIT473.js → chunk-SDOV3UHG.js} +1304 -515
- package/dist/chunk-SDOV3UHG.js.map +7 -0
- package/dist/chunk-TZ7ZP7YD.js +175 -0
- package/dist/chunk-TZ7ZP7YD.js.map +7 -0
- package/dist/cjs/centrifuge.cjs +1608 -650
- package/dist/cjs/centrifuge.cjs.map +4 -4
- package/dist/cjs/hooks.cjs +37 -2
- package/dist/cjs/hooks.cjs.map +3 -3
- package/dist/cjs/index.cjs +1692 -747
- package/dist/cjs/index.cjs.map +4 -4
- package/dist/cjs/vue.cjs +45 -2
- package/dist/cjs/vue.cjs.map +3 -3
- package/dist/core/cluster.d.ts +40 -4
- package/dist/core/cluster.d.ts.map +1 -1
- package/dist/core/data-bus.d.ts +134 -77
- package/dist/core/data-bus.d.ts.map +1 -1
- package/dist/core/dedup-manager.d.ts +98 -0
- package/dist/core/dedup-manager.d.ts.map +1 -0
- package/dist/core/environment.d.ts +43 -3
- package/dist/core/environment.d.ts.map +1 -1
- package/dist/core/replay-manager.d.ts +129 -0
- package/dist/core/replay-manager.d.ts.map +1 -0
- package/dist/core/replay-persistence.d.ts +2 -1
- package/dist/core/replay-persistence.d.ts.map +1 -1
- package/dist/core/replay-pruning.d.ts +21 -0
- package/dist/core/replay-pruning.d.ts.map +1 -0
- package/dist/core/routing.d.ts +22 -3
- package/dist/core/routing.d.ts.map +1 -1
- package/dist/core/storage-batch.d.ts +0 -4
- package/dist/core/storage-batch.d.ts.map +1 -1
- package/dist/core/trace.d.ts +55 -15
- package/dist/core/trace.d.ts.map +1 -1
- package/dist/core/types.d.ts +58 -9
- package/dist/core/types.d.ts.map +1 -1
- package/dist/core/version.d.ts +2 -0
- package/dist/core/version.d.ts.map +1 -0
- package/dist/hooks.d.ts +12 -1
- package/dist/hooks.d.ts.map +1 -1
- package/dist/hooks.js +28 -2
- package/dist/hooks.js.map +2 -2
- package/dist/index.d.ts +6 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +263 -166
- package/dist/index.js.map +3 -3
- package/dist/utils/constants.d.ts +170 -0
- package/dist/utils/constants.d.ts.map +1 -0
- package/dist/utils/error-utils.d.ts +28 -0
- package/dist/utils/error-utils.d.ts.map +1 -0
- package/dist/utils/metadata.d.ts +16 -0
- package/dist/utils/metadata.d.ts.map +1 -0
- package/dist/utils/storage-utils.d.ts +24 -0
- package/dist/utils/storage-utils.d.ts.map +1 -0
- package/dist/utils/validation.d.ts +76 -0
- package/dist/utils/validation.d.ts.map +1 -0
- package/dist/vue.d.ts +13 -1
- package/dist/vue.d.ts.map +1 -1
- package/dist/vue.js +36 -2
- package/dist/vue.js.map +2 -2
- package/dist/websocket.d.ts +6 -1
- package/dist/websocket.d.ts.map +1 -1
- package/dist/worker-mode.d.ts +6 -2
- package/dist/worker-mode.d.ts.map +1 -1
- package/docs/README.md +1 -0
- package/docs/api.md +135 -4
- package/docs/architecture.md +92 -1
- package/docs/benchmarks.md +24 -0
- package/docs/capabilities.md +24 -3
- package/docs/configuration.md +58 -0
- package/docs/getting-started.md +35 -0
- package/docs/release-checklist.md +28 -5
- package/docs/roadmap.md +81 -5
- package/docs/zh/README.md +2 -1
- package/docs/zh/api.md +134 -4
- package/docs/zh/architecture.md +61 -1
- package/docs/zh/benchmarks.md +24 -0
- package/docs/zh/capabilities.md +21 -3
- package/docs/zh/configuration.md +53 -0
- package/docs/zh/getting-started.md +35 -0
- package/docs/zh/release-checklist.md +29 -6
- package/docs/zh/roadmap.md +93 -5
- package/package.json +39 -16
- package/dist/chunk-D2SIT473.js.map +0 -7
package/dist/core/trace.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"trace.d.ts","sourceRoot":"","sources":["../../src/core/trace.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"trace.d.ts","sourceRoot":"","sources":["../../src/core/trace.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAC5C,OAAO,KAAK,EACV,qBAAqB,EACrB,gBAAgB,EAChB,qBAAqB,EACrB,mBAAmB,EACnB,kBAAkB,EAClB,sBAAsB,EACvB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAEL,gBAAgB,EAChB,UAAU,EACX,MAAM,oBAAoB,CAAC;AAE5B,uEAAuE;AACvE;;;yDAGyD;AACzD,MAAM,MAAM,gBAAgB,GAAG,CAAC,OAAO,UAAU,CAAC,CAAC,MAAM,OAAO,UAAU,CAAC,CAAC;AAE5E,oEAAoE;AACpE,MAAM,WAAW,0BAA0B;IACzC,IAAI,EAAE,OAAO,gBAAgB,CAAC,SAAS,CAAC;IACxC,MAAM,EAAE,CAAC,OAAO,sBAAsB,CAAC,CAAC,MAAM,OAAO,sBAAsB,CAAC,CAAC;IAC7E,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,4DAA4D;AAC5D,MAAM,WAAW,uBAAuB;IACtC,IAAI,EAAE,OAAO,gBAAgB,CAAC,MAAM,CAAC;IACrC,MAAM,EAAE,YAAY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C1B,OAAO,CAAC,eAAe,CAAK;gBAEhB,OAAO,CAAC,EAAE,mBAAmB,EAAE,GAAG,GAAE,MAAM,MAAiC;IASvF;uEACmE;IACnE,KAAK,IAAI,IAAI;IAOb,iEAAiE;IACjE,KAAK,IAAI,IAAI;IAOb,IAAI,IAAI,IAAI;IAKZ;;oEAEgE;IAChE,YAAY,IAAI;QAAE,SAAS,EAAE,OAAO,CAAC;QAAC,aAAa,EAAE,MAAM,CAAA;KAAE;IAI7D,4EAA4E;IAC5E,KAAK,CAAC,KAAK,EAAE,sBAAsB,GAAG,IAAI;IAK1C;;;sEAGkE;IAClE,UAAU,IAAI,sBAAsB,GAAG,IAAI;IAoB3C,gGAAgG;IAChG,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAkBnC;;;;OAIG;IACH,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAQpC;;;;;OAKG;IACH,gBAAgB,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAgBrC,iEAAiE;IACjE,mBAAmB,IAAI,IAAI;IAI3B,qBAAqB,IAAI,IAAI;IAI7B;;iFAE6E;IAC7E,OAAO,KAAK,aAAa,GAExB;IAED,uEAAuE;IACvE,KAAK,IAAI,IAAI;IAKb,OAAO,CAAC,QAAQ;IA4BhB,OAAO,CAAC,YAAY;IAYpB,OAAO,CAAC,IAAI;IAiBZ,OAAO,CAAC,QAAQ;CAWjB"}
|
package/dist/core/types.d.ts
CHANGED
|
@@ -4,7 +4,12 @@
|
|
|
4
4
|
* Defines the shapes of Worker records, topic routes, control messages,
|
|
5
5
|
* transport interfaces, trace events, and all supporting types used across
|
|
6
6
|
* the cluster coordination, DataBus public API, and diagnostics layers.
|
|
7
|
+
*
|
|
8
|
+
* The status/role/action unions are derived from the string constants in
|
|
9
|
+
* `utils/constants.ts` so the runtime values and the static types can never
|
|
10
|
+
* drift apart (no duplicate literals to keep in sync by hand).
|
|
7
11
|
*/
|
|
12
|
+
import type { CLUSTER_MESSAGE_TYPE, CONTROL_ACTION, PUBLICATION_ENVELOPE_OP, TAB_VISIBILITY, WORKER_ROLE, WORKER_STATUS } from '../utils/constants';
|
|
8
13
|
/** Convenience alias for values that may be synchronously returned or as a
|
|
9
14
|
* Promise. Used by the DataBusTransport contract so implementations can be
|
|
10
15
|
* either sync or async without changing the call signature. */
|
|
@@ -12,13 +17,13 @@ export type MaybePromise<T> = T | Promise<T>;
|
|
|
12
17
|
/** Whether this Worker can act as a Topic owner. `active` workers are eligible
|
|
13
18
|
* for new topic assignments; `standby` workers hold existing assignments but
|
|
14
19
|
* are not selected for new ones (e.g. a hidden tab that already owns topics). */
|
|
15
|
-
export type WorkerRole =
|
|
20
|
+
export type WorkerRole = (typeof WORKER_ROLE)[keyof typeof WORKER_ROLE];
|
|
16
21
|
/** Connection-level status of the transport. `error` triggers auto-recovery
|
|
17
22
|
* (subject to a cooldown); `disconnected` is a clean or suspend state. */
|
|
18
|
-
export type WorkerStatus =
|
|
23
|
+
export type WorkerStatus = (typeof WORKER_STATUS)[keyof typeof WORKER_STATUS];
|
|
19
24
|
/** Control-plane actions routed between Workers via BroadcastChannel.
|
|
20
25
|
* These are the three operations a subscriber asks the owning worker to perform. */
|
|
21
|
-
export type WorkerControlAction =
|
|
26
|
+
export type WorkerControlAction = (typeof CONTROL_ACTION)[keyof typeof CONTROL_ACTION];
|
|
22
27
|
/** Transport-neutral metadata attached to a publication. */
|
|
23
28
|
export interface DataBusPublicationMetadata {
|
|
24
29
|
/** Stable caller/server identifier propagated through the cluster and wire protocol. */
|
|
@@ -35,12 +40,45 @@ export interface DataBusPublication<TData = unknown> extends DataBusPublicationM
|
|
|
35
40
|
}
|
|
36
41
|
/** Forward-compatible JSON envelope accepted from WebSocket-style servers. */
|
|
37
42
|
export interface DataBusPublicationEnvelope<TData = unknown> {
|
|
38
|
-
op:
|
|
43
|
+
op: typeof PUBLICATION_ENVELOPE_OP;
|
|
39
44
|
publication: DataBusPublication<TData>;
|
|
40
45
|
}
|
|
41
46
|
/** Whether the tab is currently visible to the user. Only influences placement
|
|
42
47
|
* of NEW topic routes; existing routes are sticky and never migrated on hide. */
|
|
43
|
-
export type TabVisibilityState =
|
|
48
|
+
export type TabVisibilityState = (typeof TAB_VISIBILITY)[keyof typeof TAB_VISIBILITY];
|
|
49
|
+
/**
|
|
50
|
+
* A Worker's self-published registration record, written to localStorage
|
|
51
|
+
* so sibling tabs can discover and route to it.
|
|
52
|
+
*/
|
|
53
|
+
export interface WorkerThroughputSample {
|
|
54
|
+
/** Length of the sample window in ms. */
|
|
55
|
+
windowMs: number;
|
|
56
|
+
/** Publications this Worker handled within the window. */
|
|
57
|
+
messageCount: number;
|
|
58
|
+
/** Approximate payload bytes shipped by this Worker within the window. */
|
|
59
|
+
byteCount: number;
|
|
60
|
+
/** Cumulative positive scheduling overrun in ms: how much later the
|
|
61
|
+
* heartbeat ticks landed than their nominal interval. A starved event loop
|
|
62
|
+
* (the browser-observable equivalent of CPU saturation) pushes this up, so
|
|
63
|
+
* peers can steer new routes away from a throttled Worker. */
|
|
64
|
+
overrunMs: number;
|
|
65
|
+
/** Timestamp (ms) when the sample was captured. */
|
|
66
|
+
sampledAt: number;
|
|
67
|
+
}
|
|
68
|
+
/** Optional adaptive weighting for owner selection. All weights default to 0,
|
|
69
|
+
* which keeps scoring equal to the legacy pure topic-count load. When set, the
|
|
70
|
+
* per-second rates are added to the topic count, so the caller controls how
|
|
71
|
+
* much traffic activity (vs ownership breadth) should steer new routes. */
|
|
72
|
+
export interface LoadWeightingOptions {
|
|
73
|
+
/** Weight applied to messages-per-second in the effective load score. */
|
|
74
|
+
messageRateWeight?: number;
|
|
75
|
+
/** Weight applied to bytes-per-second in the effective load score. */
|
|
76
|
+
byteRateWeight?: number;
|
|
77
|
+
/** Weight applied to the scheduling-lag ratio (heartbeat overrun ÷ window).
|
|
78
|
+
* A starved event loop produces a positive ratio, steering new routes away
|
|
79
|
+
* from a Worker whose timers fire late. */
|
|
80
|
+
scheduleLagWeight?: number;
|
|
81
|
+
}
|
|
44
82
|
/**
|
|
45
83
|
* A Worker's self-published registration record, written to localStorage
|
|
46
84
|
* so sibling tabs can discover and route to it.
|
|
@@ -54,6 +92,8 @@ export interface WorkerRecord {
|
|
|
54
92
|
tabId: string;
|
|
55
93
|
/** Number of topics this Worker owns. NOT a CPU load metric. */
|
|
56
94
|
load: number;
|
|
95
|
+
/** Optional rolling traffic sample for adaptive owner weighting. */
|
|
96
|
+
throughput?: WorkerThroughputSample;
|
|
57
97
|
role: WorkerRole;
|
|
58
98
|
status: WorkerStatus;
|
|
59
99
|
visibilityState: TabVisibilityState;
|
|
@@ -107,7 +147,7 @@ export type WorkerClusterMessage<TEvent = unknown> =
|
|
|
107
147
|
* Carries the plaintext topic (for transport calls) and the opaque key
|
|
108
148
|
* (for storage/route bookkeeping). */
|
|
109
149
|
{
|
|
110
|
-
type:
|
|
150
|
+
type: typeof CLUSTER_MESSAGE_TYPE.CONTROL;
|
|
111
151
|
/** Protocol version; omitted by legacy peers and treated as version 1. */
|
|
112
152
|
protocolVersion?: number;
|
|
113
153
|
sourceWorkerId: string;
|
|
@@ -139,7 +179,7 @@ export type WorkerClusterMessage<TEvent = unknown> =
|
|
|
139
179
|
* it survives the EVENT hop so listeners can tell local dispatch from
|
|
140
180
|
* cross-tab relay, including for late subscribers replaying history. */
|
|
141
181
|
| {
|
|
142
|
-
type:
|
|
182
|
+
type: typeof CLUSTER_MESSAGE_TYPE.EVENT;
|
|
143
183
|
/** Protocol version; omitted by legacy peers and treated as version 1. */
|
|
144
184
|
protocolVersion?: number;
|
|
145
185
|
sourceWorkerId: string;
|
|
@@ -150,7 +190,7 @@ export type WorkerClusterMessage<TEvent = unknown> =
|
|
|
150
190
|
/** Nudge peers to reconcile immediately after a registry/route/subscriber
|
|
151
191
|
* write, instead of waiting for the next heartbeat (3 s default). */
|
|
152
192
|
| {
|
|
153
|
-
type:
|
|
193
|
+
type: typeof CLUSTER_MESSAGE_TYPE.REGISTRY;
|
|
154
194
|
/** Protocol version; omitted by legacy peers and treated as version 1. */
|
|
155
195
|
protocolVersion?: number;
|
|
156
196
|
sourceWorkerId: string;
|
|
@@ -158,7 +198,7 @@ export type WorkerClusterMessage<TEvent = unknown> =
|
|
|
158
198
|
/** The old owner acknowledges it released the transport subscription.
|
|
159
199
|
* Only the new owner whose route `generation` matches may act on this. */
|
|
160
200
|
| {
|
|
161
|
-
type:
|
|
201
|
+
type: typeof CLUSTER_MESSAGE_TYPE.ROUTE_RELEASED;
|
|
162
202
|
/** Protocol version; omitted by legacy peers and treated as version 1. */
|
|
163
203
|
protocolVersion?: number;
|
|
164
204
|
sourceWorkerId: string;
|
|
@@ -213,9 +253,18 @@ export interface DataBusTransport<TConfig = unknown, TData = unknown> {
|
|
|
213
253
|
unsubscribe(topic: string): MaybePromise<void>;
|
|
214
254
|
/** Publish `data` to `topic` via the server. */
|
|
215
255
|
publish(topic: string, data: unknown, options?: DataBusPublishOptions): MaybePromise<void>;
|
|
256
|
+
/** Optional burst-friendly publish: one wire frame for many items. When
|
|
257
|
+
* absent, the DataBus falls back to per-item `publish` calls. */
|
|
258
|
+
publishBatch?(topic: string, items: ReadonlyArray<DataBusPublicationItem>): MaybePromise<void>;
|
|
216
259
|
/** Close the connection and release all resources. Safe to call multiple times. */
|
|
217
260
|
stop(): MaybePromise<void>;
|
|
218
261
|
}
|
|
262
|
+
/** One entry of a batched transport publication. */
|
|
263
|
+
export interface DataBusPublicationItem {
|
|
264
|
+
data: unknown;
|
|
265
|
+
messageId?: string;
|
|
266
|
+
timestamp?: number;
|
|
267
|
+
}
|
|
219
268
|
/** Callback invoked for each publication delivered to a subscribed topic. */
|
|
220
269
|
export type DataBusMessageHandler<TData = unknown> = (message: DataBusMessage<TData>) => void;
|
|
221
270
|
/** Callback invoked on every transport status change. */
|
package/dist/core/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/core/types.ts"],"names":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
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { DependencyList } from 'react';
|
|
2
|
-
import type { CrossTabDataBus } from './core/data-bus';
|
|
2
|
+
import type { CrossTabDataBus, DataBusHealthSummary } from './core/data-bus';
|
|
3
3
|
import type { DataBusMessage, WorkerStatus } from './core/types';
|
|
4
4
|
/**
|
|
5
5
|
* Create a CrossTabDataBus for the component's lifetime.
|
|
@@ -28,4 +28,15 @@ export declare function useCrossTabSubscription<TConfig, TData>(bus: CrossTabDat
|
|
|
28
28
|
* changes identity.
|
|
29
29
|
*/
|
|
30
30
|
export declare function useCrossTabStatus<TConfig, TData>(bus: CrossTabDataBus<TConfig, TData> | null): WorkerStatus;
|
|
31
|
+
/**
|
|
32
|
+
* Mirror the bus health summary into React state.
|
|
33
|
+
*
|
|
34
|
+
* `getHealthSummary()` is a snapshot, not an event stream, so the hook polls
|
|
35
|
+
* it on an interval (default 1000 ms) and refreshes immediately on status
|
|
36
|
+
* changes and errors. Pass `intervalMs: 0` to rely on event-driven refreshes
|
|
37
|
+
* only. Returns `null` while the bus has not been created yet.
|
|
38
|
+
*/
|
|
39
|
+
export declare function useCrossTabHealth<TConfig, TData>(bus: CrossTabDataBus<TConfig, TData> | null, options?: {
|
|
40
|
+
intervalMs?: number;
|
|
41
|
+
}): DataBusHealthSummary | null;
|
|
31
42
|
//# sourceMappingURL=hooks.d.ts.map
|
package/dist/hooks.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../src/hooks.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../src/hooks.ts"],"names":[],"mappings":"AAmBA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,OAAO,CAAC;AAC5C,OAAO,KAAK,EAAE,eAAe,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAC;AAC7E,OAAO,KAAK,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAGjE;;;;;;;;;;GAUG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,KAAK,EAC/C,MAAM,EAAE,MAAM,eAAe,CAAC,OAAO,EAAE,KAAK,CAAC,EAC7C,IAAI,GAAE,cAAmB,GACxB,eAAe,CAAC,OAAO,EAAE,KAAK,CAAC,GAAG,IAAI,CAiBxC;AAED;;;;;;;GAOG;AACH,wBAAgB,uBAAuB,CAAC,OAAO,EAAE,KAAK,EACpD,GAAG,EAAE,eAAe,CAAC,OAAO,EAAE,KAAK,CAAC,GAAG,IAAI,EAC3C,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,CAAC,OAAO,EAAE,cAAc,CAAC,KAAK,CAAC,KAAK,IAAI,GAChD,IAAI,CAON;AAED;;;;GAIG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,KAAK,EAC9C,GAAG,EAAE,eAAe,CAAC,OAAO,EAAE,KAAK,CAAC,GAAG,IAAI,GAC1C,YAAY,CAWd;AAED;;;;;;;GAOG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,KAAK,EAC9C,GAAG,EAAE,eAAe,CAAC,OAAO,EAAE,KAAK,CAAC,GAAG,IAAI,EAC3C,OAAO,CAAC,EAAE;IAAE,UAAU,CAAC,EAAE,MAAM,CAAA;CAAE,GAChC,oBAAoB,GAAG,IAAI,CAsB7B"}
|
package/dist/hooks.js
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import {
|
|
2
|
+
WORKER_STATUS
|
|
3
|
+
} from "./chunk-TZ7ZP7YD.js";
|
|
4
|
+
|
|
1
5
|
// src/hooks.ts
|
|
2
6
|
import { useEffect, useRef, useState } from "react";
|
|
3
7
|
function useCrossTabDataBus(create, deps = []) {
|
|
@@ -26,10 +30,10 @@ function useCrossTabSubscription(bus, topic, handler) {
|
|
|
26
30
|
}, [bus, topic]);
|
|
27
31
|
}
|
|
28
32
|
function useCrossTabStatus(bus) {
|
|
29
|
-
const [status, setStatus] = useState(
|
|
33
|
+
const [status, setStatus] = useState(WORKER_STATUS.CONNECTING);
|
|
30
34
|
useEffect(() => {
|
|
31
35
|
if (!bus) {
|
|
32
|
-
setStatus(
|
|
36
|
+
setStatus(WORKER_STATUS.CONNECTING);
|
|
33
37
|
return;
|
|
34
38
|
}
|
|
35
39
|
setStatus(bus.getStatus());
|
|
@@ -37,8 +41,30 @@ function useCrossTabStatus(bus) {
|
|
|
37
41
|
}, [bus]);
|
|
38
42
|
return status;
|
|
39
43
|
}
|
|
44
|
+
function useCrossTabHealth(bus, options) {
|
|
45
|
+
const [health, setHealth] = useState(null);
|
|
46
|
+
useEffect(() => {
|
|
47
|
+
if (!bus) {
|
|
48
|
+
setHealth(null);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
const refresh = () => setHealth(bus.getHealthSummary());
|
|
52
|
+
refresh();
|
|
53
|
+
const unsubscribeStatus = bus.onStatus(refresh);
|
|
54
|
+
const unsubscribeError = bus.onError(refresh);
|
|
55
|
+
const intervalMs = options?.intervalMs ?? 1e3;
|
|
56
|
+
const timer = intervalMs > 0 ? setInterval(refresh, intervalMs) : null;
|
|
57
|
+
return () => {
|
|
58
|
+
unsubscribeStatus();
|
|
59
|
+
unsubscribeError();
|
|
60
|
+
if (timer) clearInterval(timer);
|
|
61
|
+
};
|
|
62
|
+
}, [bus]);
|
|
63
|
+
return health;
|
|
64
|
+
}
|
|
40
65
|
export {
|
|
41
66
|
useCrossTabDataBus,
|
|
67
|
+
useCrossTabHealth,
|
|
42
68
|
useCrossTabStatus,
|
|
43
69
|
useCrossTabSubscription
|
|
44
70
|
};
|
package/dist/hooks.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/hooks.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * React hooks adapter for cross-tab-worker-databus.\n *\n * A thin, transport-agnostic bridge between the imperative CrossTabDataBus\n * API and React component lifecycles. React is an optional peer dependency \u2014\n * this module is a separate entry point so consumers who don't use React\n * never load it.\n *\n * - `useCrossTabDataBus` owns the bus lifecycle: created on mount, stopped on\n * unmount. It is StrictMode-safe: the double-invoked effect exercises the\n * same stop/recreate path as BFCache suspend/resume.\n * - `useCrossTabSubscription` attaches a message handler with automatic\n * cleanup; the handler is read through a ref, so you can pass inline\n * closures without resubscribing on every render.\n * - `useCrossTabStatus` mirrors `bus.onStatus()` into React state.\n */\nimport { useEffect, useRef, useState } from 'react';\nimport type { DependencyList } from 'react';\nimport type { CrossTabDataBus } from './core/data-bus';\nimport type { DataBusMessage, WorkerStatus } from './core/types';\n\n/**\n * Create a CrossTabDataBus for the component's lifetime.\n *\n * @param create Factory invoked once per effect run. Return a fresh bus \u2014\n * do not share a bus instance between effects, or StrictMode's\n * mount \u2192 stop \u2192 mount cycle will stop the shared instance out from\n * under the second mount.\n * @param deps Re-create the bus when these change (default: create once).\n * @returns The active bus, or `null` before the first effect has run (SSR\n * and the initial render).\n */\nexport function useCrossTabDataBus<TConfig, TData>(\n create: () => CrossTabDataBus<TConfig, TData>,\n deps: DependencyList = []\n): CrossTabDataBus<TConfig, TData> | null {\n const [bus, setBus] = useState<CrossTabDataBus<TConfig, TData> | null>(null);\n const lifecycleGeneration = useRef(0);\n useEffect(() => {\n const generation = ++lifecycleGeneration.current;\n const instance = create();\n if (generation !== lifecycleGeneration.current) return;\n setBus(instance);\n void instance.ready().catch(() => {});\n return () => {\n if (generation === lifecycleGeneration.current) setBus(null);\n void instance.stop();\n };\n // The factory is intentionally not a dependency: callers pass an inline\n // closure and key recreation through `deps` instead.\n }, deps);\n return bus;\n}\n\n/**\n * Subscribe to `topic` for the component's lifetime. The handler is read\n * through a ref on each delivery, so inline closures are safe without\n * unsubscribing/resubscribing on re-renders.\n *\n * When `bus` is null (not yet created) the subscription is queued until the\n * bus appears \u2014 the bus itself queues it until the transport is ready.\n */\nexport function useCrossTabSubscription<TConfig, TData>(\n bus: CrossTabDataBus<TConfig, TData> | null,\n topic: string,\n handler: (message: DataBusMessage<TData>) => void\n): void {\n const handlerRef = useRef(handler);\n handlerRef.current = handler;\n useEffect(() => {\n if (!bus) return;\n return bus.subscribe(topic, message => handlerRef.current(message));\n }, [bus, topic]);\n}\n\n/**\n * Mirror the bus connection status into React state. Reports the live value\n * via `onStatus` and reads the current value synchronously whenever `bus`\n * changes identity.\n */\nexport function useCrossTabStatus<TConfig, TData>(\n bus: CrossTabDataBus<TConfig, TData> | null\n): WorkerStatus {\n const [status, setStatus] = useState<WorkerStatus>(
|
|
5
|
-
"mappings": "
|
|
4
|
+
"sourcesContent": ["/**\n * React hooks adapter for cross-tab-worker-databus.\n *\n * A thin, transport-agnostic bridge between the imperative CrossTabDataBus\n * API and React component lifecycles. React is an optional peer dependency \u2014\n * this module is a separate entry point so consumers who don't use React\n * never load it.\n *\n * - `useCrossTabDataBus` owns the bus lifecycle: created on mount, stopped on\n * unmount. It is StrictMode-safe: the double-invoked effect exercises the\n * same stop/recreate path as BFCache suspend/resume.\n * - `useCrossTabSubscription` attaches a message handler with automatic\n * cleanup; the handler is read through a ref, so you can pass inline\n * closures without resubscribing on every render.\n * - `useCrossTabStatus` mirrors `bus.onStatus()` into React state.\n * - `useCrossTabHealth` polls `bus.getHealthSummary()` into React state,\n * with event-driven refreshes on status changes and errors.\n */\nimport { useEffect, useRef, useState } from 'react';\nimport type { DependencyList } from 'react';\nimport type { CrossTabDataBus, DataBusHealthSummary } from './core/data-bus';\nimport type { DataBusMessage, WorkerStatus } from './core/types';\nimport { WORKER_STATUS } from './utils/constants';\n\n/**\n * Create a CrossTabDataBus for the component's lifetime.\n *\n * @param create Factory invoked once per effect run. Return a fresh bus \u2014\n * do not share a bus instance between effects, or StrictMode's\n * mount \u2192 stop \u2192 mount cycle will stop the shared instance out from\n * under the second mount.\n * @param deps Re-create the bus when these change (default: create once).\n * @returns The active bus, or `null` before the first effect has run (SSR\n * and the initial render).\n */\nexport function useCrossTabDataBus<TConfig, TData>(\n create: () => CrossTabDataBus<TConfig, TData>,\n deps: DependencyList = []\n): CrossTabDataBus<TConfig, TData> | null {\n const [bus, setBus] = useState<CrossTabDataBus<TConfig, TData> | null>(null);\n const lifecycleGeneration = useRef(0);\n useEffect(() => {\n const generation = ++lifecycleGeneration.current;\n const instance = create();\n if (generation !== lifecycleGeneration.current) return;\n setBus(instance);\n void instance.ready().catch(() => {});\n return () => {\n if (generation === lifecycleGeneration.current) setBus(null);\n void instance.stop();\n };\n // The factory is intentionally not a dependency: callers pass an inline\n // closure and key recreation through `deps` instead.\n }, deps);\n return bus;\n}\n\n/**\n * Subscribe to `topic` for the component's lifetime. The handler is read\n * through a ref on each delivery, so inline closures are safe without\n * unsubscribing/resubscribing on re-renders.\n *\n * When `bus` is null (not yet created) the subscription is queued until the\n * bus appears \u2014 the bus itself queues it until the transport is ready.\n */\nexport function useCrossTabSubscription<TConfig, TData>(\n bus: CrossTabDataBus<TConfig, TData> | null,\n topic: string,\n handler: (message: DataBusMessage<TData>) => void\n): void {\n const handlerRef = useRef(handler);\n handlerRef.current = handler;\n useEffect(() => {\n if (!bus) return;\n return bus.subscribe(topic, message => handlerRef.current(message));\n }, [bus, topic]);\n}\n\n/**\n * Mirror the bus connection status into React state. Reports the live value\n * via `onStatus` and reads the current value synchronously whenever `bus`\n * changes identity.\n */\nexport function useCrossTabStatus<TConfig, TData>(\n bus: CrossTabDataBus<TConfig, TData> | null\n): WorkerStatus {\n const [status, setStatus] = useState<WorkerStatus>(WORKER_STATUS.CONNECTING);\n useEffect(() => {\n if (!bus) {\n setStatus(WORKER_STATUS.CONNECTING);\n return;\n }\n setStatus(bus.getStatus());\n return bus.onStatus(setStatus);\n }, [bus]);\n return status;\n}\n\n/**\n * Mirror the bus health summary into React state.\n *\n * `getHealthSummary()` is a snapshot, not an event stream, so the hook polls\n * it on an interval (default 1000 ms) and refreshes immediately on status\n * changes and errors. Pass `intervalMs: 0` to rely on event-driven refreshes\n * only. Returns `null` while the bus has not been created yet.\n */\nexport function useCrossTabHealth<TConfig, TData>(\n bus: CrossTabDataBus<TConfig, TData> | null,\n options?: { intervalMs?: number }\n): DataBusHealthSummary | null {\n const [health, setHealth] = useState<DataBusHealthSummary | null>(null);\n useEffect(() => {\n if (!bus) {\n setHealth(null);\n return;\n }\n const refresh = () => setHealth(bus.getHealthSummary());\n refresh();\n const unsubscribeStatus = bus.onStatus(refresh);\n const unsubscribeError = bus.onError(refresh);\n const intervalMs = options?.intervalMs ?? 1_000;\n const timer = intervalMs > 0 ? setInterval(refresh, intervalMs) : null;\n return () => {\n unsubscribeStatus();\n unsubscribeError();\n if (timer) clearInterval(timer);\n };\n // The options object is intentionally not a dependency: callers pass an\n // inline literal and the interval only affects polling cadence.\n }, [bus]);\n return health;\n}\n"],
|
|
5
|
+
"mappings": ";;;;;AAkBA,SAAS,WAAW,QAAQ,gBAAgB;AAiBrC,SAAS,mBACd,QACA,OAAuB,CAAC,GACgB;AACxC,QAAM,CAAC,KAAK,MAAM,IAAI,SAAiD,IAAI;AAC3E,QAAM,sBAAsB,OAAO,CAAC;AACpC,YAAU,MAAM;AACd,UAAM,aAAa,EAAE,oBAAoB;AACzC,UAAM,WAAW,OAAO;AACxB,QAAI,eAAe,oBAAoB,QAAS;AAChD,WAAO,QAAQ;AACf,SAAK,SAAS,MAAM,EAAE,MAAM,MAAM;AAAA,IAAC,CAAC;AACpC,WAAO,MAAM;AACX,UAAI,eAAe,oBAAoB,QAAS,QAAO,IAAI;AAC3D,WAAK,SAAS,KAAK;AAAA,IACrB;AAAA,EAGF,GAAG,IAAI;AACP,SAAO;AACT;AAUO,SAAS,wBACd,KACA,OACA,SACM;AACN,QAAM,aAAa,OAAO,OAAO;AACjC,aAAW,UAAU;AACrB,YAAU,MAAM;AACd,QAAI,CAAC,IAAK;AACV,WAAO,IAAI,UAAU,OAAO,aAAW,WAAW,QAAQ,OAAO,CAAC;AAAA,EACpE,GAAG,CAAC,KAAK,KAAK,CAAC;AACjB;AAOO,SAAS,kBACd,KACc;AACd,QAAM,CAAC,QAAQ,SAAS,IAAI,SAAuB,cAAc,UAAU;AAC3E,YAAU,MAAM;AACd,QAAI,CAAC,KAAK;AACR,gBAAU,cAAc,UAAU;AAClC;AAAA,IACF;AACA,cAAU,IAAI,UAAU,CAAC;AACzB,WAAO,IAAI,SAAS,SAAS;AAAA,EAC/B,GAAG,CAAC,GAAG,CAAC;AACR,SAAO;AACT;AAUO,SAAS,kBACd,KACA,SAC6B;AAC7B,QAAM,CAAC,QAAQ,SAAS,IAAI,SAAsC,IAAI;AACtE,YAAU,MAAM;AACd,QAAI,CAAC,KAAK;AACR,gBAAU,IAAI;AACd;AAAA,IACF;AACA,UAAM,UAAU,MAAM,UAAU,IAAI,iBAAiB,CAAC;AACtD,YAAQ;AACR,UAAM,oBAAoB,IAAI,SAAS,OAAO;AAC9C,UAAM,mBAAmB,IAAI,QAAQ,OAAO;AAC5C,UAAM,aAAa,SAAS,cAAc;AAC1C,UAAM,QAAQ,aAAa,IAAI,YAAY,SAAS,UAAU,IAAI;AAClE,WAAO,MAAM;AACX,wBAAkB;AAClB,uBAAiB;AACjB,UAAI,MAAO,eAAc,KAAK;AAAA,IAChC;AAAA,EAGF,GAAG,CAAC,GAAG,CAAC;AACR,SAAO;AACT;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -9,19 +9,19 @@
|
|
|
9
9
|
* ```
|
|
10
10
|
*/
|
|
11
11
|
export { CrossTabDataBus } from './core/data-bus';
|
|
12
|
-
export type { CrossTabDataBusOptions, DataBusDedupOptions, DataBusDedupStats, DataBusDiagnostics, DataBusPersistenceRetryOptions, DataBusReplayOptions } from './core/data-bus';
|
|
12
|
+
export type { CrossTabDataBusOptions, DataBusDedupOptions, DataBusDedupStats, DataBusDiagnostics, DataBusFailureSource, DataBusHealthSummary, DataBusLastFailure, DataBusPersistenceHealth, DataBusPersistenceRetryOptions, DataBusReplayOptions } from './core/data-bus';
|
|
13
13
|
export { createIndexedDbReplayPersistence } from './core/replay-persistence';
|
|
14
14
|
export type { DataBusReplayPersistence, IndexedDbReplayPersistenceOptions } from './core/replay-persistence';
|
|
15
|
-
export type { DataBusCoordinationTraceEvent, DataBusErrorTraceEvent, DataBusLifecycleTraceEvent, DataBusMetricsTraceEvent, DataBusReliabilityTraceEvent, DataBusStatusTraceEvent, DataBusSubscriptionTraceEvent, DataBusTraceEvent, DataBusTraceMode, DataBusTraceOptions } from './core/trace';
|
|
15
|
+
export type { DataBusCoordinationTraceEvent, DataBusErrorTraceEvent, DataBusLifecycleTraceEvent, DataBusMetricsTraceEvent, DataBusReliabilityTraceEvent, DataBusStatusTraceEvent, DataBusSubscriptionTraceEvent, DataBusMetricsSnapshot, DataBusTraceEvent, DataBusTraceMode, DataBusTraceOptions } from './core/trace';
|
|
16
16
|
export { WorkerClusterRuntime } from './core/cluster';
|
|
17
17
|
export type { WorkerClusterHandlers, WorkerClusterOptions, WorkerClusterSnapshot } from './core/cluster';
|
|
18
|
-
export { createBrowserEnvironment, getOrCreateTabId } from './core/environment';
|
|
19
|
-
export type { ClusterChannel, ClusterEnvironment, StorageLike } from './core/environment';
|
|
18
|
+
export { createBrowserEnvironment, createStorageEventChannel, getOrCreateTabId } from './core/environment';
|
|
19
|
+
export type { ClusterChannel, ClusterEnvironment, StorageEventWindow, StorageLike } from './core/environment';
|
|
20
20
|
export { createOpaqueKey } from './core/hash';
|
|
21
21
|
export { WebSocketTransport, createWebSocketDataBus } from './websocket';
|
|
22
22
|
export type { CreateWebSocketDataBusOptions, WebSocketDataBusConfig, WebSocketLike } from './websocket';
|
|
23
23
|
export { selectWorkerBackend } from './worker-mode';
|
|
24
24
|
export type { WorkerBackend, WorkerMode } from './worker-mode';
|
|
25
|
-
export { DEFAULT_MAX_ACTIVE_WORKERS, hasActiveOwner, isWildcardTopic, selectActiveWorkers, selectLeastLoadedWorker, selectRebalanceTarget, topicMatchesPattern } from './core/routing';
|
|
26
|
-
export type { DataBusErrorHandler, DataBusMessage, DataBusPublication, DataBusPublicationEnvelope, DataBusPublicationMetadata, DataBusPublishOptions, DataBusMessageHandler, DataBusStatusHandler, DataBusTransport, DataBusTransportHandlers, MaybePromise, TabVisibilityState, TopicSubscriberRecord, WorkerClusterMessage, WorkerControlAction, WorkerRecord, WorkerRole, WorkerRoute, WorkerStatus } from './core/types';
|
|
25
|
+
export { DEFAULT_MAX_ACTIVE_WORKERS, approximatePayloadBytes, effectiveWorkerLoad, hasActiveOwner, isWildcardTopic, selectActiveWorkers, selectLeastLoadedWorker, selectRebalanceTarget, topicMatchesPattern } from './core/routing';
|
|
26
|
+
export type { DataBusErrorHandler, DataBusMessage, DataBusPublication, DataBusPublicationEnvelope, DataBusPublicationMetadata, DataBusPublishOptions, DataBusMessageHandler, DataBusStatusHandler, DataBusTransport, DataBusTransportHandlers, LoadWeightingOptions, MaybePromise, TabVisibilityState, TopicSubscriberRecord, WorkerClusterMessage, WorkerControlAction, WorkerRecord, WorkerRole, WorkerRoute, WorkerStatus, WorkerThroughputSample } from './core/types';
|
|
27
27
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAClD,YAAY,EAAE,sBAAsB,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,8BAA8B,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAClD,YAAY,EAAE,sBAAsB,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,kBAAkB,EAAE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