File tree Expand file tree Collapse file tree 2 files changed +30
-1
lines changed Expand file tree Collapse file tree 2 files changed +30
-1
lines changed Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ class FileProcessor extends EventEmitter {
99 super ( ) ;
1010 options = options || { } ;
1111 const glob = ( this . glob = globStream ( globPattern ) ) ;
12+ this . invokeWorker = options . invokeWorker || defaultInvokeWorker ;
1213 const workers = ( this . workers = workerFarm ( options . worker || { } , worker ) ) ;
1314
1415 let allQueued = false ;
@@ -49,7 +50,7 @@ class FileProcessor extends EventEmitter {
4950 }
5051
5152 process ( path , callback ) {
52- this . workers ( path , callback ) ;
53+ this . invokeWorker ( this . workers , path , callback ) ;
5354 }
5455
5556 destroy ( callback ) {
@@ -58,4 +59,8 @@ class FileProcessor extends EventEmitter {
5859 }
5960}
6061
62+ function defaultInvokeWorker ( workers , path , callback ) {
63+ workers ( path , callback ) ;
64+ }
65+
6166module . exports = FileProcessor ;
Original file line number Diff line number Diff line change @@ -111,4 +111,28 @@ describe('success', () => {
111111 } ) ;
112112 } ) ;
113113 } ) ;
114+
115+ test ( 'invokeWorker' , ( ) => {
116+ expect . assertions ( 3 ) ;
117+
118+ const processor = new FileProcessor ( pattern , workerPath , {
119+ invokeWorker ( workers , filepath , callback ) {
120+ workers ( filepath , ( err , result ) =>
121+ err ? callback ( err ) : callback ( null , `${ result } !` )
122+ ) ;
123+ } ,
124+ } ) ;
125+
126+ const handler = jest . fn ( ) ;
127+ processor . on ( 'processed' , handler ) ;
128+
129+ return new Promise ( ( resolve ) => {
130+ processor . on ( 'end' , ( ) => {
131+ expect ( handler ) . toHaveBeenCalledWith ( examplePath ( '1.txt' ) , 'a!' ) ;
132+ expect ( handler ) . toHaveBeenCalledWith ( examplePath ( '2.txt' ) , 'b!' ) ;
133+ expect ( handler ) . toHaveBeenCalledWith ( examplePath ( '3.txt' ) , 'c!' ) ;
134+ resolve ( ) ;
135+ } ) ;
136+ } ) ;
137+ } ) ;
114138} ) ;
You can’t perform that action at this time.
0 commit comments