1- import type { WorkerFactory } from './types.js' ;
1+ import type { WorkerFactory , WorkerManifest } from './types.js' ;
22import Logger from '@matrixai/logger' ;
33import { CreateDestroy , ready } from '@matrixai/async-init/CreateDestroy.js' ;
44import { type WorkerTaskInput } from './types.js' ;
55import * as errors from './errors.js' ;
66import WorkerPool from './WorkerPool.js' ;
77
88@CreateDestroy ( )
9- class WorkerManager {
9+ class WorkerManager < T extends WorkerManifest > {
1010 /**
1111 * Creates the WorkerManager
1212 * The workerFactory needs to be a callback:
@@ -19,18 +19,21 @@ class WorkerManager {
1919 * If `cores` is set to 0, this creates a useless worker pool
2020 * Use `undefined` to mean using all cores
2121 */
22- public static async createWorkerManager ( {
22+ public static async createWorkerManager < T extends WorkerManifest > ( {
2323 workerFactory,
24+ manifest,
2425 cores = 1 ,
2526 logger = new Logger ( this . name ) ,
2627 } : {
2728 workerFactory : WorkerFactory ;
29+ manifest : T ;
2830 cores ?: number ;
2931 logger ?: Logger ;
30- } ) : Promise < WorkerManager > {
32+ } ) : Promise < WorkerManager < T > > {
3133 logger . info ( 'Creating WorkerManager' ) ;
32- const workerManager = new this ( {
34+ const workerManager = new this < T > ( {
3335 workerFactory,
36+ manifest,
3437 cores,
3538 logger,
3639 } ) ;
@@ -39,19 +42,36 @@ class WorkerManager {
3942 }
4043
4144 protected pool : WorkerPool ;
45+ protected manifest : T ;
46+ public proxy : T ;
4247 protected logger : Logger ;
4348
4449 public constructor ( {
4550 workerFactory,
51+ manifest,
4652 cores,
4753 logger,
4854 } : {
4955 workerFactory : WorkerFactory ;
56+ manifest : T ;
5057 cores : number ;
5158 logger : Logger ;
5259 } ) {
5360 this . logger = logger ;
61+ this . manifest = manifest ;
5462 this . pool = new WorkerPool ( cores , workerFactory ) ;
63+ this . proxy = new Proxy ( this . manifest , {
64+ apply : ( ) => {
65+ throw Error ( 'TMP IMP NEVER' ) ;
66+ } ,
67+ get : ( target : T , prop ) => {
68+ if ( typeof prop === 'symbol' ) return ;
69+ return ( data , transferList ) => {
70+ console . log ( `${ prop } called with ` , data , transferList ) ;
71+ return this . call ( { type : prop , data, transferList } ) ;
72+ } ;
73+ } ,
74+ } ) ;
5575 }
5676
5777 public async destroy ( {
0 commit comments