/
Bacon.js
1 lines (1 loc) · 9.82 KB
/
Bacon.js
1
((function(){var a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y=this,z=Object.prototype.hasOwnProperty,A=function(a,b){function d(){this.constructor=a}for(var c in b)z.call(b,c)&&(a[c]=b[c]);return d.prototype=b.prototype,a.prototype=new d,a.__super__=b.prototype,a},B=function(a,b){return function(){return a.apply(b,arguments)}};(x=this.jQuery||this.Zepto)!=null&&(x.fn.asEventStream=function(b){var c;return c=this,new f(function(d){var e,f;return e=function(b){var c;c=d(s(b));if(c===a.noMore)return f()},f=function(){return c.unbind(b,e)},c.bind(b,e),f})}),a=this.Bacon={taste:"delicious"},a.noMore="veggies",a.more="moar bacon!",a.never=function(){return new f(function(a){return function(){return t}})},a.later=function(b,c){return a.sequentially(b,[c])},a.sequentially=function(b,c){return a.repeatedly(b,c).take(c.length)},a.repeatedly=function(b,c){var d,e;return d=-1,e=function(){return d++,s(c[d%c.length])},a.fromPoll(b,e)},a.fromPoll=function(b,c){return new f(function(d){var e,f,g;return f=void 0,e=function(){var b,e;e=c(),b=d(e);if(b===a.noMore||e.isEnd())return g()},g=function(){return clearInterval(f)},f=setInterval(e,b),g})},a.interval=function(b,c){var d;return c==null&&(c={}),d=function(){return s(c)},a.fromPoll(b,d)},a.constant=function(a){return new j(function(b){return b(q(a)),b(n())})},a.combineAll=function(a,b){var c,d,e,f,g;d=p(a),g=v(a);for(e=0,f=g.length;e<f;e++)c=g[e],d=b(d,c);return d},a.mergeAll=function(b){return a.combineAll(b,function(a,b){return a.merge(b)})},a.combineAsArray=function(b){var c,d;return d=function(a){return a!=null?a instanceof Array?a:[a]:[]},c=function(a,b){return d(a).concat(d(b))},a.combineAll(b,function(a,b){return a.toProperty().combine(b,c)})},a.latestValue=function(a){var b,c=this;return b=void 0,a.subscribe(function(a){if(a.hasValue())return b=a.value}),function(){return b}},e=function(){function a(){}return a.prototype.isEvent=function(){return!0},a.prototype.isEnd=function(){return!1},a.prototype.isInitial=function(){return!1},a.prototype.isNext=function(){return!1},a.prototype.hasValue=function(){return!1},a}(),h=function(a){function b(a){this.value=a}return A(b,a),b.prototype.isNext=function(){return!0},b.prototype.hasValue=function(){return!0},b.prototype.fmap=function(a){return s(a(this.value))},b.prototype.apply=function(a){return s(a)},b}(e),g=function(a){function b(){b.__super__.constructor.apply(this,arguments)}return A(b,a),b.prototype.isInitial=function(){return!0},b.prototype.isNext=function(){return!1},b.prototype.fmap=function(a){return q(a(this.value))},b.prototype.apply=function(a){return q(a)},b}(h),d=function(a){function b(){}return A(b,a),b.prototype.isEnd=function(){return!0},b.prototype.fmap=function(){return this},b.prototype.apply=function(){return this},b}(e),i=function(){function a(){}return a.prototype.onValue=function(a){return this.subscribe(function(b){if(b.hasValue())return a(b.value)})},a}(),f=function(b){function d(a){this.takeUntil=B(this.takeUntil,this),this["switch"]=B(this["switch"],this);var b;b=new c(a),this.subscribe=b.subscribe,this.hasSubscribers=b.hasSubscribers}return A(d,b),d.prototype.filter=function(b){return this.withHandler(function(c){return c.isEnd()||b(c.value)?this.push(c):a.more})},d.prototype.takeWhile=function(b){return this.withHandler(function(c){return c.isEnd()||b(c.value)?this.push(c):(this.push(n()),a.noMore)})},d.prototype.take=function(b){return this.withHandler(function(c){return c.isEnd()?this.push(c):b===1?(this.push(c),this.push(n()),a.noMore):(b--,this.push(c))})},d.prototype.skip=function(b){return this.withHandler(function(c){return c.isEnd()?this.push(c):b>0?(b--,a.more):this.push(c)})},d.prototype.map=function(a){return this.withHandler(function(b){return this.push(b.fmap(a))})},d.prototype.flatMap=function(b){var c;return c=this,new d(function(d){var e,f,g,h,i,j;return f=[],g=!1,j=function(){},i=function(){var a,b,c;j();for(b=0,c=f.length;b<c;b++)a=f[b],a();return f=[]},e=function(){if(g&&f.length===0)return d(n())},h=function(c){var h,j,k,l;return c.isEnd()?(g=!0,e()):(h=b(c.value),l=void 0,k=function(){return l!=null&&u(l,f),e()},j=function(b){var c;return b.isEnd()?(k(),a.noMore):(c=d(b),c===a.noMore&&i(),c)},l=h.subscribe(j),f.push(l))},j=c.subscribe(h),i})},d.prototype["switch"]=function(a){var b=this;return this.flatMap(function(c){return a(c).takeUntil(b)})},d.prototype.delay=function(b){return this.flatMap(function(c){return a.later(b,c)})},d.prototype.throttle=function(b){return this["switch"](function(c){return a.later(b,c)})},d.prototype.bufferWithTime=function(b){var c,d,e,f;return f=[],e=function(a){return f.push(a),f.length===1},d=function(){var a;return a=f,f=[],a},c=function(){return a.later(b).map(d)},this.filter(e).flatMap(c)},d.prototype.bufferWithCount=function(a){var b;return b=[],this.withHandler(function(c){var d,e=this;d=function(){return e.push(s(b)),b=[]};if(c.isEnd())return d(),this.push(c);b.push(c.value);if(b.length===a)return d()})},d.prototype.merge=function(b){var c;return c=this,new d(function(d){var e,f,g,h,i;return h=t,i=t,g=function(){return h(),i()},e=0,f=function(b){var c;return b.isEnd()?(e++,e===2?d(n()):a.more):(c=d(b),c===a.noMore&&g(),c)},h=c.subscribe(f),i=b.subscribe(f),g})},d.prototype.takeUntil=function(a){return new d(w(this,a))},d.prototype.toProperty=function(a){return this.scan(a,r)},d.prototype.scan=function(a,b){var d,e,f,g;return d=a,f=function(a){return a.isEnd()||(d=b(d,a.value)),this.push(a.apply(d))},e=new c(this.subscribe,f),g=function(a){return d!=null&&a(q(d)),e.subscribe(a)},new j(g)},d.prototype.distinctUntilChanged=function(){return this.withStateMachine(void 0,function(a,b){return b.isEnd()||a!==b.value?[b.value,[b]]:[a,[]]})},d.prototype.withStateMachine=function(b,c){var d;return d=b,this.withHandler(function(b){var e,f,g,h,i,j,k;e=c(d,b),f=e[0],h=e[1],d=f,i=a.more;for(j=0,k=h.length;j<k;j++){g=h[j],i=this.push(g);if(i===a.noMore)return i}return i})},d.prototype.decorateWith=function(a,b){return b.sampledBy(this,function(b,c){var d;return d=l(c),d[a]=b,d})},d.prototype.end=function(b){return b==null&&(b="end"),this.withHandler(function(c){return c.isEnd()?(this.push(s(b)),this.push(n()),a.noMore):a.more})},d.prototype.withHandler=function(a){return(new c(this.subscribe,a)).toEventStream()},d.prototype.toString=function(){return"EventStream"},d}(i),j=function(b){function c(b){var d,e=this;this.subscribe=b,this.toProperty=B(this.toProperty,this),this.changes=B(this.changes,this),this.takeUntil=B(this.takeUntil,this),this.filter=B(this.filter,this),this.map=B(this.map,this),this.sample=B(this.sample,this),d=function(b,d,f){var g,h;return g=void 0,h=void 0,new c(function(c){var i,j,k,l,m,o,p,q,r;return q=t,r=t,p=function(){return q(),r()},k=!1,m=!1,i=function(){if(k&&m)return c(n())},j=function(b,d,e){return function(f){var j;return f.isEnd()?(b(),i(),a.noMore):(d(f.value),g!=null&&h!=null?(j=e(c,f,g,h),j===a.noMore&&p,j):a.more)}},l=j(function(){return k=!0},function(a){return g=a},d),o=j(function(){return m=!0},function(a){return h=a},f),q=e.subscribe(l),r=b.subscribe(o),p})},this.combine=function(a,b){var c;return c=function(a,c,d,e){return a(c.apply(b(d,e)))},d(a,c,c)},this.sampledBy=function(a,b){var c;return b==null&&(b=o),c=function(a,c,d,e){return a(c.apply(b(d,e)))},d(a,t,c).changes().takeUntil(a.end())}}return A(c,b),c.prototype.sample=function(b){return this.sampledBy(a.interval(b,{}))},c.prototype.map=function(a){var b=this;return new c(function(c){return b.subscribe(function(b){return c(b.fmap(a))})})},c.prototype.filter=function(b){var d,e=this;return d=void 0,new c(function(c){return e.subscribe(function(e){if(!e.isEnd()&&!b(e.value))return e.isInitial()&&d!=null?c(q(d)):a.more;c(e);if(e.hasValue())return d=e.value})})},c.prototype.takeUntil=function(a){return new c(w(this,a))},c.prototype.changes=function(){var a=this;return new f(function(b){return a.subscribe(function(a){if(!a.isInitial())return b(a)})})},c.prototype.toProperty=function(){return this},c}(i),c=function(){function b(b,c){var d,e,f,g=this;b==null&&(b=function(){return t}),e=[],this.hasSubscribers=function(){return e.length>0},f=t,d=function(a){return u(a,e)},this.push=function(b){var c,f,h,i,j;j=k(e);for(h=0,i=j.length;h<i;h++)f=j[h],c=f(b),(c===a.noMore||b.isEnd())&&d(f);return g.hasSubscribers()?a.more:a.noMore},c==null&&(c=function(a){return this.push(a)}),this.handleEvent=function(a){return c.apply(g,[a])},this.subscribe=function(a){return e.push(a),e.length===1&&(f=b(g.handleEvent)),function(){d(a);if(!g.hasSubscribers())return f()}}}return b.prototype.toEventStream=function(){return new f(this.subscribe)},b.prototype.toString=function(){return"Dispatcher"},b}(),b=function(b){function d(){var b,e,f,g,h,i,j,k,l=this;g=void 0,k=[],f=[],e=function(b){return function(c){return c.isEnd()?(u(b,f),a.noMore):g(c)}},j=function(){var a,b,c;for(b=0,c=k.length;b<c;b++)a=k[b],a();return k=[]},h=function(a){var b,c,d;g=a,k=[];for(c=0,d=f.length;c<d;c++)b=f[c],k.push(b.subscribe(e(b)));return j},b=new c(h),i=function(a){return b.subscribe(a)},d.__super__.constructor.call(this,i),this.plug=function(a){f.push(a);if(g!=null)return k.push(a.subscribe(e(a)))},this.push=function(a){if(g!=null)return g(s(a))},this.end=function(){return j(),g(n())}}return A(d,b),d}(f),a.EventStream=f,a.Property=j,a.Bus=b,a.Initial=g,a.Next=h,a.End=d,w=function(b,c){return function(d){var e,f,g,h,i;return h=t,i=t,g=function(){return h(),i()},e=function(b){var c;return b.isEnd()&&i(),c=d(b),c===a.noMore&&i(),c},f=function(b){return b.isEnd()||(h(),d(n())),a.noMore},h=b.subscribe(e),i=c.subscribe(f),g}},t=function(){},r=function(a,b){return b},o=function(a,b){return a},q=function(a){return new g(a)},s=function(a){return new h(a)},n=function(){return new d},m=function(a){return a.length===0},p=function(a){return a[0]},v=function(a){return a.slice(1,a.length)},k=function(a){return a.slice(0)},l=function(a){var b,c,d;b={};for(c in a)d=a[c],b[c]=d;return b},u=function(a,b){var c;c=b.indexOf(a);if(c>=0)return b.splice(c,1)}})).call(this);