forked from paldepind/flyd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flyd.min.js
1 lines (1 loc) · 9.35 KB
/
flyd.min.js
1
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.flyd=f()}})(function(){var define,module,exports;return function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){"use strict";var curryN=require("ramda/src/curryN");function isFunction(obj){return!!(obj&&obj.constructor&&obj.call&&obj.apply)}function trueFn(){return true}var toUpdate=[];var inStream;var order=[];var orderNextIdx=-1;var flushing=false;var flyd={};flyd.stream=function(initialValue){var endStream=createDependentStream([],trueFn);var s=createStream();s.end=endStream;s.fnArgs=[];endStream.listeners.push(s);s.toJSON=function(){return s()};if(arguments.length>0)s(initialValue);return s};flyd.combine=curryN(2,combine);function combine(fn,streams){var i,s,deps,depEndStreams;var endStream=createDependentStream([],trueFn);deps=[];depEndStreams=[];for(i=0;i<streams.length;++i){if(streams[i]!==undefined){deps.push(streams[i]);if(streams[i].end!==undefined)depEndStreams.push(streams[i].end)}}s=createDependentStream(deps,fn);s.depsChanged=[];s.fnArgs=s.deps.concat([s,s.depsChanged]);s.end=endStream;endStream.listeners.push(s);addListeners(depEndStreams,endStream);endStream.deps=depEndStreams;updateStream(s);return s}flyd.isStream=function(stream){return isFunction(stream)&&"hasVal"in stream};flyd.immediate=function(s){if(s.depsMet===false){s.depsMet=true;updateStream(s)}return s};flyd.endsOn=function(endS,s){detachDeps(s.end);endS.listeners.push(s.end);s.end.deps.push(endS);return s};flyd.map=curryN(2,function(f,s){return combine(function(s,self){self(f(s.val))},[s])});flyd.on=curryN(2,function(f,s){return combine(function(s){f(s.val)},[s])});flyd.filter=curryN(2,filter);function filter(fn,s){return flyd.combine(function(s,self){if(fn(s()))self(s.val)},[s])}flyd.scan=curryN(3,function(f,acc,s){var ns=combine(function(s,self){self(acc=f(acc,s.val))},[s]);if(!ns.hasVal)ns(acc);return ns});flyd.merge=curryN(2,function(s1,s2){var s=flyd.immediate(combine(function(s1,s2,self,changed){if(changed[0]){self(changed[0]())}else if(s1.hasVal){self(s1.val)}else if(s2.hasVal){self(s2.val)}},[s1,s2]));flyd.endsOn(combine(function(){return true},[s1.end,s2.end]),s);return s});flyd.transduce=curryN(2,function(xform,source){xform=xform(new StreamTransformer);return combine(function(source,self){var res=xform["@@transducer/step"](undefined,source.val);if(res&&res["@@transducer/reduced"]===true){self.end(true);return res["@@transducer/value"]}else{return res}},[source])});flyd.curryN=curryN;function streamMap(f){return flyd.map(f,this)}function ap(s2){var s1=this;return combine(function(s1,s2,self){self(s1.val(s2.val))},[s1,s2])}function streamOn(cb){return combine(function(s){cb(s.val)},[this])}function streamFilter(fn){return filter(fn,this)}function streamToString(){return"stream("+this.val+")"}function streamUse(fn){return fn(this)}function createStream(){function s(n){if(arguments.length===0)return s.val;updateStreamValue(s,n);return s}s.hasVal=false;s.val=undefined;s.vals=[];s.listeners=[];s.queued=false;s.end=undefined;s.map=streamMap;s.use=streamUse;s.ap=ap;s.of=flyd.stream;s.on=streamOn;s.filter=streamFilter;s.toString=streamToString;return s}function createDependentStream(deps,fn){var s=createStream();s.fn=fn;s.deps=deps;s.depsMet=false;s.depsChanged=deps.length>0?[]:undefined;s.shouldUpdate=false;addListeners(deps,s);return s}function initialDepsNotMet(stream){stream.depsMet=stream.deps.every(function(s){return s.hasVal});return!stream.depsMet}function updateStream(s){if(s.depsMet!==true&&initialDepsNotMet(s)||s.end!==undefined&&s.end.val===true)return;if(inStream!==undefined){toUpdate.push(s);return}inStream=s;if(s.depsChanged)s.fnArgs[s.fnArgs.length-1]=s.depsChanged;var returnVal=s.fn.apply(s.fn,s.fnArgs);if(returnVal!==undefined){s(returnVal)}inStream=undefined;if(s.depsChanged!==undefined)s.depsChanged=[];s.shouldUpdate=false;if(flushing===false)flushUpdate()}function updateDeps(s){var i,o,list;var listeners=s.listeners;for(i=0;i<listeners.length;++i){list=listeners[i];if(list.end===s){endStream(list)}else{if(list.depsChanged!==undefined)list.depsChanged.push(s);list.shouldUpdate=true;findDeps(list)}}for(;orderNextIdx>=0;--orderNextIdx){o=order[orderNextIdx];if(o.shouldUpdate===true)updateStream(o);o.queued=false}}function findDeps(s){var i;var listeners=s.listeners;if(s.queued===false){s.queued=true;for(i=0;i<listeners.length;++i){findDeps(listeners[i])}order[++orderNextIdx]=s}}function flushUpdate(){flushing=true;while(toUpdate.length>0){var s=toUpdate.shift();if(s.vals.length>0){s.val=s.vals.shift();s.hasVal=true}if(!s.hasVal){updateStream(s)}else{updateDeps(s)}}flushing=false}function updateStreamValue(s,n){if(n!==undefined&&n!==null&&isFunction(n.then)){n.then(s);return}s.val=n;if(inStream===undefined){s.hasVal=true;flushing=true;updateDeps(s);if(toUpdate.length>0)flushUpdate();else flushing=false}else if(inStream===s){s.hasVal=true;markListeners(s,s.listeners)}else{s.vals.push(n);toUpdate.push(s)}}function markListeners(s,lists){var i,list;for(i=0;i<lists.length;++i){list=lists[i];if(list.end!==s){if(list.depsChanged!==undefined){list.depsChanged.push(s)}list.shouldUpdate=true}else{endStream(list)}}}function addListeners(deps,s){for(var i=0;i<deps.length;++i){deps[i].listeners.push(s)}}function removeListener(s,listeners){var idx=listeners.indexOf(s);listeners[idx]=listeners[listeners.length-1];listeners.length--}function detachDeps(s){for(var i=0;i<s.deps.length;++i){removeListener(s,s.deps[i].listeners)}s.deps.length=0}function endStream(s){if(s.deps!==undefined)detachDeps(s);if(s.end!==undefined)detachDeps(s.end)}function StreamTransformer(){}StreamTransformer.prototype["@@transducer/init"]=function(){};StreamTransformer.prototype["@@transducer/result"]=function(){};StreamTransformer.prototype["@@transducer/step"]=function(s,v){return v};module.exports=flyd},{"ramda/src/curryN":2}],2:[function(require,module,exports){var _arity=require("./internal/_arity");var _curry1=require("./internal/_curry1");var _curry2=require("./internal/_curry2");var _curryN=require("./internal/_curryN");module.exports=_curry2(function curryN(length,fn){if(length===1){return _curry1(fn)}return _arity(length,_curryN(length,[],fn))})},{"./internal/_arity":3,"./internal/_curry1":4,"./internal/_curry2":5,"./internal/_curryN":6}],3:[function(require,module,exports){module.exports=function _arity(n,fn){switch(n){case 0:return function(){return fn.apply(this,arguments)};case 1:return function(a0){return fn.apply(this,arguments)};case 2:return function(a0,a1){return fn.apply(this,arguments)};case 3:return function(a0,a1,a2){return fn.apply(this,arguments)};case 4:return function(a0,a1,a2,a3){return fn.apply(this,arguments)};case 5:return function(a0,a1,a2,a3,a4){return fn.apply(this,arguments)};case 6:return function(a0,a1,a2,a3,a4,a5){return fn.apply(this,arguments)};case 7:return function(a0,a1,a2,a3,a4,a5,a6){return fn.apply(this,arguments)};case 8:return function(a0,a1,a2,a3,a4,a5,a6,a7){return fn.apply(this,arguments)};case 9:return function(a0,a1,a2,a3,a4,a5,a6,a7,a8){return fn.apply(this,arguments)};case 10:return function(a0,a1,a2,a3,a4,a5,a6,a7,a8,a9){return fn.apply(this,arguments)};default:throw new Error("First argument to _arity must be a non-negative integer no greater than ten")}}},{}],4:[function(require,module,exports){var _isPlaceholder=require("./_isPlaceholder");module.exports=function _curry1(fn){return function f1(a){if(arguments.length===0||_isPlaceholder(a)){return f1}else{return fn.apply(this,arguments)}}}},{"./_isPlaceholder":7}],5:[function(require,module,exports){var _curry1=require("./_curry1");var _isPlaceholder=require("./_isPlaceholder");module.exports=function _curry2(fn){return function f2(a,b){switch(arguments.length){case 0:return f2;case 1:return _isPlaceholder(a)?f2:_curry1(function(_b){return fn(a,_b)});default:return _isPlaceholder(a)&&_isPlaceholder(b)?f2:_isPlaceholder(a)?_curry1(function(_a){return fn(_a,b)}):_isPlaceholder(b)?_curry1(function(_b){return fn(a,_b)}):fn(a,b)}}}},{"./_curry1":4,"./_isPlaceholder":7}],6:[function(require,module,exports){var _arity=require("./_arity");var _isPlaceholder=require("./_isPlaceholder");module.exports=function _curryN(length,received,fn){return function(){var combined=[];var argsIdx=0;var left=length;var combinedIdx=0;while(combinedIdx<received.length||argsIdx<arguments.length){var result;if(combinedIdx<received.length&&(!_isPlaceholder(received[combinedIdx])||argsIdx>=arguments.length)){result=received[combinedIdx]}else{result=arguments[argsIdx];argsIdx+=1}combined[combinedIdx]=result;if(!_isPlaceholder(result)){left-=1}combinedIdx+=1}return left<=0?fn.apply(this,combined):_arity(left,_curryN(length,combined,fn))}}},{"./_arity":3,"./_isPlaceholder":7}],7:[function(require,module,exports){module.exports=function _isPlaceholder(a){return a!=null&&typeof a==="object"&&a["@@functional/placeholder"]===true}},{}]},{},[1])(1)});