Skip to content

Commit

Permalink
Cleanup/Fix _write: check _opening when we want to delay _write
Browse files Browse the repository at this point in the history
Because _opened can be false even if opening finished (in case of
error), and we don't want to delay the write in case of error.

It's also done this way for delayed _flush.

Normalize comments with delayed _flush too.
  • Loading branch information
thomas-riccardi committed Feb 24, 2015
1 parent 1ac006f commit 890b03f
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/writestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ function GridWriteStream (grid, options) {
// in the call signature, which is what we want.
this._store = new grid.mongo.GridStore(grid.db, this.id, this.name, this.mode, this.options);

this._delayedWrite = null;
this._delayedFlush = null;

var self = this;
Expand Down Expand Up @@ -102,7 +103,7 @@ GridWriteStream.prototype._open = function () {
self._flushInternal(flushed);
}

// If data was sent before store is open start writing to the store
// If _write was called during _store opening, then it was delayed until now, so do the write now
if (self._delayedWrite) {
var delayedWrite = self._delayedWrite;
self._delayedWrite = null;
Expand Down Expand Up @@ -141,13 +142,13 @@ GridWriteStream.prototype._writeInternal = function (chunk, encoding, done) {
*/

GridWriteStream.prototype._write = function (chunk, encoding, done) {
// The store is not open but data is ready to be written so delay the write until the store is opened.
if (!this._opened) {
if (this._opening) {
// if we are still opening the store, then delay the write until it is open.
this._delayedWrite = {chunk: chunk, encoding: encoding, done: done};
return;
}

// The store is open so pass all arguments to _writeInternal to be written to the gridstore
// otherwise, do the write now
this._writeInternal(chunk, encoding, done);
}

Expand Down

0 comments on commit 890b03f

Please sign in to comment.