Skip to content

Commit 0cc20b5

Browse files
committed
wip: prototyping
[ci skip]
1 parent 91161db commit 0cc20b5

13 files changed

+79
-76
lines changed

src/WorkerManager.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import type { WorkerFactory } from './types';
1+
import type { WorkerFactory } from './types.js';
22
import Logger from '@matrixai/logger';
3-
import { CreateDestroy, ready } from '@matrixai/async-init/dist/CreateDestroy';
4-
import { WorkerTask } from './types';
5-
import * as errors from './errors';
6-
import WorkerPool from './WorkerPool';
3+
import { CreateDestroy, ready } from '@matrixai/async-init/CreateDestroy.js';
4+
import { WorkerTask } from './types.js';
5+
import * as errors from './errors.js';
6+
import WorkerPool from './WorkerPool.js';
77

88
@CreateDestroy()
99
class WorkerManager {
File renamed without changes.

src/WorkerPool.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { WorkerFactory, WorkerTask } from './types';
21
import type { Worker } from 'node:worker_threads';
32
import type { Subscription } from 'rxjs';
3+
import type { WorkerFactory, WorkerTask } from './types.js';
44
import { Subject } from 'rxjs';
55

66
const taskInfoSymbol = Symbol('Task Info Symbol');

src/expose.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import type { WorkerTask, WorkerManifest } from '#types.js';
2+
import { isMainThread, parentPort, threadId } from 'node:worker_threads';
3+
4+
/**
5+
* Call inside a worker to set it up and expose the supported worker functions.
6+
* Calling this within the main thread will do nothing
7+
* @param workerManifest
8+
*/
9+
function expose(workerManifest: WorkerManifest) {
10+
// We don't want to run this in the main thread since it's not acting as a worker
11+
if (isMainThread) return;
12+
const handleMessage = async (task: WorkerTask) => {
13+
console.log(task);
14+
const method = workerManifest[task.type];
15+
if (method == null) {
16+
const e = Error('TMP IMP missing handler');
17+
console.error(e);
18+
throw e;
19+
}
20+
const result = await method(task);
21+
parentPort?.postMessage(result);
22+
};
23+
const handleMessageError = (e) => {
24+
console.error(`Worker ${threadId} got error`, e);
25+
};
26+
parentPort!.once('close', () => {
27+
console.log(`Worker ${threadId} closed!`);
28+
parentPort!.off('message', handleMessage);
29+
parentPort!.off('messageerror', handleMessageError);
30+
});
31+
}
32+
33+
export { expose };

src/index.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,2 @@
11
export { default as WorkerManager } from './WorkerManager.js';
2-
export { default as workerModule } from './workerModule.js';
32
export * as errors from './errors.js';
4-
5-
export type { default as WorkerManagerInterface } from './WorkerManagerInterface.js';
6-
// Export type { WorkerModule } from './workerModule.js';
7-
export type { ModuleMethods, ModuleThread, QueuedTask } from './types.js';
File renamed without changes.
File renamed without changes.

src/types.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,9 @@ type WorkerTask<T extends string = string, D extends any = any> = {
77
data: D;
88
};
99

10-
export type { WorkerFactory, WorkerTask };
10+
type WorkerFunction<I extends any = any, O extends any = any> = (
11+
data: I,
12+
) => Promise<O>;
13+
type WorkerManifest = Record<string, WorkerFunction>;
14+
15+
export type { WorkerFactory, WorkerTask, WorkerFunction, WorkerManifest };

src/worker.js

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

src/worker.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// This is an example worker script
2+
3+
import type { WorkerManifest } from '#types.js';
4+
import { expose } from './expose.js';
5+
6+
const worker = {
7+
test: async (data: void) => {
8+
return 'hello world!';
9+
},
10+
add: async (data: { a: number; b: number }): Promise<number> => {
11+
return data.a + data.b;
12+
},
13+
sub: async (data: { a: number; b: number }): Promise<number> => {
14+
return data.a - data.b;
15+
},
16+
fac: async (data: number): Promise<number> => {
17+
let acc = 1;
18+
for (let i = 1; i < data; i++) {
19+
acc = acc * i;
20+
}
21+
return acc;
22+
},
23+
} satisfies WorkerManifest;
24+
25+
expose(worker);
26+
27+
export default worker;

0 commit comments

Comments
 (0)