Sha256: 731231ee8c5db50e458b251fb397019352ab818b6c55237ac21b6edfc7248959

Contents?: true

Size: 1.48 KB

Versions: 25

Compression:

Stored size: 1.48 KB

Contents

'use strict';

var RingBuffer = require('./ring_buffer');

var Functor = function(session, method) {
  this._session = session;
  this._method  = method;
  this._queue   = new RingBuffer(Functor.QUEUE_SIZE);
  this._stopped = false;
  this.pending  = 0;
};

Functor.QUEUE_SIZE = 8;

Functor.prototype.call = function(error, message, callback, context) {
  if (this._stopped) return;

  var record = {error: error, message: message, callback: callback, context: context, done: false},
      called = false,
      self   = this;

  this._queue.push(record);

  if (record.error) {
    record.done = true;
    this._stop();
    return this._flushQueue();
  }

  var handler = function(err, msg) {
    if (!(called ^ (called = true))) return;

    if (err) {
      self._stop();
      record.error   = err;
      record.message = null;
    } else {
      record.message = msg;
    }

    record.done = true;
    self._flushQueue();
  };

  try {
    this._session[this._method](message, handler);
  } catch (err) {
    handler(err);
  }
};

Functor.prototype._stop = function() {
  this.pending  = this._queue.length;
  this._stopped = true;
};

Functor.prototype._flushQueue = function() {
  var queue = this._queue, record;

  while (queue.length > 0 && queue.peek().done) {
    record = queue.shift();
    if (record.error) {
      this.pending = 0;
      queue.clear();
    } else {
      this.pending -= 1;
    }
    record.callback.call(record.context, record.error, record.message);
  }
};

module.exports = Functor;

Version data entries

25 entries across 25 versions & 9 rubygems

Version Path
disco_app-0.18.0 test/dummy/node_modules/websocket-extensions/lib/pipeline/functor.js
disco_app-0.18.2 test/dummy/node_modules/websocket-extensions/lib/pipeline/functor.js
disco_app-0.16.1 test/dummy/node_modules/websocket-extensions/lib/pipeline/functor.js
disco_app-0.15.2 test/dummy/node_modules/websocket-extensions/lib/pipeline/functor.js
disco_app-0.18.4 test/dummy/node_modules/websocket-extensions/lib/pipeline/functor.js
disco_app-0.18.1 test/dummy/node_modules/websocket-extensions/lib/pipeline/functor.js
disco_app-0.12.7.pre.puma.pre.3 test/dummy/node_modules/websocket-extensions/lib/pipeline/functor.js
disco_app-0.14.0 test/dummy/node_modules/websocket-extensions/lib/pipeline/functor.js
disco_app-0.13.6.pre.puma.pre.3 test/dummy/node_modules/websocket-extensions/lib/pipeline/functor.js
enju_library-0.3.8 spec/dummy/node_modules/websocket-extensions/lib/pipeline/functor.js
ilog-0.4.1 node_modules/websocket-extensions/lib/pipeline/functor.js
ilog-0.4.0 node_modules/websocket-extensions/lib/pipeline/functor.js
ilog-0.3.3 node_modules/websocket-extensions/lib/pipeline/functor.js
jester-data-8.0.0 node_modules/websocket-extensions/lib/pipeline/functor.js
ezii-os-5.2.1 node_modules/websocket-extensions/lib/pipeline/functor.js
ezii-os-2.0.1 node_modules/websocket-extensions/lib/pipeline/functor.js
ezii-os-1.1.0 node_modules/websocket-extensions/lib/pipeline/functor.js
ezii-os-1.0.0 node_modules/websocket-extensions/lib/pipeline/functor.js
ezii-os-0.0.0.1.0 node_modules/websocket-extensions/lib/pipeline/functor.js
ezii-os-0.0.0.0.1 node_modules/websocket-extensions/lib/pipeline/functor.js