JezK
Edit File: page-line_reader_js.ri
U:RDoc::TopLevel[ i I";src/nodejs_supportlib/phusion_passenger/line_reader.js:ETcRDoc::Parser::Simpleo:RDoc::Markup::Document:@parts[-o:RDoc::Markup::Verbatim;[I";Phusion Passenger - https://www.phusionpassenger.com/ ;TI"2Copyright (c) 2012-2014 Phusion Holding B.V. ;TI" ;TI"I"Passenger", "Phusion Passenger" and "Union Station" are registered ;TI"(trademarks of Phusion Holding B.V. ;TI" ;TI"RPermission is hereby granted, free of charge, to any person obtaining a copy ;TI"Sof this software and associated documentation files (the "Software"), to deal ;TI"Rin the Software without restriction, including without limitation the rights ;TI"Oto use, copy, modify, merge, publish, distribute, sublicense, and/or sell ;TI"Kcopies of the Software, and to permit persons to whom the Software is ;TI">furnished to do so, subject to the following conditions: ;TI" ;TI"PThe above copyright notice and this permission notice shall be included in ;TI"9all copies or substantial portions of the Software. ;TI" ;TI"PTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR ;TI"NIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, ;TI"QFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE ;TI"LAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER ;TI"SLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, ;TI"OOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN ;TI"THE SOFTWARE. ;T:@format0o:RDoc::Markup::Paragraph;[I"/**;To; ;[I"/ Class for reading a stream line-by-line. ;TI" Usage: ;TI" ;TI"' reader = new LineReader(stream); ;TI"' reader.readLine(function(line) { ;TI" ... ;TI" // When done: ;TI" reader.close(); ;TI" }); ;TI"/ ;T; 0o;;[I""function LineReader(stream) {;To; ;[=I"var self = this; ;TI"this.stream = stream; ;TI" ;TI"this.buffer = ''; ;TI"this.lines = []; ;TI"this.callbacks = []; ;TI"this.paused = false; ;TI" ;TI"#function handleLineBuffer() { ;TI"! var line, callback; ;TI" ;TI", if (self.lineBufferIsFull()) { ;TI"% stream.pause(); ;TI") self.paused = true; ;TI" } ;TI" ;TI"f while (self.buffer != undefined && self.lines.length > 0 && self.callbacks.length > 0) { ;TI"0 line = self.lines.shift(); ;TI"8 callback = self.callbacks.shift(); ;TI"% callback(line); ;TI" } ;TI" ;TI"( self._autoPauseOrResume(); ;TI"} ;TI" ;TI"function onData(data) { ;TI" var index, line; ;TI" ;TI"- if (self.buffer == undefined) { ;TI"( // Already closed. ;TI" return; ;TI" } ;TI" ;TI"" self.buffer += data; ;TI"A while ((index = self.buffer.indexOf("\n")) != -1) { ;TI"> line = self.buffer.substr(0, index + 1); ;TI"B self.buffer = self.buffer.substr(index + 1); ;TI", self.lines.push(line); ;TI" } ;TI"! handleLineBuffer(); ;TI"} ;TI" ;TI"function onEnd() { ;TI"- if (self.buffer != undefined) { ;TI"3 self.lines.push(self.buffer); ;TI"' self.buffer = ''; ;TI"& self.eof = true; ;TI") handleLineBuffer(); ;TI" } ;TI"} ;TI" ;TI"this.onData = onData; ;TI"this.onEnd = onEnd; ;TI" stream.on('data', onData); ;TI"stream.on('end', onEnd); ;TI"stream.resume(); ;T; 0o;;[I"};To:RDoc::Markup::BlankLine o;;[I".LineReader.prototype.close = function() {;To; ;[ I"6this.stream.removeListener('data', this.onData); ;TI"4this.stream.removeListener('end', this.onEnd); ;TI"this._pause(); ;TI"this.buffer = undefined; ;TI"this.lines = undefined; ;T; 0o;;[I"};;T@so;;[I"1LineReader.prototype.isClosed = function() {;To; ;[I"'return this.buffer === undefined; ;T; 0o;;[I"};;T@so;;[I"3LineReader.prototype.endReached = function() {;To; ;[I"return this.eof; ;T; 0o;;[I"};;T@so;;[I"9LineReader.prototype.lineBufferIsFull = function() {;To; ;[I"#return this.lines.length > 0; ;T; 0o;;[I"};;T@so;;[I"9LineReader.prototype.readLine = function(callback) {;To; ;[I""if (this.lines.length > 0) { ;TI", var line = this.lines.shift(); ;TI"< if (!this.lineBufferIsFull() && this.paused) { ;TI"* this.paused = false; ;TI"+ this.stream.resume(); ;TI" } ;TI" callback(line); ;TI"( this._autoPauseOrResume(); ;TI"} else if (this.eof) { ;TI"" callback(undefined); ;TI"} else { ;TI", this.callbacks.push(callback); ;TI"} ;T; 0o;;[I"};;T@so;;[I";LineReader.prototype._autoPauseOrResume = function() {;To; ;[I"%if (this.buffer != undefined) { ;TI"8 if (this.lineBufferIsFull() || this.eof) { ;TI"$ this._pause(); ;TI" } else { ;TI"% this._resume(); ;TI" } ;TI"} ;T; 0o;;[I"};;T@so;;[I"/LineReader.prototype._pause = function() {;To; ;[ I"if (!this.paused) { ;TI"! this.paused = true; ;TI"" this.stream.pause(); ;TI"} ;T; 0o;;[I"};;T@so;;[I"0LineReader.prototype._resume = function() {;To; ;[ I"if (this.paused) { ;TI"" this.paused = false; ;TI"# this.stream.resume(); ;TI"} ;T; 0o;;[I"};;T@so;;[I"%exports.LineReader = LineReader;;T: @file@:0@omit_headings_from_table_of_contents_below0