1
1
import { result } from '@utils' ;
2
+ import type { Serializable as CPSerializable } from 'child_process' ;
2
3
3
4
import type { InMemoryFileSystem } from '../compiler/sys/in-memory-fs' ;
4
5
import type {
@@ -2321,6 +2322,13 @@ export interface VNodeProdData {
2321
2322
[ key : string ] : any ;
2322
2323
}
2323
2324
2325
+ /**
2326
+ * An abstraction to bundle up four methods which _may_ be handled by
2327
+ * dispatching work to workers running in other OS threads or may be called
2328
+ * synchronously. Environment and `CompilerSystem` related setup code will
2329
+ * determine which one, but in either case the call sites for these methods can
2330
+ * dispatch to this shared interface.
2331
+ */
2324
2332
export interface CompilerWorkerContext {
2325
2333
optimizeCss ( inputOpts : OptimizeCssInput ) : Promise < OptimizeCssOutput > ;
2326
2334
prepareModule (
@@ -2333,17 +2341,53 @@ export interface CompilerWorkerContext {
2333
2341
transformCssToEsm ( input : TransformCssToEsmInput ) : Promise < TransformCssToEsmOutput > ;
2334
2342
}
2335
2343
2336
- export interface MsgToWorker {
2344
+ /**
2345
+ * The methods that are supported on a {@link d.CompilerWorkerContext}
2346
+ */
2347
+ export type WorkerContextMethod = keyof CompilerWorkerContext ;
2348
+
2349
+ /**
2350
+ * A little type guard which will cause a type error if the parameter `T` does
2351
+ * not satisfy {@link CPSerializable} (i.e. if it's not possible to cleanly
2352
+ * serialize it for message passing via an IPC channel).
2353
+ */
2354
+ type IPCSerializable < T extends CPSerializable > = T ;
2355
+
2356
+ /**
2357
+ * A manifest for a job that a worker thread should carry out, as determined by
2358
+ * and dispatched from the main thread. This includes the name of the task to do
2359
+ * and any arguments necessary to carry it out properly.
2360
+ *
2361
+ * This message must satisfy {@link CPSerializable} so it can be sent from the
2362
+ * main thread to a worker thread via an IPC channel
2363
+ */
2364
+ export type MsgToWorker < T extends WorkerContextMethod > = IPCSerializable < {
2337
2365
stencilId : number ;
2338
- args : any [ ] ;
2339
- }
2366
+ method : T ;
2367
+ args : Parameters < CompilerWorkerContext [ T ] > ;
2368
+ } > ;
2340
2369
2341
- export interface MsgFromWorker {
2370
+ /**
2371
+ * A manifest for a job that a worker thread should carry out, as determined by
2372
+ * and dispatched from the main thread. This includes the name of the task to do
2373
+ * and any arguments necessary to carry it out properly.
2374
+ *
2375
+ * This message must satisfy {@link CPSerializable} so it can be sent from the
2376
+ * main thread to a worker thread via an IPC channel
2377
+ */
2378
+ export type MsgFromWorker < T extends WorkerContextMethod > = IPCSerializable < {
2342
2379
stencilId ?: number ;
2343
- stencilRtnValue : any ;
2344
- stencilRtnError : string ;
2345
- }
2380
+ stencilRtnValue : ReturnType < CompilerWorkerContext [ T ] > ;
2381
+ stencilRtnError : string | null ;
2382
+ } > ;
2346
2383
2384
+ /**
2385
+ * A description of a task which should be passed to a worker in another
2386
+ * thread. This interface differs from {@link MsgToWorker} in that it doesn't
2387
+ * have to be serializable for transmission through an IPC channel, so we can
2388
+ * hold things like a `resolve` and `reject` callback to use when the task
2389
+ * completes.
2390
+ */
2347
2391
export interface CompilerWorkerTask {
2348
2392
stencilId ?: number ;
2349
2393
inputArgs ?: any [ ] ;
@@ -2352,7 +2396,17 @@ export interface CompilerWorkerTask {
2352
2396
retries ?: number ;
2353
2397
}
2354
2398
2355
- export type WorkerMsgHandler = ( msgToWorker : MsgToWorker ) => Promise < any > ;
2399
+ /**
2400
+ * A handler for IPC messages from the main thread to a worker thread. This
2401
+ * involves dispatching an action specified by a {@link MsgToWorker} object to a
2402
+ * {@link CompilerWorkerContext}.
2403
+ *
2404
+ * @param msgToWorker the message to handle
2405
+ * @returns the return value of the specified function
2406
+ */
2407
+ export type WorkerMsgHandler = < T extends WorkerContextMethod > (
2408
+ msgToWorker : MsgToWorker < T > ,
2409
+ ) => ReturnType < CompilerWorkerContext [ T ] > ;
2356
2410
2357
2411
export interface TranspileModuleResults {
2358
2412
sourceFilePath : string ;
0 commit comments