@@ -369,7 +369,6 @@ function getCreateCliWrapper (vitestPackage, frameworkVersion) {
369
369
return vitestPackage
370
370
}
371
371
372
-
373
372
// UNUSED RIGHT NOW, but we can use it to bind the async resource to the test fn
374
373
// getFn is what's used to get the test fn to run it with vitest:
375
374
// https://github.com/vitest-dev/vitest/blob/0cbad1b0d0d56f1ec60f8496678d1435f8bb8977/packages/runner/src/run.ts#L315-L321
@@ -386,29 +385,31 @@ addHook({
386
385
return suitePackage
387
386
} )
388
387
389
- const processToHandler = new WeakMap ( )
388
+ const processToHandler = new WeakSet ( )
389
+
390
+ function threadHandler ( thread ) {
391
+ if ( processToHandler . has ( thread . process ) ) {
392
+ return
393
+ }
394
+ processToHandler . add ( thread . process )
395
+ thread . process . on ( 'message' , ( message ) => {
396
+ if ( message . __tinypool_worker_message__ && message . data ) {
397
+ workerReporterCh . publish ( message . data )
398
+ }
399
+ } )
400
+ }
390
401
391
402
addHook ( {
392
403
name : 'tinypool' ,
393
404
versions : [ '>=1.0.0' ] ,
394
405
file : 'dist/index.js'
395
406
} , ( TinyPool ) => {
396
- // we can pass handle here to the worker, and then use it to send messages to the main process
397
- shimmer . wrap ( TinyPool . prototype , 'run' , run => function ( _ , { channel } ) {
398
- const res = run . apply ( this , arguments )
399
-
400
- this . threads . forEach ( thread => {
401
- if ( processToHandler . has ( thread . process ) ) {
402
- return
403
- }
404
- processToHandler . set ( thread . process , true )
405
- thread . process . on ( 'message' , ( message ) => {
406
- if ( message . __tinypool_worker_message__ && message . data ) {
407
- workerReporterCh . publish ( message . data )
408
- }
409
- } )
410
- } )
411
-
407
+ shimmer . wrap ( TinyPool . prototype , 'run' , run => async function ( ) {
408
+ // we need to do this before and after because the threads list gets recycled
409
+ // (the processes are re-created)
410
+ this . threads . forEach ( threadHandler )
411
+ const res = await run . apply ( this , arguments )
412
+ this . threads . forEach ( threadHandler )
412
413
return res
413
414
} )
414
415
0 commit comments