sodiumjs
Version:
A Functional Reactive Programming (FRP) library for JavaScript
3 lines (2 loc) • 27.6 kB
JavaScript
!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("typescript-collections"),require("sanctuary-type-classes")):"function"==typeof define&&define.amd?define(["exports","typescript-collections","sanctuary-type-classes"],n):n(t.Sodium={},t.Collections,t.Z)}(this,function(t,f,e){"use strict";var r=function(){function e(t,n){this.rank=t,this.action=n,this.seq=e.nextSeq++}return e.prototype.toString=function(){return this.seq.toString()},e.nextSeq=0,e}(),h=function(){function i(){this.inCallback=0,this.toRegen=!1,this.prioritizedQ=new f.PriorityQueue(function(t,n){return t.rank.rank<n.rank.rank?1:t.rank.rank>n.rank.rank?-1:t.seq<n.seq?1:t.seq>n.seq?-1:0}),this.entries=new f.Set(function(t){return t.toString()}),this.sampleQ=[],this.lastQ=[],this.postQ=null}return i.prototype.requestRegen=function(){this.toRegen=!0},i.prototype.prioritized=function(t,n){var e=new r(t,n);this.prioritizedQ.enqueue(e),this.entries.add(e)},i.prototype.sample=function(t){this.sampleQ.push(t)},i.prototype.last=function(t){this.lastQ.push(t)},i._collectCyclesAtEnd=function(){i.run(function(){return i.collectCyclesAtEnd=!0})},i.prototype.post=function(t,n){for(null==this.postQ&&(this.postQ=[]);this.postQ.length<=t;)this.postQ.push(null);var e=this.postQ[t],r=null===e?n:function(){e(),n()};this.postQ[t]=r},i.prototype.checkRegen=function(){if(this.toRegen){this.toRegen=!1,this.prioritizedQ.clear();for(var t=this.entries.toArray(),n=0;n<t.length;n++)this.prioritizedQ.enqueue(t[n])}},i.prototype.isActive=function(){return!!i.currentTransaction},i.prototype.close=function(){for(;;){for(;this.checkRegen(),!this.prioritizedQ.isEmpty();){var t=this.prioritizedQ.dequeue();this.entries.remove(t),t.action()}var n=this.sampleQ;this.sampleQ=[];for(var e=0;e<n.length;e++)n[e]();if(this.prioritizedQ.isEmpty()&&this.sampleQ.length<1)break}for(e=0;e<this.lastQ.length;e++)this.lastQ[e]();if(this.lastQ=[],null!=this.postQ){for(e=0;e<this.postQ.length;e++)if(null!=this.postQ[e]){var r=i.currentTransaction;try{if(0<e){i.currentTransaction=new i;try{this.postQ[e](),i.currentTransaction.close()}catch(t){throw i.currentTransaction.close(),t}}else i.currentTransaction=null,this.postQ[e]();i.currentTransaction=r}catch(t){throw i.currentTransaction=r,t}}this.postQ=null}},i.onStart=function(t){i.onStartHooks.push(t)},i.run=function(t){var n=i.currentTransaction;if(null===n){if(!i.runningOnStartHooks){i.runningOnStartHooks=!0;try{for(var e=0;e<i.onStartHooks.length;e++)i.onStartHooks[e]()}finally{i.runningOnStartHooks=!1}}i.currentTransaction=new i}try{var r=t();return null===n&&(i.currentTransaction.close(),i.currentTransaction=null,i.collectCyclesAtEnd&&(_.collectCycles(),i.collectCyclesAtEnd=!1)),r}catch(t){throw null===n&&(i.currentTransaction.close(),i.currentTransaction=null),t}},i.currentTransaction=null,i.onStartHooks=[],i.runningOnStartHooks=!1,i.collectCyclesAtEnd=!1,i}(),i=0;var o,n,p=function(){function t(t,n){if(this.registered=!1,(this.deregister_=null)===t)throw new Error("null origin!");this.origin=t,this.register_=n}return t.prototype.register=function(n){var e=this;this.registered||(this.registered=!0,null!==this.register_?this.deregister_=this.register_():(this.origin.increment(_.NULL),n.childrn.push(this.origin),this.deregister_=function(){e.origin.decrement(_.NULL);for(var t=n.childrn.length-1;0<=t;--t)if(n.childrn[t]===e.origin){n.childrn.splice(t,1);break}}))},t.prototype.deregister=function(t){this.registered&&(this.registered=!1,null!==this.deregister_&&this.deregister_())},t}();(n=o||(o={}))[n.black=0]="black",n[n.gray=1]="gray",n[n.white=2]="white",n[n.purple=3]="purple";var u=[],s=0,_=function(){function e(t,n,e){this.targets=[],this.childrn=[],this.visited=!1,this.color=o.black,this.buffered=!1,this.refCountAdj=0,this.name=t,this.rank=n,this.sources=e,this.id=s++}return e.prototype.refCount=function(){return this.targets.length},e.prototype.register=function(t){return this.increment(t)},e.prototype.deregister=function(t){this.decrement(t),h._collectCyclesAtEnd()},e.prototype.incRefCount=function(t){var n=!1;if(0==this.refCount())for(var e=0;e<this.sources.length;e++)this.sources[e].register(this);return this.targets.push(t),t.childrn.push(this),t.ensureBiggerThan(this.rank)&&(n=!0),i++,n},e.prototype.decRefCount=function(t){for(var n=!1,e=t.childrn.length-1;0<=e;e--)if(t.childrn[e]===this){t.childrn.splice(e,1);break}for(e=0;e<this.targets.length;e++)if(this.targets[e]===t){this.targets.splice(e,1),n=!0;break}if(n){if(0==this.refCount())for(e=0;e<this.sources.length;e++)this.sources[e].deregister(this);i--}},e.prototype.addSource=function(t){this.sources.push(t),0<this.refCount()&&t.register(this)},e.prototype.ensureBiggerThan=function(t){if(this.visited)return!1;if(this.rank>t)return!1;this.visited=!0,this.rank=t+1;for(var n=0;n<this.targets.length;n++)this.targets[n].ensureBiggerThan(this.rank);return!(this.visited=!1)},e.prototype.descr=function(){var t=null;switch(this.color){case o.black:t="black";break;case o.gray:t="gray";break;case o.white:t="white";break;case o.purple:t="purple"}for(var n=this.id+" "+this.name+" ["+this.refCount()+"/"+this.refCountAdj+"] "+t+" ->",e=this.children(),r=0;r<e.length;r++)n=n+" "+e[r].id;return n},e.prototype.children=function(){return this.childrn},e.prototype.increment=function(t){return this.incRefCount(t)},e.prototype.decrement=function(t){this.decRefCount(t),0==this.refCount()?this.release():this.possibleRoots()},e.prototype.release=function(){this.color=o.black,this.buffered||this.free()},e.prototype.free=function(){for(;0<this.targets.length;)this.decRefCount(this.targets[0])},e.prototype.possibleRoots=function(){this.color!=o.purple&&(this.color=o.purple,this.buffered||(this.buffered=!0,u.push(this)))},e.collectCycles=function(){if(!e.collectingCycles)try{e.collectingCycles=!0,e.markRoots(),e.scanRoots(),e.collectRoots();for(var t=e.toBeFreedList.length-1;0<=t;--t){e.toBeFreedList.splice(t,1)[0].free()}}finally{e.collectingCycles=!1}},e.markRoots=function(){for(var t=[],n=0;n<u.length;n++)u[n].color==o.purple?(u[n].markGray(),t.push(u[n])):(u[n].buffered=!1,u[n].color==o.black&&0==u[n].refCount()&&e.toBeFreedList.push(u[n]));u=t},e.scanRoots=function(){for(var t=0;t<u.length;t++)u[t].scan()},e.collectRoots=function(){for(var t=0;t<u.length;t++)u[t].buffered=!1,u[t].collectWhite();u=[]},e.prototype.markGray=function(){if(this.color!=o.gray){this.color=o.gray;for(var t=this.children(),n=0;n<t.length;n++)t[n].refCountAdj--,t[n].markGray()}},e.prototype.scan=function(){if(this.color==o.gray)if(0<this.refCount()+this.refCountAdj)this.scanBlack();else{this.color=o.white;for(var t=this.children(),n=0;n<t.length;n++)t[n].scan()}},e.prototype.scanBlack=function(){this.refCountAdj=0,this.color=o.black;for(var t=this.children(),n=0;n<t.length;n++)t[n].color!=o.black&&t[n].scanBlack()},e.prototype.collectWhite=function(){if(this.color==o.white&&!this.buffered){0,this.color=o.black,this.refCountAdj=0;for(var t=this.children(),n=0;n<t.length;n++)t[n].collectWhite();e.toBeFreedList.push(this)}},e.NULL=new e("user",1e12,[]),e.collectingCycles=!1,e.toBeFreedList=[],e}(),a=function(t,n){this.f=t,this.deps=n};function c(t){return t instanceof a?t.deps:[]}function l(t){return t instanceof a?t.f:t}var d=function(t,n){this.f=t,this.deps=n};function v(t){return t instanceof d?t.deps:[]}function g(t){return t instanceof d?t.f:t}var y=function(t,n){this.f=t,this.deps=n};function m(t){return t instanceof y?t.deps:[]}function w(t){return t instanceof y?t.f:t}var x=function(t,n){this.f=t,this.deps=n};function T(t){return t instanceof x?t.deps:[]}function V(t){return t instanceof x?t.f:t}var k=function(t,n){this.f=t,this.deps=n};function b(t){return t instanceof k?t.deps:[]}function S(t){return t instanceof k?t.f:t}var C=function(t,n){this.f=t,this.deps=n};function L(t){return t instanceof C?t.deps:[]}function N(t){return t instanceof C?t.f:t}function z(t){for(var n=[],e=0;e<t.length;e++){var r=t[e];n.push(new p(r.getVertex__(),null))}return n}var Q=function(t,n){return(Q=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,n){t.__proto__=n}||function(t,n){for(var e in n)n.hasOwnProperty(e)&&(t[e]=n[e])})(t,n)};function R(t,n){function e(){this.constructor=t}Q(t,n),t.prototype=null===n?Object.create(n):(e.prototype=n.prototype,new e)}var A=function(){function o(t){this.f=t}return o.prototype.get=function(){return this.f()},o.prototype.map=function(t){var n=this;return new o(function(){return t(n.f())})},o.prototype.lift=function(t,n){var e=this;return new o(function(){return n(e.f(),t.f())})},o.prototype.lift3=function(t,n,e){var r=this;return new o(function(){return e(r.f(),t.f(),n.f())})},o.prototype.lift4=function(t,n,e,r){var i=this;return new o(function(){return r(i.f(),t.f(),n.f(),e.f())})},o}(),q=function(){function t(){}return t.UNIT=new t,t}(),E=function(){function r(){}return r.updates=function(t){return t.getStream__()},r.value=function(e){return h.run(function(){var t=new D;h.currentTransaction.prioritized(t.getVertex__(),function(){t.send_(q.UNIT)});var n=t.snapshot1(e);return r.updates(e).orElse(n)})},r.defer=function(t){return r.split(t.map(function(t){return[t]}))},r.split=function(e){var r=new D(null);return r.setVertex__(new _("split",0,[new p(e.getVertex__(),function(){r.getVertex__().childrn.push(e.getVertex__());var t=[];return t.push(e.listen_(_.NULL,function(n){for(var t=function(t){h.currentTransaction.post(t,function(){h.run(function(){r.send_(n[t])})})},e=0;e<n.length;e++)t(e)},!1)),t.push(function(){for(var t=r.getVertex__().childrn,n=t.length-1;0<=n;--n)if(t[n]==e.getVertex__()){t.splice(n,1);break}}),function(){t.forEach(function(t){return t()}),t.splice(0,t.length)}})])),r},r}(),U=function(t,n){this.a=t,this.b=n},M=function(t){this.hasValue=!1,this.value=null,this.cell=t},B=function(){this.f=null,this.f_present=!1,this.a=null,this.a_present=!1},O=function(){function a(t,n){var e=this;this.value=t,n?h.run(function(){return e.setStream(n)}):(this.str=new F,this.vertex=new _("ConstCell",0,[]))}return a.prototype.setStream=function(t){var n=this;this.str=t;var e=this,r=new p(t.getVertex__(),function(){return t.listen_(e.vertex,function(t){null==e.valueUpdate&&h.currentTransaction.last(function(){e.value=e.valueUpdate,e.lazyInitValue=null,e.valueUpdate=null}),e.valueUpdate=t},!1)});this.vertex=new _("Cell",0,[r]);var i=new _("Cell::setStream",1e12,[]);this.vertex.register(i),h.currentTransaction.last(function(){n.vertex.deregister(i)})},a.prototype.getVertex__=function(){return this.vertex},a.prototype.getStream__=function(){return this.str},a.prototype.sample=function(){var t=this;return h.run(function(){return t.sampleNoTrans__()})},a.prototype.sampleNoTrans__=function(){return this.value},a.prototype.sampleLazy=function(){var t=this;return h.run(function(){return t.sampleLazyNoTrans__()})},a.prototype.sampleLazyNoTrans__=function(){var t=this,n=new M(t);return h.currentTransaction.sample(function(){n.value=null!=t.valueUpdate?t.valueUpdate:t.sampleNoTrans__(),n.hasValue=!0,n.cell=null}),new A(function(){return n.hasValue?n.value:n.cell.sample()})},a.prototype.map=function(t){var n=this;return h.run(function(){return E.updates(n).map(t).holdLazy(n.sampleLazy().map(l(t)))})},a.prototype.lift=function(t,n){var e=g(n),r=this.map(function(n){return function(t){return e(n,t)}});return a.apply(r,t,z(v(n)))},a.prototype.lift3=function(t,n,e){var r=w(e),i=this.map(function(e){return function(n){return function(t){return r(e,n,t)}}});return a.apply(a.apply(i,t),n,z(m(e)))},a.prototype.lift4=function(t,n,e,r){var i=V(r),o=this.map(function(r){return function(e){return function(n){return function(t){return i(r,e,n,t)}}}});return a.apply(a.apply(a.apply(o,t),n),e,z(T(r)))},a.prototype.lift5=function(t,n,e,r,i){var o=S(i),u=this.map(function(i){return function(r){return function(e){return function(n){return function(t){return o(i,r,e,n,t)}}}}});return a.apply(a.apply(a.apply(a.apply(u,t),n),e),r,z(b(i)))},a.prototype.lift6=function(t,n,e,r,i,o){var u=N(o),s=this.map(function(o){return function(i){return function(r){return function(e){return function(n){return function(t){return u(o,i,r,e,n,t)}}}}}});return a.apply(a.apply(a.apply(a.apply(a.apply(s,t),n),e),r),i,z(L(o)))},a.prototype.tracking=function(u){var e=this,r=new D(null),s=new _("tracking",0,[new p(this.vertex,function(){var o=function(){},n=function(t){for(var n=o,r=u(t).map(function(t){return t.getVertex__()}),e=0;e<r.length;++e){var i=r[e];s.childrn.push(i),i.increment(_.NULL)}o=function(){for(var t=0;t<r.length;++t){for(var n=r[t],e=0;e<s.childrn.length;++e)if(s.childrn[e]===n){s.childrn.splice(e,1);break}n.decrement(_.NULL)}},n()};n(e.sample());var t=E.updates(e).listen_(s,function(t){n(t),r.send_(t)},!1);return function(){t(),o()}})]);return r.setVertex__(s),r.holdLazy(this.sampleLazy())},a.liftArray=function(t){return a._liftArray(t,0,t.length)},a._liftArray=function(t,n,e){if(e-n==0)return new a([]);if(e-n==1)return t[n].map(function(t){return[t]});var r=Math.floor((n+e)/2);return a._liftArray(t,n,r).lift(a._liftArray(t,r,e),function(t,n){return function(){return t.concat(n)}}).map(function(t){return t()})},a.apply=function(a,c,l){return h.run(function(){var e=!1,r=new B,i=new D,t=E.updates(a),n=E.updates(c),o=function(){e||(e=!0,h.currentTransaction.prioritized(i.getVertex__(),function(){var t=r.f_present?r.f:a.sampleNoTrans__(),n=r.a_present?r.a:c.sampleNoTrans__();i.send_(t(n)),e=!1}))},u=new p(t.getVertex__(),function(){return t.listen_(i.getVertex__(),function(t){r.f=t,r.f_present=!0,o()},!1)}),s=new p(n.getVertex__(),function(){return n.listen_(i.getVertex__(),function(t){r.a=t,r.a_present=!0,o()},!1)});return i.setVertex__(new _("apply",0,[u,s].concat(l||[]))),i.holdLazy(new A(function(){return a.sampleNoTrans__()(c.sampleNoTrans__())}))})},a.switchC=function(a){return h.run(function(){var t=a.sampleLazy().map(function(t){return t.sample()}),r=new D,i=null,n=!1,o=function(){n||(n=!0,h.currentTransaction.prioritized(r.getVertex__(),function(){r.send_(i),i=null,n=!1}))},u=null,s=E.value(a),e=new p(s.getVertex__(),function(){var e=null===u?null:E.value(u).listen_(r.getVertex__(),function(t){i=t,o()},!1),t=s.listen_(r.getVertex__(),function(t){u=t;var n=E.value(t).listen_(r.getVertex__(),function(t){i=t,o()},!1);null!==e&&e(),e=n},!1);return function(){t(),e()}});return r.setVertex__(new _("switchC",0,[e])),r.holdLazy(t)})},a.switchS=function(n){return h.run(function(){var r=new D,i=function(t){r.send_(t)},t=new p(n.getVertex__(),function(){var e=n.sampleNoTrans__().listen_(r.getVertex__(),i,!1),t=n.getStream__().listen_(r.getVertex__(),function(t){var n=t.listen_(r.getVertex__(),i,!0);e(),e=n},!1);return function(){t(),e()}});return r.setVertex__(new _("switchS",0,[t])),r})},a.prototype.calm=function(r){return E.updates(this).collectLazy(this.sampleLazy(),function(t,n){var e;return e=r(t,n)?null:t,new U(e,t)}).filterNotNull().holdLazy(this.sampleLazy())},a.prototype.calmRefEq=function(){return this.calm(function(t,n){return t===n})},a.prototype.listen=function(t){var n=this;return h.run(function(){return E.value(n).listen(t)})},a["fantasy-land/of"]=function(t){return new a(t)},a.prototype["fantasy-land/map"]=function(t){return this.map(t)},a.prototype["fantasy-land/ap"]=function(t){return a.apply(t,this)},a}(),j=function(t,n){this.h=t,this.target=n},I=function(r){function t(t,n){var e=r.call(this,null,null)||this;return h.run(function(){n&&e.setStream(n),e.lazyInitValue=t}),e}return R(t,r),t.prototype.sampleNoTrans__=function(){return null==this.value&&null!=this.lazyInitValue&&(this.value=this.lazyInitValue.get(),this.lazyInitValue=null),this.value},t}(O),H=function(){this.left=null,this.left_present=!1,this.right=null,this.right_present=!1},F=function(){function t(t){this.listeners=[],this.firings=[],this.vertex=t||new _("Stream",0,[])}return t.prototype.getVertex__=function(){return this.vertex},t.prototype.map=function(t){var n=this,e=new D(null),r=l(t);return e.vertex=new _("map",0,[new p(this.vertex,function(){return n.listen_(e.vertex,function(t){e.send_(r(t))},!1)})].concat(z(c(t)))),e},t.prototype.mapTo=function(n){var t=this,e=new D(null);return e.vertex=new _("mapTo",0,[new p(this.vertex,function(){return t.listen_(e.vertex,function(t){e.send_(n)},!1)})]),e},t.prototype.orElse=function(t){return this.merge(t,function(t,n){return t})},t.prototype.merge=function(t,n){var e=this,r=g(n),i=new H,o=!1,u=new D(null),s=function(){o||(o=!0,h.currentTransaction.prioritized(u.getVertex__(),function(){i.left_present&&i.right_present?u.send_(r(i.left,i.right)):i.left_present?u.send_(i.left):i.right_present&&u.send_(i.right),i.left=null,i.left_present=!1,i.right=null,i.right_present=!1,o=!1}))},a=new _("merge",0,[new p(this.vertex,function(){return e.listen_(u.vertex,function(t){i.left=t,i.left_present=!0,s()},!1)}),new p(t.vertex,function(){return t.listen_(u.vertex,function(t){i.right=t,i.right_present=!0,s()},!1)})].concat(z(v(n))));return u.vertex=a,u},t.prototype.filter=function(t){var n=this,e=new D(null),r=l(t);return e.vertex=new _("filter",0,[new p(this.vertex,function(){return n.listen_(e.vertex,function(t){r(t)&&e.send_(t)},!1)})].concat(z(c(t)))),e},t.prototype.filterNotNull=function(){var t=this,n=new D(null);return n.vertex=new _("filterNotNull",0,[new p(this.vertex,function(){return t.listen_(n.vertex,function(t){null!==t&&n.send_(t)},!1)})]),n},t.prototype.gate=function(t){return this.snapshot(t,function(t,n){return n?t:null}).filterNotNull()},t.prototype.snapshot1=function(n){var t=this,e=new D(null);return e.vertex=new _("snapshot1",0,[new p(this.vertex,function(){return t.listen_(e.vertex,function(t){e.send_(n.sampleNoTrans__())},!1)}),new p(n.getVertex__(),null)]),e},t.prototype.snapshot=function(n,t){var e=this,r=new D(null),i=g(t);return r.vertex=new _("snapshot",0,[new p(this.vertex,function(){return e.listen_(r.vertex,function(t){r.send_(i(t,n.sampleNoTrans__()))},!1)}),new p(n.getVertex__(),null)].concat(z(v(t)))),r},t.prototype.snapshot3=function(n,e,t){var r=this,i=new D(null),o=w(t);return i.vertex=new _("snapshot",0,[new p(this.vertex,function(){return r.listen_(i.vertex,function(t){i.send_(o(t,n.sampleNoTrans__(),e.sampleNoTrans__()))},!1)}),new p(n.getVertex__(),null),new p(e.getVertex__(),null)].concat(z(m(t)))),i},t.prototype.snapshot4=function(n,e,r,t){var i=this,o=new D(null),u=V(t);return o.vertex=new _("snapshot",0,[new p(this.vertex,function(){return i.listen_(o.vertex,function(t){o.send_(u(t,n.sampleNoTrans__(),e.sampleNoTrans__(),r.sampleNoTrans__()))},!1)}),new p(n.getVertex__(),null),new p(e.getVertex__(),null),new p(r.getVertex__(),null)].concat(z(T(t)))),o},t.prototype.snapshot5=function(n,e,r,i,t){var o=this,u=new D(null),s=S(t);return u.vertex=new _("snapshot",0,[new p(this.vertex,function(){return o.listen_(u.vertex,function(t){u.send_(s(t,n.sampleNoTrans__(),e.sampleNoTrans__(),r.sampleNoTrans__(),i.sampleNoTrans__()))},!1)}),new p(n.getVertex__(),null),new p(e.getVertex__(),null),new p(r.getVertex__(),null),new p(i.getVertex__(),null)].concat(z(b(t)))),u},t.prototype.snapshot6=function(n,e,r,i,o,t){var u=this,s=new D(null),a=N(t);return s.vertex=new _("snapshot",0,[new p(this.vertex,function(){return u.listen_(s.vertex,function(t){s.send_(a(t,n.sampleNoTrans__(),e.sampleNoTrans__(),r.sampleNoTrans__(),i.sampleNoTrans__(),o.sampleNoTrans__()))},!1)}),new p(n.getVertex__(),null),new p(e.getVertex__(),null),new p(r.getVertex__(),null),new p(i.getVertex__(),null),new p(o.getVertex__(),null)].concat(z(L(t)))),s},t.prototype.hold=function(t){return new O(t,this)},t.prototype.holdLazy=function(t){return new I(t,this)},t.prototype.collect=function(t,n){return this.collectLazy(new A(function(){return t}),n)},t.prototype.collectLazy=function(o,u){var s=this;return h.run(function(){var t=new P,n=t.holdLazy(o),e=s.snapshot(n,u),r=e.map(function(t){return t.a}),i=e.map(function(t){return t.b});return t.loop(i),r})},t.prototype.accum=function(t,n){return this.accumLazy(new A(function(){return t}),n)},t.prototype.accumLazy=function(r,i){var o=this;return h.run(function(){var t=new P,n=t.holdLazy(r),e=o.snapshot(n,i);return t.loop(e),e.holdLazy(r)})},t.prototype.once=function(){var t=this;return h.run(function(){return t.gate(t.mapTo(!1).hold(!0))})},t.prototype.listen=function(t){var n=this;return h.run(function(){return n.listen_(_.NULL,t,!1)})},t.prototype.listen_=function(e,n,t){var r=this;this.vertex.register(e)&&h.currentTransaction.requestRegen();var i=new j(n,e);if(this.listeners.push(i),!t&&0!=this.firings.length){var o=this.firings.slice();h.currentTransaction.prioritized(e,function(){for(var t=0;t<o.length;t++)n(o[t])})}return function(){for(var t=!1,n=0;n<r.listeners.length;n++)if(r.listeners[n]==i){r.listeners.splice(n,1),t=!0;break}t&&r.vertex.deregister(e)}},t.prototype["fantasy-land/map"]=function(t){return this.map(t)},t.prototype["fantasy-land/concat"]=function(t){return this.merge(t,function(t,n){return e.Semigroup.test(t)?e.concat(t,n):t})},t.prototype["fantasy-land/empty"]=function(){return new t},t}(),D=function(n){function t(t){return n.call(this,t)||this}return R(t,n),t.prototype.setVertex__=function(t){this.vertex=t},t.prototype.send_=function(e){var t=this;0==this.firings.length&&h.currentTransaction.last(function(){t.firings=[]}),this.firings.push(e);for(var r=this.listeners.slice(),n=function(t){var n=r[t].h;h.currentTransaction.prioritized(r[t].target,function(){h.currentTransaction.inCallback++;try{n(e),h.currentTransaction.inCallback--}catch(t){throw h.currentTransaction.inCallback--,t}})},i=0;i<r.length;i++)n(i)},t}(F),P=function(n){function t(){var t=n.call(this)||this;if(t.assigned__=!1,t.vertex.name="StreamLoop",null===h.currentTransaction)throw new Error("StreamLoop/CellLoop must be used within an explicit transaction");return t}return R(t,n),t.prototype.loop=function(t){var n=this;if(this.assigned__)throw new Error("StreamLoop looped more than once");this.assigned__=!0,this.vertex.addSource(new p(t.getVertex__(),function(){return t.listen_(n.vertex,function(t){n.send_(t)},!1)}))},t}(D),G=function(){function t(t,n){this.f=g(t),this.out=n,this.out.getVertex__().sources=this.out.getVertex__().sources.concat(z(v(t))),this.accumValid=!1}return t.prototype.send_=function(t){var n=this;this.accumValid?this.accum=this.f(this.accum,t):(h.currentTransaction.prioritized(this.out.getVertex__(),function(){n.out.send_(n.accum),n.accumValid=!1,n.accum=null}),this.accum=t,this.accumValid=!0)},t}(),W=function(i){function t(t){var n=i.call(this)||this;return n.disableListenCheck=!1,t||(t=function(t,n){throw new Error("send() called more than once per transaction, which isn't allowed. Did you want to combine the events? Then pass a combining function to your StreamSink constructor.")}),n.coalescer=new G(t,n),n}return R(t,i),t.prototype.send=function(t){var n=this;h.run(function(){if(!n.disableListenCheck&&0==n.vertex.refCount())throw new Error("send() was invoked before listeners were registered");if(0<h.currentTransaction.inCallback)throw new Error("You are not allowed to use send() inside a Sodium callback");n.coalescer.send_(t)})},t.prototype.listen_=function(t,n,e){var r=i.prototype.listen_.call(this,t,n,e);return this.disableListenCheck=!0,r},t}(D),Y=function(t){function n(){return t.call(this,null,new P)||this}return R(n,t),n.prototype.loop=function(t){var n=this;h.run(function(){n.getStream__().loop(t.getStream__()),n.lazyInitValue=t.sampleLazy()})},n.prototype.sampleNoTrans__=function(){if(!this.getStream__().assigned__)throw new Error("CellLoop sampled before it was looped");return t.prototype.sampleNoTrans__.call(this)},n}(I),Z=function(e){function t(t,n){return e.call(this,t,new W(n))||this}return R(t,e),t.prototype.send=function(t){this.getStream__().send(t)},t}(O),J=function(){function t(t,a,c){var l=this;this._inStream=t,this._table=new f.Dictionary(c),this._vertex=new _("Router",this._inStream.getVertex__().rank+1,[]),this._vertex.addSource(new p(this._inStream.getVertex__(),function(){return l._inStream.listen_(l._vertex,function(t){for(var n=a(t),e=new f.Set(c),r=[],i=0;i<n.length;++i){var o=n[i];if(!e.contains(o)){e.add(o);var u=l._table.getValue(o);if(null!=u)for(var s=0;s<u.length;++s)r.push(u[s])}}for(i=0;i<r.length;++i)r[i].send_(t)},!0)}))}return t.prototype.filterMatches=function(e){var r=this,i=new D,t=new _("Router::filterMatches",this._vertex.rank+1,[new p(this._vertex,function(){r._vertex.increment(i.getVertex__());var t=r._table.getValue(e);return null==t&&(t=[],r._table.setValue(e,t)),t.push(i),function(){r._vertex.decrement(i.getVertex__());for(var t=r._table.getValue(e),n=t.length-1;0<=n;--n)if(t[n]==i){t.splice(n,1);break}0==t.length&&r._table.remove(e)}})]);return i.setVertex__(t),i},t}(),K=function(){},X=0,$=function(t,n){this.t=t,this.sAlarm=n,this.seq=++X},tt=function(){function t(n){var i=this;this.eventQueue=new f.BSTree(function(t,n){return t.t<n.t?-1:t.t>n.t?1:t.seq<n.seq?-1:t.seq>n.seq?1:0}),h.run(function(){i.impl=n,i.tMinimum=0;var r=new Z(n.now());i.time=r,i.time.listen(function(t){}),h.onStart(function(){for(var e=i.tMinimum=Math.max(i.tMinimum,n.now()),t=function(){var t=null;if(!i.eventQueue.isEmpty()){var n=i.eventQueue.minimum();n.t<=e&&(t=n)}if(null==t)return"break";r.send(t.t),h.run(function(){return t.sAlarm.send_(t.t)})};;){if("break"===t())break}r.send(e)})})}return t.prototype.at=function(n){var t=this,e=null,r=null,i=!1,o=null,u=!1,s=new D(null),a=function(){null!==r&&(r(),t.eventQueue.remove(e)),e=r=null,i&&(u||(u=!0,o=n.sampleNoTrans__()),null!==o&&(e=new $(o,s),t.eventQueue.add(e),r=t.impl.setTimer(o,function(){t.tMinimum=Math.max(t.tMinimum,o),h.run(function(){})})))};return s.setVertex__(new _("at",0,[new p(n.getVertex__(),function(){u=!(i=!0),h.currentTransaction.prioritized(s.getVertex__(),a);var t=n.getStream__().listen_(s.getVertex__(),function(t){o=t,u=!0,a()},!1);return function(){i=!1,a(),t()}})])),s},t}(),nt=function(t){function n(){return t.call(this,new et)||this}return R(n,t),n}(tt),et=function(t){function n(){return null!==t&&t.apply(this,arguments)||this}return R(n,t),n.prototype.setTimer=function(t,n){var e=setTimeout(n,Math.max(1e3*(t-this.now()),0));return function(){clearTimeout(e)}},n.prototype.now=function(){return.001*Date.now()},n}(K),rt=function(t){function n(){return t.call(this,new it)||this}return R(n,t),n}(tt),it=function(t){function n(){return null!==t&&t.apply(this,arguments)||this}return R(n,t),n.prototype.setTimer=function(t,n){var e=setTimeout(n,Math.max(t-this.now(),0));return function(){clearTimeout(e)}},n.prototype.now=function(){return Date.now()},n}(K),ot=function(){function t(){}return t.fromAsync=function(e){return function(t){var n=new D(null);return n.setVertex__(new _("map",0,[new p(t.getVertex__(),function(){return t.listen_(n.getVertex__(),function(t){e(t,function(t){h.run(function(){n.send_(t)})})},!1)})])),n}},t}();t.lambda1=function(t,n){return new a(t,n)},t.lambda2=function(t,n){return new d(t,n)},t.lambda3=function(t,n){return new y(t,n)},t.lambda4=function(t,n){return new x(t,n)},t.lambda5=function(t,n){return new k(t,n)},t.lambda6=function(t,n){return new C(t,n)},t.Stream=F,t.StreamLoop=P,t.StreamSink=W,t.Cell=O,t.CellLoop=Y,t.CellSink=Z,t.Router=J,t.Transaction=h,t.Tuple2=U,t.Unit=q,t.Operational=E,t.getTotalRegistrations=function(){return i},t.Vertex=_,t.TimerSystemImpl=K,t.TimerSystem=tt,t.SecondsTimerSystem=nt,t.MillisecondsTimerSystem=rt,t.IOAction=ot,Object.defineProperty(t,"__esModule",{value:!0})});
//# sourceMappingURL=sodium.umd.min.js.map