react-peer-chat 0.11.4 → 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-UFHA4GH3.js';
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';
@@ -31,8 +31,6 @@ 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;
36
34
 
37
35
  // src/lib/connection.ts
38
36
  function closeConnection(conn) {
@@ -42,12 +40,6 @@ function closeConnection(conn) {
42
40
 
43
41
  // src/lib/utils.ts
44
42
  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
43
 
52
44
  // src/lib/react.ts
53
45
  function isSetStateFunction(v) {
@@ -71,9 +63,9 @@ function useChat({
71
63
  onMessageReceived
72
64
  }) {
73
65
  const [peerEpoch, setPeerEpoch] = useState(0);
74
- const [peerGeneration, setPeerGeneration] = useState(0);
75
66
  const [audio, setAudio] = useAudio(allowed);
76
67
  const peerRef = useRef(null);
68
+ const scheduleReconnectRef = useRef(null);
77
69
  const connRef = useRef({});
78
70
  const callsRef = useRef({});
79
71
  const localStreamRef = useRef(null);
@@ -176,9 +168,29 @@ function useChat({
176
168
  Object.values(connRef.current).forEach((conn) => conn.send(event));
177
169
  onMessageSent == null ? void 0 : onMessageSent(event.message);
178
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
+ }, []);
179
179
  useEffect(() => {
180
180
  if (!text && !audio) return;
181
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;
182
194
  import('peerjs').then(
183
195
  ({
184
196
  Peer,
@@ -195,16 +207,12 @@ function useChat({
195
207
  peer.on("call", handleCall);
196
208
  peer.on("disconnected", () => {
197
209
  resetConnections();
198
- if (isMobile()) setPeerGeneration((prev) => prev + 1);
199
- else peer.reconnect();
210
+ scheduleReconnect();
200
211
  });
201
212
  peer.on("error", (error) => {
202
213
  if (error.type === "network" || error.type === "server-error") {
203
214
  resetConnections();
204
- setTimeout(() => {
205
- if (isMobile()) setPeerGeneration((prev) => prev + 1);
206
- else peer.reconnect();
207
- }, 1e3);
215
+ scheduleReconnect();
208
216
  onNetworkError == null ? void 0 : onNetworkError(error);
209
217
  }
210
218
  onPeerError(error);
@@ -212,12 +220,14 @@ function useChat({
212
220
  }
213
221
  );
214
222
  return () => {
215
- var _a;
223
+ var _a, _b;
216
224
  destroyed = true;
217
- (_a = peerRef.current) == null ? void 0 : _a.destroy();
225
+ clearTimeout(reconnectTimer);
226
+ (_a = peerRef.current) == null ? void 0 : _a.removeAllListeners();
227
+ (_b = peerRef.current) == null ? void 0 : _b.destroy();
218
228
  peerRef.current = null;
219
229
  };
220
- }, [completePeerId, peerGeneration]);
230
+ }, [completePeerId]);
221
231
  useEffect(() => {
222
232
  if (!text) return;
223
233
  const peer = peerRef.current;
@@ -236,7 +246,8 @@ function useChat({
236
246
  if (!peer) return;
237
247
  const setupAudio = () => __async(null, null, function* () {
238
248
  try {
239
- localStreamRef.current = yield navigator.mediaDevices.getUserMedia({ video: false, audio: { autoGainControl: true, noiseSuppression: true, echoCancellation: true } });
249
+ if (!localStreamRef.current)
250
+ localStreamRef.current = yield navigator.mediaDevices.getUserMedia({ video: false, audio: { autoGainControl: true, noiseSuppression: true, echoCancellation: true } });
240
251
  completeRemotePeerIds.forEach((id) => localStreamRef.current && handleCall(peer.call(id, localStreamRef.current)));
241
252
  } catch (e) {
242
253
  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-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
@@ -1,3 +1,3 @@
1
- export { useAudio, useChat, useMessages, useStorage } from './chunks/chunk-UFHA4GH3.js';
1
+ export { useAudio, useChat, useMessages, useStorage } from './chunks/chunk-JNCYZVYO.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-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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-peer-chat",
3
- "version": "0.11.4",
3
+ "version": "0.11.5",
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>",