Skip to content

Commit 5842c57

Browse files
authored
Merge pull request #56 from mmcardle/master
Make pool job propagate progress event
2 parents 3a726ea + cfeaee5 commit 5842c57

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# threads.js - Changelog
22

3+
## Next release
4+
5+
- Job class now emits 'progress' events
6+
[#56](https://github.com/andywer/threads.js/pull/56)
7+
- Credits to https://github.com/mmcardle
8+
39
## 0.7.3
410

511
- Trigger worker error event on unhandled promise rejection in worker [#49](https://github.com/andywer/threads.js/issues/49)

src/job.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,20 @@ export default class Job extends EventEmitter {
3434
}
3535

3636
executeOn(thread) {
37+
const onProgress = (...args) => this.emit('progress', ...args);
38+
const onMessage = (...args) => {
39+
this.emit('done', ...args);
40+
thread.removeListener('progress', onProgress);
41+
};
42+
const onError = (...args) => {
43+
this.emit('error', ...args);
44+
thread.removeListener('progress', onProgress);
45+
};
46+
3747
thread
38-
.once('message', this.emit.bind(this, 'done'))
39-
.once('error', this.emit.bind(this, 'error'))
48+
.on('progress', onProgress)
49+
.once('message', onMessage)
50+
.once('error', onError)
4051
.run(...this.runArgs)
4152
.send(...this.sendArgs);
4253

test/spec/job.spec.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ function createFakeThread(response) {
2424
thread.send = function() {
2525
thread.emit('error', response.error);
2626
};
27+
} else if (response.progress) {
28+
thread.send = function() {
29+
// Emit multiple progress events
30+
response.progress.forEach((p) => {thread.emit('progress', p)});
31+
};
2732
} else {
2833
thread.send = function() {
2934
thread.emit('message', ...response.response);
@@ -80,7 +85,8 @@ describe('Job', () => {
8085
const thread = {
8186
once : noop,
8287
run : noop,
83-
send : noop
88+
send : noop,
89+
on : noop
8490
};
8591
const mock = sinon.mock(thread);
8692

@@ -92,6 +98,7 @@ describe('Job', () => {
9298

9399
mock.expects('run').once().withArgs(runnable, importScripts).returnsThis();
94100
mock.expects('send').once().withArgs(param, transferables).returnsThis();
101+
mock.expects('on').once().withArgs('progress').returnsThis();
95102

96103
job
97104
.run(runnable, importScripts)
@@ -101,6 +108,25 @@ describe('Job', () => {
101108
mock.verify();
102109
});
103110

111+
it('triggers multiple progress events', () => {
112+
const progress1 = { progress: 'progress1' };
113+
const progress2 = { progress: 'progress2' };
114+
const thread = createFakeThread({
115+
progress : [ progress1, progress2 ]
116+
});
117+
118+
const job = new Job(pool);
119+
sinon.spy(job, 'emit');
120+
121+
job
122+
.run(noop)
123+
.send()
124+
.executeOn(thread);
125+
126+
sinon.assert.calledWith(job.emit, 'progress', progress1);
127+
sinon.assert.calledWith(job.emit, 'progress', progress2);
128+
});
129+
104130
it('triggers done event', () => {
105131
const thread = createFakeThread({
106132
response : [ { foo: 'bar' }, 'more data' ]

0 commit comments

Comments
 (0)