*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",
|
||||
"backoff.js": "^1.0.4",
|
||||
"eventemitter3": "^5.0.0",
|
||||
"p-defer": "^4.0.0",
|
||||
"protomux": "git+https://git.lumeweb.com/LumeWeb/protomux.git"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
|
13
src/index.ts
13
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);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue