react-mirrorstate 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/connection-manager.js +2 -0
- package/dist/index.js +1 -8
- package/package.json +1 -1
|
@@ -95,6 +95,8 @@ class WebSocketConnectionManager {
|
|
|
95
95
|
updateState(name, state) {
|
|
96
96
|
// Immediately update currentStates so subsequent reads get the latest value
|
|
97
97
|
this.currentStates.set(name, state);
|
|
98
|
+
// Notify all local subscribers immediately (for same-page component sync)
|
|
99
|
+
this.notifyListeners(name, state);
|
|
98
100
|
if (this.ws?.readyState !== WebSocket.OPEN) {
|
|
99
101
|
this.queuedUpdates.set(name, state);
|
|
100
102
|
return;
|
package/dist/index.js
CHANGED
|
@@ -5,7 +5,6 @@ import { INITIAL_STATES } from "virtual:mirrorstate/initial-states";
|
|
|
5
5
|
// Batching state for each mirror state name
|
|
6
6
|
const batchQueues = new Map();
|
|
7
7
|
const batchPending = new Map();
|
|
8
|
-
const batchCallbacks = new Map();
|
|
9
8
|
function scheduleBatchFlush(name) {
|
|
10
9
|
if (batchPending.get(name)) {
|
|
11
10
|
return;
|
|
@@ -13,7 +12,6 @@ function scheduleBatchFlush(name) {
|
|
|
13
12
|
batchPending.set(name, true);
|
|
14
13
|
queueMicrotask(() => {
|
|
15
14
|
const queue = batchQueues.get(name);
|
|
16
|
-
const callbacks = batchCallbacks.get(name);
|
|
17
15
|
if (!queue || queue.length === 0) {
|
|
18
16
|
batchPending.set(name, false);
|
|
19
17
|
return;
|
|
@@ -30,10 +28,8 @@ function scheduleBatchFlush(name) {
|
|
|
30
28
|
// Clear the queue
|
|
31
29
|
batchQueues.set(name, []);
|
|
32
30
|
batchPending.set(name, false);
|
|
33
|
-
// Update connection manager
|
|
31
|
+
// Update connection manager (which notifies all subscribers)
|
|
34
32
|
connectionManager.updateState(name, newState);
|
|
35
|
-
callbacks?.forEach((callback) => callback(newState));
|
|
36
|
-
batchCallbacks.set(name, new Set());
|
|
37
33
|
});
|
|
38
34
|
}
|
|
39
35
|
export function useMirrorState(name, initialValue) {
|
|
@@ -61,12 +57,9 @@ export function useMirrorState(name, initialValue) {
|
|
|
61
57
|
// Initialize batch queue for this name if needed
|
|
62
58
|
if (!batchQueues.has(name)) {
|
|
63
59
|
batchQueues.set(name, []);
|
|
64
|
-
batchCallbacks.set(name, new Set());
|
|
65
60
|
}
|
|
66
61
|
// Add updater to batch queue
|
|
67
62
|
batchQueues.get(name).push(updater);
|
|
68
|
-
// Add setState to callbacks
|
|
69
|
-
batchCallbacks.get(name).add(setState);
|
|
70
63
|
// Schedule batch flush
|
|
71
64
|
scheduleBatchFlush(name);
|
|
72
65
|
};
|