// `navigator.locks` for the unit tests, which run on node, where there is none. // // One lock per name: granted in the order it was asked for, and let go when // the callback settles, whether it resolved or rejected. That is what the // browser's own does for the one call the extension makes, // `navigator.locks.request(name, callback)`. Every module a test file loads // shares it, as every page of the extension and its background share the // browser's. // // Run before each test file by `setupFiles` in package.json. // For each name, a promise that settles once the last request for it has // let the lock go. const released = new Map(); function request(name, callback) { const previous = released.get(name) || Promise.resolve(); const result = previous.then(() => callback({ name, mode: "exclusive" })); released.set( name, result.catch(() => {}), ); return result; } globalThis.navigator = { locks: { request } };