*In getSwarm and swarm init, await on .opened to ensure we are ready, only when we have an active relay
*Only set mux.syncState if not previously set *change mux.syncState to emit syncProtomux *ensure the state data are numbers *check for undefined in the state data
This commit is contained in:
parent
4f6f4eacfd
commit
33b11cbde6
23
src/index.ts
23
src/index.ts
|
@ -12,6 +12,7 @@ import { logErr } from "libkmodule/dist";
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import Protomux from "protomux";
|
import Protomux from "protomux";
|
||||||
import { Mutex } from "async-mutex";
|
import { Mutex } from "async-mutex";
|
||||||
|
import defer from "p-defer";
|
||||||
|
|
||||||
const MAX_PEER_LISTENERS = 20;
|
const MAX_PEER_LISTENERS = 20;
|
||||||
|
|
||||||
|
@ -225,9 +226,16 @@ async function getSwarm(aq: ActiveQuery): Promise<Hyperswarm> {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!swarm) {
|
if (!swarm) {
|
||||||
|
if (defaultSwarm.activeRelay && defaultSwarm.ready) {
|
||||||
|
await defaultSwarm.activeRelay.dht._protocol.opened;
|
||||||
|
}
|
||||||
return defaultSwarm;
|
return defaultSwarm;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (swarm.activeRelay && swarm.ready) {
|
||||||
|
await swarm.activeRelay.dht._protocol.opened;
|
||||||
|
}
|
||||||
|
|
||||||
return swarmInstances.get(swarm) as Hyperswarm;
|
return swarmInstances.get(swarm) as Hyperswarm;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -330,9 +338,11 @@ async function handleReady(aq: ActiveQuery) {
|
||||||
|
|
||||||
if (swarm.activeRelay && swarm.ready) {
|
if (swarm.activeRelay && swarm.ready) {
|
||||||
aq.respond();
|
aq.respond();
|
||||||
|
await swarm.activeRelay.dht._protocol.opened;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
swarm.once("ready", () => {
|
swarm.once("ready", async () => {
|
||||||
|
await swarm.activeRelay.dht._protocol.opened;
|
||||||
aq.respond();
|
aq.respond();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -426,9 +436,9 @@ async function handleSyncProtomux(aq: ActiveQuery) {
|
||||||
|
|
||||||
const sync = () => send(mux);
|
const sync = () => send(mux);
|
||||||
|
|
||||||
mux.syncState = send.bind(undefined, mux);
|
if (!mux.syncState) {
|
||||||
|
mux.syncState = socket.emit.bind(socket, "syncProtomux");
|
||||||
sync();
|
}
|
||||||
|
|
||||||
socket.on("syncProtomux", sync);
|
socket.on("syncProtomux", sync);
|
||||||
|
|
||||||
|
@ -438,12 +448,13 @@ async function handleSyncProtomux(aq: ActiveQuery) {
|
||||||
["remote", "local"].forEach((field) => {
|
["remote", "local"].forEach((field) => {
|
||||||
const rField = `_${field}`;
|
const rField = `_${field}`;
|
||||||
data[field].forEach((item: any) => {
|
data[field].forEach((item: any) => {
|
||||||
if (!mux[rField][item]) {
|
item = parseInt(item);
|
||||||
|
if (typeof mux[rField][item] === "undefined") {
|
||||||
while (item > mux[rField].length) {
|
while (item > mux[rField].length) {
|
||||||
mux[rField].push(null);
|
mux[rField].push(null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!mux[rField][item]) {
|
if (typeof mux[rField][item] === "undefined") {
|
||||||
mux[rField][item] = null;
|
mux[rField][item] = null;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue