// Generated by IcedCoffeeScript 1.7.1-c
(function() {
  var Queue, iced, __iced_k, __iced_k_noop;

  iced = require('iced-runtime').iced;
  __iced_k = __iced_k_noop = function() {};

  exports.Queue = Queue = (function() {
    function Queue() {
      this._buffers = [];
      this._n = 0;
      this._eof = false;
    }

    Queue.prototype.set_eof = function() {
      return this._eof = true;
    };

    Queue.prototype.push = function(b) {
      var c;
      this._buffers.push(b);
      this._n += b.length;
      if ((c = this._cb) != null) {
        this._cb = null;
        return c();
      }
    };

    Queue.prototype.flush = function() {
      var ret;
      ret = Buffer.concat(this._buffers);
      this._buffers = [];
      this._n = 0;
      return ret;
    };

    Queue.prototype.pop_bytes = function(n) {
      var b, l, m, n_needed, ret;
      ret = [];
      m = 0;
      while ((n_needed = n - m) > 0 && this._buffers.length) {
        b = this._buffers[0];
        l = b.length;
        if (l > n_needed) {
          l = n_needed;
          b = this._buffers[0].slice(0, l);
          this._buffers[0] = this._buffers[0].slice(l);
        } else {
          this._buffers.shift();
        }
        m += l;
        this._n -= l;
        ret.push(b);
      }
      return Buffer.concat(ret);
    };

    Queue.prototype.read = function(n, cb) {
      var b, ___iced_passed_deferral, __iced_deferrals, __iced_k;
      __iced_k = __iced_k_noop;
      ___iced_passed_deferral = iced.findDeferral(arguments);
      (function(_this) {
        return (function(__iced_k) {
          var _results, _while;
          _results = [];
          _while = function(__iced_k) {
            var _break, _continue, _next;
            _break = function() {
              return __iced_k(_results);
            };
            _continue = function() {
              return iced.trampoline(function() {
                return _while(__iced_k);
              });
            };
            _next = function(__iced_next_arg) {
              _results.push(__iced_next_arg);
              return _continue();
            };
            if (!(_this._n < n && !_this._eof)) {
              return _break();
            } else {
              (function(__iced_k) {
                __iced_deferrals = new iced.Deferrals(__iced_k, {
                  parent: ___iced_passed_deferral,
                  filename: "/Users/max/src/keybase/node-client/src/queue.iced",
                  funcname: "Queue.read"
                });
                _this._cb = __iced_deferrals.defer({
                  lineno: 63
                });
                __iced_deferrals._fulfill();
              })(_next);
            }
          };
          _while(__iced_k);
        });
      })(this)((function(_this) {
        return function() {
          b = _this.pop_bytes(n);
          return cb(b);
        };
      })(this));
    };

    return Queue;

  })();

}).call(this);
