*Use pdefer to wait until the other site has responded that they synced changes and gave true
This commit is contained in:
parent
bce1bca13e
commit
a59de99e56
|
@ -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": {
|
||||||
|
|
13
src/index.ts
13
src/index.ts
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue