@daaku/kombat
Advanced tools
Comparing version 2.3.0 to 2.4.0
@@ -221,2 +221,3 @@ import { customAlphabet } from 'nanoid'; | ||
} | ||
const after = (timeout) => new Promise(resolve => setTimeout(resolve, timeout)); | ||
export class SyncDB { | ||
@@ -294,10 +295,15 @@ clock; | ||
scheduleSync(timeoutMS = 50) { | ||
if (this.nextSync) { | ||
clearTimeout(this.nextSync); | ||
} | ||
this.nextSync = setTimeout(() => { | ||
const r = this.sync(); | ||
this.#pending.add(r); | ||
r.finally(() => this.#pending.delete(r)); | ||
}, timeoutMS); | ||
let p = undefined; | ||
p = this.nextSync = (async () => { | ||
await after(timeoutMS); | ||
// if while waiting nextSync was changed, and no longer this promise, it | ||
// means another one was scheduled after us. we'll let that one do the | ||
// work instead of us. | ||
if (p !== this.nextSync) { | ||
return; | ||
} | ||
await this.sync(); | ||
})(); | ||
p.finally(() => this.#pending.delete(p)); | ||
this.#pending.add(p); | ||
} | ||
@@ -340,5 +346,14 @@ // Sync data to-and-from the Remote. | ||
async settle() { | ||
await Promise.allSettled(this.#pending.values()); | ||
let last = this.nextSync; | ||
while (true) { | ||
await Promise.allSettled(this.#pending.values()); | ||
// if no new sync was scheduled, or was scheduled and finished, we're done | ||
if (last === this.nextSync) { | ||
return; | ||
} | ||
// else we wait again if anything is pending | ||
last = this.nextSync; | ||
} | ||
} | ||
} | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@daaku/kombat", | ||
"author": "Naitik Shah <n@daaku.org>", | ||
"version": "2.3.0", | ||
"version": "2.4.0", | ||
"description": "Infrastructure for CRDT powered applications.", | ||
@@ -6,0 +6,0 @@ "repository": "git@github.com:daaku/kombat", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
35069
493