aboutsummaryrefslogtreecommitdiffstats
path: root/src/nwc-lock.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/nwc-lock.js')
-rw-r--r--src/nwc-lock.js13
1 files changed, 10 insertions, 3 deletions
diff --git a/src/nwc-lock.js b/src/nwc-lock.js
index a7b10b4..bd8586a 100644
--- a/src/nwc-lock.js
+++ b/src/nwc-lock.js
@@ -12,10 +12,17 @@
// same send at once would sign it twice (a client's retry lands seconds
// after its lookup). A waiting push keeps its waitUntil and reads the mirror
// fresh once it gets the lock.
-export async function withWorkLock(fn) {
+// The Web Lock alone didn't hold on iOS (two workers at once, neither
+// waited), so the lease in the worker's store (nwc-bg.js) is taken inside it.
+export async function withWorkLock(fn, { lease = null } = {}) {
const locks = typeof navigator !== 'undefined' && navigator.locks;
- if (!locks) return fn();
- return locks.request('nwc-bg-work', fn);
+ const run = async () => {
+ if (!lease) return fn(0);
+ const l = await lease();
+ try { return await fn(l.waited); } finally { await l.release(); }
+ };
+ if (!locks) return run();
+ return locks.request('nwc-bg-work', run);
}
export async function withRequestLock(id, fn, held = () => undefined) {