/*** Generated by streamline 0.10.17 (callbacks) - DO NOT EDIT ***/ var __rt=require('../callbacks/runtime').runtime(__filename, false),__func=__rt.__func,__cb=__rt.__cb; (function(exports) {































  "use strict";
  var globals = require("../globals");
  var dir = ("../" + globals.runtime);
  var builtins = require((dir + "/builtins"));








  exports.each = function exports_each__1(_, array, fn, thisObj) { var __frame = { name: "exports_each__1", line: 45 }; return __func(_, this, arguments, exports_each__1, 0, __frame, function __$exports_each__1() { return (function __$exports_each__1(_) {
        var __1 = ((array && array.length)); return (function __$exports_each__1(__then) { if (__1) { return array.forEach_(__cb(_, __frame, 1, 41, _, true), fn, thisObj); } else { __then(); } ; })(function __$exports_each__1() { return _(null, undefined); }); })(__cb(_, __frame, -44, 17, _, true)); }); };




  exports.map = function exports_map__2(_, array, fn, thisObj) { var __frame = { name: "exports_map__2", line: 51 }; return __func(_, this, arguments, exports_map__2, 0, __frame, function __$exports_map__2() { return (function __$exports_map__2(_) {
        var __1 = array; return (function __$exports_map__2(__then) { if (__1) { return array.map_(__cb(_, __frame, 1, 23, _, true), fn, thisObj); } else { __then(); } ; })(function __$exports_map__2() { return _(null, array); }); })(__cb(_, __frame, -50, 17, _, true)); }); };




  exports.filter = function exports_filter__3(_, array, fn, thisObj) { var __frame = { name: "exports_filter__3", line: 57 }; return __func(_, this, arguments, exports_filter__3, 0, __frame, function __$exports_filter__3() { return (function __$exports_filter__3(_) {
        var __1 = array; return (function __$exports_filter__3(__then) { if (__1) { return array.filter_(__cb(_, __frame, 1, 23, _, true), fn, thisObj); } else { __then(); } ; })(function __$exports_filter__3() { return _(null, array); }); })(__cb(_, __frame, -56, 17, _, true)); }); };




  exports.every = function exports_every__4(_, array, fn, thisObj) { var __frame = { name: "exports_every__4", line: 63 }; return __func(_, this, arguments, exports_every__4, 0, __frame, function __$exports_every__4() { return (function __$exports_every__4(_) {
        var __1 = array; return (function __$exports_every__4(__then) { if (__1) { return array.every_(__cb(_, __frame, 1, 23, _, true), fn, thisObj); } else { __then(); } ; })(function __$exports_every__4() { return _(null, undefined); }); })(__cb(_, __frame, -62, 17, _, true)); }); };




  exports.some = function exports_some__5(_, array, fn, thisObj) { var __frame = { name: "exports_some__5", line: 69 }; return __func(_, this, arguments, exports_some__5, 0, __frame, function __$exports_some__5() { return (function __$exports_some__5(_) {
        var __1 = array; return (function __$exports_some__5(__then) { if (__1) { return array.some_(__cb(_, __frame, 1, 23, _, true), fn, thisObj); } else { __then(); } ; })(function __$exports_some__5() { return _(null, undefined); }); })(__cb(_, __frame, -68, 17, _, true)); }); };




  exports.reduce = function exports_reduce__6(_, array, fn, v, thisObj) { var __frame = { name: "exports_reduce__6", line: 75 }; return __func(_, this, arguments, exports_reduce__6, 0, __frame, function __$exports_reduce__6() { return (function __$exports_reduce__6(_) {
        var __1 = array; return (function __$exports_reduce__6(__then) { if (__1) { return array.reduce_(__cb(_, __frame, 1, 23, _, true), fn, v, thisObj); } else { __then(); } ; })(function __$exports_reduce__6() { return _(null, v); }); })(__cb(_, __frame, -74, 17, _, true)); }); };




  exports.reduceRight = function exports_reduceRight__7(_, array, fn, v, thisObj) { var __frame = { name: "exports_reduceRight__7", line: 81 }; return __func(_, this, arguments, exports_reduceRight__7, 0, __frame, function __$exports_reduceRight__7() { return (function __$exports_reduceRight__7(_) {
        var __1 = array; return (function __$exports_reduceRight__7(__then) { if (__1) { return array.reduceRight_(__cb(_, __frame, 1, 23, _, true), fn, v, thisObj); } else { __then(); } ; })(function __$exports_reduceRight__7() { return _(null, v); }); })(__cb(_, __frame, -80, 17, _, true)); }); };







  exports.sort = function exports_sort__8(_, array, compare, beg, end) { var __frame = { name: "exports_sort__8", line: 90 }; return __func(_, this, arguments, exports_sort__8, 0, __frame, function __$exports_sort__8() { return (function __$exports_sort__8(_) {
        var __1 = array; return (function __$exports_sort__8(__then) { if (__1) { return array.sort_(__cb(_, __frame, 1, 23, _, true), compare, beg, end); } else { __then(); } ; })(function __$exports_sort__8() { return _(null, array); }); })(__cb(_, __frame, -89, 17, _, true)); }); };








  exports.eachKey = function exports_eachKey__9(_, obj, fn, thisObj) { var __frame = { name: "exports_eachKey__9", line: 100 }; return __func(_, this, arguments, exports_eachKey__9, 0, __frame, function __$exports_eachKey__9() {
      return ((obj ? Object.keys(obj) : [])).forEach_(__cb(_, __frame, 1, 39, _, true), function __1(_, elt) { var __frame = { name: "__1", line: 101 }; return __func(_, this, arguments, __1, 0, __frame, function __$__1() {
          return fn.call(thisObj, __cb(_, __frame, 1, 6, function __$__1() { _(); }, true), elt, obj[elt]); }); }); }); };



  exports.spray = function(fns, max) {
    return new function() {


      var funnel = exports.funnel(max);
      this.collect = function collect__1(_, count, trim) { var __this = this; var __frame = { name: "collect__1", line: 111 }; return __func(_, this, arguments, collect__1, 0, __frame, function __$collect__1() {
















          return (function(callback) { if ((typeof (callback) != "function")) { throw new Error("invalid call to collect: no callback") }; var results = (trim ? [] : new Array(fns.length)); count = ((count < 0) ? fns.length : Math.min(count, fns.length)); if ((count == 0)) { return callback(null, results) }; var collected = 0; for (var i = 0; (i < fns.length); i++) { (function(i) { funnel(function(err, result) { if (err) { return callback(err) }; if (trim) { results.push(result); } else { results[i] = result; }; if ((++collected == count)) { return callback(null, results) }; }, fns[i]); })(i); }; }).call(__this, __cb(_, __frame, 17, 7, _, true)); }); };

      this.collectOne = function collectOne__2(_) { var result, __this = this; var __frame = { name: "collectOne__2", line: 130 }; return __func(_, this, arguments, collectOne__2, 0, __frame, function __$collectOne__2() {
          return __this.collect(__cb(_, __frame, 1, 22, function ___(__0, __1) { result = __1;
            return _(null, (result && result[0])); }, true), 1, true); }); };

      this.collectAll = function collectAll__3(_) { var __this = this; var __frame = { name: "collectAll__3", line: 134 }; return __func(_, this, arguments, collectAll__3, 0, __frame, function __$collectAll__3() {
          return __this.collect(__cb(_, __frame, 1, 16, _, true), -1, false); }); }; }; };































  exports.funnel = builtins.funnel;








  exports.handshake = function() {
    var callback = null, notified = false;
    return {
      wait: __rt.streamlinify(function(cb) {
        if (callback) { throw new Error("already waiting") };
        if (notified) { exports.setImmediate(cb); } else {
          callback = cb; };
        notified = false;
      }, 0),
      notify: function() {
        if (!callback) { notified = true; } else {
          exports.setImmediate(callback); };
        callback = null; } }; };













  exports.queue = function(options) {
    options = (options || { });
    var max = ((options.max != null) ? options.max : -1);
    var callback = null, q = [], pendingWrites = [];
    return {
      read: __rt.streamlinify(function(cb) {
        if (callback) { throw new Error("already getting") };
        if ((q.length > 0)) {
          var item = q.shift();

          if ((q.length === 0)) { q = []; };
          exports.setImmediate(function() {
            cb(null, item); });

          if ((pendingWrites.length > 0)) {
            var wr = pendingWrites.shift();
            exports.setImmediate(function() {
              wr[0](null, wr[1]); }); } ; }


         else {
          callback = cb; } ;

      }, 0),
      write: __rt.streamlinify(function(cb, item) {
        if (this.put(item)) {
          exports.setImmediate(function() {
            cb(); }); }

         else {
          pendingWrites.push([cb,item,]); } ;

      }, 0),
      put: function(item, force) {
        if (!callback) {
          if ((((max >= 0) && (q.length >= max)) && !force)) { return false };
          q.push(item); }
         else {
          var cb = callback;
          callback = null;
          exports.setImmediate(function() {
            cb(null, item); }); } ;


        return true; },

      end: function() {
        this.put(undefined, true); } }; };








  exports.collect = function exports_collect__10(_, futures) { var __frame = { name: "exports_collect__10", line: 258 }; return __func(_, this, arguments, exports_collect__10, 0, __frame, function __$exports_collect__10() { return (function __$exports_collect__10(_) {
        var __2 = futures; if (!__2) { return _(null, __2); } ; return futures.map_(__cb(_, __frame, 1, 28, _, true), function __1(_, future) { var __frame = { name: "__1", line: 259 }; return __func(_, this, arguments, __1, 0, __frame, function __$__1() {
            return future(__cb(_, __frame, 1, 10, _, true)); }); }); })(__cb(_, __frame, -257, 17, _, true)); }); };




  var globals = require("../globals");
  exports.setContext = function(ctx) {
    var old = globals.context;
    globals.context = ctx;
    return old; };

  exports.getContext = function() {
    return globals.context; };








  exports.trampoline = __rt.streamlinify(function(cb, fn, thisObj) {
    fn = globals.withContext(fn, globals.context);
    exports.setImmediate(function() {
      fn.call(thisObj, cb); });

  }, 0);




  exports.setImmediate = ((typeof setImmediate === "function") ? setImmediate : function(fn) {
    setTimeout(fn, 0); });






  var nextTick = (((typeof process === "object") && (typeof process.nextTick === "function")) ? process.nextTick : function(cb) {
    cb(); });



  exports.nextTick = function exports_nextTick__11(_) { var __frame = { name: "exports_nextTick__11", line: 304 }; return __func(_, this, arguments, exports_nextTick__11, 0, __frame, function __$exports_nextTick__11() {
      return nextTick(__cb(_, __frame, 1, 2, function __$exports_nextTick__11() { _(); }, true)); }); };




  exports.setTimeout = function(fn, millis) {



    var max = 2147483647;
    if ((millis > max)) {
      return setTimeout(function() {
        exports.setTimeout(fn, (millis - max));
      }, max); }
     else {
      return setTimeout(function() {
        fn(false);
      }, millis); } ; };




  exports.setInterval = function(fn, millis) {
    return setInterval(function() {
      fn(false);
    }, millis); };





  exports.sleep = function exports_sleep__12(_, millis) { var __frame = { name: "exports_sleep__12", line: 336 }; return __func(_, this, arguments, exports_sleep__12, 0, __frame, function __$exports_sleep__12() {
      return setTimeout(__cb(_, __frame, 1, 9, _, true), millis); }); };


  exports.eventHandler = function(fn) {
    return function() {
      var that = this;
      var args = Array.prototype.slice(arguments, 0);
      return (function __1(_) { var __frame = { name: "__1", line: 344 }; return __func(_, this, arguments, __1, 0, __frame, function __$__1() {
          return fn.apply_(__cb(_, __frame, 1, 14, _, true), that, args, 0); });
      })(function(err) {
        if (err) { throw err }; }); }; };





  exports.apply = function apply(_, fn, thisObj, args, index) { var __frame = { name: "apply", line: 353 }; return __func(_, this, arguments, apply, 0, __frame, function __$apply() {
      return fn.apply_(__cb(_, __frame, 1, 12, _, true), thisObj, args, index); }); };






  exports.callWithTimeout = __rt.streamlinify(function(cb, fn, millis) {
    var tid = setTimeout(function() {
      if (cb) {
        var ex = new Error("timeout");
        ex.code = "ETIMEOUT";
        ex.errno = "ETIMEOUT";
        cb(ex);
        cb = null; } ;

    }, millis);
    fn(function(err, result) {
      if (cb) {
        clearTimeout(tid);
        cb(err, result);
        cb = null; } ; });


  }, 0);

})(((typeof exports !== "undefined") ? exports : (Streamline.flows = (Streamline.flows || {}))));
