react-peer-chat 0.11.3 → 0.11.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.
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useChat } from './chunk-
|
|
1
|
+
import { useChat } from './chunk-JNCYZVYO.js';
|
|
2
2
|
import { BiSolidMessageX, BiSolidMessageDetail, GrSend, BsFillMicFill, BsFillMicMuteFill } from './chunk-QIPTWGEX.js';
|
|
3
3
|
import { __objRest, __spreadValues } from './chunk-FZ4QVG4I.js';
|
|
4
4
|
import React, { useRef, useState, useEffect } from 'react';
|
|
@@ -62,8 +62,10 @@ function useChat({
|
|
|
62
62
|
onMessageSent,
|
|
63
63
|
onMessageReceived
|
|
64
64
|
}) {
|
|
65
|
-
const [
|
|
65
|
+
const [peerEpoch, setPeerEpoch] = useState(0);
|
|
66
66
|
const [audio, setAudio] = useAudio(allowed);
|
|
67
|
+
const peerRef = useRef(null);
|
|
68
|
+
const scheduleReconnectRef = useRef(null);
|
|
67
69
|
const connRef = useRef({});
|
|
68
70
|
const callsRef = useRef({});
|
|
69
71
|
const localStreamRef = useRef(null);
|
|
@@ -166,9 +168,29 @@ function useChat({
|
|
|
166
168
|
Object.values(connRef.current).forEach((conn) => conn.send(event));
|
|
167
169
|
onMessageSent == null ? void 0 : onMessageSent(event.message);
|
|
168
170
|
}
|
|
171
|
+
useEffect(() => {
|
|
172
|
+
const onOnline = () => {
|
|
173
|
+
var _a, _b;
|
|
174
|
+
return ((_a = peerRef.current) == null ? void 0 : _a.disconnected) && ((_b = scheduleReconnectRef.current) == null ? void 0 : _b.call(scheduleReconnectRef));
|
|
175
|
+
};
|
|
176
|
+
window.addEventListener("online", onOnline);
|
|
177
|
+
return () => window.removeEventListener("online", onOnline);
|
|
178
|
+
}, []);
|
|
169
179
|
useEffect(() => {
|
|
170
180
|
if (!text && !audio) return;
|
|
171
181
|
let destroyed = false;
|
|
182
|
+
let reconnecting = false;
|
|
183
|
+
let reconnectTimer;
|
|
184
|
+
scheduleReconnectRef.current = () => {
|
|
185
|
+
if (destroyed || reconnecting) return;
|
|
186
|
+
reconnecting = true;
|
|
187
|
+
reconnectTimer = setTimeout(() => {
|
|
188
|
+
var _a;
|
|
189
|
+
if (!destroyed && ((_a = peerRef.current) == null ? void 0 : _a.disconnected)) peerRef.current.reconnect();
|
|
190
|
+
reconnecting = false;
|
|
191
|
+
}, 1e3);
|
|
192
|
+
};
|
|
193
|
+
const scheduleReconnect = scheduleReconnectRef.current;
|
|
172
194
|
import('peerjs').then(
|
|
173
195
|
({
|
|
174
196
|
Peer,
|
|
@@ -176,36 +198,40 @@ function useChat({
|
|
|
176
198
|
supports: { audioVideo, data }
|
|
177
199
|
}
|
|
178
200
|
}) => {
|
|
201
|
+
if (destroyed) return;
|
|
179
202
|
if (!data || !audioVideo) return onError(new Error("Browser not supported! Try some other browser."));
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
203
|
+
peerRef.current = new Peer(completePeerId, __spreadValues({ config: defaultConfig }, peerOptions));
|
|
204
|
+
setPeerEpoch((prev) => prev + 1);
|
|
205
|
+
const peer = peerRef.current;
|
|
206
|
+
peer.on("connection", handleConnection);
|
|
207
|
+
peer.on("call", handleCall);
|
|
208
|
+
peer.on("disconnected", () => {
|
|
184
209
|
resetConnections();
|
|
185
|
-
|
|
210
|
+
scheduleReconnect();
|
|
186
211
|
});
|
|
187
|
-
|
|
212
|
+
peer.on("error", (error) => {
|
|
188
213
|
if (error.type === "network" || error.type === "server-error") {
|
|
189
214
|
resetConnections();
|
|
190
|
-
|
|
215
|
+
scheduleReconnect();
|
|
191
216
|
onNetworkError == null ? void 0 : onNetworkError(error);
|
|
192
217
|
}
|
|
193
218
|
onPeerError(error);
|
|
194
219
|
});
|
|
195
|
-
if (destroyed) peer2.destroy();
|
|
196
|
-
else setPeer(peer2);
|
|
197
220
|
}
|
|
198
221
|
);
|
|
199
222
|
return () => {
|
|
223
|
+
var _a, _b;
|
|
200
224
|
destroyed = true;
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
225
|
+
clearTimeout(reconnectTimer);
|
|
226
|
+
(_a = peerRef.current) == null ? void 0 : _a.removeAllListeners();
|
|
227
|
+
(_b = peerRef.current) == null ? void 0 : _b.destroy();
|
|
228
|
+
peerRef.current = null;
|
|
205
229
|
};
|
|
206
230
|
}, [completePeerId]);
|
|
207
231
|
useEffect(() => {
|
|
208
|
-
if (!text
|
|
232
|
+
if (!text) return;
|
|
233
|
+
const peer = peerRef.current;
|
|
234
|
+
if (!peer) return;
|
|
209
235
|
const connectData = () => completeRemotePeerIds.forEach((id) => handleConnection(peer.connect(id)));
|
|
210
236
|
if (peer.open) connectData();
|
|
211
237
|
peer.on("open", connectData);
|
|
@@ -213,12 +239,15 @@ function useChat({
|
|
|
213
239
|
peer.off("open", connectData);
|
|
214
240
|
resetConnections("data");
|
|
215
241
|
};
|
|
216
|
-
}, [text,
|
|
242
|
+
}, [text, peerEpoch]);
|
|
217
243
|
useEffect(() => {
|
|
218
|
-
if (!audio
|
|
244
|
+
if (!audio) return;
|
|
245
|
+
const peer = peerRef.current;
|
|
246
|
+
if (!peer) return;
|
|
219
247
|
const setupAudio = () => __async(null, null, function* () {
|
|
220
248
|
try {
|
|
221
|
-
localStreamRef.current
|
|
249
|
+
if (!localStreamRef.current)
|
|
250
|
+
localStreamRef.current = yield navigator.mediaDevices.getUserMedia({ video: false, audio: { autoGainControl: true, noiseSuppression: true, echoCancellation: true } });
|
|
222
251
|
completeRemotePeerIds.forEach((id) => localStreamRef.current && handleCall(peer.call(id, localStreamRef.current)));
|
|
223
252
|
} catch (e) {
|
|
224
253
|
setAudio(false);
|
|
@@ -237,7 +266,7 @@ function useChat({
|
|
|
237
266
|
audioContextRef.current = null;
|
|
238
267
|
mixerRef.current = null;
|
|
239
268
|
};
|
|
240
|
-
}, [audio,
|
|
269
|
+
}, [audio, peerEpoch]);
|
|
241
270
|
return { peerId: completePeerId, remotePeers, messages, sendMessage, audio, setAudio };
|
|
242
271
|
}
|
|
243
272
|
function useMessages() {
|
package/dist/components.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export { Chat as default } from './chunks/chunk-
|
|
2
|
-
import './chunks/chunk-
|
|
1
|
+
export { Chat as default } from './chunks/chunk-7LX3CBZL.js';
|
|
2
|
+
import './chunks/chunk-JNCYZVYO.js';
|
|
3
3
|
import './chunks/chunk-QIPTWGEX.js';
|
|
4
4
|
import './chunks/chunk-ZYFPSCFE.js';
|
|
5
5
|
import './chunks/chunk-FZ4QVG4I.js';
|
package/dist/hooks.js
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export { Chat as default } from './chunks/chunk-
|
|
2
|
-
export { useChat } from './chunks/chunk-
|
|
1
|
+
export { Chat as default } from './chunks/chunk-7LX3CBZL.js';
|
|
2
|
+
export { useChat } from './chunks/chunk-JNCYZVYO.js';
|
|
3
3
|
import './chunks/chunk-QIPTWGEX.js';
|
|
4
4
|
export { clearChat } from './chunks/chunk-ZYFPSCFE.js';
|
|
5
5
|
import './chunks/chunk-FZ4QVG4I.js';
|
package/dist/types.d.ts
CHANGED
|
@@ -3,6 +3,13 @@ export { PeerOptions } from 'peerjs';
|
|
|
3
3
|
import { CSSProperties, DetailedHTMLProps, HTMLAttributes, SetStateAction, ReactNode } from 'react';
|
|
4
4
|
|
|
5
5
|
type Connection = DataConnection | MediaConnection;
|
|
6
|
+
type ChildrenOptions = {
|
|
7
|
+
remotePeers: RemotePeers;
|
|
8
|
+
messages: Message[];
|
|
9
|
+
sendMessage: (message: InputMessage) => void;
|
|
10
|
+
audio: boolean;
|
|
11
|
+
setAudio: (value: SetStateAction<boolean>) => void;
|
|
12
|
+
};
|
|
6
13
|
type ErrorHandler<E = Error> = (error: E) => void;
|
|
7
14
|
type InputMessage = {
|
|
8
15
|
id: string;
|
|
@@ -30,16 +37,10 @@ type UseChatProps = {
|
|
|
30
37
|
onMessageSent?: MessageEventHandler;
|
|
31
38
|
onMessageReceived?: MessageEventHandler;
|
|
32
39
|
};
|
|
33
|
-
type ChildrenOptions = {
|
|
34
|
-
remotePeers: RemotePeers;
|
|
35
|
-
messages: Message[];
|
|
36
|
-
sendMessage: (message: InputMessage) => void;
|
|
37
|
-
audio: boolean;
|
|
38
|
-
setAudio: (value: SetStateAction<boolean>) => void;
|
|
39
|
-
};
|
|
40
40
|
type UseChatReturn = ChildrenOptions & {
|
|
41
41
|
peerId: string;
|
|
42
42
|
};
|
|
43
|
+
type VoidFunction = () => void;
|
|
43
44
|
type IconProps = DetailedHTMLProps<HTMLAttributes<HTMLSpanElement>, HTMLSpanElement>;
|
|
44
45
|
type ChatProps = UseChatProps & {
|
|
45
46
|
dialogOptions?: DialogOptions;
|
|
@@ -55,4 +56,4 @@ type DialogPosition = "left" | "center" | "right";
|
|
|
55
56
|
type DivProps = DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>;
|
|
56
57
|
type RemotePeers = Record<string, string>;
|
|
57
58
|
|
|
58
|
-
export type { ChatProps, Children, ChildrenOptions, Connection, DialogOptions, DialogPosition, DivProps, ErrorHandler, IconProps, InputMessage, Message, MessageEventHandler, PeerErrorHandler, RemotePeerId, RemotePeers, ResetConnectionType, UseChatProps, UseChatReturn };
|
|
59
|
+
export type { ChatProps, Children, ChildrenOptions, Connection, DialogOptions, DialogPosition, DivProps, ErrorHandler, IconProps, InputMessage, Message, MessageEventHandler, PeerErrorHandler, RemotePeerId, RemotePeers, ResetConnectionType, UseChatProps, UseChatReturn, VoidFunction };
|
package/package.json
CHANGED