Skip to content

Commit a82399a

Browse files
authored
chore: Improve types (#25)
1 parent 99021d5 commit a82399a

File tree

6 files changed

+23
-40
lines changed

6 files changed

+23
-40
lines changed

src/index.ts

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,3 @@ export * from './workers/AsyncWorker.js';
1212
export * from "./workers/workerListener.js";
1313
export * from './workers/WorkerTerminatedMessage.js';
1414
export * from "./workers/WorkItem.js";
15-
16-
/**
17-
* Token primitive type.
18-
*/
19-
export type Token = Int32Array;
20-
21-
/**
22-
* Helper type that enables proper inference of payload and return types.
23-
*
24-
* Used by the async workers to provide proper Intellisense.
25-
*/
26-
export type WorkerTasks<T extends Record<string, (...args: any) => any>> = {
27-
[K in keyof T]: {
28-
payload: Parameters<T[K]>[0];
29-
return: ReturnType<T[K]>;
30-
}
31-
};
32-
33-
/**
34-
* Message sent to a worker.
35-
*/
36-
export type AsyncMessage<Tasks extends Record<string, (...args: any) => any>> = {
37-
task: keyof Tasks;
38-
workItemId: number;
39-
cancelToken?: Token;
40-
payload?: WorkerTasks<Tasks>[keyof Tasks]['payload'];
41-
};

src/workers.d.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,17 @@ export type WorkerTasks<T extends Record<string, (...args: any) => any>> = {
1919
/**
2020
* Message sent to a worker.
2121
*/
22-
export type AsyncMessage = {
22+
export type AsyncMessage<Tasks extends Record<string, (...args: any) => any>> = {
23+
task: keyof Tasks;
24+
workItemId: number;
25+
cancelToken?: Token;
26+
payload?: WorkerTasks<Tasks>[keyof Tasks]['payload'];
27+
};
28+
29+
/**
30+
* Message sent to a worker.
31+
*/
32+
export type AsyncMessageUntyped = {
2333
workItemId: number;
2434
task: string;
2535
cancelToken?: Token;
@@ -58,7 +68,7 @@ export type QueueingOptions = {
5868

5969
export interface IWorker {
6070
connect(id: number, processMessage: ProcessMessageFn, resolve: (data: any) => void, reject: (reason: any) => void): DisconnectFn | undefined;
61-
post(message: AsyncMessage, transferables: Transferable[] | undefined): void;
71+
post(message: AsyncMessageUntyped, transferables: Transferable[] | undefined): void;
6272
terminate(): boolean;
6373
}
6474

src/workers/InternalSharedWorker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { CancelledMessage } from "../cancellation/CancelledMessage.js";
2-
import type { AsyncMessage, AsyncResponse, DisconnectFn, IWorker, ProcessMessageFn, RejectFn, TaskCancelledMessage } from "../workers.js";
2+
import type { AsyncMessageUntyped, AsyncResponse, DisconnectFn, IWorker, ProcessMessageFn, RejectFn, TaskCancelledMessage } from "../workers.js";
33

44
function isTaskCancelledMessage(message: any): message is TaskCancelledMessage {
55
return message?.payload?._$cancelled === true && typeof message.workItemId === 'number';
@@ -50,7 +50,7 @@ export class InternalSharedWorker implements IWorker {
5050
};
5151
}
5252

53-
post(message: AsyncMessage, transferables: Transferable[] | undefined): void {
53+
post(message: AsyncMessageUntyped, transferables: Transferable[] | undefined): void {
5454
this.#worker.port.postMessage(message, { transfer: transferables });
5555
}
5656

src/workers/InternalWorker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { CancelledMessage } from "../cancellation/CancelledMessage.js";
2-
import type { AsyncMessage, AsyncResponse, DisconnectFn, IWorker, ProcessMessageFn, RejectFn, TaskCancelledMessage } from "../workers.js";
2+
import type { AsyncMessageUntyped, AsyncResponse, DisconnectFn, IWorker, ProcessMessageFn, RejectFn, TaskCancelledMessage } from "../workers.js";
33
import { WorkerTerminatedMessage } from "./WorkerTerminatedMessage.js";
44

55
function isTaskCancelledMessage(message: any): message is TaskCancelledMessage {
@@ -51,7 +51,7 @@ export class InternalWorker implements IWorker {
5151
};
5252
}
5353

54-
post(message: AsyncMessage, transferables: Transferable[] | undefined): void {
54+
post(message: AsyncMessageUntyped, transferables: Transferable[] | undefined): void {
5555
if (this.#terminated) {
5656
throw new Error('The worker has been terminated and cannot accept new messages.');
5757
}

src/workers/WorkItemInternal.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { CancellationSource } from "../cancellation/CancellationSource.js";
22
import { CancelledMessage } from "../cancellation/CancelledMessage.js";
3-
import type { AsyncMessage, DisconnectFn, IWorker, QueueingOptions, WorkItemData } from "../workers.js";
3+
import type { AsyncMessageUntyped, DisconnectFn, IWorker, QueueingOptions, WorkItemData } from "../workers.js";
44
import { WorkItemStatus, type WorkItemStatusEnum } from "./AsyncWorker.js";
5-
import { WorkerTerminatedMessage } from "./WorkerTerminatedMessage";
5+
import { WorkerTerminatedMessage } from "./WorkerTerminatedMessage.js";
66

77
export class WorkItemInternal<TResult = any> {
88
worker;
@@ -13,7 +13,7 @@ export class WorkItemInternal<TResult = any> {
1313
status: WorkItemStatusEnum;
1414
disconnect: DisconnectFn | undefined;
1515

16-
constructor(worker: IWorker, data: WorkItemData<TResult>, options: QueueingOptions | undefined) {
16+
constructor(worker: IWorker, data: WorkItemData<TResult>, options?: QueueingOptions | undefined) {
1717
this.status = WorkItemStatus.Enqueued;
1818
this.cancelled = false;
1919
this.worker = worker;
@@ -57,7 +57,7 @@ export class WorkItemInternal<TResult = any> {
5757
return;
5858
}
5959
this.status = WorkItemStatus.Started;
60-
const wiPayload: AsyncMessage = {
60+
const wiPayload: AsyncMessageUntyped = {
6161
workItemId: this.data.id,
6262
task: this.data.task,
6363
cancelToken: this.cancellationSource?.token,

src/workers/workerListener.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { TaskCancelledError } from "../cancellation/TaskCancelledError.js";
2-
import { AsyncMessage, AsyncResponse } from "../workers.js";
2+
import type { AsyncMessageUntyped, AsyncResponse } from "../workers.d.ts";
33

44
/**
55
* Defines the function provided to worker tasks so workers can communicate back to the calling thread.
66
*/
77
export type PostFn = (payload: any, options?: WindowPostMessageOptions) => void;
88

9-
function isAsyncMessage(msg: any): msg is AsyncMessage {
9+
function isAsyncMessage(msg: any): msg is AsyncMessageUntyped {
1010
return typeof msg.workItemId === 'number' && msg.task && typeof msg.task === 'string';
1111
}
1212

@@ -24,7 +24,7 @@ function post(workItemId: number, payload: any, options?: WindowPostMessageOptio
2424
* @returns The sought listener.
2525
*/
2626
export function workerListener<Tasks extends Record<string, (...args: any) => any>>(workerTasks: Tasks) {
27-
return (ev: MessageEvent<AsyncMessage>) => {
27+
return (ev: MessageEvent<AsyncMessageUntyped>) => {
2828
if (!isAsyncMessage(ev.data)) {
2929
return;
3030
}

0 commit comments

Comments
 (0)