From a59de99e562a9b4aad3817e669be2127d463f664 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Wed, 5 Apr 2023 18:01:59 -0400 Subject: [PATCH] *Use pdefer to wait until the other site has responded that they synced changes and gave true --- package.json | 1 + src/index.ts | 13 ++++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 68cdcd5..d5bce9f 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "b4a": "^1.6.3", "backoff.js": "^1.0.4", "eventemitter3": "^5.0.0", + "p-defer": "^4.0.0", "protomux": "git+https://git.lumeweb.com/LumeWeb/protomux.git" }, "devDependencies": { diff --git a/src/index.ts b/src/index.ts index 33f88de..2063500 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,6 +11,7 @@ import Backoff from "backoff.js"; import { Mutex } from "async-mutex"; // @ts-ignore import Protomux from "protomux"; +import defer from "p-defer"; export class SwarmClient extends Client { private useDefaultSwarm: boolean; @@ -193,10 +194,18 @@ export class Socket extends Client { private async _initSync() { const mux = Protomux.from(this); + let updateDone = defer(); + const [update] = this.connectModule( "syncProtomux", { id: this.id }, async (data: any) => { + if (data === true) { + updateDone.resolve(); + updateDone = defer(); + return; + } + await this.syncMutex.acquire(); ["remote", "local"].forEach((field) => { @@ -219,17 +228,19 @@ export class Socket extends Client { } }); mux._free = mux._free.filter((item: any) => item !== undefined); + update(true); this.syncMutex.release(); } ); - const send = (mux: any) => { + const send = async (mux: any) => { update({ remote: Object.keys(mux._remote), local: Object.keys(mux._local), free: mux._free, }); + await updateDone.promise; }; mux.syncState = send.bind(undefined, mux); }