diff --git a/Executable.ts b/Executable.ts index 7bf4827..93c4ce2 100644 --- a/Executable.ts +++ b/Executable.ts @@ -1,4 +1,4 @@ -import type { Promisable } from "type-fest"; +import { type Promisable } from "./deps.ts"; /** * The Runner interface. diff --git a/ExecutableWorker.ts b/ExecutableWorker.ts index 4d0191e..2376354 100644 --- a/ExecutableWorker.ts +++ b/ExecutableWorker.ts @@ -1,6 +1,10 @@ -import { releaseProxy, Remote, UnproxyOrClone, wrap } from "comlink"; -import type { Promisable } from "type-fest"; -import type { Executable } from "./Executable.ts"; +import { + comlink, + type Promisable, + type Remote, + type UnproxyOrClone, +} from "./deps.ts"; +import { type Executable } from "./Executable.ts"; /** * A Web Worker implementation in a `workerpool` compatible format, uses @@ -20,7 +24,7 @@ export class ExecutableWorker< constructor(uri: string, options?: Omit) { this.#worker = new Worker(uri, { ...options, type: "module" }); - this.#linked = wrap>(this.#worker); + this.#linked = comlink.wrap>(this.#worker); } execute(payload: TPayload) { @@ -40,7 +44,7 @@ export class ExecutableWorker< } async dispose() { - await this.#linked[releaseProxy](); + await this.#linked[comlink.releaseProxy](); this.#worker.terminate(); } } diff --git a/README.md b/README.md index e54602f..d822561 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,9 @@ An unopinionated small scale worker pool abstraction which serves as a base inte ## Basic Usage +You need to implement your own `enqueue` and `dequeue` logic, see an in-memory +implementation in the examples section. + ```ts import { Executable, Workerpool } from "https://deno.land/x/workerpool/mod.ts"; @@ -33,6 +36,8 @@ class RunnerB implements Executable {...} const pool = new Workerpool({ concurrency: 2, workers: [RunnerA, RunnerB] + // enqueue() {...} + // dequeue() {...} }); pool @@ -41,11 +46,11 @@ pool .start(); ``` -## Runner Examples +## Examples ### In-memory Queue -As a proof of concept, this is the most basic implementation of an in-memory queue. +As a proof of concept, this is a simple implementation of an in-memory queue. ```ts type Payload = any; @@ -54,23 +59,23 @@ type MemoryMutexTask = Task & { active?: boolean }; const tasks = new Set(); const pool = new Workerpool({ concurrency: 1, - runners: [runnerA, runnerB], + workers: [RunnerA, RunnerB], enqueue(task: MemoryMutexTask) { task.active = false; tasks.add(task); }, dequeue() { // Uncomment the following line for FIFO queues - // if ([...tasks].some(({ active }) => active)) return; + // for (const { active } of task) if (active) return; for (const task of tasks) { if (!task.active) { - task.busy = true; + task.active = true; return task; } } }, - onTaskFinish(error, result, { task }) { + onTaskFinished(error, result, { task }) { tasks.delete(task); if (error) { diff --git a/Runner.ts b/Runner.ts index 3034fc5..e6bded7 100644 --- a/Runner.ts +++ b/Runner.ts @@ -1,4 +1,4 @@ -import { Executable } from "./Executable.ts"; +import { type Executable } from "./Executable.ts"; export class RunnerExecutionError extends Error { constructor( diff --git a/Task.ts b/Task.ts index d5408f8..38237ea 100644 --- a/Task.ts +++ b/Task.ts @@ -1,4 +1,4 @@ -import type { JsonValue } from "type-fest"; +import { type JsonValue } from "./deps.ts"; export interface Task { /** diff --git a/Workerpool.test.ts b/Workerpool.test.ts index c87dda2..6d919b2 100644 --- a/Workerpool.test.ts +++ b/Workerpool.test.ts @@ -1,15 +1,14 @@ -import { proxy } from "comlink"; import { assertEquals } from "https://deno.land/std@0.155.0/testing/asserts.ts"; import { describe, it } from "https://deno.land/std@0.155.0/testing/bdd.ts"; import { assertSpyCalls, spy, } from "https://deno.land/std@0.155.0/testing/mock.ts"; -import type { Class, SetOptional } from "type-fest"; import { Executable } from "./Executable.ts"; import { ExecutableWorker } from "./ExecutableWorker.ts"; import { Task } from "./Task.ts"; import { Workerpool } from "./Workerpool.ts"; +import { comlink, type Class, type SetOptional } from "./deps.ts"; export type ArrowFunction = (...args: unknown[]) => unknown; type MemoryMutexTask = Task & { active?: boolean }; @@ -113,7 +112,7 @@ describe("Workerpool", () => { // Temporarily ignored, see https://github.com/GoogleChromeLabs/comlink/issues/598 it.ignore("should support web workers", async () => { let counter = 0; - const callback = proxy(() => { + const callback = comlink.proxy(() => { counter++; }); diff --git a/Workerpool.ts b/Workerpool.ts index ea1bcca..e3c998b 100644 --- a/Workerpool.ts +++ b/Workerpool.ts @@ -1,7 +1,7 @@ -import type { Class, JsonValue, Promisable, SetOptional } from "type-fest"; -import { Executable } from "./Executable.ts"; +import type { Class, JsonValue, Promisable, SetOptional } from "./deps.ts"; +import type { Executable } from "./Executable.ts"; import { Runner, RunnerExecutionError } from "./Runner.ts"; -import { Task } from "./Task.ts"; +import type { Task } from "./Task.ts"; type CallbackContext = { task: Task; diff --git a/__test__/example-worker.ts b/__test__/example-worker.ts index a18dd38..9ae8b55 100644 --- a/__test__/example-worker.ts +++ b/__test__/example-worker.ts @@ -1,9 +1,9 @@ /// /// -import { expose } from "comlink"; -import type { Executable } from "../Executable.ts"; -import { ArrowFunction } from "../Workerpool.test.ts"; +import { comlink } from "../deps.ts"; +import { type Executable } from "../Executable.ts"; +import { type ArrowFunction } from "../Workerpool.test.ts"; const exposedObject: Executable = { async execute(payload) { @@ -14,4 +14,4 @@ const exposedObject: Executable = { }, }; -expose(exposedObject); +comlink.expose(exposedObject); diff --git a/deno.json b/deno.json deleted file mode 100644 index a6a4d9b..0000000 --- a/deno.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "imports": { - "comlink": "https://deno.land/x/comlink@4.3.1/mod.ts", - "type-fest": "https://deno.land/x/fest@v3.5.7-alpha.2/mod.ts" - } -} diff --git a/deps.ts b/deps.ts new file mode 100644 index 0000000..fced71e --- /dev/null +++ b/deps.ts @@ -0,0 +1,4 @@ +export * as comlink from "https://deno.land/x/comlink@4.3.1/mod.ts"; + +export type * from "https://deno.land/x/comlink@4.3.1/mod.ts"; +export type * from "https://deno.land/x/fest@v3.5.7-alpha.2/mod.ts";