(function (root, factory) {
  if (typeof define === 'function' && define.amd) {
    define(['exports'], factory)
  } else if (typeof exports === 'object') {
    factory(exports)
    if (typeof module === 'object' && module !== null) {
      module.exports = exports = exports.midware
    }
  } else {
    factory(root)
  }
}(this, function (exports) {
  'use strict'

  var slice = Array.prototype.slice
  midware.VERSION = '0.1.6'

  function midware(ctx) {
    var calls = use.stack = []
    ctx = ctx || null
       
    function use() {
      var args = toArray(arguments)

      args.forEach(function (fn) {
        if (typeof fn === 'function') {
          calls.push(fn)
        }
      })

      return ctx
    }

    use.run = function run() {
      var done, args = toArray(arguments)
      
      if (typeof args[args.length - 1] === 'function') {
        done = args.pop()
      }
      
      if (!calls.length) {
        if (done) done.call(ctx)
        return
      }
      
      var stack = calls.slice()
      args.push(next)
      
      function exec() {
        var fn = stack.shift()
        try {
          fn.apply(ctx, args)
        } catch (e) {
          next(e)
        }
      }

      function next(err, end) {
        if (err || end || !stack.length) {
          stack = null
          if (done) { done.call(ctx, err, end) }
          return
        }
        exec()
      }

      exec()
    }

    use.remove = function (name) {
      for (var i = 0, l = calls.length; i < l; i += 1) {
        var fn = calls[i]
        if (fn === name || fn.name === name) {
          calls.splice(i, 1)
          break
        }
      }
    }

        
    use.flush = function () {
      calls.splice(0)
    }

    return use
  }

  function toArray(nargs) {
    var args = new Array(nargs.length)
    for (var i = 0, l = args.length; i < l; i += 1) {
      args[i] = nargs[i]
    }
    return args
  }

  exports.midware = midware
}))

@@ -365,73 +365,8 @@
 t'%0A%0A
-  var slice = Array.prototype.slice%0A  midware.VERSION = '0.1.6'%0A%0A
   fu
@@ -474,27 +474,16 @@
  %7B%0A     
- var args =
  toArray
@@ -498,27 +498,21 @@
 ts)%0A
-%0A
       
-args.forEach
+.filter
 (fun
@@ -528,28 +528,31 @@
 ) %7B%0A        
-if (
+return 
 typeof fn ==
@@ -567,36 +567,48 @@
 ion'
-) %7B
 %0A      
+%7D)%0A
     
-calls.push
+  .forEach(function 
 (fn)
+ %7B
 %0A   
@@ -604,33 +604,46 @@
  (fn) %7B%0A        
-%7D
+calls.push(fn)
 %0A      %7D)%0A%0A     
@@ -636,17 +636,16 @@
      %7D)%0A
-%0A
       re
@@ -1004,20 +1004,23 @@
 unction 
-exec
+runNext
 () %7B%0A   
@@ -1267,18 +1267,16 @@
 f (done)
- %7B
  done.ca
@@ -1292,18 +1292,16 @@
 rr, end)
- %7D
 %0A       
@@ -1297,32 +1297,32 @@
 nd)%0A        
-  return
+%7D else %7B
 %0A        %7D%0A 
@@ -1314,25 +1314,35 @@
 e %7B%0A        
-%7D
+  runNext()
 %0A        exe
@@ -1334,30 +1334,25 @@
 t()%0A        
-exec()
+%7D
 %0A      %7D%0A%0A  
@@ -1359,12 +1359,15 @@
     
-exec
+runNext
 ()%0A 
@@ -1611,17 +1611,8 @@
  %7D%0A%0A
-        %0A
     
@@ -1858,16 +1858,46 @@
 rgs%0A  %7D%0A
+  %0A  midware.VERSION = '0.1.6'
 %0A  expor

filter stack