react-peer-chat 0.11.1 → 0.11.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,4 +1,4 @@
1
- import { useChat } from './chunk-B2B7BBRE.js';
1
+ import { useChat } from './chunk-MVAZLLW2.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';
@@ -66,6 +66,7 @@ function useChat({
66
66
  const [audio, setAudio] = useAudio(allowed);
67
67
  const connRef = useRef({});
68
68
  const callsRef = useRef({});
69
+ const localStreamRef = useRef(null);
69
70
  const audioContextRef = useRef(null);
70
71
  const mixerRef = useRef(null);
71
72
  const sourceNodesRef = useRef({});
@@ -94,9 +95,10 @@ function useChat({
94
95
  }
95
96
  function handleConnection(conn) {
96
97
  const peerId2 = conn.peer;
97
- if (connRef.current[peerId2]) return conn.close();
98
- connRef.current[peerId2] = conn;
99
98
  conn.on("open", () => {
99
+ var _a;
100
+ (_a = connRef.current[peerId2]) == null ? void 0 : _a.close();
101
+ connRef.current[peerId2] = conn;
100
102
  conn.on("data", ({ type, message, messages: messages2, remotePeerName }) => {
101
103
  switch (type) {
102
104
  case "init":
@@ -112,13 +114,18 @@ function useChat({
112
114
  });
113
115
  conn.on("close", () => {
114
116
  conn.removeAllListeners();
115
- delete connRef.current[peerId2];
117
+ if (connRef.current[peerId2] === conn) delete connRef.current[peerId2];
116
118
  });
117
119
  }
118
120
  function handleCall(call) {
119
121
  const peerId2 = call.peer;
120
- if (callsRef.current[peerId2]) return call.close();
122
+ if (!call.localStream) {
123
+ if (!localStreamRef.current) return call.close();
124
+ call.answer(localStreamRef.current);
125
+ }
121
126
  call.on("stream", () => {
127
+ var _a;
128
+ (_a = callsRef.current[peerId2]) == null ? void 0 : _a.close();
122
129
  callsRef.current[peerId2] = call;
123
130
  if (!audioContextRef.current) audioContextRef.current = new AudioContext();
124
131
  if (audioContextRef.current.state === "suspended") audioContextRef.current.resume();
@@ -137,8 +144,10 @@ function useChat({
137
144
  });
138
145
  call.on("close", () => {
139
146
  call.removeAllListeners();
140
- removePeerAudio(peerId2);
141
- delete callsRef.current[peerId2];
147
+ if (callsRef.current[peerId2] === call) {
148
+ removePeerAudio(peerId2);
149
+ delete callsRef.current[peerId2];
150
+ }
142
151
  });
143
152
  }
144
153
  function receiveMessage(message) {
@@ -207,13 +216,10 @@ function useChat({
207
216
  }, [text, peer]);
208
217
  useEffect(() => {
209
218
  if (!audio || !peer) return;
210
- let localStream;
211
219
  const setupAudio = () => __async(null, null, function* () {
212
220
  try {
213
- localStream = yield navigator.mediaDevices.getUserMedia({ video: false, audio: { autoGainControl: true, noiseSuppression: true, echoCancellation: true } });
214
- completeRemotePeerIds.forEach((id) => {
215
- if (!callsRef.current[id]) handleCall(peer.call(id, localStream));
216
- });
221
+ localStreamRef.current = yield navigator.mediaDevices.getUserMedia({ video: false, audio: { autoGainControl: true, noiseSuppression: true, echoCancellation: true } });
222
+ completeRemotePeerIds.forEach((id) => localStreamRef.current && handleCall(peer.call(id, localStreamRef.current)));
217
223
  } catch (e) {
218
224
  setAudio(false);
219
225
  onError(new Error("Microphone not accessible"));
@@ -222,11 +228,12 @@ function useChat({
222
228
  if (peer.open) setupAudio();
223
229
  peer.on("open", setupAudio);
224
230
  return () => {
225
- var _a;
231
+ var _a, _b;
226
232
  peer.off("open", setupAudio);
227
- localStream == null ? void 0 : localStream.getTracks().forEach((track) => track.stop());
233
+ (_a = localStreamRef.current) == null ? void 0 : _a.getTracks().forEach((track) => track.stop());
228
234
  resetConnections("call");
229
- (_a = audioContextRef.current) == null ? void 0 : _a.close();
235
+ (_b = audioContextRef.current) == null ? void 0 : _b.close();
236
+ localStreamRef.current = null;
230
237
  audioContextRef.current = null;
231
238
  mixerRef.current = null;
232
239
  };
@@ -1,5 +1,5 @@
1
- export { Chat as default } from './chunks/chunk-YQPRV5JQ.js';
2
- import './chunks/chunk-B2B7BBRE.js';
1
+ export { Chat as default } from './chunks/chunk-DBJS3VES.js';
2
+ import './chunks/chunk-MVAZLLW2.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-B2B7BBRE.js';
1
+ export { useAudio, useChat, useMessages, useStorage } from './chunks/chunk-MVAZLLW2.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-YQPRV5JQ.js';
2
- export { useChat } from './chunks/chunk-B2B7BBRE.js';
1
+ export { Chat as default } from './chunks/chunk-DBJS3VES.js';
2
+ export { useChat } from './chunks/chunk-MVAZLLW2.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-peer-chat",
3
- "version": "0.11.1",
3
+ "version": "0.11.3",
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>",