UNPKG

baconjs

Version:

A small functional reactive programming lib for JavaScript.

1 lines 48.5 kB
function nop(){}const isArray=Array.isArray||function(t){return t instanceof Array};function isObservable(t){return t&&t._isObservable}function all(t,e){for(var n=0;n<t.length;n++)if(!e(t[n]))return!1;return!0}function always(t){return()=>t}function any(t,e){for(var n=0;n<t.length;n++)if(e(t[n]))return!0;return!1}function bind(t,e){return function(){return t.apply(e,arguments)}}function contains(t,e){return-1!==indexOf(t,e)}function each(t,e){for(var n in t){if(Object.prototype.hasOwnProperty.call(t,n))e(n,t[n])}}function empty(t){return 0===t.length}function filter(t,e){for(var n,r=[],i=0;i<e.length;i++)t(n=e[i])&&r.push(n);return r}function flatMap(t,e){return fold(e,[],(function(e,n){return e.concat(t(n))}))}function flip(t){return(e,n)=>t(n,e)}function fold(t,e,n){for(var r=0;r<t.length;r++)e=n(e,t[r]);return e}function head(t){return t[0]}function id(t){return t}function indexOfDefault(t,e){return t.indexOf(e)}function indexOfFallback(t,e){for(var n=0;n<t.length;n++)if(e===t[n])return n;return-1}const indexOf=Array.prototype.indexOf?indexOfDefault:indexOfFallback;function indexWhere(t,e){for(var n=0;n<t.length;n++)if(e(t[n]))return n;return-1}function isFunction(t){return"function"==typeof t}function last(t){return t[t.length-1]}function map(t,e){for(var n,r=[],i=0;i<e.length;i++)n=e[i],r.push(t(n));return r}function negate(t){return function(e){return!t(e)}}function remove(t,e){var n=indexOf(e,t);if(n>=0)return e.splice(n,1)}function tail(t){return t.slice(1,t.length)}function toArray(t){return isArray(t)?t:[t]}function toFunction(t){return"function"==typeof t?t:e=>t}function toString(t){var e={}.hasOwnProperty;try{if(recursionDepth++,null==t)return"undefined";if(isFunction(t))return"function";if(isArray(t))return recursionDepth>5?"[..]":"["+map(toString,t).toString()+"]";if(null!=(null!=t?t.toString:void 0)&&t.toString!==Object.prototype.toString)return t.toString();if("object"==typeof t){if(recursionDepth>5)return"{..}";var n=[];for(var r in t){if(!e.call(t,r))continue;let i=function(){try{return t[r]}catch(t){return t}}();n.push(toString(r)+":"+toString(i))}return"{"+n+"}"}return t}finally{recursionDepth--}}function without(t,e){return filter((function(e){return e!==t}),e)}var _={indexOf:indexOf,indexWhere:indexWhere,head:head,always:always,negate:negate,empty:empty,tail:tail,filter:filter,map:map,each:each,toArray:toArray,contains:contains,id:id,last:last,all:all,any:any,without:without,remove:remove,fold:fold,flatMap:flatMap,bind:bind,isFunction:isFunction,toFunction:toFunction,toString:toString},recursionDepth=0;const more=void 0,noMore="<no-more>";function assert(t,e){if(!e)throw new Error(t)}function assertEventStream(t){if(!(null!=t?t._isEventStream:void 0))throw new Error("not an EventStream : "+t)}function assertObservable(t){if(!(null!=t?t._isObservable:void 0))throw new Error("not an Observable : "+t)}function assertFunction(t){return assert("not a function : "+t,_.isFunction(t))}function assertArray(t){if(!isArray(t))throw new Error("not an array : "+t)}function assertNoArguments(t){return assert("no arguments supported",0===t.length)}const defaultScheduler={setTimeout:(t,e)=>setTimeout(t,e),setInterval:(t,e)=>setInterval(t,e),clearInterval:t=>clearInterval(t),clearTimeout:t=>clearTimeout(t),now:()=>(new Date).getTime()},GlobalScheduler={scheduler:defaultScheduler};function getScheduler(){return GlobalScheduler.scheduler}function setScheduler(t){GlobalScheduler.scheduler=t}var rootEvent=void 0,waiterObs=[],waiters={},aftersStack=[],aftersStackHeight=0,flushed={},processingAfters=!1;function toString$1(){return _.toString({rootEvent:rootEvent,processingAfters:processingAfters,waiterObs:waiterObs,waiters:waiters,aftersStack:aftersStack,aftersStackHeight:aftersStackHeight,flushed:flushed})}function ensureStackHeight(t){t<=aftersStackHeight||(aftersStack[t-1]||(aftersStack[t-1]=[[],0]),aftersStackHeight=t)}function isInTransaction(){return void 0!==rootEvent}function soonButNotYet(t,e){rootEvent?whenDoneWith(t,e):GlobalScheduler.scheduler.setTimeout(e,0)}function afterTransaction(t,e){if(!rootEvent&&!processingAfters)return e();ensureStackHeight(1);for(var n=0;n<aftersStackHeight-1&&!containsObs(t,aftersStack[n][0]);)n++;aftersStack[n][0].push([t,e]),rootEvent||processAfters()}function containsObs(t,e){for(var n=0;n<e.length;n++)if(e[n][0].id==t.id)return!0;return!1}function processAfters(){let t=aftersStackHeight;if(!t)return;let e=!processingAfters;processingAfters=!0;try{for(;aftersStackHeight>=t;){var n=aftersStack[aftersStackHeight-1];if(!n)throw new Error("Unexpected stack top: "+n);var[r,i]=n;if(!(i<r.length)){n[0]=[],n[1]=0;break}var[,s]=r[i];n[1]++,ensureStackHeight(aftersStackHeight+1);var o=!1;try{for(s(),o=!0;aftersStackHeight>t&&0==aftersStack[aftersStackHeight-1][0].length;)aftersStackHeight--}finally{o||(aftersStack=[],aftersStackHeight=0)}}}finally{e&&(processingAfters=!1)}}function whenDoneWith(t,e){if(rootEvent){var n=waiters[t.id];return void 0===n?(n=waiters[t.id]=[e],waiterObs.push(t)):n.push(e)}return e()}function flush(){for(;waiterObs.length>0;)flushWaiters(0,!0);flushed={}}function flushWaiters(t,e){var n=waiterObs[t],r=n.id,i=waiters[r];waiterObs.splice(t,1),delete waiters[r],e&&waiterObs.length>0&&flushDepsOf(n);for(var s=0;s<i.length;s++)(0,i[s])()}function flushDepsOf(t){if(!flushed[t.id]){for(var e,n=t.internalDeps(),r=0;r<n.length;r++){if(flushDepsOf(e=n[r]),waiters[e.id])flushWaiters(_.indexOf(waiterObs,e),!1)}flushed[t.id]=!0}}function inTransaction(t,e,n,r){if(rootEvent)return n.apply(e,r);rootEvent=t;try{var i=n.apply(e,r);flush()}finally{rootEvent=void 0,processAfters()}return i}function currentEventId(){return rootEvent?rootEvent.id:void 0}function wrappedSubscribe(t,e,n){assertFunction(n);let r=!1,i=!1,s=()=>{i=!0},o=()=>{r=!0,s()};return s=e((function(e){return afterTransaction(t,(function(){r||n(e)===noMore&&o()})),more})),i&&s(),o}function hasWaiters(){return waiterObs.length>0}var UpdateBarrier={toString:toString$1,whenDoneWith:whenDoneWith,hasWaiters:hasWaiters,inTransaction:inTransaction,currentEventId:currentEventId,wrappedSubscribe:wrappedSubscribe,afterTransaction:afterTransaction,soonButNotYet:soonButNotYet,isInTransaction:isInTransaction};class Desc{constructor(t,e,n=[]){this._isDesc=!0,this.context=t,this.method=e,this.args=n}deps(){return this.cachedDeps||(this.cachedDeps=findDeps([this.context].concat(this.args))),this.cachedDeps}toString(){let t=_.map(_.toString,this.args);return _.toString(this.context)+"."+_.toString(this.method)+"("+t+")"}}function describe(t,e,...n){const r=t||e;return r&&r._isDesc?t||e:new Desc(t,e,n)}function findDeps(t){return isArray(t)?_.flatMap(findDeps,t):isObservable(t)?[t]:(null!=t?t._isSource:void 0)?[t.obs]:[]}const nullSink=()=>more,nullVoidSink=()=>more;function withStateMachine(t,e,n){return n.transform(withStateMachineT(t,e),new Desc(n,"withStateMachine",[t,e]))}function withStateMachineT(t,e){let n=t;return(t,r)=>{var i=e(n,t),[s,o]=i;n=s;for(var u=more,a=0;a<o.length;a++){if((u=r(o[a]))===noMore)return u}return u}}class Some{constructor(t){this._isSome=!0,this.isDefined=!0,this.value=t}getOrElse(t){return this.value}get(){return this.value}filter(t){return t(this.value)?new Some(this.value):None}map(t){return new Some(t(this.value))}forEach(t){t(this.value)}toArray(){return[this.value]}inspect(){return"Some("+this.value+")"}toString(){return this.inspect()}}const None={_isNone:!0,getOrElse:t=>t,get(){throw new Error("None.get()")},filter:()=>None,map:()=>None,forEach(){},isDefined:!1,toArray:()=>[],inspect:()=>"None",toString(){return this.inspect()}};function none(){return None}function toOption(t){return t&&(t._isSome||t._isNone)?t:new Some(t)}function isNone(t){return null!=t&&t._isNone}var eventIdCounter=0;class Event{constructor(){this.id=++eventIdCounter,this.isEvent=!0,this._isEvent=!0,this.isEnd=!1,this.isInitial=!1,this.isNext=!1,this.isError=!1,this.hasValue=!1}filter(t){return!0}inspect(){return this.toString()}log(){return this.toString()}toNext(){return this}}class Value extends Event{constructor(t){if(super(),this.hasValue=!0,t instanceof Event)throw new Error$1("Wrapping an event inside other event");this.value=t}fmap(t){return this.apply(t(this.value))}filter(t){return t(this.value)}toString(){return _.toString(this.value)}log(){return this.value}}class Next extends Value{constructor(t){super(t),this.isNext=!0,this._isNext=!0}apply(t){return new Next(t)}}class Initial extends Value{constructor(t){super(t),this.isInitial=!0,this._isInitial=!0}apply(t){return new Initial(t)}toNext(){return new Next(this.value)}}class NoValue extends Event{constructor(){super(...arguments),this.hasValue=!1}fmap(t){return this}}class End extends NoValue{constructor(){super(...arguments),this.isEnd=!0}toString(){return"<end>"}}class Error$1 extends NoValue{constructor(t){super(),this.isError=!0,this.error=t}toString(){return"<error> "+_.toString(this.error)}}function initialEvent(t){return new Initial(t)}function nextEvent(t){return new Next(t)}function endEvent(){return new End}function toEvent(t){return t&&t._isEvent?t:nextEvent(t)}function isEvent(t){return t&&t._isEvent}function isInitial(t){return t&&t._isInitial}function isError(t){return t.isError}function hasValue(t){return t.hasValue}function isEnd(t){return t.isEnd}function isNext(t){return t.isNext}function equals(t,e){return t===e}function skipDuplicates(t,e=equals){let n=new Desc(t,"skipDuplicates",[]);return withStateMachine(none(),(function(t,n){return hasValue(n)?n.isInitial||isNone(t)||!e(t.get(),n.value)?[new Some(n.value),[n]]:[t,[]]:[t,[n]]}),t).withDesc(n)}function take(t,e,n){return e.transform(takeT(t),n||new Desc(e,"take",[t]))}function takeT(t){return(e,n)=>e.hasValue?--t>0?n(e):(0===t&&n(e),n(endEvent()),noMore):n(e)}function log(t,e){e.subscribe((function(e){return"undefined"!=typeof console&&"function"==typeof console.log&&console.log(...t.concat([e.log()])),more}))}function doLogT(t){return(e,n)=>("undefined"!=typeof console&&null!==console&&"function"==typeof console.log&&console.log(...t.concat([e.log()])),n(e))}function doErrorT(t){return(e,n)=>(isError(e)&&t(e.error),n(e))}function doActionT(t){return(e,n)=>(hasValue(e)&&t(e.value),n(e))}function doEndT(t){return(e,n)=>(isEnd(e)&&t(),n(e))}function scan(t,e,n){let r,i=e,s=!1;return r=new Property(new Desc(t,"scan",[e,n]),e=>{var o=!1,u=nop,a=more;const c=function(){return o||(o=s=!0,(a=e(new Initial(i)))===noMore&&(u(),u=nop)),a};return u=t.subscribeInternal((function(t){if(hasValue(t)){if(s&&t.isInitial)return more;t.isInitial||c(),o=s=!0;var r=n(i,t.value);return i=r,e(t.apply(r))}return t.isEnd&&(a=c()),a!==noMore?e(t):a})),UpdateBarrier.whenDoneWith(r,c),u})}function mapEndT(t){let e=_.toFunction(t);return function(t,n){return isEnd(t)?(n(nextEvent(e(t))),n(endEvent()),noMore):n(t)}}function mapErrorT(t){let e=_.toFunction(t);return function(t,n){return isError(t)?n(nextEvent(e(t.error))):n(t)}}function skipErrors(t){return t.transform((function(t,e){return isError(t)?more:e(t)}),new Desc(t,"skipErrors",[]))}function last$1(t){var e;return t.transform((function(t,n){return isEnd(t)?(e&&n(e),n(endEvent()),noMore):hasValue(t)?(e=t,more):n(t)})).withDesc(new Desc(t,"last",[]))}class CompositeUnsubscribe{constructor(t=[]){this.unsubscribed=!1,this.unsubscribe=_.bind(this.unsubscribe,this),this.unsubscribed=!1,this.subscriptions=[],this.starting=[];for(var e,n=0;n<t.length;n++)e=t[n],this.add(e)}add(t){if(!this.unsubscribed){var e=!1,n=nop;this.starting.push(t);n=t(this.unsubscribe,()=>{this.unsubscribed||(e=!0,this.remove(n),_.remove(t,this.starting))}),this.unsubscribed||e?n():this.subscriptions.push(n),_.remove(t,this.starting)}}remove(t){if(!this.unsubscribed)return void 0!==_.remove(t,this.subscriptions)?t():void 0}unsubscribe(){if(!this.unsubscribed){this.unsubscribed=!0;for(var t=this.subscriptions,e=0;e<t.length;e++)t[e]();this.subscriptions=[],this.starting=[]}}count(){return this.unsubscribed?0:this.subscriptions.length+this.starting.length}empty(){return 0===this.count()}}function streamSubscribeToPropertySubscribe(t,e){return function(n){var r=!1,i=!1,s=nop,o=more,u=function(){if(!r)return t.forEach((function(t){if(r=!0,(o=n(new Initial(t)))===noMore)return s(),s=nop,nop}))};return s=e((function(e){return e instanceof Value?e.isInitial&&!i?(t=new Some(e.value),more):(e.isInitial||u(),r=!0,t=new Some(e.value),n(e)):(e.isEnd&&(o=u()),o!==noMore?n(e):o)})),i=!0,u(),s}}function propertyFromStreamSubscribe(t,e){return assertFunction(e),new Property(t,streamSubscribeToPropertySubscribe(none(),e))}function once(t){const e=new EventStream(new Desc("Bacon","once",[t]),(function(n){return UpdateBarrier.soonButNotYet(e,(function(){n(toEvent(t)),n(endEvent())})),nop}));return e}function flatMap_(t,e,n={}){const r=e,i=[r],s=[],o=e._isProperty;let u=!1;const a=(o?propertyFromStreamSubscribe:newEventStreamAllowSync)(n.desc||new Desc(e,"flatMap_",[t]),(function(e){const i=new CompositeUnsubscribe,a=[];function c(n){if(o&&n.isInitial){if(u)return more;u=!0}const r=makeObservable(t(n));return s.push(r),i.add((function(t,n){return r.subscribeInternal((function(i){if(i.isEnd)return _.remove(r,s),function(){const t=a.shift();t&&c(t)}(),l(n),noMore;{i=i.toNext();const n=e(i);return n===noMore&&t(),n}}))}))}function l(t){return t(),i.empty()?e(endEvent()):more}return i.add((function(t,s){return r.subscribeInternal((function(t){return t.isEnd?l(s):t.isError&&!n.mapError?e(t):n.firstOnly&&i.count()>1?more:i.unsubscribed?noMore:(n.limit&&i.count()>n.limit?a.push(t):c(t),more)}))})),i.unsubscribe}));return a.internalDeps=function(){return s.length?i.concat(s):i},a}function handleEventValueWith(t){return"function"==typeof t?e=>hasValue(e)?t(e.value):e:e=>t}function makeObservable(t){return isObservable(t)?t:once(t)}function flatMapEvent(t,e){return flatMap_(e,t,{mapError:!0,desc:new Desc(t,"flatMapEvent",[e])})}function endAsValue(t){return t.transform((t,e)=>isEnd(t)?(e(nextEvent({})),e(endEvent()),noMore):more)}function endOnError(t,e=(t=>!0)){return t.transform((t,n)=>isError(t)&&e(t.error)?(n(t),n(endEvent())):n(t),new Desc(t,"endOnError",[]))}class Source{constructor(t,e){this._isSource=!0,this.flatten=!0,this.ended=!1,this.obs=t,this.sync=e}subscribe(t){return this.obs.subscribeInternal(t)}toString(){return this.obs.toString()}markEnded(){this.ended=!0}mayHave(t){return!0}}class DefaultSource extends Source{consume(){return this.value}push(t){this.value=t}hasAtLeast(t){return!!this.value}}class ConsumingSource extends Source{constructor(t,e){super(t,e),this.flatten=!1,this.queue=[]}consume(){return this.queue.shift()}push(t){this.queue.push(t)}mayHave(t){return!this.ended||this.queue.length>=t}hasAtLeast(t){return this.queue.length>=t}}class BufferingSource extends Source{constructor(t){super(t,!0),this.queue=[]}consume(){const t=this.queue;return this.queue=[],{value:t}}push(t){return this.queue.push(t.value)}hasAtLeast(t){return!0}}function isTrigger(t){return null!=t&&(t._isSource?t.sync:t._isEventStream)}function fromObservable(t){return null!=t&&t._isSource?t:null!=t&&t._isProperty?new DefaultSource(t,!1):new ConsumingSource(t,!0)}function never(){return new EventStream(describe("Bacon","never"),t=>(t(endEvent()),nop))}function when(...t){return when_(newEventStream,t)}function whenP(...t){return when_(propertyFromStreamSubscribe,t)}function when_(t,e){if(0===e.length)return never();var[n,r]=processRawPatterns(extractRawPatterns(e));if(!n.length)return never();var i=any(n,t=>t.flatten)&&containsDuplicateDeps(map(t=>t.obs,n)),s=t(new Desc("Bacon","when",Array.prototype.slice.call(e)),(function(t){var e=[],o=!1;function u(t){for(var e=0;e<t.ixs.length;e++){let r=t.ixs[e];if(!n[r.index].hasAtLeast(r.count))return!1}return!0}function a(t){for(var e=0;e<t.ixs.length;e++){let r=t.ixs[e];if(!n[r.index].mayHave(r.count))return!0}return!1}function c(t){return!t.source.flatten}return new CompositeUnsubscribe(map((function(l){return function(h){function f(){return UpdateBarrier.whenDoneWith(s,p)}function p(){var i=function i(){var s;if(void 0!==(s=e.pop()))for(var o,a=more,l=0;l<r.length;l++)if(u(o=r[l])){const r=[];for(var h=0;h<o.ixs.length;h++){let t=n[o.ixs[h].index].consume();if(!t)throw new Error("Event was undefined");r.push(t.value)}let u=o.f.apply(null,r);return a=t(s.e.apply(u)),e.length&&(e=filter(c,e)),a===noMore?a:i()}return more}();o&&(all(n,cannotSync)||all(r,a))&&(i=noMore,t(endEvent())),i===noMore&&h()}return l.subscribe((function(n){var r=more;if(n.isEnd)o=!0,l.markEnded(),f();else if(n.isError)r=t(n);else{let t=n;l.push(t),l.sync&&(e.push({source:l,e:t}),i||UpdateBarrier.hasWaiters()?f():p())}return r===noMore&&h(),r}))}}),n)).unsubscribe}));return s}function processRawPatterns(t){var e=[],n=[];for(let c=0;c<t.length;c++){let[l,h]=t[c];for(var r,i={f:h,ixs:[]},s=!1,o=0;o<l.length;o++){r=l[o];var u=indexOf(e,r);s||(s=isTrigger(r)),u<0&&(e.push(r),u=e.length-1);for(var a=0;a<i.ixs.length;a++){let t=i.ixs[a];t.index===u&&t.count++}i.ixs.push({index:u,count:1})}if(l.length>0&&!s)throw new Error("At least one EventStream required, none found in "+l);l.length>0&&n.push(i)}return[map(fromObservable,e),n]}function extractLegacyPatterns(t){for(var e=0,n=t.length,r=[];e<n;){let n=toArray(t[e++]),i=toFunction(t[e++]);r.push([n,i])}return assert("when: expecting arguments in the form (Observable+,function)+",n%2==0),r}function isTypedOrRawPattern(t){return t instanceof Array&&!isObservable(t[t.length-1])}function isRawPattern(t){return t[0]instanceof Array}function extractRawPatterns(t){let e=[];for(let n=0;n<t.length;n++){let r=t[n];if(!isTypedOrRawPattern(r))return extractLegacyPatterns(t);if(isRawPattern(r))e.push([r[0],toFunction(r[1])]);else{let t=r.slice(0,r.length-1),n=toFunction(r[r.length-1]);e.push([t,n])}}return e}function containsDuplicateDeps(t,e=[]){return any(t,(function t(n){if(contains(e,n))return!0;var r=n.internalDeps();return r.length?(e.push(n),any(r,t)):(e.push(n),!1)}))}function cannotSync(t){return!t.sync||t.ended}function withLatestFromE(t,e,n){return when([new DefaultSource(e.toProperty(),!1),new DefaultSource(t,!0),flip(n)]).withDesc(new Desc(t,"withLatestFrom",[e,n]))}function withLatestFromP(t,e,n){return whenP([new DefaultSource(e.toProperty(),!1),new DefaultSource(t,!0),flip(n)]).withDesc(new Desc(t,"withLatestFrom",[e,n]))}function withLatestFrom(t,e,n){if(t instanceof Property)return withLatestFromP(t,e,n);if(t instanceof EventStream)return withLatestFromE(t,e,n);throw new Error("Unknown observable: "+t)}function map$1(t,e){return e instanceof Property?withLatestFrom(t,e,(t,e)=>e):t.transform(mapT(e),new Desc(t,"map",[e]))}function mapT(t){let e=_.toFunction(t);return(t,n)=>n(t.fmap(e))}function constant(t){return new Property(new Desc("Bacon","constant",[t]),(function(e){return e(initialEvent(t)),e(endEvent()),nop}))}function argumentsToObservables(t){return t=Array.prototype.slice.call(t),_.flatMap(singleToObservables,t)}function singleToObservables(t){return isObservable(t)?[t]:isArray(t)?argumentsToObservables(t):[constant(t)]}function argumentsToObservablesAndFunction(t){return _.isFunction(t[0])?[argumentsToObservables(Array.prototype.slice.call(t,1)),t[0]]:[argumentsToObservables(Array.prototype.slice.call(t,0,t.length-1)),_.last(t)]}function groupSimultaneous(...t){return groupSimultaneous_(argumentsToObservables(t))}function groupSimultaneous_(t,e){return when_((t,n)=>new EventStream(t,n,void 0,e),[_.map(t=>new BufferingSource(t),t),function(...t){return t}]).withDesc(new Desc("Bacon","groupSimultaneous",t))}function awaiting(t,e){return groupSimultaneous_([t,e],allowSync).map(t=>0===t[1].length).toProperty(!1).skipDuplicates().withDesc(new Desc(t,"awaiting",[e]))}function combineAsArray(...t){if((t=argumentsToObservables(t)).length){for(var e=[],n=0;n<t.length;n++){let r=isObservable(t[n])?t[n]:constant(t[n]);e.push(wrap(r))}return whenP([e,(...t)=>t]).withDesc(new Desc("Bacon","combineAsArray",t))}return constant([])}function combineWith(...t){var[e,n]=argumentsToObservablesAndFunction(arguments),r=new Desc("Bacon","combineWith",[n,...e]);return combineAsArray(e).map((function(t){return n(...t)})).withDesc(r)}const combine=combineWith;function combineTwo(t,e,n){return whenP([[wrap(t),wrap(e)],n]).withDesc(new Desc(t,"combine",[e,n]))}function wrap(t){return new DefaultSource(t,!0)}function skip(t,e){return t.transform((t,n)=>t.hasValue&&e>0?(e--,more):n(t),new Desc(t,"skip",[e]))}function flatMapConcat(t,e){return flatMap_(handleEventValueWith(e),t,{desc:new Desc(t,"flatMapConcat",[e]),limit:1})}function fromBinder(t,e=_.id){var n=new Desc("Bacon","fromBinder",[t,e]);return new EventStream(n,(function(n){var r=!1,i=!1,s=function(){if(!r)return null!=o?(o(),r=!0):i=!0},o=t((function(...t){var r=e(...t);let i=isArray(r)&&isEvent(_.last(r))?r:[r];for(var o=more,u=0;u<i.length;u++){let t=toEvent(i[u]);if((o=n(t))===noMore||t.isEnd)return s(),o}return o}));return i&&s(),s}))}function fromPoll(t,e){var n=new Desc("Bacon","fromPoll",[t,e]);return fromBinder((function(e){var n=GlobalScheduler.scheduler.setInterval(e,t);return function(){return GlobalScheduler.scheduler.clearInterval(n)}}),e).withDesc(n)}function interval(t,e){return fromPoll(t,(function(){return nextEvent(e)})).withDesc(new Desc("Bacon","interval",[t,e]))}function makeCombinator(t){return"function"==typeof t?t:_.id}function sampledBy(t,e,n){return t instanceof EventStream?sampledByE(t,e,n):sampledByP(t,e,n)}function sampledByP(t,e,n){return withLatestFrom(e,t,flip(makeCombinator(n))).withDesc(new Desc(t,"sampledBy",[e]))}function sampledByE(t,e,n){return sampledByP(t.toProperty(),e,n).withDesc(new Desc(t,"sampledBy",[e]))}function sampleP(t,e){return sampledByP(t,interval(e,{}),(t,e)=>t).withDesc(new Desc(t,"sample",[e]))}function transformP(t,e,n){return new Property(new Desc(t,"transform",[e]),n=>t.subscribeInternal(t=>e(t,n))).withDesc(n)}function transformE(t,e,n){return new EventStream(new Desc(t,"transform",[e]),n=>t.subscribeInternal(t=>e(t,n)),void 0,allowSync).withDesc(n)}function composeT(t,e){let n;const r=t=>e(t,n);return(e,i)=>(n=i,t(e,r))}function toPredicate(t){if("boolean"==typeof t)return _.always(t);if("function"!=typeof t)throw new Error("Not a function: "+t);return t}function withPredicate(t,e,n,r){return e instanceof Property?withLatestFrom(t,e,(t,e)=>[t,e]).transform(composeT(n(t=>t[1]),mapT(t=>t[0])),r):t.transform(n(toPredicate(e)),r)}function filter$1(t,e){return withPredicate(t,e,filterT,new Desc(t,"filter",[e]))}function filterT(t){return(e,n)=>e.filter(t)?n(e):more}function not(t){return t.map(t=>!t).withDesc(new Desc(t,"not",[]))}function and(t,e){return t.combine(toProperty(e),(t,e)=>!(!t||!e)).withDesc(new Desc(t,"and",[e]))}function or(t,e){return t.combine(toProperty(e),(t,e)=>t||e).withDesc(new Desc(t,"or",[e]))}function toProperty(t){return isProperty(t)?t:constant(t)}function flatMapFirst(t,e){return flatMap_(handleEventValueWith(e),t,{firstOnly:!0,desc:new Desc(t,"flatMapFirst",[e])})}function concatE(t,e,n){return new EventStream(new Desc(t,"concat",[e]),(function(n){var r=nop,i=t.dispatcher.subscribe((function(t){return t.isEnd?(r=e.toEventStream().dispatcher.subscribe(n),more):n(t)}));return function(){return i(),r()}}),void 0,n)}function concatAll(...t){let e=argumentsToObservables(t);return(e.length?fold(tail(e),head(e).toEventStream(),(t,e)=>t.concat(e)):never()).withDesc(new Desc("Bacon","concatAll",e))}function transformPropertyChanges(t,e,n){let r,i;const s=e(new EventStream(describe(t,"changes",[]),e=>t.dispatcher.subscribe((function(t){return!r&&isInitial(t)&&(r=t,UpdateBarrier.whenDoneWith(o,(function(){if(!i)throw new Error("Init sequence fail");i(r)}))),t.isInitial?more:e(t)})),void 0,allowSync)),o=propertyFromStreamSubscribe(n,t=>(i=t,s.dispatcher.subscribe((function(e){t(e)}))));return o}function fold$1(t,e,n){return t.scan(e,n).last().withDesc(new Desc(t,"fold",[e,n]))}function startWithE(t,e){return once(e).concat(t).withDesc(new Desc(t,"startWith",[e]))}function startWithP(t,e){return t.scan(e,(t,e)=>e).withDesc(new Desc(t,"startWith",[e]))}const endMarker={};function takeUntil(t,e){let n=groupSimultaneous_([t.mapEnd(endMarker),e.skipErrors()],allowSync);return t instanceof Property&&(n=n.toProperty()),n.transform((function(t,e){if(hasValue(t)){var[n,r]=t.value;if(r.length)return e(endEvent());for(var i=more,s=0;s<n.length;s++){let t=n[s];if(t===endMarker)return e(endEvent());i=e(nextEvent(t))}return i}return e(t)}),new Desc(t,"takeUntil",[e]))}function flatMap$1(t,e){return flatMap_(handleEventValueWith(e),t,{desc:new Desc(t,"flatMap",[e])})}function flatMapError(t,e){return flatMap_(t=>{if(t instanceof Error$1){let n=t.error;return e(n)}return t},t,{mapError:!0,desc:new Desc(t,"flatMapError",[e])})}var spies=[],running=!1;function registerObs(t){if(spies.length&&!running)try{running=!0,spies.forEach((function(e){e(t)}))}finally{running=!1}}const spy=t=>spies.push(t);function flatMapLatest(t,e){let n=_.toFunction(e);var r=isProperty(t)?t.toEventStream(allowSync):t;let i=flatMap$1(r,t=>makeObservable(n(t)).takeUntil(r));return isProperty(t)&&(i=i.toProperty()),i.withDesc(new Desc(t,"flatMapLatest",[n]))}class Dispatcher{constructor(t,e,n){this.pushing=!1,this.ended=!1,this.prevError=void 0,this.unsubSrc=void 0,this._subscribe=e,this._handleEvent=n,this.subscribe=_.bind(this.subscribe,this),this.handleEvent=_.bind(this.handleEvent,this),this.subscriptions=[],this.observable=t,this.queue=[]}hasSubscribers(){return this.subscriptions.length>0}removeSub(t){return this.subscriptions=_.without(t,this.subscriptions),this.subscriptions}push(t){return t.isEnd&&(this.ended=!0),UpdateBarrier.inTransaction(t,this,this.pushIt,[t])}pushToSubscriptions(t){try{let e=this.subscriptions;const n=e.length;for(let r=0;r<n;r++){const n=e[r];(n.sink(t)===noMore||t.isEnd)&&this.removeSub(n)}return!0}catch(t){throw this.pushing=!1,this.queue=[],t}}pushIt(t){if(this.pushing)return this.queue.push(t),more;if(t!==this.prevError){for(t.isError&&(this.prevError=t),this.pushing=!0,this.pushToSubscriptions(t),this.pushing=!1;;){let t=this.queue.shift();if(!t)break;this.push(t)}return this.hasSubscribers()?more:(this.unsubscribeFromSource(),noMore)}}handleEvent(t){return this._handleEvent?this._handleEvent(t):this.push(t)}unsubscribeFromSource(){this.unsubSrc&&this.unsubSrc(),this.unsubSrc=void 0}subscribe(t){if(this.ended)return t(endEvent()),nop;{assertFunction(t);let e={sink:t};return this.subscriptions.push(e),1===this.subscriptions.length&&(this.unsubSrc=this._subscribe(this.handleEvent),assertFunction(this.unsubSrc)),()=>{if(this.removeSub(e),!this.hasSubscribers())return this.unsubscribeFromSource()}}}inspect(){return this.observable.toString()}}class PropertyDispatcher extends Dispatcher{constructor(t,e,n){super(t,e,n),this.current=none(),this.propertyEnded=!1,this.subscribe=_.bind(this.subscribe,this)}push(t){return t.isEnd&&(this.propertyEnded=!0),t instanceof Value?(this.current=new Some(t),this.currentValueRootId=UpdateBarrier.currentEventId()):t.hasValue&&console.error("Unknown event, two Bacons loaded?",t.constructor),super.push(t)}maybeSubSource(t,e){return e===noMore?nop:this.propertyEnded?(t(endEvent()),nop):super.subscribe(t)}subscribe(t){var e=more;if(this.current.isDefined&&(this.hasSubscribers()||this.propertyEnded)){var n=UpdateBarrier.currentEventId(),r=this.currentValueRootId;return!this.propertyEnded&&r&&n&&n!==r?(UpdateBarrier.whenDoneWith(this.observable,()=>{if(this.currentValueRootId===r)return t(initialEvent(this.current.get().value))}),this.maybeSubSource(t,e)):(UpdateBarrier.inTransaction(void 0,this,()=>e=t(initialEvent(this.current.get().value)),[]),this.maybeSubSource(t,e))}return this.maybeSubSource(t,e)}inspect(){return this.observable+" current= "+this.current}}function flatMapWithConcurrencyLimit(t,e,n){return flatMap_(handleEventValueWith(n),t,{desc:new Desc(t,"flatMapWithConcurrencyLimit",[e,n]),limit:e})}function bufferWithTime(t,e){return bufferWithTimeOrCount(t,e,Number.MAX_VALUE).withDesc(new Desc(t,"bufferWithTime",[e]))}function bufferWithCount(t,e){return bufferWithTimeOrCount(t,void 0,e).withDesc(new Desc(t,"bufferWithCount",[e]))}function bufferWithTimeOrCount(t,e,n){const r=toDelayFunction(e);function i(t){return t.values.length===n?t.flush():void 0!==r?t.schedule(r):void 0}var s=new Desc(t,"bufferWithTimeOrCount",[e,n]);return buffer(t,i,i).withDesc(s)}class Buffer{constructor(t,e){this.push=t=>more,this.scheduled=null,this.end=void 0,this.values=[],this.onFlush=t,this.onInput=e}flush(){if(this.scheduled&&(GlobalScheduler.scheduler.clearTimeout(this.scheduled),this.scheduled=null),this.values.length>0){var t=this.values;this.values=[];var e=this.push(nextEvent(t));if(null!=this.end)return this.push(this.end);if(e!==noMore)return this.onFlush(this)}else if(null!=this.end)return this.push(this.end)}schedule(t){if(!this.scheduled)return this.scheduled=t(()=>this.flush())}}function toDelayFunction(t){if(void 0!==t){if("number"==typeof t){var e=t;return function(t){return GlobalScheduler.scheduler.setTimeout(t,e)}}return t}}function buffer(t,e=nop,n=nop){var r=more,i=new Buffer(n,e);return t.transform((t,n)=>(i.push=n,hasValue(t)?(i.values.push(t.value),e(i)):isError(t)?r=n(t):isEnd(t)&&(i.end=t,i.scheduled||i.flush()),r)).withDesc(new Desc(t,"buffer",[]))}function asyncWrapSubscribe(t,e){var n=!1;return function(r){const i=UpdateBarrier.isInTransaction();var s;function o(){var t=s||[];s=void 0;for(var e=0;e<t.length;e++){var n=t[e];r(n)}}n=!0;try{return e((function(e){return n||s?(s?s.push(e):(s=[e],i?UpdateBarrier.soonButNotYet(t,o):GlobalScheduler.scheduler.setTimeout(o,0)),more):r(e)}))}finally{n=!1}}}function mergeAll(...t){let e=argumentsToObservables(t);return e.length?new EventStream(new Desc("Bacon","mergeAll",e),(function(t){var n=0,r=map((function(r){return function(i){return r.subscribeInternal((function(r){if(r.isEnd)return++n===e.length?t(endEvent()):more;r=r.toNext();var s=t(r);return s===noMore&&i(),s}))}}),e);return new CompositeUnsubscribe(r).unsubscribe})):never()}function later(t,e){return fromBinder((function(n){var r=GlobalScheduler.scheduler.setTimeout((function(){return n([toEvent(e),endEvent()])}),t);return function(){return GlobalScheduler.scheduler.clearTimeout(r)}})).withDesc(new Desc("Bacon","later",[t,e]))}function delay(t,e){return t.transformChanges(new Desc(t,"delay",[e]),(function(t){return t.flatMap((function(t){return later(e,t)}))}))}function debounce(t,e){return t.transformChanges(new Desc(t,"debounce",[e]),(function(t){return t.flatMapLatest((function(t){return later(e,t)}))}))}function debounceImmediate(t,e){return t.transformChanges(new Desc(t,"debounceImmediate",[e]),(function(t){return t.flatMapFirst((function(t){return once(t).concat(later(e,t).errors())}))}))}function throttle(t,e){return t.transformChanges(new Desc(t,"throttle",[e]),t=>t.bufferWithTime(e).map(t=>t[t.length-1]))}function bufferingThrottle(t,e){var n=new Desc(t,"bufferingThrottle",[e]);return t.transformChanges(n,t=>t.flatMapConcat(t=>once(t).concat(later(e,t).errors())))}function takeWhile(t,e){return withPredicate(t,e,takeWhileT,new Desc(t,"takeWhile",[e]))}function takeWhileT(t){return(e,n)=>e.filter(t)?n(e):(n(endEvent()),noMore)}function skipUntil(t,e){var n=e.transform(composeT(takeT(1),mapT(!0))).toProperty().startWith(!1);return t.filter(n).withDesc(new Desc(t,"skipUntil",[e]))}function skipWhile(t,e){return withPredicate(t,e,skipWhileT,new Desc(t,"skipWhile",[e]))}function skipWhileT(t){var e=!1;return function(n,r){return!e&&hasValue(n)&&t(n.value)?more:(n.hasValue&&(e=!0),r(n))}}function groupBy(t,e,n=_.id){var r={};return t.transform(composeT(filterT(t=>!r[e(t)]),mapT((function(i){var s=e(i),o=t.changes().filter(t=>e(t)===s),u=once(i).concat(o),a=n(u,i).toEventStream().transform((t,e)=>{let n=e(t);return t.isEnd&&delete r[s],n});return r[s]=a,a}))))}function slidingWindow(t,e,n=0){return t.scan([],(function(t,n){return t.concat([n]).slice(-e)})).filter((function(t){return t.length>=n})).withDesc(new Desc(t,"slidingWindow",[e,n]))}const nullMarker={};function diff(t,e,n){return transformP(scan(t,[e,nullMarker],(t,e)=>[e,n(t[0],e)]),composeT(filterT(t=>t[1]!==nullMarker),mapT(t=>t[1])),new Desc(t,"diff",[e,n]))}function flatScan(t,e,n){let r=e;return t.flatMapConcat(t=>makeObservable(n(r,t)).doAction(t=>r=t)).toProperty().startWith(e).withDesc(new Desc(t,"flatScan",[e,n]))}function holdWhen(t,e){var n=!1,r=[],i=!1;return new EventStream(new Desc(t,"holdWhen",[e]),(function(s){var o=new CompositeUnsubscribe,u=!1,a=function(t){return t&&t(),o.empty()&&u?s(endEvent()):more};return o.add((function(t,o){return e.subscribeInternal((function(t){if(hasValue(t)){n=t.value;var e=more;if(!n){var u=r;r=[];for(var c=0;c<u.length;c++)e=s(nextEvent(u[c]));i&&(s(endEvent()),o(),e=noMore)}return e}return t.isEnd?a(o):s(t)}))})),o.add((function(e,o){return t.subscribeInternal((function(t){return n&&hasValue(t)?(r.push(t.value),more):t.isEnd&&r.length?(i=!0,a(o)):s(t)}))})),u=!0,a(),o.unsubscribe}))}function zipAsArray(...t){return when([_.map(t=>t.toEventStream(),argumentsToObservables(t)),(...t)=>t]).withDesc(new Desc("Bacon","zipAsArray",t))}function zipWith(t,...e){var[e,t]=argumentsToObservablesAndFunction(arguments);return when([e=_.map(t=>t.toEventStream(),e),t]).withDesc(new Desc("Bacon","zipWith",[t].concat(e)))}function zip(t,e,n){return zipWith(n||Array,t,e).withDesc(new Desc(t,"zip",[e]))}function combineTemplate(t){function e(t,e,n){return(function(t){return t[t.length-1]}(t))[e]=n,n}function n(t){return isArray(t)?[]:{}}function r(t){if(isObservable(t))return!0;if(t&&(t.constructor==Object||t.constructor==Array))for(var e in t)if(Object.prototype.hasOwnProperty.call(t,e)){if(r(t[e]))return!0}}function i(t,i){if(isObservable(i))u.push(i),o.push(function(t,n){return function(r,i){e(r,t,i[n])}}(t,u.length-1));else if(r(i)){const r=function(t){t.pop()};o.push(function(t,r){return function(i){const s=n(r);e(i,t,s),i.push(s)}}(t,i)),s(i),o.push(r)}else o.push(function(t,n){return function(r){e(r,t,n)}}(t,i))}function s(t){_.each(t,i)}const o=[],u=[];return(r(t)?(s(t),combineAsArray(u).map((function(e){const r=n(t),i=[r];for(var s=0;s<o.length;s++)(0,o[s])(i,e);return r}))):constant(t)).withDesc(new Desc("Bacon","combineTemplate",[t]))}function decode(t,e){return t.combine(combineTemplate(e),(t,e)=>e[t]).withDesc(new Desc(t,"decode",[e]))}function firstToPromise(t,e){const n=(e,n)=>t.subscribe(t=>(hasValue(t)&&e(t.value),isError(t)&&n(t.error),noMore));if("function"==typeof e)return new e(n);if("function"==typeof Promise)return new Promise(n);throw new Error("There isn't default Promise, use shim or parameter")}function toPromise(t,e){return t.last().firstToPromise(e)}var idCounter=0;class Observable{constructor(t){this.id=++idCounter,this._isObservable=!0,this.desc=t,this.initialDesc=t}awaiting(t){return awaiting(this,t)}bufferingThrottle(t){return bufferingThrottle(this,t)}combine(t,e){return combineTwo(this,t,e).withDesc(new Desc(this,"combine",[t,e]))}debounce(t){return debounce(this,t)}debounceImmediate(t){return debounceImmediate(this,t)}decode(t){return decode(this,t)}delay(t){return delay(this,t)}deps(){return this.desc.deps()}diff(t,e){return diff(this,t,e)}doAction(t){return this.transform(doActionT(t),new Desc(this,"doAction",[t]))}doEnd(t){return this.transform(doEndT(t),new Desc(this,"doEnd",[t]))}doError(t){return this.transform(doErrorT(t),new Desc(this,"doError",[t]))}doLog(...t){return this.transform(doLogT(t),new Desc(this,"doLog",t))}endAsValue(){return endAsValue(this)}endOnError(t=(t=>!0)){return endOnError(this,t)}errors(){return this.filter(t=>!1).withDesc(new Desc(this,"errors"))}filter(t){return filter$1(this,t)}first(){return take(1,this,new Desc(this,"first"))}firstToPromise(t){return firstToPromise(this,t)}fold(t,e){return fold$1(this,t,e)}forEach(t=nullSink){return this.onValue(t)}holdWhen(t){return holdWhen(this,t)}inspect(){return this.toString()}internalDeps(){return this.initialDesc.deps()}last(){return last$1(this)}log(...t){return log(t,this),this}mapEnd(t){return this.transform(mapEndT(t),new Desc(this,"mapEnd",[t]))}mapError(t){return this.transform(mapErrorT(t),new Desc(this,"mapError",[t]))}name(t){return this._name=t,this}onEnd(t=nullVoidSink){return this.subscribe((function(e){return e.isEnd?t():more}))}onError(t=nullSink){return this.subscribe((function(e){return isError(e)?t(e.error):more}))}onValue(t=nullSink){return this.subscribe((function(e){return hasValue(e)?t(e.value):more}))}onValues(t){return this.onValue((function(e){return t(...e)}))}reduce(t,e){return fold$1(this,t,e)}sampledBy(t){return sampledBy(this,t,arguments[1])}scan(t,e){return scan(this,t,e)}skip(t){return skip(this,t)}skipDuplicates(t){return skipDuplicates(this,t)}skipErrors(){return skipErrors(this)}skipUntil(t){return skipUntil(this,t)}skipWhile(t){return skipWhile(this,t)}slidingWindow(t,e=0){return slidingWindow(this,t,e)}subscribe(t=nullSink){return UpdateBarrier.wrappedSubscribe(this,t=>this.subscribeInternal(t),t)}take(t){return take(t,this)}takeUntil(t){return takeUntil(this,t)}takeWhile(t){return takeWhile(this,t)}throttle(t){return throttle(this,t)}toPromise(t){return toPromise(this,t)}toString(){return this._name?this._name:this.desc.toString()}withDesc(t){return t&&(this.desc=t),this}withDescription(t,e,...n){return this.desc=describe(t,e,...n),this}zip(t,e){return zip(this,t,e)}}class Property extends Observable{constructor(t,e,n){super(t),this._isProperty=!0,assertFunction(e),this.dispatcher=new PropertyDispatcher(this,e,n),registerObs(this)}and(t){return and(this,t)}changes(){return new EventStream(new Desc(this,"changes",[]),t=>this.dispatcher.subscribe((function(e){return e.isInitial?more:t(e)})))}concat(t){return this.transformChanges(describe(this,"concat",t),e=>e.concat(t))}transformChanges(t,e){return transformPropertyChanges(this,e,t)}flatMap(t){return flatMap$1(this,t)}flatMapConcat(t){return flatMapConcat(this,t)}flatMapError(t){return flatMapError(this,t)}flatMapEvent(t){return flatMapEvent(this,t)}flatMapFirst(t){return flatMapFirst(this,t)}flatMapLatest(t){return flatMapLatest(this,t)}flatMapWithConcurrencyLimit(t,e){return flatMapWithConcurrencyLimit(this,t,e)}groupBy(t,e){return groupBy(this,t,e)}map(t){return map$1(this,t)}not(){return not(this)}or(t){return or(this,t)}sample(t){return sampleP(this,t)}startWith(t){return startWithP(this,t)}subscribeInternal(t=nullSink){return this.dispatcher.subscribe(t)}toEventStream(t){return new EventStream(new Desc(this,"toEventStream",[]),t=>this.subscribeInternal((function(e){return t(e.toNext())})),void 0,t)}toProperty(){return assertNoArguments(arguments),this}transform(t,e){return transformP(this,t,e)}withLatestFrom(t,e){return withLatestFromP(this,t,e)}withStateMachine(t,e){return withStateMachine(t,e,this)}}function isProperty(t){return!!t._isProperty}const allowSync={forceAsync:!1};class EventStream extends Observable{constructor(t,e,n,r){super(t),this._isEventStream=!0,r!==allowSync&&(e=asyncWrapSubscribe(this,e)),this.dispatcher=new Dispatcher(this,e,n),registerObs(this)}bufferWithTime(t){return bufferWithTime(this,t)}bufferWithCount(t){return bufferWithCount(this,t)}bufferWithTimeOrCount(t,e){return bufferWithTimeOrCount(this,t,e)}changes(){return this}concat(t,e){return concatE(this,t,e)}transformChanges(t,e){return e(this).withDesc(t)}flatMap(t){return flatMap$1(this,t)}flatMapConcat(t){return flatMapConcat(this,t)}flatMapError(t){return flatMapError(this,t)}flatMapFirst(t){return flatMapFirst(this,t)}flatMapLatest(t){return flatMapLatest(this,t)}flatMapWithConcurrencyLimit(t,e){return flatMapWithConcurrencyLimit(this,t,e)}flatMapEvent(t){return flatMapEvent(this,t)}flatScan(t,e){return flatScan(this,t,e)}groupBy(t,e){return groupBy(this,t,e)}map(t){return map$1(this,t)}merge(t){return assertEventStream(t),mergeAll(this,t).withDesc(new Desc(this,"merge",[t]))}not(){return not(this)}startWith(t){return startWithE(this,t)}subscribeInternal(t=nullSink){return this.dispatcher.subscribe(t)}toEventStream(){return this}toProperty(t){let e=arguments.length?toOption(t):none(),n=this.dispatcher,r=new Desc(this,"toProperty",Array.prototype.slice.apply(arguments)),i=n.subscribe;return new Property(r,streamSubscribeToPropertySubscribe(e,i))}transform(t,e){return transformE(this,t,e)}withLatestFrom(t,e){return withLatestFromE(this,t,e)}withStateMachine(t,e){return withStateMachine(t,e,this)}}function newEventStream(t,e){return new EventStream(t,e)}function newEventStreamAllowSync(t,e){return new EventStream(t,e,void 0,allowSync)}function symbol(t){return"undefined"!=typeof Symbol&&Symbol[t]?Symbol[t]:"undefined"!=typeof Symbol&&"function"==typeof Symbol.for?Symbol[t]=Symbol.for(t):"@@"+t}class ESObservable{constructor(t){this.observable=t}subscribe(t,e,n){const r="function"==typeof t?{next:t,error:e,complete:n}:t,i={closed:!1,unsubscribe:function(){i.closed=!0,s()}},s=this.observable.subscribe((function(t){hasValue(t)&&r.next?r.next(t.value):isError(t)?(r.error&&r.error(t.error),i.unsubscribe()):t.isEnd&&(i.closed=!0,r.complete&&r.complete())}));return i}}function update(t,...e){let n=extractRawPatterns(e);for(var r=0;r<n.length;r++){let t=n[r];t[1]=lateBindFirst(t[1])}return when(...n).scan(t,(function(t,e){return e(t)})).withDesc(new Desc("Bacon","update",[t,...e]))}function lateBindFirst(t){return function(...e){return function(n){return t(...[n].concat(e))}}}function fromArray(t){if(assertArray(t),t.length){var e=0,n=new EventStream(new Desc("Bacon","fromArray",[t]),(function(r){var i=!1,s=more,o=!1,u=!1;return UpdateBarrier.soonButNotYet(n,(function a(){if(u=!0,!o){if(e===t.length)return r(endEvent()),!1;for(o=!0;u;)if(u=!1,s!==noMore&&!i){var c=t[e++];(s=r(toEvent(c)))!==noMore&&(e===t.length?r(endEvent()):UpdateBarrier.afterTransaction(n,a))}return o=!1}})),function(){return i=!0}}));return n}return never().withDesc(new Desc("Bacon","fromArray",t))}function isEventSourceFn(t){return _.isFunction(t)}ESObservable.prototype[symbol("observable")]=function(){return this},Observable.prototype.toESObservable=function(){return new ESObservable(this)},Observable.prototype[symbol("observable")]=Observable.prototype.toESObservable;var eventMethods=[["addEventListener","removeEventListener"],["addListener","removeListener"],["on","off"],["bind","unbind"]],findHandlerMethods=function(t){for(var e,n=0;n<eventMethods.length;n++){var r=[t[(e=eventMethods[n])[0]],t[e[1]]];if(r[0]&&r[1])return r}for(var i=0;i<eventMethods.length;i++){var s=t[(e=eventMethods[i])[0]];if(s)return[s,function(){}]}throw new Error("No suitable event methods in "+t)};function fromEvent(t,e,n){var[r,i]=findHandlerMethods(t),s=new Desc("Bacon","fromEvent",[t,e]);return fromBinder((function(n){return isEventSourceFn(e)?(e(r.bind(t),n),function(){return e(i.bind(t),n)}):(r.call(t,e,n),function(){return i.call(t,e,n)})}),n).withDesc(s)}function onValues(...t){return combineAsArray(t.slice(0,t.length-1)).onValues(t[arguments.length-1])}function repeat(t){var e=0;return fromBinder((function(n){var r=!1,i=more,s=function(){};function o(t){return t.isEnd?(r?u():r=!0,more):i=n(t)}function u(){var u;for(r=!0;r&&i!==noMore;)u=t(e++),r=!1,u?s=u.subscribeInternal(o):n(endEvent());r=!0}return u(),()=>s()})).withDesc(new Desc("Bacon","repeat",[t]))}function repeatedly(t,e){var n=0;return fromPoll(t,(function(){return e[n++%e.length]})).withDesc(new Desc("Bacon","repeatedly",[t,e]))}function silence(t){return later(t,"").filter(!1).withDesc(new Desc("Bacon","silence",[t]))}function retry(t){if(!_.isFunction(t.source))throw new Error("'source' option has to be a function");var e=t.source,n=t.retries||0,r=0,i=t.delay||function(){return 0},s=t.isRetryable||function(){return!0},o=!1,u=null;return repeat((function(t){function a(){return e(t).endOnError().transform((function(t,e){return isError(t)?s((u=t).error)&&(0===n||r<n)?more:(o=!0,e(t)):(hasValue(t)&&(u=null,o=!0),e(t))}))}if(!o){if(u){var c={error:u.error,retriesDone:r},l=silence(i(c));return r++,l.concat(once(null).flatMap(a))}return a()}})).withDesc(new Desc("Bacon","retry",[t]))}function sequentially(t,e){var n=0;return fromPoll(t,(function(){var t=e[n++];return n<e.length?t:n===e.length?[toEvent(t),endEvent()]:endEvent()})).withDesc(new Desc("Bacon","sequentially",[t,e]))}function valueAndEnd(t){return[toEvent(t),endEvent()]}function fromPromise(t,e,n=valueAndEnd){return fromBinder((function(n){const r=t.then(n,t=>n(new Error$1(t)));return r&&"function"==typeof r.done&&r.done(),e?function(){if("function"==typeof t.abort)return t.abort()}:function(){}}),n).withDesc(new Desc("Bacon","fromPromise",[t]))}function withMethodCallSupport(t){return function(e,...n){if("object"==typeof e&&n.length){var r=e,i=n[0];e=function(...t){return r[i](...t)},n=n.slice(1)}return t(e,...n)}}function partiallyApplied(t,e){return function(...n){return t(...e.concat(n))}}const makeFunction_=withMethodCallSupport((function(t,...e){return _.isFunction(t)?e.length?partiallyApplied(t,e):t:_.always(t)}));function makeFunction(t,e){return makeFunction_(t,...e)}function fromCallback(t,...e){return fromBinder((function(n){return makeFunction(t,e)(n),nop}),(function(t){return[t,endEvent()]})).withDesc(new Desc("Bacon","fromCallback",[t,...e]))}function fromNodeCallback(t,...e){return fromBinder((function(n){return makeFunction(t,e)(n),nop}),(function(t,e){return t?[new Error$1(t),endEvent()]:[e,endEvent()]})).withDesc(new Desc("Bacon","fromNodeCallback",[t,...e]))}function fromESObservable(t){var e;e=t[symbol("observable")]?t[symbol("observable")]():t;var n=new Desc("Bacon","fromESObservable",[e]);return new EventStream(n,(function(t){var n=e.subscribe({error:function(e){t(new Error$1(e)),t(new End)},next:function(e){t(new Next(e))},complete:function(){t(new End)}});return n.unsubscribe?function(){n.unsubscribe()}:n}))}class Bus extends EventStream{constructor(){super(new Desc("Bacon","Bus",[]),t=>this.subscribeAll(t)),this.pushing=!1,this.pushQueue=void 0,this.ended=!1,this.subscriptions=[],this.unsubAll=_.bind(this.unsubAll,this),this.push=_.bind(this.push,this),this.subscriptions=[],this.ended=!1}plug(t){if(assertObservable(t),!this.ended){var e={input:t,unsub:void 0};return this.subscriptions.push(e),void 0!==this.sink&&this.subscribeInput(e),()=>this.unsubscribeInput(t)}}end(){if(this.ended=!0,this.unsubAll(),"function"==typeof this.sink)return this.sink(endEvent())}push(t){if(!this.ended&&"function"==typeof this.sink){var e=!this.pushing;if(!e)return this.pushQueue||(this.pushQueue=[]),void this.pushQueue.push(t);this.pushing=!0;try{return this.sink(nextEvent(t))}finally{if(e&&this.pushQueue){for(var n=0;n<this.pushQueue.length;){var r=this.pushQueue[n];this.sink(nextEvent(r)),n++}this.pushQueue=void 0}this.pushing=!1}}}error(t){if("function"==typeof this.sink)return this.sink(new Error$1(t))}unsubAll(){for(var t,e=this.subscriptions,n=0;n<e.length;n++)"function"==typeof(t=e[n]).unsub&&t.unsub()}subscribeAll(t){if(this.ended)t(endEvent());else{this.sink=t;for(var e,n=this.subscriptions.slice(),r=0;r<n.length;r++)e=n[r],this.subscribeInput(e)}return this.unsubAll}guardedSink(t){return e=>e.isEnd?(this.unsubscribeInput(t),noMore):this.sink?this.sink(e):more}subscribeInput(t){return t.unsub=t.input.subscribeInternal(this.guardedSink(t.input)),t.unsub}unsubscribeInput(t){for(var e,n=this.subscriptions,r=0;r<n.length;r++)if((e=n[r]).input===t)return"function"==typeof e.unsub&&e.unsub(),void this.subscriptions.splice(r,1)}}function tryF(t){return function(e){try{return once(t(e))}catch(t){return once(new Error$1(t))}}}const $={asEventStream(t,e,n){return _.isFunction(e)&&(n=e,e=void 0),fromBinder(n=>(this.on(t,e,n),()=>this.off(t,e,n)),n).withDesc(new Desc(this.selector||this,"asEventStream",[t]))},init(t){t.fn.asEventStream=$.asEventStream}},version="3.0.20";export{$,Bus,CompositeUnsubscribe,Desc,End,Error$1 as Error,Event,EventStream,Initial,Next,Observable,Property,Value,_,combine,combineAsArray,combineTemplate,combineTwo,combineWith,concatAll,constant,fromArray,fromBinder,fromCallback,fromESObservable,fromEvent,fromEvent as fromEventTarget,fromNodeCallback,fromPoll,fromPromise,getScheduler,groupSimultaneous,hasValue,interval,isEnd,isError,isEvent,isInitial,isNext,isProperty,later,mergeAll,more,never,noMore,nullSink,nullVoidSink,onValues,once,repeat,repeatedly,retry,sequentially,setScheduler,silence,spy,tryF as try,update,version,when,zipAsArray,zipWith};