@peerbit/react 0.0.13 → 0.0.15
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/index.d.ts +4 -1
- package/lib/esm/index.js +4 -1
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/lockstorage.d.ts +4 -2
- package/lib/esm/lockstorage.js +32 -19
- package/lib/esm/lockstorage.js.map +1 -1
- package/lib/esm/useCount.d.ts +11 -0
- package/lib/esm/useCount.js +40 -0
- package/lib/esm/useCount.js.map +1 -0
- package/lib/esm/useLocal.d.ts +18 -2
- package/lib/esm/useLocal.js +45 -11
- package/lib/esm/useLocal.js.map +1 -1
- package/lib/esm/useOnline.d.ts +10 -0
- package/lib/esm/useOnline.js +52 -0
- package/lib/esm/useOnline.js.map +1 -0
- package/lib/esm/usePeer.d.ts +24 -5
- package/lib/esm/usePeer.js +106 -71
- package/lib/esm/usePeer.js.map +1 -1
- package/lib/esm/useProgram.d.ts +7 -2
- package/lib/esm/useProgram.js +36 -12
- package/lib/esm/useProgram.js.map +1 -1
- package/lib/esm/utils.d.ts +7 -3
- package/lib/esm/utils.js +58 -9
- package/lib/esm/utils.js.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +9 -1
- package/src/lockstorage.ts +47 -23
- package/src/useCount.tsx +60 -0
- package/src/useLocal.tsx +83 -17
- package/src/useOnline.tsx +66 -0
- package/src/usePeer.tsx +149 -103
- package/src/useProgram.tsx +43 -24
- package/src/utils.ts +75 -20
package/lib/esm/usePeer.js
CHANGED
|
@@ -3,7 +3,7 @@ import React, { useContext } from "react";
|
|
|
3
3
|
import { Peerbit } from "peerbit";
|
|
4
4
|
import { DirectSub } from "@peerbit/pubsub";
|
|
5
5
|
import { yamux } from "@chainsafe/libp2p-yamux";
|
|
6
|
-
import { getFreeKeypair,
|
|
6
|
+
import { getFreeKeypair, getClientId, inIframe, cookiesWhereClearedJustNow, } from "./utils.js";
|
|
7
7
|
import { noise } from "@chainsafe/libp2p-noise";
|
|
8
8
|
import { v4 as uuid } from "uuid";
|
|
9
9
|
import { FastMutex } from "./lockstorage.js";
|
|
@@ -15,6 +15,12 @@ import { webSockets } from "@libp2p/websockets";
|
|
|
15
15
|
import { circuitRelayTransport } from "@libp2p/circuit-relay-v2";
|
|
16
16
|
import * as filters from "@libp2p/websockets/filters";
|
|
17
17
|
import { detectIncognito } from "detectincognitojs";
|
|
18
|
+
export class ClientBusyError extends Error {
|
|
19
|
+
constructor(message) {
|
|
20
|
+
super(message);
|
|
21
|
+
this.name = "CreateClientError";
|
|
22
|
+
}
|
|
23
|
+
}
|
|
18
24
|
if (!window.name) {
|
|
19
25
|
window.name = uuid();
|
|
20
26
|
}
|
|
@@ -26,44 +32,97 @@ export const PeerProvider = (options) => {
|
|
|
26
32
|
const [persisted, setPersisted] = React.useState(undefined);
|
|
27
33
|
const [loading, setLoading] = React.useState(true);
|
|
28
34
|
const [connectionState, setConnectionState] = React.useState("disconnected");
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
|
|
35
|
+
const [tabIndex, setTabIndex] = React.useState(-1);
|
|
36
|
+
const [error, setError] = React.useState(undefined); // <-- error state
|
|
37
|
+
// Decide which options to use based on whether we're in an iframe.
|
|
38
|
+
// If options.top is defined, assume we have separate settings for iframe vs. host.
|
|
39
|
+
const nodeOptions = options.top
|
|
40
|
+
? inIframe()
|
|
41
|
+
? options.iframe
|
|
42
|
+
: { ...options, ...options.top } // we merge root and top options, TODO should this be made in a different way to prevent confusion about top props?
|
|
43
|
+
: options;
|
|
44
|
+
// If running as a proxy (iframe), expect a targetOrigin.
|
|
45
|
+
const computedTargetOrigin = nodeOptions.type === "proxy"
|
|
46
|
+
? nodeOptions.targetOrigin
|
|
47
|
+
: undefined;
|
|
48
|
+
const memo = React.useMemo(() => {
|
|
49
|
+
if (nodeOptions.type === "proxy") {
|
|
50
|
+
return {
|
|
51
|
+
type: "proxy",
|
|
52
|
+
peer,
|
|
53
|
+
promise,
|
|
54
|
+
loading,
|
|
55
|
+
status: connectionState,
|
|
56
|
+
persisted,
|
|
57
|
+
targetOrigin: computedTargetOrigin,
|
|
58
|
+
error,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
return {
|
|
63
|
+
type: "node",
|
|
64
|
+
peer,
|
|
65
|
+
promise,
|
|
66
|
+
loading,
|
|
67
|
+
status: connectionState,
|
|
68
|
+
persisted,
|
|
69
|
+
tabIndex,
|
|
70
|
+
error,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
}, [
|
|
32
74
|
loading,
|
|
75
|
+
promise,
|
|
33
76
|
connectionState,
|
|
34
|
-
|
|
35
|
-
persisted: persisted,
|
|
36
|
-
}), [
|
|
37
|
-
loading,
|
|
38
|
-
!!promise,
|
|
39
|
-
connectionState,
|
|
40
|
-
peer?.identity?.publicKey?.hashcode(),
|
|
77
|
+
peer,
|
|
41
78
|
persisted,
|
|
79
|
+
tabIndex,
|
|
80
|
+
computedTargetOrigin,
|
|
81
|
+
error,
|
|
42
82
|
]);
|
|
43
83
|
useMount(() => {
|
|
44
84
|
setLoading(true);
|
|
45
85
|
const fn = async () => {
|
|
46
86
|
await sodium.ready;
|
|
47
|
-
if (peer) {
|
|
48
|
-
await peer.stop();
|
|
49
|
-
setPeer(undefined);
|
|
50
|
-
}
|
|
51
87
|
let newPeer;
|
|
52
|
-
const nodeOptions = options.top
|
|
53
|
-
? inIframe()
|
|
54
|
-
? options.iframe
|
|
55
|
-
: options.top
|
|
56
|
-
: options;
|
|
57
88
|
if (nodeOptions.type !== "proxy") {
|
|
58
89
|
const releaseFirstLock = cookiesWhereClearedJustNow();
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
90
|
+
const sessionId = getClientId("session");
|
|
91
|
+
const mutex = new FastMutex({
|
|
92
|
+
clientId: sessionId,
|
|
93
|
+
timeout: 1e3,
|
|
94
|
+
});
|
|
95
|
+
if (nodeOptions.singleton) {
|
|
96
|
+
const localId = getClientId("local");
|
|
97
|
+
try {
|
|
98
|
+
const lockKey = localId + "-singleton";
|
|
99
|
+
globalThis.onbeforeunload = function () {
|
|
100
|
+
mutex.release(lockKey);
|
|
101
|
+
};
|
|
102
|
+
await mutex.lock(lockKey, () => true, {
|
|
103
|
+
replaceIfSameClient: true,
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
catch (error) {
|
|
107
|
+
console.error("Failed to lock singleton client", error);
|
|
108
|
+
throw new ClientBusyError("Failed to lock single client");
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
let nodeId;
|
|
112
|
+
if (nodeOptions.keypair) {
|
|
113
|
+
nodeId = nodeOptions.keypair;
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
const kp = await getFreeKeypair("", mutex, undefined, {
|
|
117
|
+
releaseFirstLock,
|
|
118
|
+
releaseLockIfSameId: true,
|
|
119
|
+
});
|
|
120
|
+
globalThis.onbeforeunload = function () {
|
|
121
|
+
mutex.release(kp.path);
|
|
122
|
+
};
|
|
123
|
+
nodeId = kp.key;
|
|
124
|
+
setTabIndex(kp.index);
|
|
125
|
+
}
|
|
67
126
|
const peerId = nodeId.toPeerId();
|
|
68
127
|
let directory = undefined;
|
|
69
128
|
if (!nodeOptions.inMemory &&
|
|
@@ -72,70 +131,39 @@ export const PeerProvider = (options) => {
|
|
|
72
131
|
setPersisted(persisted);
|
|
73
132
|
if (!persisted) {
|
|
74
133
|
setPersisted(false);
|
|
75
|
-
|
|
76
|
-
console.error("Request persistance but was not given permission by browser. Adding this site to your bookmarks or enabling push notifications might allow your chrome browser to persist data");
|
|
77
|
-
}
|
|
78
|
-
else {
|
|
79
|
-
console.error("Request persistance but was not given permission by browser.");
|
|
80
|
-
}
|
|
134
|
+
console.error("Request persistence but permission was not granted by browser.");
|
|
81
135
|
}
|
|
82
136
|
else {
|
|
83
137
|
directory = `./repo/${peerId.toString()}/`;
|
|
84
138
|
}
|
|
85
139
|
}
|
|
86
|
-
// We create a new directrory to make tab to tab communication go smoothly
|
|
87
140
|
console.log("Create client");
|
|
88
141
|
newPeer = await Peerbit.create({
|
|
89
142
|
libp2p: {
|
|
90
|
-
addresses: {
|
|
91
|
-
listen: [
|
|
92
|
-
"/p2p-circuit",
|
|
93
|
-
/* "/webrtc" */
|
|
94
|
-
], // TMP disable because flaky behaviour with libp2p 1.8.1
|
|
95
|
-
},
|
|
143
|
+
addresses: { listen: ["/p2p-circuit"] },
|
|
96
144
|
connectionEncrypters: [noise()],
|
|
97
|
-
peerId,
|
|
98
|
-
connectionManager: {
|
|
99
|
-
|
|
100
|
-
},
|
|
101
|
-
connectionMonitor: {
|
|
102
|
-
enabled: false,
|
|
103
|
-
},
|
|
145
|
+
peerId,
|
|
146
|
+
connectionManager: { maxConnections: 100 },
|
|
147
|
+
connectionMonitor: { enabled: false },
|
|
104
148
|
streamMuxers: [yamux()],
|
|
105
149
|
...(nodeOptions.network === "local"
|
|
106
150
|
? {
|
|
107
151
|
connectionGater: {
|
|
108
|
-
denyDialMultiaddr: () =>
|
|
109
|
-
// by default we refuse to dial local addresses from the browser since they
|
|
110
|
-
// are usually sent by remote peers broadcasting undialable multiaddrs but
|
|
111
|
-
// here we are explicitly connecting to a local node so do not deny dialing
|
|
112
|
-
// any discovered address
|
|
113
|
-
return false;
|
|
114
|
-
},
|
|
152
|
+
denyDialMultiaddr: () => false,
|
|
115
153
|
},
|
|
116
154
|
transports: [
|
|
117
|
-
|
|
118
|
-
webSockets({
|
|
119
|
-
filter: filters.all,
|
|
120
|
-
}),
|
|
155
|
+
webSockets({ filter: filters.all }),
|
|
121
156
|
circuitRelayTransport(),
|
|
122
|
-
/* webRTC(), */ // TMP disable because flaky behaviour with libp2p 1.8.1
|
|
123
157
|
],
|
|
124
158
|
}
|
|
125
159
|
: {
|
|
126
160
|
transports: [
|
|
127
161
|
webSockets({ filter: filters.wss }),
|
|
128
162
|
circuitRelayTransport(),
|
|
129
|
-
/* webRTC(), */ // TMP disable because flaky behaviour with libp2p 1.8.1
|
|
130
163
|
],
|
|
131
164
|
}),
|
|
132
165
|
services: {
|
|
133
|
-
pubsub: (c) => new DirectSub(c, {
|
|
134
|
-
canRelayMessage: true,
|
|
135
|
-
/* connectionManager: {
|
|
136
|
-
autoDial: false,
|
|
137
|
-
}, */
|
|
138
|
-
}),
|
|
166
|
+
pubsub: (c) => new DirectSub(c, { canRelayMessage: true }),
|
|
139
167
|
identify: identify(),
|
|
140
168
|
},
|
|
141
169
|
},
|
|
@@ -147,7 +175,6 @@ export const PeerProvider = (options) => {
|
|
|
147
175
|
network: nodeOptions.network === "local" ? "local" : "remote",
|
|
148
176
|
});
|
|
149
177
|
setConnectionState("connecting");
|
|
150
|
-
// Resolve bootstrap nodes async (we want to return before this is done)
|
|
151
178
|
const connectFn = async () => {
|
|
152
179
|
try {
|
|
153
180
|
if (nodeOptions.network === "local") {
|
|
@@ -155,7 +182,6 @@ export const PeerProvider = (options) => {
|
|
|
155
182
|
(await (await fetch("http://localhost:8082/peer/id")).text()));
|
|
156
183
|
}
|
|
157
184
|
else {
|
|
158
|
-
// TODO fix types. When proxy client this will not be available
|
|
159
185
|
if (nodeOptions.bootstrap) {
|
|
160
186
|
for (const addr of nodeOptions.bootstrap) {
|
|
161
187
|
await newPeer.dial(addr);
|
|
@@ -180,18 +206,27 @@ export const PeerProvider = (options) => {
|
|
|
180
206
|
promise.then(() => {
|
|
181
207
|
console.log("Bootstrap done");
|
|
182
208
|
});
|
|
183
|
-
// Make sure data flow as expected between tabs and windows locally (offline states)
|
|
184
209
|
if (nodeOptions.waitForConnnected !== false) {
|
|
185
210
|
await promise;
|
|
186
211
|
}
|
|
187
212
|
}
|
|
188
213
|
else {
|
|
214
|
+
// When in proxy mode (iframe), use the provided targetOrigin.
|
|
189
215
|
newPeer = await createClient(nodeOptions.targetOrigin);
|
|
190
216
|
}
|
|
191
217
|
setPeer(newPeer);
|
|
192
218
|
setLoading(false);
|
|
193
219
|
};
|
|
194
|
-
|
|
220
|
+
const fnWithErrorHandling = async () => {
|
|
221
|
+
try {
|
|
222
|
+
await fn();
|
|
223
|
+
}
|
|
224
|
+
catch (error) {
|
|
225
|
+
setError(error);
|
|
226
|
+
setLoading(false);
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
setPromise(fnWithErrorHandling());
|
|
195
230
|
});
|
|
196
231
|
return (_jsx(PeerContext.Provider, { value: memo, children: options.children }));
|
|
197
232
|
};
|
package/lib/esm/usePeer.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"usePeer.js","sourceRoot":"","sources":["../../src/usePeer.tsx"],"names":[],"mappings":";AAAA,OAAO,KAAK,EAAE,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC;AAE1C,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAClC,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,KAAK,EAAE,MAAM,yBAAyB,CAAC;AAChD,OAAO,EACH,cAAc,EACd,
|
|
1
|
+
{"version":3,"file":"usePeer.js","sourceRoot":"","sources":["../../src/usePeer.tsx"],"names":[],"mappings":";AAAA,OAAO,KAAK,EAAE,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC;AAE1C,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAClC,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,KAAK,EAAE,MAAM,yBAAyB,CAAC;AAChD,OAAO,EACH,cAAc,EACd,WAAW,EACX,QAAQ,EACR,0BAA0B,GAC7B,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,KAAK,EAAE,MAAM,yBAAyB,CAAC;AAChD,OAAO,EAAE,EAAE,IAAI,IAAI,EAAE,MAAM,MAAM,CAAC;AAElC,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;A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AAC;gBACD,MAAM,EAAE,EAAE,CAAC;YACf,CAAC;YAAC,OAAO,KAAU,EAAE,CAAC;gBAClB,QAAQ,CAAC,KAAK,CAAC,CAAC;gBAChB,UAAU,CAAC,KAAK,CAAC,CAAC;YACtB,CAAC;QACL,CAAC,CAAC;QACF,UAAU,CAAC,mBAAmB,EAAE,CAAC,CAAC;IACtC,CAAC,CAAC,CAAC;IAEH,OAAO,CACH,KAAC,WAAW,CAAC,QAAQ,IAAC,KAAK,EAAE,IAAI,YAC5B,OAAO,CAAC,QAAQ,GACE,CAC1B,CAAC;AACN,CAAC,CAAC"}
|
package/lib/esm/useProgram.d.ts
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
import { Program, OpenOptions, ProgramEvents } from "@peerbit/program";
|
|
2
|
+
import { PublicSignKey } from "@peerbit/crypto";
|
|
2
3
|
type ExtractArgs<T> = T extends Program<infer Args> ? Args : never;
|
|
3
4
|
type ExtractEvents<T> = T extends Program<any, infer Events> ? Events : never;
|
|
4
|
-
export declare const useProgram: <P extends Program<ExtractArgs<P>, ExtractEvents<P>> & Program<any, ProgramEvents>>(addressOrOpen?: P | string, options?: OpenOptions<P>
|
|
5
|
+
export declare const useProgram: <P extends Program<ExtractArgs<P>, ExtractEvents<P>> & Program<any, ProgramEvents>>(addressOrOpen?: P | string, options?: OpenOptions<P> & {
|
|
6
|
+
id?: string;
|
|
7
|
+
keepOpenOnUnmount?: boolean;
|
|
8
|
+
}) => {
|
|
5
9
|
program: P | undefined;
|
|
6
10
|
session: any;
|
|
7
11
|
loading: boolean;
|
|
8
12
|
promise: Promise<P> | undefined;
|
|
9
|
-
|
|
13
|
+
peers: PublicSignKey[];
|
|
14
|
+
id: string | undefined;
|
|
10
15
|
};
|
|
11
16
|
export {};
|
package/lib/esm/useProgram.js
CHANGED
|
@@ -1,21 +1,25 @@
|
|
|
1
1
|
import { usePeer } from "./usePeer.js";
|
|
2
2
|
import { useEffect, useReducer, useRef, useState } from "react";
|
|
3
|
-
const
|
|
3
|
+
const addressOrDefined = (p) => {
|
|
4
4
|
try {
|
|
5
5
|
return p?.address;
|
|
6
6
|
}
|
|
7
7
|
catch (error) {
|
|
8
|
-
return
|
|
8
|
+
return !!p;
|
|
9
9
|
}
|
|
10
10
|
};
|
|
11
11
|
export const useProgram = (addressOrOpen, options) => {
|
|
12
12
|
const { peer } = usePeer();
|
|
13
13
|
let [program, setProgram] = useState();
|
|
14
|
+
const [id, setId] = useState(options?.id);
|
|
14
15
|
let [loading, setLoading] = useState(true);
|
|
15
16
|
const [session, forceUpdate] = useReducer((x) => x + 1, 0);
|
|
16
17
|
let programLoadingRef = useRef();
|
|
17
|
-
const [
|
|
18
|
+
const [peers, setPeers] = useState([]);
|
|
18
19
|
let closingRef = useRef(Promise.resolve());
|
|
20
|
+
/* if (options?.debug) {
|
|
21
|
+
console.log("useProgram", addressOrOpen, options);
|
|
22
|
+
} */
|
|
19
23
|
useEffect(() => {
|
|
20
24
|
if (!peer || !addressOrOpen) {
|
|
21
25
|
return;
|
|
@@ -28,17 +32,28 @@ export const useProgram = (addressOrOpen, options) => {
|
|
|
28
32
|
.then((p) => {
|
|
29
33
|
changeListener = () => {
|
|
30
34
|
p.getReady().then((set) => {
|
|
31
|
-
|
|
35
|
+
setPeers([...set.values()]);
|
|
32
36
|
});
|
|
33
37
|
};
|
|
34
38
|
p.events.addEventListener("join", changeListener);
|
|
35
39
|
p.events.addEventListener("leave", changeListener);
|
|
36
|
-
p.getReady()
|
|
37
|
-
|
|
40
|
+
p.getReady()
|
|
41
|
+
.then((set) => {
|
|
42
|
+
setPeers([...set.values()]);
|
|
43
|
+
})
|
|
44
|
+
.catch((e) => {
|
|
45
|
+
console.log("Error getReady()", e);
|
|
38
46
|
});
|
|
39
47
|
setProgram(p);
|
|
40
48
|
forceUpdate();
|
|
49
|
+
if (options?.id) {
|
|
50
|
+
setId(p.address);
|
|
51
|
+
}
|
|
41
52
|
return p;
|
|
53
|
+
})
|
|
54
|
+
.catch((e) => {
|
|
55
|
+
console.error("failed to open", e);
|
|
56
|
+
throw e;
|
|
42
57
|
})
|
|
43
58
|
.finally(() => {
|
|
44
59
|
setLoading(false);
|
|
@@ -50,28 +65,37 @@ export const useProgram = (addressOrOpen, options) => {
|
|
|
50
65
|
// TODO don't close on reopen the same db?
|
|
51
66
|
if (programLoadingRef.current) {
|
|
52
67
|
closingRef.current =
|
|
53
|
-
programLoadingRef.current.then((p) =>
|
|
54
|
-
|
|
55
|
-
|
|
68
|
+
programLoadingRef.current.then((p) => {
|
|
69
|
+
const unsubscribe = () => {
|
|
70
|
+
p.events.removeEventListener("join", changeListener);
|
|
71
|
+
p.events.removeEventListener("leave", changeListener);
|
|
72
|
+
};
|
|
73
|
+
if (options?.keepOpenOnUnmount) {
|
|
74
|
+
unsubscribe();
|
|
75
|
+
}
|
|
56
76
|
if (programLoadingRef.current === startRef) {
|
|
57
77
|
setProgram(undefined);
|
|
58
78
|
programLoadingRef.current = undefined;
|
|
79
|
+
return;
|
|
59
80
|
}
|
|
60
|
-
|
|
81
|
+
return p.close().then(unsubscribe);
|
|
82
|
+
}) || Promise.resolve();
|
|
61
83
|
}
|
|
62
84
|
};
|
|
63
85
|
}, [
|
|
64
86
|
peer?.identity.publicKey.hashcode(),
|
|
87
|
+
options?.id,
|
|
65
88
|
typeof addressOrOpen === "string"
|
|
66
89
|
? addressOrOpen
|
|
67
|
-
:
|
|
90
|
+
: addressOrDefined(addressOrOpen),
|
|
68
91
|
]);
|
|
69
92
|
return {
|
|
70
93
|
program,
|
|
71
94
|
session,
|
|
72
95
|
loading,
|
|
73
96
|
promise: programLoadingRef.current,
|
|
74
|
-
|
|
97
|
+
peers,
|
|
98
|
+
id,
|
|
75
99
|
};
|
|
76
100
|
};
|
|
77
101
|
//# sourceMappingURL=useProgram.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useProgram.js","sourceRoot":"","sources":["../../src/useProgram.tsx"],"names":[],"mappings":"AACA,OAAO,EA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AC,CAAC,aAAa;YACf,CAAC,CAAC,gBAAgB,CAAC,aAAa,CAAC;KACxC,CAAC,CAAC;IACH,OAAO;QACH,OAAO;QACP,OAAO;QACP,OAAO;QACP,OAAO,EAAE,iBAAiB,CAAC,OAAO;QAClC,KAAK;QACL,EAAE;KACL,CAAC;AACN,CAAC,CAAC"}
|
package/lib/esm/utils.d.ts
CHANGED
|
@@ -1,15 +1,19 @@
|
|
|
1
1
|
import { Ed25519Keypair } from "@peerbit/crypto";
|
|
2
2
|
import { FastMutex } from "./lockstorage";
|
|
3
3
|
export declare const cookiesWhereClearedJustNow: () => boolean;
|
|
4
|
-
export declare const
|
|
5
|
-
export declare const releaseKey: (path: string, lock
|
|
6
|
-
export declare const getFreeKeypair: (id
|
|
4
|
+
export declare const getClientId: (type: "session" | "local") => string;
|
|
5
|
+
export declare const releaseKey: (path: string, lock: FastMutex) => void;
|
|
6
|
+
export declare const getFreeKeypair: (id: string | undefined, lock: FastMutex, lockCondition?: () => boolean, options?: {
|
|
7
7
|
releaseLockIfSameId?: boolean;
|
|
8
8
|
releaseFirstLock?: boolean;
|
|
9
9
|
}) => Promise<{
|
|
10
|
+
index: number;
|
|
10
11
|
path: string;
|
|
11
12
|
key: Ed25519Keypair;
|
|
12
13
|
}>;
|
|
13
14
|
export declare const getAllKeyPairs: (id?: string) => Promise<Ed25519Keypair[]>;
|
|
14
15
|
export declare const getKeypair: (keyName: string) => Promise<Ed25519Keypair>;
|
|
15
16
|
export declare const inIframe: () => boolean;
|
|
17
|
+
export declare function debounceLeadingTrailing<T extends (this: any, ...args: any[]) => void>(func: T, delay: number): ((this: ThisParameterType<T>, ...args: Parameters<T>) => void) & {
|
|
18
|
+
cancel: () => void;
|
|
19
|
+
};
|
package/lib/esm/utils.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { serialize, deserialize } from "@dao-xyz/borsh";
|
|
2
2
|
import { Ed25519Keypair, toBase64, fromBase64 } from "@peerbit/crypto";
|
|
3
|
-
import { FastMutex } from "./lockstorage";
|
|
4
3
|
import { v4 as uuid } from "uuid";
|
|
5
4
|
import sodium from "libsodium-wrappers";
|
|
6
|
-
const
|
|
5
|
+
const CLIENT_ID_STORAGE_KEY = "CLIENT_ID";
|
|
7
6
|
export const cookiesWhereClearedJustNow = () => {
|
|
8
7
|
const lastPersistedAt = localStorage.getItem("lastPersistedAt");
|
|
9
8
|
if (lastPersistedAt) {
|
|
@@ -12,23 +11,24 @@ export const cookiesWhereClearedJustNow = () => {
|
|
|
12
11
|
localStorage.setItem("lastPersistedAt", Date.now().toString());
|
|
13
12
|
return true;
|
|
14
13
|
};
|
|
15
|
-
export const
|
|
16
|
-
const
|
|
14
|
+
export const getClientId = (type) => {
|
|
15
|
+
const storage = type === "session" ? sessionStorage : localStorage;
|
|
16
|
+
const idFromStorage = storage.getItem(CLIENT_ID_STORAGE_KEY);
|
|
17
17
|
if (idFromStorage) {
|
|
18
18
|
return idFromStorage;
|
|
19
19
|
}
|
|
20
20
|
else {
|
|
21
21
|
const id = uuid(); // generate unique UUID
|
|
22
|
-
|
|
22
|
+
storage.setItem(CLIENT_ID_STORAGE_KEY, id);
|
|
23
23
|
return id;
|
|
24
24
|
}
|
|
25
25
|
};
|
|
26
26
|
const ID_COUNTER_KEY = "idc/";
|
|
27
27
|
const getKeyId = (prefix, id) => prefix + "/" + id;
|
|
28
|
-
export const releaseKey = (path, lock
|
|
28
|
+
export const releaseKey = (path, lock) => {
|
|
29
29
|
lock.release(path);
|
|
30
30
|
};
|
|
31
|
-
export const getFreeKeypair = async (id = "", lock
|
|
31
|
+
export const getFreeKeypair = async (id = "", lock, lockCondition = () => true, options) => {
|
|
32
32
|
await sodium.ready;
|
|
33
33
|
const idCounterKey = ID_COUNTER_KEY + id;
|
|
34
34
|
await lock.lock(idCounterKey, () => true);
|
|
@@ -36,7 +36,16 @@ export const getFreeKeypair = async (id = "", lock = new FastMutex({ clientId: g
|
|
|
36
36
|
for (let i = 0; i < 10000; i++) {
|
|
37
37
|
const key = getKeyId(id, i);
|
|
38
38
|
let lockedInfo = lock.getLockedInfo(key);
|
|
39
|
-
console.log(
|
|
39
|
+
/* console.log(
|
|
40
|
+
"KEY KEY AT",
|
|
41
|
+
key,
|
|
42
|
+
id,
|
|
43
|
+
i,
|
|
44
|
+
lockedInfo,
|
|
45
|
+
lockedInfo === lock.clientId,
|
|
46
|
+
options
|
|
47
|
+
);
|
|
48
|
+
*/
|
|
40
49
|
if (lockedInfo) {
|
|
41
50
|
if ((lockedInfo === lock.clientId &&
|
|
42
51
|
options?.releaseLockIfSameId) ||
|
|
@@ -47,11 +56,11 @@ export const getFreeKeypair = async (id = "", lock = new FastMutex({ clientId: g
|
|
|
47
56
|
continue;
|
|
48
57
|
}
|
|
49
58
|
}
|
|
50
|
-
console.log("aquire id at", i);
|
|
51
59
|
await lock.lock(key, lockCondition);
|
|
52
60
|
localStorage.setItem(idCounterKey, JSON.stringify(Math.max(idCounter, i + 1)));
|
|
53
61
|
await lock.release(idCounterKey);
|
|
54
62
|
return {
|
|
63
|
+
index: i,
|
|
55
64
|
path: key,
|
|
56
65
|
key: await getKeypair(key),
|
|
57
66
|
};
|
|
@@ -105,4 +114,44 @@ export const inIframe = () => {
|
|
|
105
114
|
return true;
|
|
106
115
|
}
|
|
107
116
|
};
|
|
117
|
+
export function debounceLeadingTrailing(func, delay) {
|
|
118
|
+
let timeoutId = null;
|
|
119
|
+
let lastArgs = null;
|
|
120
|
+
let lastThis;
|
|
121
|
+
let pendingTrailing = false;
|
|
122
|
+
const debounced = function (...args) {
|
|
123
|
+
if (!timeoutId) {
|
|
124
|
+
// Leading call: no timer means this is the first call in this period.
|
|
125
|
+
func.apply(this, args);
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
// Subsequent calls during the delay mark that a trailing call is needed.
|
|
129
|
+
pendingTrailing = true;
|
|
130
|
+
}
|
|
131
|
+
// Always update with the most recent context and arguments.
|
|
132
|
+
lastArgs = args;
|
|
133
|
+
lastThis = this;
|
|
134
|
+
// Reset the timer.
|
|
135
|
+
if (timeoutId) {
|
|
136
|
+
clearTimeout(timeoutId);
|
|
137
|
+
}
|
|
138
|
+
timeoutId = setTimeout(() => {
|
|
139
|
+
timeoutId = null;
|
|
140
|
+
// If there were any calls during the delay, call the function on the trailing edge.
|
|
141
|
+
if (pendingTrailing && lastArgs) {
|
|
142
|
+
func.apply(lastThis, lastArgs);
|
|
143
|
+
}
|
|
144
|
+
// Reset the trailing flag after the trailing call.
|
|
145
|
+
pendingTrailing = false;
|
|
146
|
+
}, delay);
|
|
147
|
+
};
|
|
148
|
+
debounced.cancel = () => {
|
|
149
|
+
if (timeoutId) {
|
|
150
|
+
clearTimeout(timeoutId);
|
|
151
|
+
timeoutId = null;
|
|
152
|
+
}
|
|
153
|
+
pendingTrailing = false;
|
|
154
|
+
};
|
|
155
|
+
return debounced;
|
|
156
|
+
}
|
|
108
157
|
//# sourceMappingURL=utils.js.map
|
package/lib/esm/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AACxD,OAAO,EAAE,cAAc,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AACxD,OAAO,EAAE,cAAc,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAEvE,OAAO,EAAE,EAAE,IAAI,IAAI,EAAE,MAAM,MAAM,CAAC;AAClC,OAAO,MAAM,MAAM,oBAAoB,CAAC;AAExC,MAAM,qBAAqB,GAAG,WAAW,CAAC;AAC1C,MAAM,CAAC,MAAM,0BAA0B,GAA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EAAE;YACxB,SAAS,GAAG,IAAI,CAAC;YACjB,oFAAoF;YACpF,IAAI,eAAe,IAAI,QAAQ,EAAE,CAAC;gBAC9B,IAAI,CAAC,KAAK,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;YACnC,CAAC;YACD,mDAAmD;YACnD,eAAe,GAAG,KAAK,CAAC;QAC5B,CAAC,EAAE,KAAK,CAAC,CAAC;IACd,CAEC,CAAC;IAEF,SAAS,CAAC,MAAM,GAAG,GAAG,EAAE;QACpB,IAAI,SAAS,EAAE,CAAC;YACZ,YAAY,CAAC,SAAS,CAAC,CAAC;YACxB,SAAS,GAAG,IAAI,CAAC;QACrB,CAAC;QACD,eAAe,GAAG,KAAK,CAAC;IAC5B,CAAC,CAAC;IAEF,OAAO,SAAS,CAAC;AACrB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@peerbit/react",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.15",
|
|
4
4
|
"homepage": "https://dao-xyz.github.io/peerbit-examples",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "lib/esm/index.js",
|
|
@@ -70,5 +70,5 @@
|
|
|
70
70
|
"last 1 safari version"
|
|
71
71
|
]
|
|
72
72
|
},
|
|
73
|
-
"gitHead": "
|
|
73
|
+
"gitHead": "bbc31ba767abdd37bc0b44c000910555b42dfd78"
|
|
74
74
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export {
|
|
2
|
+
PeerProvider,
|
|
3
|
+
PeerContext,
|
|
4
|
+
usePeer,
|
|
5
|
+
ClientBusyError,
|
|
6
|
+
} from "./usePeer.js";
|
|
2
7
|
export * from "./utils.js";
|
|
3
8
|
export { FastMutex } from "./lockstorage.js";
|
|
4
9
|
export { useProgram } from "./useProgram.js";
|
|
5
10
|
export { useLocal } from "./useLocal.js";
|
|
11
|
+
export { useOnline } from "./useOnline.js";
|
|
12
|
+
export { useCount } from "./useCount.js";
|
|
13
|
+
export { debounceLeadingTrailing } from "./utils.js";
|