marko/runtime/html/BufferedWriter.js
2016-11-02 16:48:09 -06:00

41 lines
928 B
JavaScript

'use strict';
/**
* Simple wrapper that can be used to wrap a stream
* to reduce the number of write calls. In Node.js world,
* each stream.write() becomes a chunk. We can avoid overhead
* by reducing the number of chunks by buffering the output.
*/
function BufferedWriter(wrappedStream) {
this._buffer = '';
this._wrapped = wrappedStream;
}
BufferedWriter.prototype = {
write: function(str) {
this._buffer += str;
},
flush: function() {
if (this._buffer.length !== 0) {
this._wrapped.write(this._buffer);
this._buffer = '';
if (this._wrapped.flush) {
this._wrapped.flush();
}
}
},
end: function() {
this.flush();
if (!this._wrapped.isTTY) {
this._wrapped.end();
}
},
clear: function() {
this._buffer = '';
}
};
module.exports = BufferedWriter;