gecko-dev/testing/web-platform/tests/web-locks/resources/worker.js
Joshua Bell 3cb7b1360c Bug 1472022 [wpt PR 11713] - Web Locks API: Upstream tests to web-platform-tests (as .tentative), a=testonly
Automatic update from web-platform-testsWeb Locks API: Upstream tests to web-platform-tests (as .tentative)

* Move tests from http/tests/locks to external/wpt/web-locks
* Name with '.tentative' infix since this is still just a proposal
* Name most tests with '.https' infix since feature is [SecureContext]
   (and remove redirect-to-secure-host logic in tests)
* Consolidate window/worker interfaces test with '.any.js' variant
* Update paths to service worker helpers

Bug: 857265
Change-Id: Iaf254c5d53ff5b6bb65de4516df85307ad3a6519
Reviewed-on: https://chromium-review.googlesource.com/1118764
Commit-Queue: Victor Costan <pwnall@chromium.org>
Reviewed-by: Victor Costan <pwnall@chromium.org>
Cr-Commit-Position: refs/heads/master@{#573991}

--

wpt-commits: 89ccb892ad85159b093dab6956ca9caea7bb4edc
wpt-pr: 11713
2018-07-24 21:46:13 +00:00

39 lines
967 B
JavaScript

'use strict';
// Map of id => function that releases a lock.
const held = new Map();
let next_lock_id = 1;
self.addEventListener('message', e => {
function respond(data) {
self.postMessage(Object.assign(data, {rqid: e.data.rqid}));
}
switch (e.data.op) {
case 'request':
navigator.locks.request(
e.data.name, {
mode: e.data.mode || 'exclusive',
ifAvailable: e.data.ifAvailable || false
}, lock => {
if (lock === null) {
respond({ack: 'request', failed: true});
return;
}
let lock_id = next_lock_id++;
let release;
const promise = new Promise(r => { release = r; });
held.set(lock_id, release);
respond({ack: 'request', lock_id: lock_id});
return promise;
});
break;
case 'release':
held.get(e.data.lock_id)();
held.delete(e.data.lock_id);
respond({ack: 'release', lock_id: e.data.lock_id});
break;
}
});