]> git.codecow.com Git - libnemo.git/commitdiff
Refactor worker ID handling.
authorChris Duncan <chris@codecow.com>
Tue, 12 May 2026 18:38:38 +0000 (11:38 -0700)
committerChris Duncan <chris@codecow.com>
Tue, 12 May 2026 18:38:38 +0000 (11:38 -0700)
src/lib/vault/index.ts

index bb75ffbaeaf862a7eef224940c5cefbecbf8189f..dd80441b1ecd426bc395f67984c3ef696da0fbd8 100644 (file)
@@ -4,10 +4,14 @@
 import { Worker as NodeWorker } from 'node:worker_threads'
 import { Data } from '../database'
 
-type Task = {
+type TaskData = {
+       [key: string]: Data | Record<string, Data>
        url: string
        id: number
-       data: Record<string, Data | Record<string, Data>>
+}
+
+type Task = {
+       data: TaskData
        reject: (value: any) => void
        resolve: (value: any) => void
 }
@@ -51,14 +55,17 @@ export class Vault {
 
        get isLocked (): boolean { return this.#isLocked }
 
-       request<T extends Data | Record<string, Data>> (data: Record<string, Data | Record<string, Data>>): Promise<Record<string, T>> {
+       request<T extends Data | Record<string, Data>> (payload: Record<string, Data | Record<string, Data>>): Promise<Record<string, T>> {
                if (this.#isTerminated) {
                        throw new Error(TERMINATED)
                }
+               const data: TaskData = {
+                       ...payload,
+                       url: this.#url,
+                       id: performance.now(),
+               }
                return new Promise((resolve, reject): void => {
                        const task: Task = {
-                               url: this.#url,
-                               id: performance.now(),
                                data,
                                resolve,
                                reject
@@ -85,15 +92,13 @@ export class Vault {
        #process = (): void => {
                this.#job = this.#queue.shift()
                if (this.#job != null) {
-                       const { url, id, data, reject } = this.#job
+                       const { data, reject } = this.#job
                        const buffers: ArrayBuffer[] = []
                        for (const d of Object.values(data)) {
                                if (d instanceof ArrayBuffer) {
                                        buffers.push(d)
                                }
                        }
-                       data.url = url
-                       data.id = id
                        try {
                                BROWSER: this.#worker.postMessage(data, buffers)
                                NODE: this.#worker.postMessage({ data }, buffers)
@@ -115,7 +120,7 @@ export class Vault {
                if (this.#job == null) {
                        throw new Error('Vault worker returned results without an associated job.')
                }
-               const { url, id, resolve, reject } = this.#job
+               const { data: { url, id }, resolve, reject } = this.#job
                if (url == null) {
                        throw new Error('Vault worker job missing URL')
                }