|
| 1 | +import { Semaphore, type Lock } from "./semaphore"; |
| 2 | +import type { Message, Response } from "./types"; |
| 3 | +import { dlog } from "./dlog"; |
| 4 | + |
| 5 | +class OpfsUser { |
| 6 | + private dispatchSemaphore: Semaphore; |
| 7 | + private responseSemaphore: Semaphore; |
| 8 | + private responseLock: Lock | null; |
| 9 | + private response: Response | null; |
| 10 | + private worker: Worker; |
| 11 | + |
| 12 | + constructor(label?: string, workerPath: string = "./src/worker.ts") { |
| 13 | + this.dispatchSemaphore = new Semaphore(`${label ?? workerPath} dispatch`, 1); |
| 14 | + this.responseSemaphore = new Semaphore(`${label ?? workerPath} response`, 1); |
| 15 | + this.response = null; |
| 16 | + this.responseLock = null; |
| 17 | + |
| 18 | + this.worker = new Worker(workerPath, { |
| 19 | + preload: "../../ts/gen/redb-opfs" |
| 20 | + }); |
| 21 | + this.worker.addEventListener("error", (event) => { |
| 22 | + throw new Error(event.message); |
| 23 | + }); |
| 24 | + this.worker.addEventListener("message", |
| 25 | + (event) => { |
| 26 | + dlog("opfsu: received message from worker"); |
| 27 | + this.response = event.data; |
| 28 | + this.releaseResponseLock(); |
| 29 | + }); |
| 30 | + } |
| 31 | + |
| 32 | + private releaseResponseLock() { |
| 33 | + if (this.responseLock !== null) { |
| 34 | + this.responseLock.release(); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + private async dispatch(message: Message): Promise<Response> { |
| 39 | + const dispatchLock = await this.dispatchSemaphore.acquire(); |
| 40 | + |
| 41 | + try { |
| 42 | + // get the response lock. This will be released when we receive a response. |
| 43 | + // This means we can efficiently block on a reacquisition attempt. |
| 44 | + this.responseLock = await this.responseSemaphore.acquire(); |
| 45 | + dlog(`opfsu: dispatching message to worker`); |
| 46 | + |
| 47 | + this.worker.postMessage(message); |
| 48 | + // now wait until the response is received (and immediately release it) |
| 49 | + dlog(`opfsu: waiting for response from worker`); |
| 50 | + (await this.responseSemaphore.acquire()).release(); |
| 51 | + // now we have a response we can return |
| 52 | + const response = this.response ?? new Uint8Array(); |
| 53 | + this.response = null; |
| 54 | + return response; |
| 55 | + } finally { |
| 56 | + dispatchLock.release() |
| 57 | + this.releaseResponseLock() |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** Store some data in Opfs */ |
| 62 | + async store(offset: bigint, data: Uint8Array): Promise<void> { |
| 63 | + dlog(`opfsu: storing ${data.length} bytes at offset ${offset}`); |
| 64 | + await this.dispatch({ op: "store", offset, data }) |
| 65 | + } |
| 66 | + |
| 67 | + /** Retrieve `size` bytes from Opfs */ |
| 68 | + async load(offset: bigint, size: number): Promise<Uint8Array> { |
| 69 | + dlog(`opfsu: loading ${size} bytes at offset ${offset}`); |
| 70 | + return await this.dispatch({ op: "load", offset, size }) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +// lets cache some squares in opfs so we never have to recompute them |
| 75 | +const opfsUser = new OpfsUser("opfs"); |
| 76 | + |
| 77 | +const squares = new Uint8Array(16); |
| 78 | +for (var i = 0; i < 16; i++) { |
| 79 | + squares[i] = i * i; |
| 80 | +} |
| 81 | + |
| 82 | +// store the squares |
| 83 | +console.log("main: storing data") |
| 84 | +await opfsUser.store(BigInt(0), squares); |
| 85 | + |
| 86 | +// what's the square of 4? |
| 87 | +console.log("main: retrieving data") |
| 88 | +const foursquared = (await opfsUser.load(BigInt(4), 1))[0]; |
| 89 | +console.log("main: square of 4 is: ", foursquared); |
0 commit comments