-
Notifications
You must be signed in to change notification settings - Fork 31
/
conduit.min.js
8 lines (8 loc) · 1.68 KB
/
conduit.min.js
1
2
3
4
5
6
7
8
/**
* conduitjs - Give any method a pre/post invocation pipeline....
* Author: Jim Cowart (http://freshbrewedcode.com/jimcowart)
* Version: v0.3.2
* Url: http://github.com/ifandelse/ConduitJS
* License: MIT
*/
(function(t,n){"object"==typeof module&&module.exports?module.exports=n():"function"==typeof define&&define.amd?define(n(t)):t.Conduit=n(t)})(this,function(){function t(t){if("function"!=typeof t.target)throw new Error("You can only make functions into Conduits.");var n={pre:t.pre||[],post:t.post||[],all:[]},e=t.context,r=t.target,o={isTarget:!0,fn:t.sync?function(){var t=Array.prototype.slice.call(arguments,0),n=r.apply(e,t);return n}:function(t){var n=Array.prototype.slice.call(arguments,1);n.splice(1,1,r.apply(e,n)),t.apply(this,n)}},a=function(){n.all=n.pre.concat([o].concat(n.post))};a();var c=function(){var r,o,a=0,c=function p(){var c,u,i=Array.prototype.slice.call(arguments,0),f=a;a+=1,f<n.all.length&&(c=n.all[f],o="target"===o?"after":c.isTarget?"target":"before",t.sync?"before"===o?(u=c.fn.apply(c.context||e,i),p.apply(this,u||i)):(r=c.fn.apply(c.context||e,i)||r,p.apply(this,[r].concat(i))):c.fn.apply(c.context||e,[p].concat(i)))};return c.apply(this,arguments),r};return c.steps=function(){return n.all},c.context=function(t){return 0===arguments.length?e:void(e=t)},c.before=function(t,e){t="function"==typeof t?{fn:t}:t,e=e||{},e.prepend?n.pre.unshift(t):n.pre.push(t),a()},c.after=function(t,e){t="function"==typeof t?{fn:t}:t,e=e||{},e.prepend?n.post.unshift(t):n.post.push(t),a()},c.clear=function(){n={pre:[],post:[],all:[]},a()},c.target=function(t){return t&&(r=t),r},c}return{Sync:function(n){return n.sync=!0,t.call(this,n)},Async:function(n){return t.call(this,n)}}});