@peerbit/react 0.0.9 → 0.0.11
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/lib/esm/__tests__/lockstorage.test.js +39 -35
- package/lib/esm/__tests__/lockstorage.test.js.map +1 -1
- package/lib/esm/__tests__/utils.test.js +21 -17
- package/lib/esm/__tests__/utils.test.js.map +1 -1
- package/lib/esm/index.d.ts +1 -0
- package/lib/esm/index.js +1 -0
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/lockstorage.d.ts +2 -2
- package/lib/esm/lockstorage.js.map +1 -1
- package/lib/esm/useLocal.d.ts +2 -0
- package/lib/esm/useLocal.js +30 -0
- package/lib/esm/useLocal.js.map +1 -0
- package/lib/esm/usePeer.js +24 -22
- package/lib/esm/usePeer.js.map +1 -1
- package/lib/esm/useProgram.d.ts +1 -1
- package/lib/esm/useProgram.js +11 -9
- package/lib/esm/useProgram.js.map +1 -1
- package/lib/esm/utils.d.ts +6 -2
- package/lib/esm/utils.js +16 -4
- package/lib/esm/utils.js.map +1 -1
- package/package.json +10 -9
- package/src/__tests__/lockstorage.test.ts +47 -38
- package/src/__tests__/utils.test.ts +23 -17
- package/src/index.ts +1 -0
- package/src/lockstorage.ts +1 -1
- package/src/useLocal.tsx +35 -0
- package/src/usePeer.tsx +31 -21
- package/src/useProgram.tsx +20 -11
- package/src/utils.ts +31 -4
package/src/utils.ts
CHANGED
|
@@ -4,13 +4,23 @@ import { FastMutex } from "./lockstorage";
|
|
|
4
4
|
import { v4 as uuid } from "uuid";
|
|
5
5
|
import sodium from "libsodium-wrappers";
|
|
6
6
|
|
|
7
|
+
const TAB_ID_KEY = "TAB_ID";
|
|
8
|
+
export const cookiesWhereClearedJustNow = () => {
|
|
9
|
+
const lastPersistedAt = localStorage.getItem("lastPersistedAt");
|
|
10
|
+
if (lastPersistedAt) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
localStorage.setItem("lastPersistedAt", Date.now().toString());
|
|
14
|
+
return true;
|
|
15
|
+
};
|
|
16
|
+
|
|
7
17
|
export const getTabId = () => {
|
|
8
|
-
const idFromStorage = sessionStorage.getItem(
|
|
18
|
+
const idFromStorage = sessionStorage.getItem(TAB_ID_KEY);
|
|
9
19
|
if (idFromStorage) {
|
|
10
20
|
return idFromStorage;
|
|
11
21
|
} else {
|
|
12
22
|
const id = uuid(); // generate unique UUID
|
|
13
|
-
sessionStorage.setItem(
|
|
23
|
+
sessionStorage.setItem(TAB_ID_KEY, id);
|
|
14
24
|
return id;
|
|
15
25
|
}
|
|
16
26
|
};
|
|
@@ -30,7 +40,10 @@ export const getFreeKeypair = async (
|
|
|
30
40
|
id: string = "",
|
|
31
41
|
lock: FastMutex = new FastMutex({ clientId: getTabId() }),
|
|
32
42
|
lockCondition: () => boolean = () => true,
|
|
33
|
-
|
|
43
|
+
options?: {
|
|
44
|
+
releaseLockIfSameId?: boolean;
|
|
45
|
+
releaseFirstLock?: boolean;
|
|
46
|
+
}
|
|
34
47
|
) => {
|
|
35
48
|
await sodium.ready;
|
|
36
49
|
const idCounterKey = ID_COUNTER_KEY + id;
|
|
@@ -39,8 +52,22 @@ export const getFreeKeypair = async (
|
|
|
39
52
|
for (let i = 0; i < 10000; i++) {
|
|
40
53
|
const key = getKeyId(id, i);
|
|
41
54
|
let lockedInfo = lock.getLockedInfo(key);
|
|
55
|
+
console.log(
|
|
56
|
+
"KEY KEY AT",
|
|
57
|
+
key,
|
|
58
|
+
id,
|
|
59
|
+
i,
|
|
60
|
+
lockedInfo,
|
|
61
|
+
lockedInfo === lock.clientId,
|
|
62
|
+
options
|
|
63
|
+
);
|
|
64
|
+
|
|
42
65
|
if (lockedInfo) {
|
|
43
|
-
if (
|
|
66
|
+
if (
|
|
67
|
+
(lockedInfo === lock.clientId &&
|
|
68
|
+
options?.releaseLockIfSameId) ||
|
|
69
|
+
options?.releaseFirstLock
|
|
70
|
+
) {
|
|
44
71
|
await lock.release(key); // Release lock
|
|
45
72
|
} else {
|
|
46
73
|
continue;
|