/* automatically generated by JSCoverage - do not edit */
if (typeof _$jscoverage === 'undefined') _$jscoverage = {};
if (! _$jscoverage['plugins/Middleware.js']) {
  _$jscoverage['plugins/Middleware.js'] = [];
  _$jscoverage['plugins/Middleware.js'][1] = 0;
  _$jscoverage['plugins/Middleware.js'][7] = 0;
  _$jscoverage['plugins/Middleware.js'][49] = 0;
  _$jscoverage['plugins/Middleware.js'][50] = 0;
  _$jscoverage['plugins/Middleware.js'][71] = 0;
  _$jscoverage['plugins/Middleware.js'][72] = 0;
  _$jscoverage['plugins/Middleware.js'][73] = 0;
  _$jscoverage['plugins/Middleware.js'][74] = 0;
  _$jscoverage['plugins/Middleware.js'][75] = 0;
  _$jscoverage['plugins/Middleware.js'][76] = 0;
  _$jscoverage['plugins/Middleware.js'][78] = 0;
  _$jscoverage['plugins/Middleware.js'][79] = 0;
  _$jscoverage['plugins/Middleware.js'][82] = 0;
  _$jscoverage['plugins/Middleware.js'][83] = 0;
  _$jscoverage['plugins/Middleware.js'][84] = 0;
  _$jscoverage['plugins/Middleware.js'][87] = 0;
  _$jscoverage['plugins/Middleware.js'][89] = 0;
  _$jscoverage['plugins/Middleware.js'][91] = 0;
  _$jscoverage['plugins/Middleware.js'][114] = 0;
  _$jscoverage['plugins/Middleware.js'][115] = 0;
  _$jscoverage['plugins/Middleware.js'][116] = 0;
  _$jscoverage['plugins/Middleware.js'][118] = 0;
  _$jscoverage['plugins/Middleware.js'][139] = 0;
  _$jscoverage['plugins/Middleware.js'][141] = 0;
  _$jscoverage['plugins/Middleware.js'][142] = 0;
  _$jscoverage['plugins/Middleware.js'][144] = 0;
  _$jscoverage['plugins/Middleware.js'][169] = 0;
  _$jscoverage['plugins/Middleware.js'][170] = 0;
  _$jscoverage['plugins/Middleware.js'][171] = 0;
  _$jscoverage['plugins/Middleware.js'][172] = 0;
  _$jscoverage['plugins/Middleware.js'][174] = 0;
  _$jscoverage['plugins/Middleware.js'][195] = 0;
  _$jscoverage['plugins/Middleware.js'][196] = 0;
  _$jscoverage['plugins/Middleware.js'][197] = 0;
  _$jscoverage['plugins/Middleware.js'][198] = 0;
  _$jscoverage['plugins/Middleware.js'][200] = 0;
  _$jscoverage['plugins/Middleware.js'][206] = 0;
}
_$jscoverage['plugins/Middleware.js'][1]++;
var func = require("../base/functions"), obj = require("../base/object"), Promise = require("../promise").Promise, define = require("../define").define;
_$jscoverage['plugins/Middleware.js'][7]++;
var Middleware = define(null, {instance: {__hooks: {pre: {}, post: {}}, constructor: (function () {
  _$jscoverage['plugins/Middleware.js'][49]++;
  this.__hooks = obj.merge({}, this.__hooks);
  _$jscoverage['plugins/Middleware.js'][50]++;
  this["super"](arguments);
}), _hook: (function (state, op, args) {
  _$jscoverage['plugins/Middleware.js'][71]++;
  args = args || [];
  _$jscoverage['plugins/Middleware.js'][72]++;
  var promise = new Promise();
  _$jscoverage['plugins/Middleware.js'][73]++;
  var funcs, length;
  _$jscoverage['plugins/Middleware.js'][74]++;
  if (this.__hooks[state] && (funcs = this.__hooks[state][op]) != null && (length = funcs.length) > 0) {
    _$jscoverage['plugins/Middleware.js'][75]++;
    var count = 0;
    _$jscoverage['plugins/Middleware.js'][76]++;
    var next = func.hitch(this, (function () {
  _$jscoverage['plugins/Middleware.js'][78]++;
  if (count == length) {
    _$jscoverage['plugins/Middleware.js'][79]++;
    promise.callback();
  }
  else {
    _$jscoverage['plugins/Middleware.js'][82]++;
    var nextArgs = args.slice(0);
    _$jscoverage['plugins/Middleware.js'][83]++;
    nextArgs.unshift(next);
    _$jscoverage['plugins/Middleware.js'][84]++;
    funcs[count++].apply(this, nextArgs);
  }
}));
    _$jscoverage['plugins/Middleware.js'][87]++;
    next();
  }
  else {
    _$jscoverage['plugins/Middleware.js'][89]++;
    promise.callback();
  }
  _$jscoverage['plugins/Middleware.js'][91]++;
  return promise;
}), pre: (function (fun, callback) {
  _$jscoverage['plugins/Middleware.js'][114]++;
  var hook = this.__hooks.pre[fun];
  _$jscoverage['plugins/Middleware.js'][115]++;
  if (! hook) {
    _$jscoverage['plugins/Middleware.js'][116]++;
    hook = this.__hooks.pre[fun] = [];
  }
  _$jscoverage['plugins/Middleware.js'][118]++;
  hook.push(callback);
}), post: (function (fun, callback) {
  _$jscoverage['plugins/Middleware.js'][139]++;
  var hook = this.__hooks.post[fun];
  _$jscoverage['plugins/Middleware.js'][141]++;
  if (hook == undefined) {
    _$jscoverage['plugins/Middleware.js'][142]++;
    hook = this.__hooks.post[fun] = [];
  }
  _$jscoverage['plugins/Middleware.js'][144]++;
  hook.push(callback);
})}, static: {pre: (function (name, cb) {
  _$jscoverage['plugins/Middleware.js'][169]++;
  var hooks = this.prototype.__hooks;
  _$jscoverage['plugins/Middleware.js'][170]++;
  var hook = hooks.pre[name];
  _$jscoverage['plugins/Middleware.js'][171]++;
  if (! hook) {
    _$jscoverage['plugins/Middleware.js'][172]++;
    hook = hooks.pre[name] = [];
  }
  _$jscoverage['plugins/Middleware.js'][174]++;
  hook.push(cb);
}), post: (function (name, cb) {
  _$jscoverage['plugins/Middleware.js'][195]++;
  var hooks = this.prototype.__hooks;
  _$jscoverage['plugins/Middleware.js'][196]++;
  var hook = hooks.post[name];
  _$jscoverage['plugins/Middleware.js'][197]++;
  if (! hook) {
    _$jscoverage['plugins/Middleware.js'][198]++;
    hook = hooks.post[name] = [];
  }
  _$jscoverage['plugins/Middleware.js'][200]++;
  hook.push(cb);
})}});
_$jscoverage['plugins/Middleware.js'][206]++;
module.exports = exports = Middleware;
_$jscoverage['plugins/Middleware.js'].source = ["var func = require(\"../base/functions\"),","        obj = require(\"../base/object\"),","        Promise = require(\"../promise\").Promise,","        define = require(\"../define\").define;","","","var Middleware = define(null, {","            instance : {","                /** @lends comb.plugins.Middleware.prototype */","","","                __hooks : {pre : {}, post : {}},","","","                /**","                 * @class Plugin to enable middleware on a class","                 *","                 * @example","                 *","                 * var Mammal = define(comb.plugins.Middleware, {","                 *  instance : {","                 *","                 *    constructor: function(options) {","                 *        options = options || {};","                 *        this.super(arguments);","                 *        this._type = options.type || \"mammal\";","                 *    },","                 *","                 *    speak : function() {","                 *        var ret = new comb.Promise();","                 *        this._hook(\"pre\", \"speak\")","                 *                .then(comb.hitch(this, \"_hook\", \"post\", \"speak\"), hitch(ret, \"errback\"))","                 *                .then(comb.hitch(ret, \"callback\"), comb.hitch(ret, \"errback\"));","                 *        return ret;","                 *    }","                 *  }","                 *});","                 *","                 *  Mammal.pre('speak', function(next){","                 *     //do something meaningful","                 *     next();","                 *  });","                 *  var m = new Mammal({color : \"gold\"});","                 *  m.speak();","                 *","                 * @constructs","                 */","                constructor : function() {","                    this.__hooks = obj.merge({}, this.__hooks);","                    this.super(arguments);","                },","","                /**","                 * &lt;p&gt;Protected!&lt;/p&gt;","                 *","                 * &lt;p&gt;Call to initiate middleware for the topic&lt;/p&gt;","                 * &lt;p&gt;&lt;b&gt;NOTE:&lt;/b&gt; this function takes a variable number of arguments","                 *       whatever comes after the op param will be passed into","                 *       the listening function, with the last argument to the listenting","                 *       function being the next function&lt;/p&gt;","                 *","                 *","                 * @public","                 * @param {\"pre\"|\"post\"} state the state in which the hook should be called","                 * @param {String} op the operation that is being acted upong","                 * @param args arguments to be passed into the listening functions.","                 * @returns {comb.Promise} a promise to use after middleware chain completes","                 *","                 */","                _hook : function(state, op, args) {","                    args = args || [];","                    var promise = new Promise();","                    var funcs, length;","                    if (this.__hooks[state] &amp;&amp; (funcs = this.__hooks[state][op]) != null &amp;&amp; (length = funcs.length) &gt; 0) {","                        var count = 0;","                        var next = func.hitch(this, function() {","                            //if Ive looped through all of them callback","                            if (count == length) {","                                promise.callback();","                            } else {","                                //call next","                                var nextArgs = args.slice(0);","                                nextArgs.unshift(next);","                                funcs[count++].apply(this, nextArgs);","                            }","                        });","                        next();","                    } else {","                        promise.callback();","                    }","                    return promise;","                },","","                /**","                 * Use to listen to before an event occurred i.e. pre save","                 *","                 * &lt;b&gt;NOTE:&lt;/b&gt;&lt;/br&gt;","                 * &lt;ul&gt;","                 *     &lt;li&gt;You must call next in order for the middleware chain to complete&lt;/li&gt;","                 *      &lt;li&gt;This connects to events on the instance of an object, not all instances!&lt;/li&gt;","                 *      &lt;li&gt;Hooks are called in the order they are received!&lt;/li&gt;","                 *      &lt;li&gt; When connecting your callback will be called in the scope of the class&lt;/br&gt;if you want a certain scope use {@link comb.hitch}&lt;/li&gt;","                 *  &lt;/ul&gt;","                 *","                 * @example","                 *      instance.pre(\"save\", function(args,...., next){","                 *          //do something...","                 *          //you have to call next!!!!!","                 *          next();","                 *      });","                 *","                 * */","                pre : function(fun, callback) {","                    var hook = this.__hooks.pre[fun];","                    if (!hook) {","                        hook = this.__hooks.pre[fun] = [];","                    }","                    hook.push(callback);","                },","","                /**","                 * &lt;p&gt;Use to listen to after an event has occurred i.e. post save&lt;/p&gt;","                 * &lt;b&gt;NOTE:&lt;/b&gt;&lt;/br&gt;","                 * &lt;ul&gt;","                 *     &lt;li&gt;You must call next in order for the middleware chain to complete&lt;/li&gt;","                 *      &lt;li&gt;This connects to events on the instance of an object, NOT all instances!&lt;/li&gt;","                 *      &lt;li&gt;Hooks are called in the order they are received!&lt;/li&gt;","                 *      &lt;li&gt;When connecting your callback will be called in the scope of the class&lt;/br&gt;if you want a certain scope use {@link comb.hitch}&lt;/li&gt;","                 *  &lt;/ul&gt;","                 * @example","                 *","                 * instance.post(\"save\", function(next){","                 *                //do something...","                 *                 //you have to call next!!!!!","                 *                 next();","                 *          });","                 * */","                post : function(fun, callback) {","                    var hook = this.__hooks.post[fun];","                    //if I havent initialized it create it;","                    if (hook == undefined) {","                        hook = this.__hooks.post[fun] = [];","                    }","                    hook.push(callback);","                }","            },","","            static : {","                /** @lends comb.plugins.Middleware */","","                /**","                 *&lt;p&gt; Use to listen to after an event has occurred i.e. post save&lt;/p&gt;","                 *","                 * &lt;b&gt;NOTE:&lt;/b&gt;&lt;/br&gt;","                 * &lt;ul&gt;","                 *     &lt;li&gt;You must call next in order for the middleware chain to complete&lt;/li&gt;","                 *      &lt;li&gt;This connects to events on ALL instances of an object&lt;/li&gt;","                 *      &lt;li&gt;Hooks are called in the order they are received!&lt;/li&gt;","                 *      &lt;li&gt;When connecting your callback will be called in the scope of the class&lt;/br&gt;if you want a certain scope use {@link comb.hitch}&lt;/li&gt;","                 *  &lt;/ul&gt;","                 *","                 * @example","                 * Class.pre(\"save\", function(next){","                 *               ...","                 *               //you must call next","                 *          });","                 * */","                pre : function(name, cb) {","                    var hooks = this.prototype.__hooks;","                    var hook = hooks.pre[name];","                    if (!hook) {","                        hook = hooks.pre[name] = [];","                    }","                    hook.push(cb);","                },","","                /**","                 *&lt;p&gt;Use to listen to after an event has occurred i.e. post save&lt;/p&gt;","                 *","                 *&lt;b&gt;NOTE:&lt;/b&gt;&lt;/br&gt;","                 * &lt;ul&gt;","                 *     &lt;li&gt;You must call next in order for the middleware chain to complete&lt;/li&gt;","                 *      &lt;li&gt;This connects to events on ALL instances of an object&lt;/li&gt;","                 *      &lt;li&gt;Hooks are called in the order they are received!&lt;/li&gt;","                 *      &lt;li&gt;When connecting your callback will be called in the scope of the class&lt;/br&gt;if you want a certain scope use {@link comb.hitch}&lt;/li&gt;","                 *  &lt;/ul&gt;","                 *","                 * @example","                 * Class.post(\"save\", function(next){","                 *               ...","                 *               //you must call next","                 *          });","                 * */","                post : function(name, cb) {","                    var hooks = this.prototype.__hooks;","                    var hook = hooks.post[name];","                    if (!hook) {","                        hook = hooks.post[name] = [];","                    }","                    hook.push(cb);","                }","            }","","        });","","module.exports = exports = Middleware;"];
