*Use pdefer to wait until the other site has responded that they synced changes and gave true

This commit is contained in:
Derrick Hammer 2023-04-05 18:01:59 -04:00
parent bce1bca13e
commit a59de99e56
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 13 additions and 1 deletions

View File

@ -11,6 +11,7 @@
"b4a": "^1.6.3", "b4a": "^1.6.3",
"backoff.js": "^1.0.4", "backoff.js": "^1.0.4",
"eventemitter3": "^5.0.0", "eventemitter3": "^5.0.0",
"p-defer": "^4.0.0",
"protomux": "git+https://git.lumeweb.com/LumeWeb/protomux.git" "protomux": "git+https://git.lumeweb.com/LumeWeb/protomux.git"
}, },
"devDependencies": { "devDependencies": {

View File

@ -11,6 +11,7 @@ import Backoff from "backoff.js";
import { Mutex } from "async-mutex"; import { Mutex } from "async-mutex";
// @ts-ignore // @ts-ignore
import Protomux from "protomux"; import Protomux from "protomux";
import defer from "p-defer";
export class SwarmClient extends Client { export class SwarmClient extends Client {
private useDefaultSwarm: boolean; private useDefaultSwarm: boolean;
@ -193,10 +194,18 @@ export class Socket extends Client {
private async _initSync() { private async _initSync() {
const mux = Protomux.from(this); const mux = Protomux.from(this);
let updateDone = defer();
const [update] = this.connectModule( const [update] = this.connectModule(
"syncProtomux", "syncProtomux",
{ id: this.id }, { id: this.id },
async (data: any) => { async (data: any) => {
if (data === true) {
updateDone.resolve();
updateDone = defer();
return;
}
await this.syncMutex.acquire(); await this.syncMutex.acquire();
["remote", "local"].forEach((field) => { ["remote", "local"].forEach((field) => {
@ -219,17 +228,19 @@ export class Socket extends Client {
} }
}); });
mux._free = mux._free.filter((item: any) => item !== undefined); mux._free = mux._free.filter((item: any) => item !== undefined);
update(true);
this.syncMutex.release(); this.syncMutex.release();
} }
); );
const send = (mux: any) => { const send = async (mux: any) => {
update({ update({
remote: Object.keys(mux._remote), remote: Object.keys(mux._remote),
local: Object.keys(mux._local), local: Object.keys(mux._local),
free: mux._free, free: mux._free,
}); });
await updateDone.promise;
}; };
mux.syncState = send.bind(undefined, mux); mux.syncState = send.bind(undefined, mux);
} }