1
- import type { WorkerFactory } from './types.js' ;
1
+ import type { WorkerFactory , WorkerManifest } from './types.js' ;
2
2
import Logger from '@matrixai/logger' ;
3
3
import { CreateDestroy , ready } from '@matrixai/async-init/CreateDestroy.js' ;
4
4
import { type WorkerTaskInput } from './types.js' ;
5
5
import * as errors from './errors.js' ;
6
6
import WorkerPool from './WorkerPool.js' ;
7
7
8
8
@CreateDestroy ( )
9
- class WorkerManager {
9
+ class WorkerManager < T extends WorkerManifest > {
10
10
/**
11
11
* Creates the WorkerManager
12
12
* The workerFactory needs to be a callback:
@@ -19,18 +19,21 @@ class WorkerManager {
19
19
* If `cores` is set to 0, this creates a useless worker pool
20
20
* Use `undefined` to mean using all cores
21
21
*/
22
- public static async createWorkerManager ( {
22
+ public static async createWorkerManager < T extends WorkerManifest > ( {
23
23
workerFactory,
24
+ manifest,
24
25
cores = 1 ,
25
26
logger = new Logger ( this . name ) ,
26
27
} : {
27
28
workerFactory : WorkerFactory ;
29
+ manifest : T ;
28
30
cores ?: number ;
29
31
logger ?: Logger ;
30
- } ) : Promise < WorkerManager > {
32
+ } ) : Promise < WorkerManager < T > > {
31
33
logger . info ( 'Creating WorkerManager' ) ;
32
- const workerManager = new this ( {
34
+ const workerManager = new this < T > ( {
33
35
workerFactory,
36
+ manifest,
34
37
cores,
35
38
logger,
36
39
} ) ;
@@ -39,19 +42,36 @@ class WorkerManager {
39
42
}
40
43
41
44
protected pool : WorkerPool ;
45
+ protected manifest : T ;
46
+ public proxy : T ;
42
47
protected logger : Logger ;
43
48
44
49
public constructor ( {
45
50
workerFactory,
51
+ manifest,
46
52
cores,
47
53
logger,
48
54
} : {
49
55
workerFactory : WorkerFactory ;
56
+ manifest : T ;
50
57
cores : number ;
51
58
logger : Logger ;
52
59
} ) {
53
60
this . logger = logger ;
61
+ this . manifest = manifest ;
54
62
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
+ } ) ;
55
75
}
56
76
57
77
public async destroy ( {
0 commit comments