react-mirrorstate 0.2.0 → 0.2.3
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.
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
import debug from "debug";
|
|
2
|
-
const logger = debug("mirrorstate:ws-manager");
|
|
3
1
|
class WebSocketConnectionManager {
|
|
4
2
|
ws = null;
|
|
5
3
|
isConnecting = false;
|
|
6
4
|
listeners = new Map();
|
|
7
5
|
currentStates = new Map();
|
|
8
|
-
getWebSocketConfig() {
|
|
6
|
+
async getWebSocketConfig() {
|
|
9
7
|
try {
|
|
10
|
-
const config =
|
|
8
|
+
const config = await import("virtual:mirrorstate/config");
|
|
11
9
|
return config;
|
|
12
10
|
}
|
|
13
11
|
catch {
|
|
@@ -22,6 +20,12 @@ class WebSocketConnectionManager {
|
|
|
22
20
|
const host = window.location.host;
|
|
23
21
|
return `${protocol}//${host}${path}`;
|
|
24
22
|
}
|
|
23
|
+
cleanup() {
|
|
24
|
+
this.isConnecting = false;
|
|
25
|
+
this.ws = null;
|
|
26
|
+
this.pendingUpdates.forEach((timeout) => clearTimeout(timeout));
|
|
27
|
+
this.pendingUpdates.clear();
|
|
28
|
+
}
|
|
25
29
|
async connect() {
|
|
26
30
|
if (this.ws?.readyState === WebSocket.OPEN || this.isConnecting) {
|
|
27
31
|
return;
|
|
@@ -31,23 +35,18 @@ class WebSocketConnectionManager {
|
|
|
31
35
|
return;
|
|
32
36
|
}
|
|
33
37
|
this.isConnecting = true;
|
|
34
|
-
const config = this.getWebSocketConfig();
|
|
38
|
+
const config = await this.getWebSocketConfig();
|
|
35
39
|
const wsUrl = this.buildWebSocketURL(config.WS_PATH);
|
|
36
|
-
logger(`Connecting to ${wsUrl}`);
|
|
37
40
|
this.ws = new WebSocket(wsUrl);
|
|
38
41
|
this.ws.onopen = () => {
|
|
39
42
|
this.isConnecting = false;
|
|
40
|
-
logger("WebSocket connected");
|
|
41
43
|
};
|
|
42
44
|
this.ws.onclose = () => {
|
|
43
|
-
this.
|
|
44
|
-
this.ws = null;
|
|
45
|
-
logger("WebSocket closed");
|
|
45
|
+
this.cleanup();
|
|
46
46
|
};
|
|
47
47
|
this.ws.onerror = () => {
|
|
48
|
-
this.isConnecting = false;
|
|
49
|
-
this.ws = null;
|
|
50
48
|
console.error("WebSocket error");
|
|
49
|
+
this.cleanup();
|
|
51
50
|
};
|
|
52
51
|
this.ws.onmessage = (event) => {
|
|
53
52
|
try {
|
|
@@ -55,12 +54,10 @@ class WebSocketConnectionManager {
|
|
|
55
54
|
if (data.type === "initialState") {
|
|
56
55
|
this.currentStates.set(data.name, data.state);
|
|
57
56
|
this.notifyListeners(data.name, data.state);
|
|
58
|
-
logger(`Initial state loaded: ${data.name}`, data.state);
|
|
59
57
|
}
|
|
60
58
|
if (data.type === "fileChange") {
|
|
61
59
|
this.currentStates.set(data.name, data.state);
|
|
62
60
|
this.notifyListeners(data.name, data.state);
|
|
63
|
-
logger(`State updated: ${data.name}`, data.state);
|
|
64
61
|
}
|
|
65
62
|
}
|
|
66
63
|
catch (error) {
|
|
@@ -103,7 +100,6 @@ class WebSocketConnectionManager {
|
|
|
103
100
|
// Check if this is actually a different state
|
|
104
101
|
const lastState = this.lastSentState.get(name);
|
|
105
102
|
if (lastState === state) {
|
|
106
|
-
logger(`Skipping duplicate state update for ${name}`);
|
|
107
103
|
return;
|
|
108
104
|
}
|
|
109
105
|
// Debounce rapid updates
|
|
@@ -115,7 +111,6 @@ class WebSocketConnectionManager {
|
|
|
115
111
|
this.currentStates.set(name, state);
|
|
116
112
|
this.lastSentState.set(name, state);
|
|
117
113
|
this.pendingUpdates.delete(name);
|
|
118
|
-
logger(`Sent state update for ${name}`);
|
|
119
114
|
}, 10);
|
|
120
115
|
this.pendingUpdates.set(name, timeout);
|
|
121
116
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-mirrorstate",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"description": "React library for bidirectional state synchronization with MirrorState",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -22,14 +22,12 @@
|
|
|
22
22
|
"directory": "packages/react-mirrorstate"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"
|
|
26
|
-
"immer": "^10.0.3"
|
|
25
|
+
"immer": "^10.1.3"
|
|
27
26
|
},
|
|
28
27
|
"devDependencies": {
|
|
29
|
-
"@types/
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"typescript": "^5.3.3"
|
|
28
|
+
"@types/react": "^19.2.2",
|
|
29
|
+
"react": "^19.2.0",
|
|
30
|
+
"typescript": "^5.9.3"
|
|
33
31
|
},
|
|
34
32
|
"peerDependencies": {
|
|
35
33
|
"react": ">=18.0.0"
|