Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Jun 21, 2020
1 parent fbe7a31 commit 6069c26
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
14 changes: 10 additions & 4 deletions lib/_stream_writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@ function nop() {}

const kFlush = Symbol('kFlush');
class WritableState extends WritableStateBase {
constructor(options, stream, isDuplex) {
constructor(options, stream) {
super(options);

let isDuplex = options.isDuplex;

// Duplex streams are both readable and writable, but share
// the same options object.
// However, some cases require setting options to different
Expand Down Expand Up @@ -207,6 +209,7 @@ function Writable(options) {

WritableBase.call(this, {
...options,
isDuplex,
start: function() {
const state = this._writableState;
if (!state.writing) {
Expand All @@ -215,7 +218,8 @@ function Writable(options) {
finishMaybe(this, state);
},
write: writeOrBuffer,
flush: function(state, cb) {
flush: function(cb) {
const state = this._writableState;
// .end() fully uncorks.
if (state.corked) {
state.corked = 1;
Expand All @@ -226,7 +230,7 @@ function Writable(options) {

finishMaybe(this, state);
}
}, isDuplex, WritableState);
}, WritableState);
}
Writable.WritableState = WritableState;
Writable.prototype = ObjectCreate(WritableBase.prototype);
Expand All @@ -249,7 +253,9 @@ Writable.prototype.uncork = function() {
// If we're already writing something, then just put this
// in the queue, and wait our turn. Otherwise, call _write
// If we return false, then we need a drain event, so set that flag.
function writeOrBuffer(state, chunk, encoding, callback) {
function writeOrBuffer(chunk, encoding, callback) {
const state = this._writableState;

if (typeof callback !== 'function')
callback = nop;

Expand Down
8 changes: 4 additions & 4 deletions lib/internal/streams/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class WritableStateBase {
const kWrite = Symbol('kWrite');
const kFlush = Symbol('kFlush');

function WritableBase(options, isDuplex, State) {
function WritableBase(options, State = WritableStateBase) {
if (typeof options.write !== 'function') {
throw new ERR_INVALID_ARG_TYPE('option.write', 'function', options.write);
}
Expand All @@ -98,7 +98,7 @@ function WritableBase(options, isDuplex, State) {
this[kWrite] = options.write;
this[kFlush] = options.flush;

this._writableState = new State(options, this, isDuplex);
this._writableState = new State(options, this);

destroyImpl.construct(this, options.start);
}
Expand Down Expand Up @@ -150,7 +150,7 @@ WritableBase.prototype.write = function(chunk, encoding, cb) {
}

// TODO(ronag): Move more logic from Writable into custom cb.
const ret = this[kWrite](state, chunk, encoding, cb);
const ret = this[kWrite](chunk, encoding, cb);
// Return false if errored or destroyed in order to break
// any synchronous while(stream.write(data)) loops.
return ret && !state.errored && !state.destroyed;
Expand Down Expand Up @@ -196,7 +196,7 @@ WritableBase.prototype.end = function(chunk, encoding, cb) {
if (!state.errored && !state.ending) {
state.ending = true;
let called = false;
this[kFlush](state, (err) => {
this[kFlush]((err) => {
if (called) {
errorOrDestroy(this, new ERR_MULTIPLE_CALLBACK());
return;
Expand Down

0 comments on commit 6069c26

Please sign in to comment.