@chromahq/react 1.0.4 → 1.0.5
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/index.d.ts +3 -0
- package/dist/index.js +41 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
type ConnectionStatus = 'connecting' | 'connected' | 'disconnected' | 'error';
|
|
2
2
|
interface Bridge {
|
|
3
3
|
send: <Req = unknown, Res = unknown>(key: string, payload?: Req) => Promise<Res>;
|
|
4
|
+
broadcast: (key: string, payload: any) => void;
|
|
5
|
+
on: (key: string, handler: (payload: any) => void) => void;
|
|
6
|
+
off: (key: string, handler: (payload: any) => void) => void;
|
|
4
7
|
isConnected: boolean;
|
|
5
8
|
}
|
|
6
9
|
interface BridgeContextValue {
|
package/dist/index.js
CHANGED
|
@@ -16,6 +16,7 @@ const BridgeProvider = ({
|
|
|
16
16
|
const pendingRef = useRef(
|
|
17
17
|
/* @__PURE__ */ new Map()
|
|
18
18
|
);
|
|
19
|
+
const eventListenersRef = useRef(/* @__PURE__ */ new Map());
|
|
19
20
|
const uidRef = useRef(0);
|
|
20
21
|
const reconnectTimeoutRef = useRef(null);
|
|
21
22
|
const retryCountRef = useRef(0);
|
|
@@ -56,6 +57,7 @@ const BridgeProvider = ({
|
|
|
56
57
|
reject(new Error("Bridge disconnected"));
|
|
57
58
|
});
|
|
58
59
|
pendingRef.current.clear();
|
|
60
|
+
eventListenersRef.current.clear();
|
|
59
61
|
setBridge(null);
|
|
60
62
|
isConnectingRef.current = false;
|
|
61
63
|
}, []);
|
|
@@ -115,6 +117,17 @@ const BridgeProvider = ({
|
|
|
115
117
|
resolve(msg.data);
|
|
116
118
|
}
|
|
117
119
|
pendingRef.current.delete(msg.id);
|
|
120
|
+
} else if (msg.type === "broadcast" && msg.key) {
|
|
121
|
+
const listeners = eventListenersRef.current.get(msg.key);
|
|
122
|
+
if (listeners) {
|
|
123
|
+
listeners.forEach((handler) => {
|
|
124
|
+
try {
|
|
125
|
+
handler(msg.payload);
|
|
126
|
+
} catch (error2) {
|
|
127
|
+
console.warn("[Bridge] Error in event handler:", error2);
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
}
|
|
118
131
|
}
|
|
119
132
|
});
|
|
120
133
|
port.onDisconnect.addListener(() => {
|
|
@@ -181,6 +194,34 @@ const BridgeProvider = ({
|
|
|
181
194
|
}
|
|
182
195
|
});
|
|
183
196
|
},
|
|
197
|
+
broadcast: (key, payload) => {
|
|
198
|
+
if (!portRef.current) {
|
|
199
|
+
console.warn("[Bridge] Cannot broadcast - disconnected");
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
try {
|
|
203
|
+
portRef.current.postMessage({ type: "broadcast", key, payload });
|
|
204
|
+
} catch (e) {
|
|
205
|
+
console.warn("[Bridge] Broadcast failed:", e);
|
|
206
|
+
}
|
|
207
|
+
},
|
|
208
|
+
on: (key, handler) => {
|
|
209
|
+
let listeners = eventListenersRef.current.get(key);
|
|
210
|
+
if (!listeners) {
|
|
211
|
+
listeners = /* @__PURE__ */ new Set();
|
|
212
|
+
eventListenersRef.current.set(key, listeners);
|
|
213
|
+
}
|
|
214
|
+
listeners.add(handler);
|
|
215
|
+
},
|
|
216
|
+
off: (key, handler) => {
|
|
217
|
+
const listeners = eventListenersRef.current.get(key);
|
|
218
|
+
if (listeners) {
|
|
219
|
+
listeners.delete(handler);
|
|
220
|
+
if (listeners.size === 0) {
|
|
221
|
+
eventListenersRef.current.delete(key);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
},
|
|
184
225
|
isConnected: true
|
|
185
226
|
};
|
|
186
227
|
setBridge(bridgeInstance);
|