tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

bfcache-test-worker.js (1118B)


      1 'use strict';
      2 
      3 import {tryToCreateLock} from './bfcache-test-helpers.js';
      4 
      5 let openSAH;
      6 
      7 export async function createSAH(mode, fileName) {
      8  if (openSAH) {
      9    throw new Error('Already have an open access handle.');
     10  }
     11  openSAH = await tryToCreateLock(
     12      fileName, fileHandle => fileHandle.createSyncAccessHandle({mode}));
     13  return openSAH !== undefined;
     14 }
     15 
     16 export async function releaseSAH() {
     17  if (!openSAH) {
     18    throw new Error('No open access handle.');
     19  }
     20  await openSAH.close();
     21  openSAH = undefined;
     22 }
     23 
     24 export async function createAndReleaseSAH(mode, fileName) {
     25  const sahLock = await tryToCreateLock(
     26      fileName, fileHandle => fileHandle.createSyncAccessHandle({mode}));
     27  await sahLock?.close();
     28  return sahLock !== undefined;
     29 }
     30 
     31 // Functions exposed to the renderer.
     32 const funcs = {
     33  createSAH,
     34  releaseSAH,
     35  createAndReleaseSAH,
     36 };
     37 
     38 // Sets up a message handler that calls the `funcName` in `funcs` with `args`
     39 // and then postMessages the result back to the renderer.
     40 addEventListener('message', async ({data: {funcName, args}}) => {
     41  postMessage(await funcs[funcName](...args));
     42 });