1
0
Fork 0
firefox/testing/web-platform/tests/fs/resources/bfcache-test-worker.js
Daniel Baumann 5e9a113729
Adding upstream version 140.0.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
2025-06-25 09:37:52 +02:00

42 lines
1.1 KiB
JavaScript

'use strict';
import {tryToCreateLock} from './bfcache-test-helpers.js';
let openSAH;
export async function createSAH(mode, fileName) {
if (openSAH) {
throw new Error('Already have an open access handle.');
}
openSAH = await tryToCreateLock(
fileName, fileHandle => fileHandle.createSyncAccessHandle({mode}));
return openSAH !== undefined;
}
export async function releaseSAH() {
if (!openSAH) {
throw new Error('No open access handle.');
}
await openSAH.close();
openSAH = undefined;
}
export async function createAndReleaseSAH(mode, fileName) {
const sahLock = await tryToCreateLock(
fileName, fileHandle => fileHandle.createSyncAccessHandle({mode}));
await sahLock?.close();
return sahLock !== undefined;
}
// Functions exposed to the renderer.
const funcs = {
createSAH,
releaseSAH,
createAndReleaseSAH,
};
// Sets up a message handler that calls the `funcName` in `funcs` with `args`
// and then postMessages the result back to the renderer.
addEventListener('message', async ({data: {funcName, args}}) => {
postMessage(await funcs[funcName](...args));
});