Skip to content

Commit

Permalink
refactor: adapt fixed queue
Browse files Browse the repository at this point in the history
  • Loading branch information
metcoder95 committed May 20, 2024
1 parent 83027aa commit b56e61c
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 204 deletions.
189 changes: 0 additions & 189 deletions src/fixed-queue.ts

This file was deleted.

15 changes: 9 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
TaskQueue,
isTaskQueue,
ArrayTaskQueue,
FixedQueue,
TaskInfo,
PiscinaTask,
TransferList,
Expand All @@ -49,7 +50,6 @@ import {
toHistogramIntegerNano,
maybeFileURLToPath
} from './common';
import FixedQueue from './fixed-queue';

const { version } = JSON.parse(
readFileSync(
Expand Down Expand Up @@ -922,6 +922,14 @@ export default class Piscina<T = any, R = any> extends EventEmitterAsyncResource
return Piscina;
}

static get FixedQueue () {
return FixedQueue;
}

static get ArrayTaskQueue () {
return ArrayTaskQueue;
}

static move (val : Transferable | TransferListItem | ArrayBufferView | ArrayBuffer | MessagePort) {
if (val != null && typeof val === 'object' && typeof val !== 'function') {
if (!isTransferable(val)) {
Expand All @@ -946,11 +954,6 @@ export default class Piscina<T = any, R = any> extends EventEmitterAsyncResource
export const move = Piscina.move;
export const isWorkerThread = Piscina.isWorkerThread;
export const workerData = Piscina.workerData;
// Mutate Piscina class to allow named import in commonjs
// @ts-expect-error
Piscina.FixedQueue = FixedQueue;
// @ts-expect-error
Piscina.ArrayTaskQueue = ArrayTaskQueue;

export {
Piscina,
Expand Down
1 change: 1 addition & 0 deletions src/task_queue/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { kTransferable, kValue, kQueueOptions } from '../symbols';
import type { Task, TaskQueue, PiscinaTask } from './common';

export { ArrayTaskQueue } from './array_queue';
export { FixedQueue } from './fixed_queue';

Check failure on line 13 in src/task_queue/index.ts

View workflow job for this annotation

GitHub Actions / Tests (ubuntu-latest, 18.x)

Cannot find module './fixed_queue' or its corresponding type declarations.

Check failure on line 13 in src/task_queue/index.ts

View workflow job for this annotation

GitHub Actions / Tests (ubuntu-latest, 20.x)

Cannot find module './fixed_queue' or its corresponding type declarations.

Check failure on line 13 in src/task_queue/index.ts

View workflow job for this annotation

GitHub Actions / Tests (ubuntu-latest, 22.x)

Cannot find module './fixed_queue' or its corresponding type declarations.

Check failure on line 13 in src/task_queue/index.ts

View workflow job for this annotation

GitHub Actions / Tests (macos-latest, 18.x)

Cannot find module './fixed_queue' or its corresponding type declarations.

Check failure on line 13 in src/task_queue/index.ts

View workflow job for this annotation

GitHub Actions / Tests (macos-latest, 20.x)

Cannot find module './fixed_queue' or its corresponding type declarations.

Check failure on line 13 in src/task_queue/index.ts

View workflow job for this annotation

GitHub Actions / Tests (macos-latest, 22.x)

Cannot find module './fixed_queue' or its corresponding type declarations.

Check failure on line 13 in src/task_queue/index.ts

View workflow job for this annotation

GitHub Actions / Tests (windows-latest, 18.x)

Cannot find module './fixed_queue' or its corresponding type declarations.

Check failure on line 13 in src/task_queue/index.ts

View workflow job for this annotation

GitHub Actions / Tests (windows-latest, 20.x)

Cannot find module './fixed_queue' or its corresponding type declarations.

Check failure on line 13 in src/task_queue/index.ts

View workflow job for this annotation

GitHub Actions / Tests (windows-latest, 22.x)

Cannot find module './fixed_queue' or its corresponding type declarations.

export type TaskCallback = (err: Error, result: any) => void
// Grab the type of `transferList` off `MessagePort`. At the time of writing,
Expand Down
4 changes: 2 additions & 2 deletions test/fixed-queue.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { test } from 'tap';
import { Task } from '../dist/types';
import { kQueueOptions } from '../dist/symbols';
import { Piscina, FixedQueue } from '..';
import { Piscina, FixedQueue, PiscinaTask as Task } from '..';
import { resolve } from 'node:path';

// @ts-expect-error
class QueueTask implements Task {
get [kQueueOptions] () {
return null;
Expand Down
13 changes: 6 additions & 7 deletions test/task-queue.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Piscina from '..';
import Piscina, { PiscinaTask, TaskQueue } from '..';
import { test } from 'tap';
import { resolve } from 'path';
import { TaskQueue, Task } from '../dist/task_queue/index';

test('will put items into a task queue until they can run', async ({ equal }) => {
const pool = new Piscina({
Expand Down Expand Up @@ -221,19 +220,19 @@ test('custom task queue works', async ({ equal, ok }) => {
let pushCalled : boolean = false;

class CustomTaskPool implements TaskQueue {
tasks: Task[] = [];
tasks: PiscinaTask[] = [];

get size () : number {
sizeCalled = true;
return this.tasks.length;
}

shift () : Task | null {
shift () : PiscinaTask | null {
shiftCalled = true;
return this.tasks.length > 0 ? this.tasks.shift() as Task : null;
return this.tasks.length > 0 ? this.tasks.shift() as PiscinaTask : null;
}

push (task : Task) : void {
push (task : PiscinaTask) : void {
pushCalled = true;
this.tasks.push(task);

Expand All @@ -246,7 +245,7 @@ test('custom task queue works', async ({ equal, ok }) => {
}
}

remove (task : Task) : void {
remove (task : PiscinaTask) : void {
const index = this.tasks.indexOf(task);
this.tasks.splice(index, 1);
}
Expand Down

0 comments on commit b56e61c

Please sign in to comment.