thread-stream
Advanced tools
Comparing version 0.6.0 to 0.7.0
41
index.js
@@ -109,2 +109,3 @@ 'use strict' | ||
this.needDrain = false | ||
this.closed = false | ||
@@ -116,4 +117,15 @@ this.buf = '' | ||
case 'READY': | ||
this.ready = true | ||
this.emit('ready') | ||
if (this._sync) { | ||
this.ready = true | ||
this.flushSync() | ||
this.emit('ready') | ||
} else { | ||
this.once('drain', function () { | ||
this.flush(() => { | ||
this.ready = true | ||
this.emit('ready') | ||
}) | ||
}) | ||
nextFlush(this) | ||
} | ||
break | ||
@@ -128,5 +140,10 @@ case 'FINISH': | ||
this.worker.on('exit', () => { | ||
this.worker.on('exit', (code) => { | ||
this.closed = true | ||
setImmediate(() => { | ||
this.emit('close') | ||
if (code === 0) { | ||
this.emit('close') | ||
} else { | ||
this.emit('error', new Error('The worker thread exited')) | ||
} | ||
}) | ||
@@ -153,2 +170,6 @@ }) | ||
write (data) { | ||
if (this.closed) { | ||
throw new Error('the worker has exited') | ||
} | ||
if (!this.ready || this.flushing) { | ||
@@ -174,2 +195,6 @@ this.buf += data | ||
end () { | ||
if (this.closed) { | ||
throw new Error('the worker has exited') | ||
} | ||
if (!this.ready) { | ||
@@ -200,2 +225,6 @@ this.once('ready', this.end.bind(this)) | ||
flush (cb) { | ||
if (this.closed) { | ||
throw new Error('the worker has exited') | ||
} | ||
// TODO write all .buf | ||
@@ -269,2 +298,6 @@ const writeIndex = Atomics.load(this._state, WRITE_INDEX) | ||
flushSync () { | ||
if (this.closed) { | ||
throw new Error('the worker has exited') | ||
} | ||
this._writeSync() | ||
@@ -271,0 +304,0 @@ this._flushSync() |
{ | ||
"name": "thread-stream", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"description": "A streaming way to send data to a Node.js Worker Thread", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -241,1 +241,45 @@ 'use strict' | ||
}) | ||
test('flushSync on ready if sync=true', function (t) { | ||
t.plan(4) | ||
const dest = file() | ||
const stream = new ThreadStream({ | ||
filename: join(__dirname, 'to-file.js'), | ||
workerData: { dest }, | ||
sync: true | ||
}) | ||
stream.on('ready', () => { | ||
readFile(dest, 'utf8', (err, data) => { | ||
t.error(err) | ||
t.equal(data, 'hello world\nsomething else\n') | ||
stream.end() | ||
}) | ||
}) | ||
t.ok(stream.write('hello world\n')) | ||
t.ok(stream.write('something else\n')) | ||
}) | ||
test('flush on ready if sync=false', function (t) { | ||
t.plan(4) | ||
const dest = file() | ||
const stream = new ThreadStream({ | ||
filename: join(__dirname, 'to-file.js'), | ||
workerData: { dest }, | ||
sync: false | ||
}) | ||
stream.on('ready', () => { | ||
readFile(dest, 'utf8', (err, data) => { | ||
t.error(err) | ||
t.equal(data, 'hello world\nsomething else\n') | ||
stream.end() | ||
}) | ||
}) | ||
t.ok(stream.write('hello world\n')) | ||
t.ok(stream.write('something else\n')) | ||
}) |
29294
21
975