diff -r 000000000000 -r 6474c204b198 testing/xpcshell/node-spdy/lib/spdy/scheduler.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/testing/xpcshell/node-spdy/lib/spdy/scheduler.js Wed Dec 31 06:09:35 2014 +0100 @@ -0,0 +1,60 @@ +var scheduler = exports; + +// +// ### function Scheduler (connection) +// #### @connection {spdy.Connection} active connection +// Connection's streams scheduler +// +function Scheduler(connection) { + this.connection = connection; + this.priorities = [[], [], [], [], [], [], [], []]; + this._tickListener = null; +} + +// +// ### function create (connection) +// #### @connection {spdy.Connection} active connection +// +exports.create = function create(connection) { + return new Scheduler(connection); +}; + +// +// ### function schedule (stream, data) +// #### @stream {spdy.Stream} Source stream +// #### @data {Buffer} data to write on tick +// Use stream priority to invoke callbacks in right order +// +Scheduler.prototype.schedule = function schedule(stream, data) { + this.priorities[stream.priority].push(data); +}; + +// +// ### function tick () +// Add .nextTick callback if not already present +// +Scheduler.prototype.tick = function tick() { + if (this._tickListener !== null) return; + var self = this; + this._tickListener = function() { + var priorities = self.priorities; + + self._tickListener = null; + self.priorities = [[], [], [], [], [], [], [], []]; + + // Run all priorities + for (var i = 0; i < 8; i++) { + for (var j = 0; j < priorities[i].length; j++) { + self.connection.write( + priorities[i][j] + ); + } + } + }; + + if (this.connection.parser.drained) { + process.nextTick(this._tickListener); + } else { + this.connection.parser.once('drain', this._tickListener); + } +};