react-peer-chat 0.11.7 → 0.11.9
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.
- package/dist/chunks/{chunk-24RW4ODA.js → chunk-KLBS4XTE.js} +5 -2
- package/dist/chunks/chunk-MRYWIJDZ.js +46 -0
- package/dist/chunks/{chunk-7UUTQKOW.js → chunk-SO7XYGB7.js} +1 -1
- package/dist/components.js +3 -3
- package/dist/hooks.js +2 -2
- package/dist/index.js +3 -3
- package/dist/lib/storage.d.ts +10 -5
- package/dist/lib/storage.js +1 -1
- package/package.json +1 -1
- package/dist/chunks/chunk-ZYFPSCFE.js +0 -22
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { getStorage, setStorage } from './chunk-
|
|
1
|
+
import { getStorage, subscribeToStorage, setStorage } from './chunk-MRYWIJDZ.js';
|
|
2
2
|
import { __spreadValues, __async, __spreadProps } from './chunk-FZ4QVG4I.js';
|
|
3
3
|
import { useState, useRef, useMemo, useEffect } from 'react';
|
|
4
4
|
|
|
@@ -288,7 +288,7 @@ function useMessages() {
|
|
|
288
288
|
return [messages, setMessages, addMessage];
|
|
289
289
|
}
|
|
290
290
|
function useStorage(key, initialValue, local = false) {
|
|
291
|
-
const [storedValue, setStoredValue] = useState(() => typeof window === "undefined" ? initialValue : getStorage(key,
|
|
291
|
+
const [storedValue, setStoredValue] = useState(() => typeof window === "undefined" ? initialValue : getStorage(key, local, initialValue));
|
|
292
292
|
const setValue = (value) => {
|
|
293
293
|
setStoredValue((prev) => {
|
|
294
294
|
const next = isSetStateFunction(value) ? value(prev) : value;
|
|
@@ -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,46 @@
|
|
|
1
|
+
// src/lib/storage.ts
|
|
2
|
+
var listeners = /* @__PURE__ */ new Map();
|
|
3
|
+
function clearChat() {
|
|
4
|
+
removeStorage("rpc-remote-peer", false);
|
|
5
|
+
removeStorage("rpc-messages", false);
|
|
6
|
+
}
|
|
7
|
+
var getStorageInstance = (local) => local ? localStorage : sessionStorage;
|
|
8
|
+
var getNamespacedKey = (key, local) => `${local ? "local" : "session"}:${key}`;
|
|
9
|
+
function getStorage(key, local, fallbackValue) {
|
|
10
|
+
if (typeof window === "undefined") return fallbackValue;
|
|
11
|
+
const value = getStorageInstance(local).getItem(key);
|
|
12
|
+
if (value) {
|
|
13
|
+
try {
|
|
14
|
+
return JSON.parse(value);
|
|
15
|
+
} catch (e) {
|
|
16
|
+
removeStorage(key, local);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
if (fallbackValue !== void 0) setStorage(key, fallbackValue, local);
|
|
20
|
+
return fallbackValue;
|
|
21
|
+
}
|
|
22
|
+
function publish(key, local, value) {
|
|
23
|
+
const callbacks = listeners.get(getNamespacedKey(key, local));
|
|
24
|
+
if (callbacks) callbacks.forEach((callback) => callback(value));
|
|
25
|
+
}
|
|
26
|
+
function removeStorage(key, local) {
|
|
27
|
+
getStorageInstance(local).removeItem(key);
|
|
28
|
+
publish(key, local);
|
|
29
|
+
}
|
|
30
|
+
function setStorage(key, value, local) {
|
|
31
|
+
if (typeof value === "function") value = value(getStorage(key, local));
|
|
32
|
+
getStorageInstance(local).setItem(key, JSON.stringify(value));
|
|
33
|
+
publish(key, local, value);
|
|
34
|
+
}
|
|
35
|
+
function subscribeToStorage(key, local, callback) {
|
|
36
|
+
key = getNamespacedKey(key, local);
|
|
37
|
+
if (!listeners.has(key)) listeners.set(key, /* @__PURE__ */ new Set());
|
|
38
|
+
const set = listeners.get(key);
|
|
39
|
+
set.add(callback);
|
|
40
|
+
return () => {
|
|
41
|
+
set.delete(callback);
|
|
42
|
+
if (set.size === 0) listeners.delete(key);
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export { clearChat, getStorage, removeStorage, setStorage, subscribeToStorage };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useChat } from './chunk-
|
|
1
|
+
import { useChat } from './chunk-KLBS4XTE.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';
|
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-SO7XYGB7.js';
|
|
2
|
+
import './chunks/chunk-KLBS4XTE.js';
|
|
3
3
|
import './chunks/chunk-QIPTWGEX.js';
|
|
4
|
-
import './chunks/chunk-
|
|
4
|
+
import './chunks/chunk-MRYWIJDZ.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-
|
|
2
|
-
import './chunks/chunk-
|
|
1
|
+
export { useAudio, useChat, useMessages, useStorage } from './chunks/chunk-KLBS4XTE.js';
|
|
2
|
+
import './chunks/chunk-MRYWIJDZ.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-
|
|
2
|
-
export { useChat } from './chunks/chunk-
|
|
1
|
+
export { Chat as default } from './chunks/chunk-SO7XYGB7.js';
|
|
2
|
+
export { useChat } from './chunks/chunk-KLBS4XTE.js';
|
|
3
3
|
import './chunks/chunk-QIPTWGEX.js';
|
|
4
|
-
export { clearChat } from './chunks/chunk-
|
|
4
|
+
export { clearChat } from './chunks/chunk-MRYWIJDZ.js';
|
|
5
5
|
import './chunks/chunk-FZ4QVG4I.js';
|
package/dist/lib/storage.d.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
declare function getStorage<T>(key: string, fallbackValue?: T, local?: boolean): T | undefined;
|
|
1
|
+
import { VoidFunction } from '../types.js';
|
|
2
|
+
import 'peerjs';
|
|
3
|
+
import 'react';
|
|
5
4
|
|
|
6
|
-
|
|
5
|
+
declare function clearChat(): void;
|
|
6
|
+
declare function getStorage<T>(key: string, local: boolean, fallbackValue?: T): T | undefined;
|
|
7
|
+
declare function removeStorage(key: string, local: boolean): void;
|
|
8
|
+
declare function setStorage(key: string, value: unknown, local: boolean): void;
|
|
9
|
+
declare function subscribeToStorage<T>(key: string, local: boolean, callback: (value: T) => void): VoidFunction;
|
|
10
|
+
|
|
11
|
+
export { clearChat, getStorage, removeStorage, setStorage, subscribeToStorage };
|
package/dist/lib/storage.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { clearChat, getStorage, removeStorage, setStorage } from '../chunks/chunk-
|
|
1
|
+
export { clearChat, getStorage, removeStorage, setStorage, subscribeToStorage } from '../chunks/chunk-MRYWIJDZ.js';
|
|
2
2
|
import '../chunks/chunk-FZ4QVG4I.js';
|
package/package.json
CHANGED
|
@@ -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 };
|