Skip to content

Commit

Permalink
fix(deps): replace import_map with deps.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
vicary committed Feb 22, 2024
1 parent cdb64ca commit 90ccee4
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Executable.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Promisable } from "type-fest";
import { type Promisable } from "./deps.ts";

/**
* The Runner interface.
Expand Down
14 changes: 9 additions & 5 deletions ExecutableWorker.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -20,7 +24,7 @@ export class ExecutableWorker<

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

execute(payload: TPayload) {
Expand All @@ -40,7 +44,7 @@ export class ExecutableWorker<
}

async dispose() {
await this.#linked[releaseProxy]();
await this.#linked[comlink.releaseProxy]();
this.#worker.terminate();
}
}
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -33,6 +36,8 @@ class RunnerB implements Executable {...}
const pool = new Workerpool({
concurrency: 2,
workers: [RunnerA, RunnerB]
// enqueue() {...}
// dequeue() {...}
});

pool
Expand All @@ -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;
Expand All @@ -54,23 +59,23 @@ type MemoryMutexTask = Task<Payload> & { active?: boolean };
const tasks = new Set<MemoryMutexTask>();
const pool = new Workerpool<Payload>({
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) {
Expand Down
2 changes: 1 addition & 1 deletion Runner.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Executable } from "./Executable.ts";
import { type Executable } from "./Executable.ts";

export class RunnerExecutionError extends Error {
constructor(
Expand Down
2 changes: 1 addition & 1 deletion Task.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { JsonValue } from "type-fest";
import { type JsonValue } from "./deps.ts";

export interface Task<TPayload = JsonValue> {
/**
Expand Down
5 changes: 2 additions & 3 deletions Workerpool.test.ts
Original file line number Diff line number Diff line change
@@ -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<TPayload> = Task<TPayload> & { active?: boolean };
Expand Down Expand Up @@ -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++;
});

Expand Down
6 changes: 3 additions & 3 deletions Workerpool.ts
Original file line number Diff line number Diff line change
@@ -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<TPayload = JsonValue, TResult = unknown> = {
task: Task<TPayload>;
Expand Down
8 changes: 4 additions & 4 deletions __test__/example-worker.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/// <reference no-default-lib="true" />
/// <reference lib="deno.worker" />

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<ArrowFunction> = {
async execute(payload) {
Expand All @@ -14,4 +14,4 @@ const exposedObject: Executable<ArrowFunction> = {
},
};

expose(exposedObject);
comlink.expose(exposedObject);
6 changes: 0 additions & 6 deletions deno.json

This file was deleted.

4 changes: 4 additions & 0 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -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";

0 comments on commit 90ccee4

Please sign in to comment.