react-peer-chat 0.11.7 → 0.11.8

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-24RW4ODA.js';
1
+ import { useChat } from './chunk-NWSTDMKZ.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';
@@ -1,4 +1,4 @@
1
- import { getStorage, setStorage } from './chunk-ZYFPSCFE.js';
1
+ import { getStorage, subscribeToStorage, setStorage } from './chunk-RM3QSN63.js';
2
2
  import { __spreadValues, __async, __spreadProps } from './chunk-FZ4QVG4I.js';
3
3
  import { useState, useRef, useMemo, useEffect } from 'react';
4
4
 
@@ -296,6 +296,9 @@ function useStorage(key, initialValue, local = false) {
296
296
  return next;
297
297
  });
298
298
  };
299
+ useEffect(() => {
300
+ return subscribeToStorage(key, local, (value) => setStoredValue(value));
301
+ }, [key, local]);
299
302
  return [storedValue, setValue];
300
303
  }
301
304
  function useAudio(allowed) {
@@ -0,0 +1,55 @@
1
+ // src/lib/storage.ts
2
+ var listeners = /* @__PURE__ */ new Map();
3
+ function clearChat() {
4
+ removeStorage("rpc-remote-peer");
5
+ removeStorage("rpc-messages");
6
+ }
7
+ var getStorageInstance = (local = true) => local ? localStorage : sessionStorage;
8
+ function clearStorage(prefix = "", local = true) {
9
+ const storage = getStorageInstance(local);
10
+ const keysToRemove = [];
11
+ for (let i = 0; i < storage.length; i++) {
12
+ const key = storage.key(i);
13
+ if (key && key.startsWith(prefix)) keysToRemove.push(key);
14
+ }
15
+ keysToRemove.forEach((key) => removeStorage(key, local));
16
+ }
17
+ var getNamespacedKey = (key, local = true) => `${local ? "local" : "session"}:${key}`;
18
+ function getStorage(key, fallbackValue, local = true) {
19
+ if (typeof window === "undefined") return fallbackValue;
20
+ const value = getStorageInstance(local).getItem(key);
21
+ if (value) {
22
+ try {
23
+ return JSON.parse(value);
24
+ } catch (e) {
25
+ removeStorage(key, local);
26
+ }
27
+ }
28
+ if (fallbackValue !== void 0) setStorage(key, fallbackValue, local);
29
+ return fallbackValue;
30
+ }
31
+ function publish(key, local, value) {
32
+ const callbacks = listeners.get(getNamespacedKey(key, local));
33
+ if (callbacks) callbacks.forEach((callback) => callback(value));
34
+ }
35
+ function removeStorage(key, local = true) {
36
+ getStorageInstance(local).removeItem(key);
37
+ publish(key, local);
38
+ }
39
+ function setStorage(key, value, local = true) {
40
+ if (typeof value === "function") value = value(getStorage(key, void 0, local));
41
+ getStorageInstance(local).setItem(key, JSON.stringify(value));
42
+ publish(key, local, value);
43
+ }
44
+ function subscribeToStorage(key, local, callback) {
45
+ key = getNamespacedKey(key, local);
46
+ if (!listeners.has(key)) listeners.set(key, /* @__PURE__ */ new Set());
47
+ const set = listeners.get(key);
48
+ set.add(callback);
49
+ return () => {
50
+ set.delete(callback);
51
+ if (set.size === 0) listeners.delete(key);
52
+ };
53
+ }
54
+
55
+ export { clearChat, clearStorage, getStorage, removeStorage, setStorage, subscribeToStorage };
@@ -1,5 +1,5 @@
1
- export { Chat as default } from './chunks/chunk-7UUTQKOW.js';
2
- import './chunks/chunk-24RW4ODA.js';
1
+ export { Chat as default } from './chunks/chunk-63W3RVVG.js';
2
+ import './chunks/chunk-NWSTDMKZ.js';
3
3
  import './chunks/chunk-QIPTWGEX.js';
4
- import './chunks/chunk-ZYFPSCFE.js';
4
+ import './chunks/chunk-RM3QSN63.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-24RW4ODA.js';
2
- import './chunks/chunk-ZYFPSCFE.js';
1
+ export { useAudio, useChat, useMessages, useStorage } from './chunks/chunk-NWSTDMKZ.js';
2
+ import './chunks/chunk-RM3QSN63.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-7UUTQKOW.js';
2
- export { useChat } from './chunks/chunk-24RW4ODA.js';
1
+ export { Chat as default } from './chunks/chunk-63W3RVVG.js';
2
+ export { useChat } from './chunks/chunk-NWSTDMKZ.js';
3
3
  import './chunks/chunk-QIPTWGEX.js';
4
- export { clearChat } from './chunks/chunk-ZYFPSCFE.js';
4
+ export { clearChat } from './chunks/chunk-RM3QSN63.js';
5
5
  import './chunks/chunk-FZ4QVG4I.js';
@@ -1,6 +1,12 @@
1
- declare const removeStorage: (key: string, local?: boolean) => void;
2
- declare const clearChat: () => void;
3
- declare const setStorage: (key: string, value: unknown, local?: boolean) => void;
1
+ import { VoidFunction } from '../types.js';
2
+ import 'peerjs';
3
+ import 'react';
4
+
5
+ declare function clearChat(): void;
6
+ declare function clearStorage(prefix?: string, local?: boolean): void;
4
7
  declare function getStorage<T>(key: string, fallbackValue?: T, local?: boolean): T | undefined;
8
+ declare function removeStorage(key: string, local?: boolean): void;
9
+ declare function setStorage(key: string, value: unknown, local?: boolean): void;
10
+ declare function subscribeToStorage<T>(key: string, local: boolean, callback: (value: T) => void): VoidFunction;
5
11
 
6
- export { clearChat, getStorage, removeStorage, setStorage };
12
+ export { clearChat, clearStorage, getStorage, removeStorage, setStorage, subscribeToStorage };
@@ -1,2 +1,2 @@
1
- export { clearChat, getStorage, removeStorage, setStorage } from '../chunks/chunk-ZYFPSCFE.js';
1
+ export { clearChat, clearStorage, getStorage, removeStorage, setStorage, subscribeToStorage } from '../chunks/chunk-RM3QSN63.js';
2
2
  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.7",
3
+ "version": "0.11.8",
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>",
@@ -1,22 +0,0 @@
1
- // src/lib/storage.ts
2
- var getStorageInstance = (local = true) => local ? localStorage : sessionStorage;
3
- var removeStorage = (key, local = true) => getStorageInstance(local).removeItem(key);
4
- var clearChat = () => {
5
- removeStorage("rpc-remote-peer");
6
- removeStorage("rpc-messages");
7
- };
8
- var setStorage = (key, value, local = true) => getStorageInstance(local).setItem(key, JSON.stringify(value));
9
- function getStorage(key, fallbackValue, local = true) {
10
- const value = getStorageInstance(local).getItem(key);
11
- if (value) {
12
- try {
13
- return JSON.parse(value);
14
- } catch (e) {
15
- removeStorage(key, local);
16
- }
17
- }
18
- if (fallbackValue !== void 0) setStorage(key, fallbackValue, local);
19
- return fallbackValue;
20
- }
21
-
22
- export { clearChat, getStorage, removeStorage, setStorage };