react-peer-chat 0.11.4 → 0.11.6

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-UFHA4GH3.js';
1
+ import { useChat } from './chunk-XJSK56RM.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';
@@ -31,8 +31,7 @@ var defaults = {
31
31
  peerOptions: {},
32
32
  remotePeerId: []
33
33
  };
34
- var iosRegex = /iPhone|iPad|iPod/i;
35
- var mobileRegex = /Android|webOS|BlackBerry|IEMobile|Opera Mini/i;
34
+ var maxReconnectionAttempts = 2;
36
35
 
37
36
  // src/lib/connection.ts
38
37
  function closeConnection(conn) {
@@ -42,12 +41,6 @@ function closeConnection(conn) {
42
41
 
43
42
  // src/lib/utils.ts
44
43
  var addPrefix = (str) => `rpc-${str}`;
45
- function isMobile(iOS = true) {
46
- var _a;
47
- let result = (_a = navigator.userAgentData) == null ? void 0 : _a.mobile;
48
- result != null ? result : result = mobileRegex.test(navigator.userAgent) || iOS && iosRegex.test(navigator.userAgent);
49
- return result;
50
- }
51
44
 
52
45
  // src/lib/react.ts
53
46
  function isSetStateFunction(v) {
@@ -74,6 +67,7 @@ function useChat({
74
67
  const [peerGeneration, setPeerGeneration] = useState(0);
75
68
  const [audio, setAudio] = useAudio(allowed);
76
69
  const peerRef = useRef(null);
70
+ const scheduleReconnectRef = useRef(null);
77
71
  const connRef = useRef({});
78
72
  const callsRef = useRef({});
79
73
  const localStreamRef = useRef(null);
@@ -176,9 +170,36 @@ function useChat({
176
170
  Object.values(connRef.current).forEach((conn) => conn.send(event));
177
171
  onMessageSent == null ? void 0 : onMessageSent(event.message);
178
172
  }
173
+ useEffect(() => {
174
+ const onOnline = () => {
175
+ var _a, _b;
176
+ return ((_a = peerRef.current) == null ? void 0 : _a.disconnected) && ((_b = scheduleReconnectRef.current) == null ? void 0 : _b.call(scheduleReconnectRef));
177
+ };
178
+ window.addEventListener("online", onOnline);
179
+ return () => window.removeEventListener("online", onOnline);
180
+ }, []);
179
181
  useEffect(() => {
180
182
  if (!text && !audio) return;
181
183
  let destroyed = false;
184
+ let reconnecting = false;
185
+ let reconnectAttempts = 0;
186
+ let reconnectTimer;
187
+ scheduleReconnectRef.current = () => {
188
+ if (destroyed || reconnecting) return;
189
+ reconnecting = true;
190
+ reconnectTimer = setTimeout(() => {
191
+ const peer = peerRef.current;
192
+ if (peer) {
193
+ reconnectAttempts++;
194
+ if (reconnectAttempts >= maxReconnectionAttempts) {
195
+ setPeerGeneration((prev) => prev + 1);
196
+ reconnectAttempts = 0;
197
+ } else peer.reconnect();
198
+ }
199
+ reconnecting = false;
200
+ }, 1e3);
201
+ };
202
+ const scheduleReconnect = scheduleReconnectRef.current;
182
203
  import('peerjs').then(
183
204
  ({
184
205
  Peer,
@@ -191,20 +212,17 @@ function useChat({
191
212
  peerRef.current = new Peer(completePeerId, __spreadValues({ config: defaultConfig }, peerOptions));
192
213
  setPeerEpoch((prev) => prev + 1);
193
214
  const peer = peerRef.current;
215
+ peer.on("open", () => reconnectAttempts = 0);
194
216
  peer.on("connection", handleConnection);
195
217
  peer.on("call", handleCall);
196
218
  peer.on("disconnected", () => {
197
219
  resetConnections();
198
- if (isMobile()) setPeerGeneration((prev) => prev + 1);
199
- else peer.reconnect();
220
+ scheduleReconnect();
200
221
  });
201
222
  peer.on("error", (error) => {
202
223
  if (error.type === "network" || error.type === "server-error") {
203
224
  resetConnections();
204
- setTimeout(() => {
205
- if (isMobile()) setPeerGeneration((prev) => prev + 1);
206
- else peer.reconnect();
207
- }, 1e3);
225
+ scheduleReconnect();
208
226
  onNetworkError == null ? void 0 : onNetworkError(error);
209
227
  }
210
228
  onPeerError(error);
@@ -212,9 +230,13 @@ function useChat({
212
230
  }
213
231
  );
214
232
  return () => {
215
- var _a;
233
+ var _a, _b;
216
234
  destroyed = true;
217
- (_a = peerRef.current) == null ? void 0 : _a.destroy();
235
+ reconnecting = false;
236
+ reconnectAttempts = 0;
237
+ clearTimeout(reconnectTimer);
238
+ (_a = peerRef.current) == null ? void 0 : _a.removeAllListeners();
239
+ (_b = peerRef.current) == null ? void 0 : _b.destroy();
218
240
  peerRef.current = null;
219
241
  };
220
242
  }, [completePeerId, peerGeneration]);
@@ -236,7 +258,8 @@ function useChat({
236
258
  if (!peer) return;
237
259
  const setupAudio = () => __async(null, null, function* () {
238
260
  try {
239
- localStreamRef.current = yield navigator.mediaDevices.getUserMedia({ video: false, audio: { autoGainControl: true, noiseSuppression: true, echoCancellation: true } });
261
+ if (!localStreamRef.current)
262
+ localStreamRef.current = yield navigator.mediaDevices.getUserMedia({ video: false, audio: { autoGainControl: true, noiseSuppression: true, echoCancellation: true } });
240
263
  completeRemotePeerIds.forEach((id) => localStreamRef.current && handleCall(peer.call(id, localStreamRef.current)));
241
264
  } catch (e) {
242
265
  setAudio(false);
@@ -1,5 +1,5 @@
1
- export { Chat as default } from './chunks/chunk-HNBONO2J.js';
2
- import './chunks/chunk-UFHA4GH3.js';
1
+ export { Chat as default } from './chunks/chunk-7M2EWH66.js';
2
+ import './chunks/chunk-XJSK56RM.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
@@ -1,3 +1,3 @@
1
- export { useAudio, useChat, useMessages, useStorage } from './chunks/chunk-UFHA4GH3.js';
1
+ export { useAudio, useChat, useMessages, useStorage } from './chunks/chunk-XJSK56RM.js';
2
2
  import './chunks/chunk-ZYFPSCFE.js';
3
3
  import './chunks/chunk-FZ4QVG4I.js';
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
- export { Chat as default } from './chunks/chunk-HNBONO2J.js';
2
- export { useChat } from './chunks/chunk-UFHA4GH3.js';
1
+ export { Chat as default } from './chunks/chunk-7M2EWH66.js';
2
+ export { useChat } from './chunks/chunk-XJSK56RM.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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-peer-chat",
3
- "version": "0.11.4",
3
+ "version": "0.11.6",
4
4
  "description": "An easy to use react component for impleting peer-to-peer chatting.",
5
5
  "license": "MIT",
6
6
  "author": "Sahil Aggarwal <aggarwalsahil2004@gmail.com>",