Skip to content

Commit

Permalink
Move stream options code to shared file [refactor]
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Oct 5, 2019
1 parent 5cf77c5 commit 790e8c6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
14 changes: 3 additions & 11 deletions lib/readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
const {Readable} = require('stream');

// Imports
const {bufferFromIterator, getBytesOption} = require('./shared'),
const {bufferFromIterator, getStreamOptions, getBytesOption} = require('./shared'),
{ITERATOR, BYTES, MAX_SIZE, OVERFLOW, READING} = require('./symbols');

// Exports
Expand All @@ -27,16 +27,8 @@ const {bufferFromIterator, getBytesOption} = require('./shared'),
module.exports = class GeneratorReadStream extends Readable {
constructor(gen, options) {
// Call super constructor
const streamOptions = {};
if (!options) {
options = {};
} else {
for (const opt of ['highWaterMark', 'encoding']) {
if (options[opt] != null) streamOptions[opt] = options[opt];
}
}

super(streamOptions);
if (!options) options = {};
super(getStreamOptions(options));

// Save options
this[BYTES] = getBytesOption(options);
Expand Down
9 changes: 9 additions & 0 deletions lib/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

module.exports = {
bufferFromIterator,
getStreamOptions,
getBytesOption
};

Expand Down Expand Up @@ -76,6 +77,14 @@ function bufferFromIterator(iterator, size, bytes, overflow) {
return {buffer, overflow, ended: false};
}

function getStreamOptions(options) {
const streamOptions = {};
for (const opt of ['highWaterMark', 'encoding']) {
if (options[opt] != null) streamOptions[opt] = options[opt];
}
return streamOptions;
}

function getBytesOption(options) {
const {bytes} = options;
if (bytes == null) return 1;
Expand Down
9 changes: 2 additions & 7 deletions lib/writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
const {Writable} = require('stream');

// Imports
const {bufferFromIterator, getBytesOption} = require('./shared'),
const {bufferFromIterator, getStreamOptions, getBytesOption} = require('./shared'),
{ITERATOR, BYTES, OVERFLOW, CALLBACK, BYTES_READ, ERRORED} = require('./symbols');

// Exports
Expand All @@ -21,20 +21,15 @@ const {bufferFromIterator, getBytesOption} = require('./shared'),
module.exports = class GeneratorWriteStream extends Writable {
constructor(gen, options, cb) {
// Conform options
const streamOptions = {};
if (typeof options === 'function') {
cb = options;
options = {};
} else if (!options) {
options = {};
} else {
for (const opt of ['highWaterMark', 'emitClose']) {
if (options[opt] != null) streamOptions[opt] = options[opt];
}
}

// Call super constructor
super(streamOptions);
super(getStreamOptions(options));

// Get bytes option
this[BYTES] = getBytesOption(options);
Expand Down

0 comments on commit 790e8c6

Please sign in to comment.