Skip to content

Commit cea4464

Browse files
justinvdmjustinvdm
authored andcommitted
feat: Support configuring how workers are invoked
1 parent 3c16ab6 commit cea4464

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff 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+
6166
module.exports = FileProcessor;

test/index.spec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff 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
});

0 commit comments

Comments
 (0)