@peerbit/react 0.0.18 → 0.0.20
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 +1 -1
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/usePeer.d.ts +5 -2
- package/lib/esm/usePeer.js +31 -26
- package/lib/esm/usePeer.js.map +1 -1
- package/lib/esm/useQuery.d.ts +3 -0
- package/lib/esm/useQuery.js +52 -16
- package/lib/esm/useQuery.js.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +1 -0
- package/src/usePeer.tsx +38 -28
- package/src/useQuery.tsx +78 -16
package/lib/esm/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { PeerProvider, PeerContext, usePeer, ClientBusyError, } from "./usePeer.js";
|
|
1
|
+
export { PeerProvider, PeerContext, usePeer, ClientBusyError, type NetworkOption, } from "./usePeer.js";
|
|
2
2
|
export * from "./utils.js";
|
|
3
3
|
export { FastMutex } from "./lockstorage.js";
|
|
4
4
|
export { useProgram } from "./useProgram.js";
|
package/lib/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,YAAY,EACZ,WAAW,EACX,OAAO,EACP,eAAe,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,YAAY,EACZ,WAAW,EACX,OAAO,EACP,eAAe,GAElB,MAAM,cAAc,CAAC;AACtB,cAAc,YAAY,CAAC;AAC3B,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC7C,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,QAAQ,IAAI,QAAQ,EAAE,MAAM,eAAe,CAAC;AACrD,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,uBAAuB,EAAE,MAAM,YAAY,CAAC"}
|
package/lib/esm/usePeer.d.ts
CHANGED
|
@@ -35,12 +35,15 @@ type IFrameOptions = {
|
|
|
35
35
|
type: "proxy";
|
|
36
36
|
targetOrigin: string;
|
|
37
37
|
};
|
|
38
|
+
export type NetworkOption = {
|
|
39
|
+
type: "local" | "remote";
|
|
40
|
+
bootstrap?: (Multiaddr | string)[];
|
|
41
|
+
};
|
|
38
42
|
type NodeOptions = {
|
|
39
43
|
type?: "node";
|
|
40
|
-
network: "local" | "remote";
|
|
44
|
+
network: "local" | "remote" | NetworkOption;
|
|
41
45
|
waitForConnnected?: boolean;
|
|
42
46
|
keypair?: Ed25519Keypair;
|
|
43
|
-
bootstrap?: (Multiaddr | string)[];
|
|
44
47
|
host?: boolean;
|
|
45
48
|
singleton?: boolean;
|
|
46
49
|
};
|
package/lib/esm/usePeer.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
2
|
import React, { useContext } from "react";
|
|
3
3
|
import { Peerbit } from "peerbit";
|
|
4
|
-
import { DirectSub } from "@peerbit/pubsub";
|
|
5
|
-
import { yamux } from "@chainsafe/libp2p-yamux";
|
|
6
4
|
import { getFreeKeypair, getClientId, inIframe, cookiesWhereClearedJustNow, } from "./utils.js";
|
|
7
5
|
import { noise } from "@chainsafe/libp2p-noise";
|
|
8
6
|
import { v4 as uuid } from "uuid";
|
|
@@ -10,9 +8,7 @@ import { FastMutex } from "./lockstorage.js";
|
|
|
10
8
|
import sodium from "libsodium-wrappers";
|
|
11
9
|
import { useMount } from "./useMount.js";
|
|
12
10
|
import { createClient, createHost } from "@peerbit/proxy-window";
|
|
13
|
-
import { identify } from "@libp2p/identify";
|
|
14
11
|
import { webSockets } from "@libp2p/websockets";
|
|
15
|
-
import { circuitRelayTransport } from "@libp2p/circuit-relay-v2";
|
|
16
12
|
import * as filters from "@libp2p/websockets/filters";
|
|
17
13
|
import { detectIncognito } from "detectincognitojs";
|
|
18
14
|
const isInStandaloneMode = () => window.matchMedia("(display-mode: standalone)").matches ||
|
|
@@ -36,7 +32,7 @@ const subscribeToUnload = (fn) => {
|
|
|
36
32
|
export const PeerProvider = (options) => {
|
|
37
33
|
const [peer, setPeer] = React.useState(undefined);
|
|
38
34
|
const [promise, setPromise] = React.useState(undefined);
|
|
39
|
-
const [persisted, setPersisted] = React.useState(
|
|
35
|
+
const [persisted, setPersisted] = React.useState(false);
|
|
40
36
|
const [loading, setLoading] = React.useState(true);
|
|
41
37
|
const [connectionState, setConnectionState] = React.useState("disconnected");
|
|
42
38
|
const [tabIndex, setTabIndex] = React.useState(-1);
|
|
@@ -151,32 +147,36 @@ export const PeerProvider = (options) => {
|
|
|
151
147
|
console.log("Create client");
|
|
152
148
|
newPeer = await Peerbit.create({
|
|
153
149
|
libp2p: {
|
|
154
|
-
addresses: {
|
|
150
|
+
addresses: {
|
|
151
|
+
listen: [
|
|
152
|
+
/* "/p2p-circuit" */
|
|
153
|
+
],
|
|
154
|
+
},
|
|
155
155
|
connectionEncrypters: [noise()],
|
|
156
156
|
peerId,
|
|
157
157
|
connectionManager: { maxConnections: 100 },
|
|
158
158
|
connectionMonitor: { enabled: false },
|
|
159
|
-
streamMuxers: [yamux()],
|
|
160
159
|
...(nodeOptions.network === "local"
|
|
161
160
|
? {
|
|
162
161
|
connectionGater: {
|
|
163
162
|
denyDialMultiaddr: () => false,
|
|
164
163
|
},
|
|
165
164
|
transports: [
|
|
166
|
-
webSockets({ filter: filters.all }),
|
|
167
|
-
|
|
165
|
+
webSockets({ filter: filters.all }) /* ,
|
|
166
|
+
circuitRelayTransport(), */,
|
|
168
167
|
],
|
|
169
168
|
}
|
|
170
169
|
: {
|
|
171
170
|
transports: [
|
|
172
|
-
webSockets(
|
|
173
|
-
|
|
171
|
+
webSockets() /* ,
|
|
172
|
+
circuitRelayTransport(), */,
|
|
174
173
|
],
|
|
175
|
-
})
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
174
|
+
}) /*
|
|
175
|
+
services: {
|
|
176
|
+
pubsub: (c) =>
|
|
177
|
+
new DirectSub(c, { canRelayMessage: true }),
|
|
178
|
+
identify: identify(),
|
|
179
|
+
}, */,
|
|
180
180
|
},
|
|
181
181
|
directory,
|
|
182
182
|
});
|
|
@@ -188,18 +188,23 @@ export const PeerProvider = (options) => {
|
|
|
188
188
|
setConnectionState("connecting");
|
|
189
189
|
const connectFn = async () => {
|
|
190
190
|
try {
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
191
|
+
const network = nodeOptions.network;
|
|
192
|
+
if (network === "local" ||
|
|
193
|
+
(network.type === "local" &&
|
|
194
|
+
!network.bootstrap)) {
|
|
195
|
+
const localAddress = "/ip4/127.0.0.1/tcp/8002/ws/p2p/" +
|
|
196
|
+
(await (await fetch("http://localhost:8082/peer/id")).text());
|
|
197
|
+
console.log("Dialing local address", localAddress);
|
|
198
|
+
await newPeer.dial(localAddress);
|
|
199
|
+
}
|
|
200
|
+
else if (!network ||
|
|
201
|
+
network === "remote" ||
|
|
202
|
+
(network.type === "remote" && !network.bootstrap)) {
|
|
203
|
+
await newPeer["bootstrap"]?.();
|
|
194
204
|
}
|
|
195
205
|
else {
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
await newPeer.dial(addr);
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
else {
|
|
202
|
-
await newPeer["bootstrap"]?.();
|
|
206
|
+
for (const addr of network.bootstrap) {
|
|
207
|
+
await newPeer.dial(addr);
|
|
203
208
|
}
|
|
204
209
|
}
|
|
205
210
|
setConnectionState("connected");
|
package/lib/esm/usePeer.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"usePeer.js","sourceR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,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/useQuery.d.ts
CHANGED
|
@@ -10,6 +10,9 @@ type QueryOptions = {
|
|
|
10
10
|
};
|
|
11
11
|
export declare const useQuery: <T extends Record<string, any>, I extends Record<string, any>, R extends boolean | undefined = true, RT = R extends false ? WithContext<I> : WithContext<T>>(db?: Documents<T, I>, options?: {
|
|
12
12
|
resolve?: R;
|
|
13
|
+
waitForReplicators?: boolean | {
|
|
14
|
+
timeout?: number;
|
|
15
|
+
};
|
|
13
16
|
transform?: (result: WithContext<RT>) => Promise<WithContext<RT>>;
|
|
14
17
|
debounce?: number;
|
|
15
18
|
debug?: boolean | {
|
package/lib/esm/useQuery.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { useState, useEffect, useRef } from "react";
|
|
2
2
|
import { ClosedError, } from "@peerbit/document";
|
|
3
|
+
import { AbortError } from "@peerbit/time";
|
|
3
4
|
const logWithId = (options, ...args) => {
|
|
4
5
|
if (!options?.debug)
|
|
5
6
|
return;
|
|
@@ -17,14 +18,23 @@ export const useQuery = (db, options) => {
|
|
|
17
18
|
const loadingMoreRef = useRef(false);
|
|
18
19
|
const iteratorRef = useRef(null);
|
|
19
20
|
const emptyResultsRef = useRef(false);
|
|
21
|
+
const closeControllerRef = useRef(null);
|
|
20
22
|
const updateAll = (combined, fromChange) => {
|
|
21
23
|
logWithId(options, "Loading more items, new combined length", combined.length, "from change", fromChange);
|
|
22
24
|
allRef.current = combined;
|
|
23
25
|
setAll(combined);
|
|
24
26
|
};
|
|
25
|
-
const reset = () => {
|
|
26
|
-
|
|
27
|
+
const reset = (fromRef) => {
|
|
28
|
+
if (iteratorRef.current != null && iteratorRef.current !== fromRef) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
closeControllerRef.current?.abort();
|
|
32
|
+
closeControllerRef.current = new AbortController();
|
|
27
33
|
emptyResultsRef.current = false;
|
|
34
|
+
logWithId(options, "reset", {
|
|
35
|
+
id: iteratorRef.current?.id,
|
|
36
|
+
size: allRef.current.length,
|
|
37
|
+
});
|
|
28
38
|
!iteratorRef.current?.iterator.done() &&
|
|
29
39
|
iteratorRef.current?.iterator?.close();
|
|
30
40
|
iteratorRef.current = null;
|
|
@@ -36,16 +46,14 @@ export const useQuery = (db, options) => {
|
|
|
36
46
|
// Initialize the iterator only once or when query changes
|
|
37
47
|
useEffect(() => {
|
|
38
48
|
if (!db || db.closed || options?.query === null) {
|
|
39
|
-
reset();
|
|
49
|
+
reset(null);
|
|
40
50
|
return;
|
|
41
51
|
}
|
|
42
52
|
const initIterator = () => {
|
|
43
53
|
// Don't make this async, it will cause issues with the iterator refs
|
|
44
54
|
try {
|
|
45
55
|
// Initialize the iterator and load initial batch.
|
|
46
|
-
|
|
47
|
-
iteratorRef.current?.iterator.close();
|
|
48
|
-
iteratorRef.current = {
|
|
56
|
+
const ref = {
|
|
49
57
|
id: options?.id,
|
|
50
58
|
iterator: db.index.iterate(options?.query ?? {}, {
|
|
51
59
|
local: options?.local ?? true,
|
|
@@ -53,18 +61,24 @@ export const useQuery = (db, options) => {
|
|
|
53
61
|
resolve: options?.resolve,
|
|
54
62
|
}),
|
|
55
63
|
};
|
|
56
|
-
|
|
64
|
+
iteratorRef.current = ref;
|
|
65
|
+
logWithId(options, "Initializing iterator", {
|
|
66
|
+
id: options?.id,
|
|
67
|
+
options: options,
|
|
68
|
+
});
|
|
57
69
|
loadMore(); // initial load
|
|
70
|
+
return ref;
|
|
58
71
|
}
|
|
59
72
|
catch (error) {
|
|
60
73
|
console.error("Error initializing iterator", error);
|
|
74
|
+
return null;
|
|
61
75
|
}
|
|
62
76
|
};
|
|
63
77
|
// Reset state when the db or query changes.
|
|
64
|
-
reset();
|
|
65
|
-
initIterator();
|
|
78
|
+
reset(iteratorRef.current);
|
|
79
|
+
const newIteratorRef = initIterator();
|
|
66
80
|
let handleChange = undefined;
|
|
67
|
-
if (options?.onChange) {
|
|
81
|
+
if (options?.onChange && options?.onChange?.merge !== false) {
|
|
68
82
|
let mergeFunction = typeof options.onChange.merge === "function"
|
|
69
83
|
? options.onChange.merge
|
|
70
84
|
: (change) => change;
|
|
@@ -84,6 +98,7 @@ export const useQuery = (db, options) => {
|
|
|
84
98
|
}
|
|
85
99
|
else {
|
|
86
100
|
merged = await db.index.updateResults(allRef.current, filteredChange, options?.query || {}, options?.resolve ?? true);
|
|
101
|
+
logWithId(options, "After update", allRef.current, merged);
|
|
87
102
|
const expectedDiff = filteredChange.added.length -
|
|
88
103
|
filteredChange.removed.length;
|
|
89
104
|
if (merged === allRef.current ||
|
|
@@ -107,11 +122,11 @@ export const useQuery = (db, options) => {
|
|
|
107
122
|
return () => {
|
|
108
123
|
handleChange &&
|
|
109
124
|
db.events.removeEventListener("change", handleChange);
|
|
110
|
-
reset();
|
|
125
|
+
reset(newIteratorRef);
|
|
111
126
|
};
|
|
112
127
|
}, [
|
|
113
128
|
db?.closed ? undefined : db?.address,
|
|
114
|
-
options?.id ? options?.id : options?.query,
|
|
129
|
+
options?.id != null ? options?.id : options?.query,
|
|
115
130
|
options?.resolve,
|
|
116
131
|
]);
|
|
117
132
|
// Define the loadMore function
|
|
@@ -133,9 +148,27 @@ export const useQuery = (db, options) => {
|
|
|
133
148
|
loadingMoreRef.current = true;
|
|
134
149
|
try {
|
|
135
150
|
// Fetch next batchSize number of items:
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
151
|
+
logWithId(options, "wait for replicators for iterator " + iterator.id);
|
|
152
|
+
if (options?.waitForReplicators !== false) {
|
|
153
|
+
let timeout = 5e3;
|
|
154
|
+
if (typeof options?.waitForReplicators === "object") {
|
|
155
|
+
timeout = options.waitForReplicators.timeout ?? 1e4;
|
|
156
|
+
}
|
|
157
|
+
await db?.log
|
|
158
|
+
.waitForReplicators({
|
|
159
|
+
timeout,
|
|
160
|
+
signal: closeControllerRef.current?.signal,
|
|
161
|
+
})
|
|
162
|
+
.catch((e) => {
|
|
163
|
+
if (e instanceof AbortError) {
|
|
164
|
+
// Ignore abort error
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
console.warn("Remote replicators not ready", e);
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
logWithId(options, "loadMore: loading more items for iterator " +
|
|
171
|
+
iteratorRef.current?.id, "should resolve?: " + options?.resolve, "query local?: " + options?.local, "query remote?: " + options?.remote, "isReplicating: " + (await db?.log.isReplicating()));
|
|
139
172
|
let newItems = await iterator.iterator.next(batchSize);
|
|
140
173
|
if (options?.transform) {
|
|
141
174
|
newItems = await Promise.all(newItems.map((item) => options.transform(item)));
|
|
@@ -146,9 +179,12 @@ export const useQuery = (db, options) => {
|
|
|
146
179
|
logWithId(options, "Iterator ref changed, not updating state", {
|
|
147
180
|
refBefore: iterator.id,
|
|
148
181
|
currentRef: iteratorRef.current?.id,
|
|
182
|
+
ignoredItems: newItems.length,
|
|
149
183
|
});
|
|
150
184
|
return;
|
|
151
185
|
}
|
|
186
|
+
logWithId(options, "loadMore: loaded more items for iterator " +
|
|
187
|
+
iteratorRef.current?.id, "new items length", newItems.length, "all items length", allRef.current.length);
|
|
152
188
|
emptyResultsRef.current = newItems.length === 0;
|
|
153
189
|
if (newItems.length > 0) {
|
|
154
190
|
let prev = allRef.current;
|
|
@@ -164,7 +200,7 @@ export const useQuery = (db, options) => {
|
|
|
164
200
|
updateAll(combined, null);
|
|
165
201
|
}
|
|
166
202
|
else {
|
|
167
|
-
logWithId(options, "no new items, not updating state for iterator" +
|
|
203
|
+
logWithId(options, "no new items, not updating state for iterator " +
|
|
168
204
|
iteratorRef.current?.id +
|
|
169
205
|
" existing results length", allRef.current.length);
|
|
170
206
|
}
|
package/lib/esm/useQuery.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useQuery.js","sourceRoot":"","sources":["../../src/useQuery.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AACpD,OAAO,EACH,WAAW,GAKd,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"useQuery.js","sourceRoot":"","sources":["../../src/useQuery.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AACpD,OAAO,EACH,WAAW,GAKd,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAQ3C,MAAM,SAAS,GAAG,CACd,OAAyD,EACzD,GAAG,IAAW,EAChB,EAAE;IACA,IAAI,CAAC,OAAO,EAAE,KAAK;QAAE,OAAO;IAE5B,IAAI,OAAO,OAAO,CAAC,KAAK,KAAK,SAAS,EAAE,CAAC;QACrC,OAAO,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,CAAC;IACzB,CAAC;SAAM,IAAI,OAAO,OAAO,CAAC,KAAK,CAAC,EAAE,KAAK,QAAQ,EAAE,CAAC;QAC9C,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,EAAE,GAAG,IAAI,CAAC,CAAC;IAC3C,CAAC;AACL,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,QAAQ,GAAG,CAMpB,EAAoB,EACpB,OA4BgB,EAClB,EAAE;IACA,MAAM,CAAC,GAAG,EAAE,MAAM,CAAC,GAAG,QAAQ,CAAoB,EAAE,CAAC,CAAC;IACtD,MAAM,MAAM,GAAG,MAAM,CAAoB,EAAE,CAAC,CAAC;IAC7C,MAAM,CAAC,SAAS,EAAE,YAAY,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC;IAClD,MAAM,cAAc,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;IACrC,MAA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,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@peerbit/react",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.20",
|
|
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": "94ef93f10fa02217c41f2c66f4b9ec8599dddde6"
|
|
74
74
|
}
|
package/src/index.ts
CHANGED
package/src/usePeer.tsx
CHANGED
|
@@ -2,7 +2,6 @@ import React, { useContext } from "react";
|
|
|
2
2
|
import { Multiaddr } from "@multiformats/multiaddr";
|
|
3
3
|
import { Peerbit } from "peerbit";
|
|
4
4
|
import { DirectSub } from "@peerbit/pubsub";
|
|
5
|
-
import { yamux } from "@chainsafe/libp2p-yamux";
|
|
6
5
|
import {
|
|
7
6
|
getFreeKeypair,
|
|
8
7
|
getClientId,
|
|
@@ -80,12 +79,16 @@ type IFrameOptions = {
|
|
|
80
79
|
targetOrigin: string;
|
|
81
80
|
};
|
|
82
81
|
|
|
82
|
+
export type NetworkOption = {
|
|
83
|
+
type: "local" | "remote";
|
|
84
|
+
bootstrap?: (Multiaddr | string)[];
|
|
85
|
+
};
|
|
86
|
+
|
|
83
87
|
type NodeOptions = {
|
|
84
88
|
type?: "node";
|
|
85
|
-
network: "local" | "remote";
|
|
89
|
+
network: "local" | "remote" | NetworkOption;
|
|
86
90
|
waitForConnnected?: boolean;
|
|
87
91
|
keypair?: Ed25519Keypair;
|
|
88
|
-
bootstrap?: (Multiaddr | string)[];
|
|
89
92
|
host?: boolean;
|
|
90
93
|
singleton?: boolean;
|
|
91
94
|
};
|
|
@@ -112,9 +115,7 @@ export const PeerProvider = (options: PeerOptions) => {
|
|
|
112
115
|
const [promise, setPromise] = React.useState<Promise<void> | undefined>(
|
|
113
116
|
undefined
|
|
114
117
|
);
|
|
115
|
-
const [persisted, setPersisted] = React.useState<boolean
|
|
116
|
-
undefined
|
|
117
|
-
);
|
|
118
|
+
const [persisted, setPersisted] = React.useState<boolean>(false);
|
|
118
119
|
const [loading, setLoading] = React.useState<boolean>(true);
|
|
119
120
|
const [connectionState, setConnectionState] =
|
|
120
121
|
React.useState<ConnectionStatus>("disconnected");
|
|
@@ -246,33 +247,36 @@ export const PeerProvider = (options: PeerOptions) => {
|
|
|
246
247
|
console.log("Create client");
|
|
247
248
|
newPeer = await Peerbit.create({
|
|
248
249
|
libp2p: {
|
|
249
|
-
addresses: {
|
|
250
|
+
addresses: {
|
|
251
|
+
listen: [
|
|
252
|
+
/* "/p2p-circuit" */
|
|
253
|
+
],
|
|
254
|
+
},
|
|
250
255
|
connectionEncrypters: [noise()],
|
|
251
256
|
peerId,
|
|
252
257
|
connectionManager: { maxConnections: 100 },
|
|
253
258
|
connectionMonitor: { enabled: false },
|
|
254
|
-
streamMuxers: [yamux()],
|
|
255
259
|
...(nodeOptions.network === "local"
|
|
256
260
|
? {
|
|
257
261
|
connectionGater: {
|
|
258
262
|
denyDialMultiaddr: () => false,
|
|
259
263
|
},
|
|
260
264
|
transports: [
|
|
261
|
-
webSockets({ filter: filters.all }),
|
|
262
|
-
|
|
265
|
+
webSockets({ filter: filters.all }) /* ,
|
|
266
|
+
circuitRelayTransport(), */,
|
|
263
267
|
],
|
|
264
268
|
}
|
|
265
269
|
: {
|
|
266
270
|
transports: [
|
|
267
|
-
webSockets(
|
|
268
|
-
|
|
271
|
+
webSockets() /* ,
|
|
272
|
+
circuitRelayTransport(), */,
|
|
269
273
|
],
|
|
270
|
-
})
|
|
274
|
+
}) /*
|
|
271
275
|
services: {
|
|
272
276
|
pubsub: (c) =>
|
|
273
277
|
new DirectSub(c, { canRelayMessage: true }),
|
|
274
278
|
identify: identify(),
|
|
275
|
-
},
|
|
279
|
+
}, */,
|
|
276
280
|
},
|
|
277
281
|
directory,
|
|
278
282
|
});
|
|
@@ -287,22 +291,28 @@ export const PeerProvider = (options: PeerOptions) => {
|
|
|
287
291
|
|
|
288
292
|
const connectFn = async () => {
|
|
289
293
|
try {
|
|
290
|
-
|
|
291
|
-
|
|
294
|
+
const network = nodeOptions.network;
|
|
295
|
+
if (
|
|
296
|
+
network === "local" ||
|
|
297
|
+
((network as NetworkOption).type === "local" &&
|
|
298
|
+
!(network as NetworkOption).bootstrap)
|
|
299
|
+
) {
|
|
300
|
+
const localAddress =
|
|
292
301
|
"/ip4/127.0.0.1/tcp/8002/ws/p2p/" +
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
302
|
+
(await (
|
|
303
|
+
await fetch("http://localhost:8082/peer/id")
|
|
304
|
+
).text());
|
|
305
|
+
console.log("Dialing local address", localAddress);
|
|
306
|
+
await newPeer.dial(localAddress);
|
|
307
|
+
} else if (
|
|
308
|
+
!network ||
|
|
309
|
+
network === "remote" ||
|
|
310
|
+
(network.type === "remote" && !network.bootstrap)
|
|
311
|
+
) {
|
|
312
|
+
await newPeer["bootstrap"]?.();
|
|
299
313
|
} else {
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
await newPeer.dial(addr);
|
|
303
|
-
}
|
|
304
|
-
} else {
|
|
305
|
-
await newPeer["bootstrap"]?.();
|
|
314
|
+
for (const addr of network.bootstrap!) {
|
|
315
|
+
await newPeer.dial(addr);
|
|
306
316
|
}
|
|
307
317
|
}
|
|
308
318
|
setConnectionState("connected");
|
package/src/useQuery.tsx
CHANGED
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
WithContext,
|
|
8
8
|
} from "@peerbit/document";
|
|
9
9
|
import * as indexerTypes from "@peerbit/indexer-interface";
|
|
10
|
+
import { AbortError } from "@peerbit/time";
|
|
10
11
|
|
|
11
12
|
type QueryLike = {
|
|
12
13
|
query?: indexerTypes.Query[] | indexerTypes.QueryLike;
|
|
@@ -36,6 +37,7 @@ export const useQuery = <
|
|
|
36
37
|
db?: Documents<T, I>,
|
|
37
38
|
options?: {
|
|
38
39
|
resolve?: R;
|
|
40
|
+
waitForReplicators?: boolean | { timeout?: number };
|
|
39
41
|
transform?: (result: WithContext<RT>) => Promise<WithContext<RT>>;
|
|
40
42
|
debounce?: number;
|
|
41
43
|
debug?: boolean | { id: string };
|
|
@@ -72,6 +74,7 @@ export const useQuery = <
|
|
|
72
74
|
iterator: ResultsIterator<WithContext<RT>>;
|
|
73
75
|
} | null>(null);
|
|
74
76
|
const emptyResultsRef = useRef(false);
|
|
77
|
+
const closeControllerRef = useRef<AbortController | null>(null);
|
|
75
78
|
|
|
76
79
|
const updateAll = (
|
|
77
80
|
combined: WithContext<RT>[],
|
|
@@ -90,10 +93,24 @@ export const useQuery = <
|
|
|
90
93
|
setAll(combined);
|
|
91
94
|
};
|
|
92
95
|
|
|
93
|
-
const reset = (
|
|
94
|
-
|
|
96
|
+
const reset = (
|
|
97
|
+
fromRef: {
|
|
98
|
+
id?: string;
|
|
99
|
+
iterator: ResultsIterator<WithContext<RT>>;
|
|
100
|
+
} | null
|
|
101
|
+
) => {
|
|
102
|
+
if (iteratorRef.current != null && iteratorRef.current !== fromRef) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
closeControllerRef.current?.abort();
|
|
106
|
+
closeControllerRef.current = new AbortController();
|
|
95
107
|
|
|
96
108
|
emptyResultsRef.current = false;
|
|
109
|
+
logWithId(options, "reset", {
|
|
110
|
+
id: iteratorRef.current?.id,
|
|
111
|
+
size: allRef.current.length,
|
|
112
|
+
});
|
|
113
|
+
|
|
97
114
|
!iteratorRef.current?.iterator.done() &&
|
|
98
115
|
iteratorRef.current?.iterator?.close();
|
|
99
116
|
iteratorRef.current = null;
|
|
@@ -106,17 +123,16 @@ export const useQuery = <
|
|
|
106
123
|
// Initialize the iterator only once or when query changes
|
|
107
124
|
useEffect(() => {
|
|
108
125
|
if (!db || db.closed || options?.query === null) {
|
|
109
|
-
reset();
|
|
126
|
+
reset(null);
|
|
110
127
|
return;
|
|
111
128
|
}
|
|
129
|
+
|
|
112
130
|
const initIterator = () => {
|
|
113
131
|
// Don't make this async, it will cause issues with the iterator refs
|
|
114
132
|
try {
|
|
115
133
|
// Initialize the iterator and load initial batch.
|
|
116
134
|
|
|
117
|
-
|
|
118
|
-
iteratorRef.current?.iterator.close();
|
|
119
|
-
iteratorRef.current = {
|
|
135
|
+
const ref = {
|
|
120
136
|
id: options?.id,
|
|
121
137
|
iterator: db.index.iterate(options?.query ?? {}, {
|
|
122
138
|
local: options?.local ?? true,
|
|
@@ -124,24 +140,31 @@ export const useQuery = <
|
|
|
124
140
|
resolve: options?.resolve as any,
|
|
125
141
|
}) as any as ResultsIterator<WithContext<RT>>,
|
|
126
142
|
};
|
|
143
|
+
iteratorRef.current = ref;
|
|
127
144
|
|
|
128
|
-
logWithId(options, "Initializing iterator",
|
|
145
|
+
logWithId(options, "Initializing iterator", {
|
|
146
|
+
id: options?.id,
|
|
147
|
+
options: options,
|
|
148
|
+
});
|
|
129
149
|
|
|
130
150
|
loadMore(); // initial load
|
|
151
|
+
return ref;
|
|
131
152
|
} catch (error) {
|
|
132
153
|
console.error("Error initializing iterator", error);
|
|
154
|
+
return null;
|
|
133
155
|
}
|
|
134
156
|
};
|
|
135
157
|
|
|
136
158
|
// Reset state when the db or query changes.
|
|
137
|
-
reset();
|
|
138
|
-
|
|
159
|
+
reset(iteratorRef.current);
|
|
160
|
+
|
|
161
|
+
const newIteratorRef = initIterator();
|
|
139
162
|
|
|
140
163
|
let handleChange:
|
|
141
164
|
| undefined
|
|
142
165
|
| ((e: CustomEvent<DocumentsChange<T>>) => void | Promise<void>) =
|
|
143
166
|
undefined;
|
|
144
|
-
if (options?.onChange) {
|
|
167
|
+
if (options?.onChange && options?.onChange?.merge !== false) {
|
|
145
168
|
let mergeFunction =
|
|
146
169
|
typeof options.onChange.merge === "function"
|
|
147
170
|
? options.onChange.merge
|
|
@@ -171,6 +194,7 @@ export const useQuery = <
|
|
|
171
194
|
options?.query || {},
|
|
172
195
|
options?.resolve ?? true
|
|
173
196
|
);
|
|
197
|
+
logWithId(options, "After update", allRef.current, merged);
|
|
174
198
|
const expectedDiff =
|
|
175
199
|
filteredChange.added.length -
|
|
176
200
|
filteredChange.removed.length;
|
|
@@ -204,11 +228,11 @@ export const useQuery = <
|
|
|
204
228
|
return () => {
|
|
205
229
|
handleChange &&
|
|
206
230
|
db.events.removeEventListener("change", handleChange);
|
|
207
|
-
reset();
|
|
231
|
+
reset(newIteratorRef);
|
|
208
232
|
};
|
|
209
233
|
}, [
|
|
210
234
|
db?.closed ? undefined : db?.address,
|
|
211
|
-
options?.id ? options?.id : options?.query,
|
|
235
|
+
options?.id != null ? options?.id : options?.query,
|
|
212
236
|
options?.resolve,
|
|
213
237
|
]);
|
|
214
238
|
|
|
@@ -234,12 +258,39 @@ export const useQuery = <
|
|
|
234
258
|
loadingMoreRef.current = true;
|
|
235
259
|
try {
|
|
236
260
|
// Fetch next batchSize number of items:
|
|
237
|
-
await db?.log.waitForReplicators({ timeout: 1e4 });
|
|
238
261
|
logWithId(
|
|
239
262
|
options,
|
|
240
|
-
"
|
|
241
|
-
iteratorRef.current?.id
|
|
263
|
+
"wait for replicators for iterator " + iterator.id
|
|
242
264
|
);
|
|
265
|
+
if (options?.waitForReplicators !== false) {
|
|
266
|
+
let timeout = 5e3;
|
|
267
|
+
if (typeof options?.waitForReplicators === "object") {
|
|
268
|
+
timeout = options.waitForReplicators.timeout ?? 1e4;
|
|
269
|
+
}
|
|
270
|
+
await db?.log
|
|
271
|
+
.waitForReplicators({
|
|
272
|
+
timeout,
|
|
273
|
+
signal: closeControllerRef.current?.signal,
|
|
274
|
+
})
|
|
275
|
+
.catch((e) => {
|
|
276
|
+
if (e instanceof AbortError) {
|
|
277
|
+
// Ignore abort error
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
console.warn("Remote replicators not ready", e);
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
logWithId(
|
|
285
|
+
options,
|
|
286
|
+
"loadMore: loading more items for iterator " +
|
|
287
|
+
iteratorRef.current?.id,
|
|
288
|
+
"should resolve?: " + options?.resolve,
|
|
289
|
+
"query local?: " + options?.local,
|
|
290
|
+
"query remote?: " + options?.remote,
|
|
291
|
+
"isReplicating: " + (await db?.log.isReplicating())
|
|
292
|
+
);
|
|
293
|
+
|
|
243
294
|
let newItems: WithContext<RT>[] = await iterator.iterator.next(
|
|
244
295
|
batchSize
|
|
245
296
|
);
|
|
@@ -256,10 +307,21 @@ export const useQuery = <
|
|
|
256
307
|
logWithId(options, "Iterator ref changed, not updating state", {
|
|
257
308
|
refBefore: iterator.id,
|
|
258
309
|
currentRef: iteratorRef.current?.id,
|
|
310
|
+
ignoredItems: newItems.length,
|
|
259
311
|
});
|
|
260
312
|
return;
|
|
261
313
|
}
|
|
262
314
|
|
|
315
|
+
logWithId(
|
|
316
|
+
options,
|
|
317
|
+
"loadMore: loaded more items for iterator " +
|
|
318
|
+
iteratorRef.current?.id,
|
|
319
|
+
"new items length",
|
|
320
|
+
newItems.length,
|
|
321
|
+
"all items length",
|
|
322
|
+
allRef.current.length
|
|
323
|
+
);
|
|
324
|
+
|
|
263
325
|
emptyResultsRef.current = newItems.length === 0;
|
|
264
326
|
|
|
265
327
|
if (newItems.length > 0) {
|
|
@@ -283,7 +345,7 @@ export const useQuery = <
|
|
|
283
345
|
} else {
|
|
284
346
|
logWithId(
|
|
285
347
|
options,
|
|
286
|
-
"no new items, not updating state for iterator" +
|
|
348
|
+
"no new items, not updating state for iterator " +
|
|
287
349
|
iteratorRef.current?.id +
|
|
288
350
|
" existing results length",
|
|
289
351
|
allRef.current.length
|