Skip to content

Commit 90ccee4

Browse files
committed
fix(deps): replace import_map with deps.ts
1 parent cdb64ca commit 90ccee4

10 files changed

Lines changed: 36 additions & 30 deletions

File tree

‎Executable.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Promisable } from "type-fest";
1+
import { type Promisable } from "./deps.ts";
22

33
/**
44
* The Runner interface.

‎ExecutableWorker.ts‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
import { releaseProxy, Remote, UnproxyOrClone, wrap } from "comlink";
2-
import type { Promisable } from "type-fest";
3-
import type { Executable } from "./Executable.ts";
1+
import {
2+
comlink,
3+
type Promisable,
4+
type Remote,
5+
type UnproxyOrClone,
6+
} from "./deps.ts";
7+
import { type Executable } from "./Executable.ts";
48

59
/**
610
* A Web Worker implementation in a `workerpool` compatible format, uses
@@ -20,7 +24,7 @@ export class ExecutableWorker<
2024

2125
constructor(uri: string, options?: Omit<WorkerOptions, "type">) {
2226
this.#worker = new Worker(uri, { ...options, type: "module" });
23-
this.#linked = wrap<Executable<TPayload, TResult>>(this.#worker);
27+
this.#linked = comlink.wrap<Executable<TPayload, TResult>>(this.#worker);
2428
}
2529

2630
execute(payload: TPayload) {
@@ -40,7 +44,7 @@ export class ExecutableWorker<
4044
}
4145

4246
async dispose() {
43-
await this.#linked[releaseProxy]();
47+
await this.#linked[comlink.releaseProxy]();
4448
this.#worker.terminate();
4549
}
4650
}

‎README.md‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ An unopinionated small scale worker pool abstraction which serves as a base inte
2424

2525
## Basic Usage
2626

27+
You need to implement your own `enqueue` and `dequeue` logic, see an in-memory
28+
implementation in the examples section.
29+
2730
```ts
2831
import { Executable, Workerpool } from "https://deno.land/x/workerpool/mod.ts";
2932

@@ -33,6 +36,8 @@ class RunnerB implements Executable {...}
3336
const pool = new Workerpool({
3437
concurrency: 2,
3538
workers: [RunnerA, RunnerB]
39+
// enqueue() {...}
40+
// dequeue() {...}
3641
});
3742

3843
pool
@@ -41,11 +46,11 @@ pool
4146
.start();
4247
```
4348

44-
## Runner Examples
49+
## Examples
4550

4651
### In-memory Queue
4752

48-
As a proof of concept, this is the most basic implementation of an in-memory queue.
53+
As a proof of concept, this is a simple implementation of an in-memory queue.
4954

5055
```ts
5156
type Payload = any;
@@ -54,23 +59,23 @@ type MemoryMutexTask = Task<Payload> & { active?: boolean };
5459
const tasks = new Set<MemoryMutexTask>();
5560
const pool = new Workerpool<Payload>({
5661
concurrency: 1,
57-
runners: [runnerA, runnerB],
62+
workers: [RunnerA, RunnerB],
5863
enqueue(task: MemoryMutexTask) {
5964
task.active = false;
6065
tasks.add(task);
6166
},
6267
dequeue() {
6368
// Uncomment the following line for FIFO queues
64-
// if ([...tasks].some(({ active }) => active)) return;
69+
// for (const { active } of task) if (active) return;
6570

6671
for (const task of tasks) {
6772
if (!task.active) {
68-
task.busy = true;
73+
task.active = true;
6974
return task;
7075
}
7176
}
7277
},
73-
onTaskFinish(error, result, { task }) {
78+
onTaskFinished(error, result, { task }) {
7479
tasks.delete(task);
7580

7681
if (error) {

‎Runner.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Executable } from "./Executable.ts";
1+
import { type Executable } from "./Executable.ts";
22

33
export class RunnerExecutionError extends Error {
44
constructor(

‎Task.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { JsonValue } from "type-fest";
1+
import { type JsonValue } from "./deps.ts";
22

33
export interface Task<TPayload = JsonValue> {
44
/**

‎Workerpool.test.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
import { proxy } from "comlink";
21
import { assertEquals } from "https://deno.land/std@0.155.0/testing/asserts.ts";
32
import { describe, it } from "https://deno.land/std@0.155.0/testing/bdd.ts";
43
import {
54
assertSpyCalls,
65
spy,
76
} from "https://deno.land/std@0.155.0/testing/mock.ts";
8-
import type { Class, SetOptional } from "type-fest";
97
import { Executable } from "./Executable.ts";
108
import { ExecutableWorker } from "./ExecutableWorker.ts";
119
import { Task } from "./Task.ts";
1210
import { Workerpool } from "./Workerpool.ts";
11+
import { comlink, type Class, type SetOptional } from "./deps.ts";
1312

1413
export type ArrowFunction = (...args: unknown[]) => unknown;
1514
type MemoryMutexTask<TPayload> = Task<TPayload> & { active?: boolean };
@@ -113,7 +112,7 @@ describe("Workerpool", () => {
113112
// Temporarily ignored, see https://github.com/GoogleChromeLabs/comlink/issues/598
114113
it.ignore("should support web workers", async () => {
115114
let counter = 0;
116-
const callback = proxy(() => {
115+
const callback = comlink.proxy(() => {
117116
counter++;
118117
});
119118

‎Workerpool.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import type { Class, JsonValue, Promisable, SetOptional } from "type-fest";
2-
import { Executable } from "./Executable.ts";
1+
import type { Class, JsonValue, Promisable, SetOptional } from "./deps.ts";
2+
import type { Executable } from "./Executable.ts";
33
import { Runner, RunnerExecutionError } from "./Runner.ts";
4-
import { Task } from "./Task.ts";
4+
import type { Task } from "./Task.ts";
55

66
type CallbackContext<TPayload = JsonValue, TResult = unknown> = {
77
task: Task<TPayload>;

‎__test__/example-worker.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/// <reference no-default-lib="true" />
22
/// <reference lib="deno.worker" />
33

4-
import { expose } from "comlink";
5-
import type { Executable } from "../Executable.ts";
6-
import { ArrowFunction } from "../Workerpool.test.ts";
4+
import { comlink } from "../deps.ts";
5+
import { type Executable } from "../Executable.ts";
6+
import { type ArrowFunction } from "../Workerpool.test.ts";
77

88
const exposedObject: Executable<ArrowFunction> = {
99
async execute(payload) {
@@ -14,4 +14,4 @@ const exposedObject: Executable<ArrowFunction> = {
1414
},
1515
};
1616

17-
expose(exposedObject);
17+
comlink.expose(exposedObject);

‎deno.json‎

Lines changed: 0 additions & 6 deletions
This file was deleted.

‎deps.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export * as comlink from "https://deno.land/x/comlink@4.3.1/mod.ts";
2+
3+
export type * from "https://deno.land/x/comlink@4.3.1/mod.ts";
4+
export type * from "https://deno.land/x/fest@v3.5.7-alpha.2/mod.ts";

0 commit comments

Comments
 (0)