1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/testing/xpcshell/node-spdy/lib/spdy/scheduler.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,60 @@ 1.4 +var scheduler = exports; 1.5 + 1.6 +// 1.7 +// ### function Scheduler (connection) 1.8 +// #### @connection {spdy.Connection} active connection 1.9 +// Connection's streams scheduler 1.10 +// 1.11 +function Scheduler(connection) { 1.12 + this.connection = connection; 1.13 + this.priorities = [[], [], [], [], [], [], [], []]; 1.14 + this._tickListener = null; 1.15 +} 1.16 + 1.17 +// 1.18 +// ### function create (connection) 1.19 +// #### @connection {spdy.Connection} active connection 1.20 +// 1.21 +exports.create = function create(connection) { 1.22 + return new Scheduler(connection); 1.23 +}; 1.24 + 1.25 +// 1.26 +// ### function schedule (stream, data) 1.27 +// #### @stream {spdy.Stream} Source stream 1.28 +// #### @data {Buffer} data to write on tick 1.29 +// Use stream priority to invoke callbacks in right order 1.30 +// 1.31 +Scheduler.prototype.schedule = function schedule(stream, data) { 1.32 + this.priorities[stream.priority].push(data); 1.33 +}; 1.34 + 1.35 +// 1.36 +// ### function tick () 1.37 +// Add .nextTick callback if not already present 1.38 +// 1.39 +Scheduler.prototype.tick = function tick() { 1.40 + if (this._tickListener !== null) return; 1.41 + var self = this; 1.42 + this._tickListener = function() { 1.43 + var priorities = self.priorities; 1.44 + 1.45 + self._tickListener = null; 1.46 + self.priorities = [[], [], [], [], [], [], [], []]; 1.47 + 1.48 + // Run all priorities 1.49 + for (var i = 0; i < 8; i++) { 1.50 + for (var j = 0; j < priorities[i].length; j++) { 1.51 + self.connection.write( 1.52 + priorities[i][j] 1.53 + ); 1.54 + } 1.55 + } 1.56 + }; 1.57 + 1.58 + if (this.connection.parser.drained) { 1.59 + process.nextTick(this._tickListener); 1.60 + } else { 1.61 + this.connection.parser.once('drain', this._tickListener); 1.62 + } 1.63 +};