]> zoso.dev Git - libnemo.git/commitdiff
Tabs.
authorChris Duncan <chris@zoso.dev>
Mon, 18 Nov 2024 21:51:17 +0000 (13:51 -0800)
committerChris Duncan <chris@zoso.dev>
Mon, 18 Nov 2024 21:51:17 +0000 (13:51 -0800)
src/lib/thread.ts

index 948d6036b56f3f15b9a189190424a18274e8a22b..171416b0bca1886c9841e5f6ed72a6f9b82cbca9 100644 (file)
@@ -2,58 +2,58 @@
 // SPDX-License-Identifier: GPL-3.0-or-later
 
 if (globalThis.Worker == null) {
-  const { Worker } = await import('node:worker_threads')
-  //@ts-expect-error
-  Worker.prototype.addEventListener = Worker.prototype.addListener
-  //@ts-expect-error
-  globalThis.Worker = Worker
+       const { Worker } = await import('node:worker_threads')
+       //@ts-expect-error
+       Worker.prototype.addEventListener = Worker.prototype.addListener
+       //@ts-expect-error
+       globalThis.Worker = Worker
 }
 
 type Task = {
-  data: object,
-  resolve: Function
+       data: object,
+       resolve: Function
 }
 
 /**
 * Processes tasks from a queue using a Web Worker.
 */
 export class Thread {
-  #isAvailable: boolean = true
-  #queue: Task[] = []
-  #task?: Task
-  #worker: Worker
+       #isAvailable: boolean = true
+       #queue: Task[] = []
+       #task?: Task
+       #worker: Worker
 
-  #post (next: Task) {
-    this.#isAvailable = false
-    this.#task = next
-    this.#worker.postMessage(next.data)
-  }
+       #post (next: Task) {
+               this.#isAvailable = false
+               this.#task = next
+               this.#worker.postMessage(next.data)
+       }
 
-  constructor (url: string | URL) {
-    this.#worker = new Worker(new URL(url, import.meta.url), { type: 'module' })
-    this.#worker.addEventListener('message', (message) => {
-      const result = message.data ?? message
-      if (this.#task == null) {
-        throw new ReferenceError(`Error resolving Worker result: ${result}`)
-      }
-      const resolve = this.#task.resolve
-      const next = this.#queue.shift()
-      if (next == null) {
-        this.#isAvailable = true
-      } else {
-        this.#post(next)
-      }
-      resolve(result)
-    })
-  }
+       constructor (url: string | URL) {
+               this.#worker = new Worker(new URL(url, import.meta.url), { type: 'module' })
+               this.#worker.addEventListener('message', (message) => {
+                       const result = message.data ?? message
+                       if (this.#task == null) {
+                               throw new ReferenceError(`Error resolving Worker result: ${result}`)
+                       }
+                       const resolve = this.#task.resolve
+                       const next = this.#queue.shift()
+                       if (next == null) {
+                               this.#isAvailable = true
+                       } else {
+                               this.#post(next)
+                       }
+                       resolve(result)
+               })
+       }
 
-  async work (data: object): Promise<any> {
-    return new Promise(resolve => {
-      if (this.#isAvailable) {
-        this.#post({ data, resolve })
-      } else {
-        this.#queue.push({ data, resolve })
-      }
-    })
-  }
+       async work (data: object): Promise<any> {
+               return new Promise(resolve => {
+                       if (this.#isAvailable) {
+                               this.#post({ data, resolve })
+                       } else {
+                               this.#queue.push({ data, resolve })
+                       }
+               })
+       }
 }