Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@
"import": "./dist/index.js"
}
},
"author": "José Pablo Ramírez <webJose@gmail.com>",
"repository": {
"type": "git",
"url": "git+https://github.com/WJSoftware/wjfe-async-workers.git"
},
"author": {
"email": "webJose@gmail.com",
"name": "José Pablo Ramírez Vargas"
},
"homepage": "https://wjsoftware.github.io/wjfe-async-workers/",
"license": "MIT",
"devDependencies": {
"publint": "^0.2.10",
Expand Down
2 changes: 1 addition & 1 deletion pages/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"dev": "vite dev",
"build": "vite build",
"preview": "vite preview",
"deploy-pages": "npm ci && npm run build && gh-pages -b Pages -d build -t true",
"deploy-pages": "npm run build && gh-pages -b Pages -d build -t true",
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
"lint": "eslint ."
Expand Down
2 changes: 1 addition & 1 deletion src/workers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export type QueueingOptions = {
}

export interface IWorker {
connect(id: number, processMessage: ProcessMessageFn, resolve: (data: any) => void, reject: (reason: any) => void): DisconnectFn;
connect(id: number, processMessage: ProcessMessageFn, resolve: (data: any) => void, reject: (reason: any) => void): DisconnectFn | undefined;
post(message: AsyncMessage, transferables: Transferable[] | undefined): void;
terminate(): boolean;
}
Expand Down
2 changes: 1 addition & 1 deletion src/workers/InternalSharedWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class InternalSharedWorker implements IWorker {
processMessage: ProcessMessageFn,
resolve: (data: any) => void,
reject: (reason: any) => void
): DisconnectFn {
): DisconnectFn | undefined {
const listener = this.#listenerFactory(id, processMessage, resolve);
this.#worker.port.addEventListener('message', listener);
this.#worker.port.start();
Expand Down
3 changes: 2 additions & 1 deletion src/workers/InternalWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ export class InternalWorker implements IWorker {
};
}

connect(id: number, processMessage: ProcessMessageFn, resolve: (data: any) => void, reject: (reason: any) => void): DisconnectFn {
connect(id: number, processMessage: ProcessMessageFn, resolve: (data: any) => void, reject: (reason: any) => void): DisconnectFn | undefined {
if (this.#terminated) {
reject(new WorkerTerminatedMessage(id));
return;
}
const listener = this.#listenerFactory(id, processMessage, resolve);
this.#worker.addEventListener('message', listener);
Expand Down
3 changes: 2 additions & 1 deletion src/workers/WorkItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export class WorkItem<TResult> {
if (this.status === WorkItemStatus.Enqueued) {
this.#internal.data.reject(new CancelledMessage(true));
}
return this.#internal.cancellationSource || !this.#internal.disconnect;
this.#internal.cancelled ||= !!this.#internal.cancellationSource || this.status === WorkItemStatus.Cancelled;
return !!this.#internal.cancellationSource || this.status === WorkItemStatus.Cancelled;
}
}
8 changes: 5 additions & 3 deletions src/workers/WorkItemInternal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ export class WorkItemInternal<TResult = any> {
data;
options;
cancellationSource;
cancelled;
status: WorkItemStatusEnum;
disconnect: DisconnectFn | undefined;

constructor(worker: IWorker, data: WorkItemData<TResult>, options: QueueingOptions | undefined) {
this.status = WorkItemStatus.Enqueued;
this.cancelled = false;
this.worker = worker;
this.data = {
...data,
Expand Down Expand Up @@ -45,15 +47,15 @@ export class WorkItemInternal<TResult = any> {
}

start() {
if (this.cancellationSource && CancellationSource.isSignalled(this.cancellationSource.token)) {
return;
}
this.disconnect = this.worker.connect(
this.data.id,
this.options?.processMessage ?? this.#defaultProcessMessage.bind(this),
this.data.resolve,
this.data.reject
);
if (this.cancelled) {
return;
}
this.status = WorkItemStatus.Started;
const wiPayload: AsyncMessage = {
workItemId: this.data.id,
Expand Down