Comparing version 0.9.0 to 0.9.1
@@ -0,1 +1,8 @@ | ||
## 0.9.1 | ||
New Stuff: | ||
- `adopt` method for allowing reactors to become dependent without needing to be | ||
started in a reaction cycle. Should have been in 0.8.0. | ||
## 0.9.0 | ||
@@ -2,0 +9,0 @@ |
@@ -76,2 +76,4 @@ /** | ||
adopt(child: Reactor<any>): Reactor<T>; | ||
react(value: T): void; | ||
@@ -78,0 +80,0 @@ |
@@ -422,2 +422,12 @@ // UMD loader | ||
function adopt (parentBase, childBase) { | ||
orphan(childBase); | ||
if (parentBase.active) { | ||
childBase.parentReactor = parentBase; | ||
util_addToArray(parentBase.dependentReactors, childBase); | ||
} else { | ||
stop(childBase); | ||
} | ||
} | ||
function reactors_maybeReact (base) { | ||
@@ -507,2 +517,9 @@ if (base.yielding) { | ||
return this; | ||
}, | ||
adopt: function (child) { | ||
if (child._type !== types_REACTION) { | ||
throw Error("reactors can only adopt reactors"); | ||
} | ||
adopt(this._base, child._base); | ||
return this; | ||
} | ||
@@ -509,0 +526,0 @@ }); |
@@ -1,8 +0,8 @@ | ||
!function(t,n){"use strict";t&&"function"==typeof t.define&&t.define.amd?t.define(["exports"],n):n("undefined"!=typeof exports?exports:t.Derivable={})}(this,function(t){"use strict";function n(t){for(var n=1;n<arguments.length;n++)for(var e=arguments[n],r=tt(e),i=r.length;i--;){var u=r[i];t[u]=e[u]}return t}function e(t){return Object.prototype.toString.call(t).slice(8,-1)}function r(t,n){return t===n?0!==t||1/t===1/n:t!==t&&n!==n}function i(t,n){return Object.hasOwnProperty.call(n,t)}function u(t,n,a,o){var c=e(t);if(c!==e(n))return!1;if("Boolean"===c||"Number"===c||"String"===c)return"object"==typeof t?"object"==typeof n&&r(t.valueOf(),n.valueOf()):r(t,n);if(r(t,n))return!0;if("RegExp"===c)return t.source===n.source&&t.global===n.global&&t.ignoreCase===n.ignoreCase&&t.multiline===n.multiline&&t.sticky===n.sticky&&t.unicode===n.unicode;if(Object(t)===t){if("Date"===c&&t.getTime()!==n.getTime())return!1;var s=tt(t);if(s.length!==tt(n).length)return!1;for(var f=a.length-1;f>=0;){if(a[f]===t)return o[f]===n;f-=1}for(a[a.length]=t,o[o.length]=n,f=s.length-1;f>=0;){var l=s[f];if(!i(l,n)||!u(n[l],t[l],a,o))return!1;f-=1}return a.pop(),o.pop(),!0}return!1}function a(t,n){return t&&"function"==typeof t.equals?t.equals(n):u(t,n,[],[])}function o(t,n){var e=t.indexOf(n);0>e&&t.push(n)}function c(t,n){var e=t.indexOf(n);e>=0&&t.splice(e,1)}function s(t,n){return t.indexOf(n)>=0}function f(){return nt++}function l(t,n){return Array.prototype.slice.call(t,n)}function h(t){return null!==t&&void 0!==t}function p(t,n){if(t._type===vt){if(t.reacting)throw new Error("Cycle detected! Don't do this!");n.push(t)}else for(var e=t._children.length;e--;){var r=t._children[e];r._state!==ot&&(r._state=ot,p(r,n))}}function v(t){var n;switch(t._state){case it:case ut:for(n=t._children.length;n--;){var e=t._children[n];v(e),e._state!==ct&&t._children.splice(n,1)}t._state=ct;break;case ot:if(t._type===vt)t._state=ct;else{var r=[];for(n=t._parents.length;n--;){var i=t._parents[n];if(i._state!==ut){t._state=at;break}r.push([i,i._value])}t._state!==at&&(t._state=st, | ||
t._parents=r)}break;case ct:case at:case st:break;default:throw new Error("can't sweep state "+t._state)}}function _(t){var n=!1;switch(t._type){case lt:t._state=ct,n=!0;break;case ht:case pt:t._state=rt,t._value=et,n=!0;break;case vt:t._state=ct,n=!1}if(n){for(var e=t._children.length;e--;)_(t._children[e]);t._children=[]}}function d(t){return ft.push([]),t(),ft.pop()}function g(t){ft.length>0&&o(ft[ft.length-1],t)}function y(){throw yt}function w(){return{currentTxn:null}}function k(t){return null!==t.currentTxn}function m(t){return t.currentTxn}function b(t,n){n._parent=t.currentTxn,n._state=_t,t.currentTxn=n}function T(t,n){var e=t.currentTxn;if(t.currentTxn=e._parent,e._state!==_t)throw new Error("unexpected state: "+e._state);n(e)}function E(t){T(t,function(t){t._state=dt,t.onCommit&&t.onCommit()})}function x(t){T(t,function(t){t._state=gt,t.onAbort&&t.onAbort()})}function O(t,n,e){b(t,n);try{e(y)}catch(r){if(x(t),r!==yt)throw r;return}E(t)}function R(t,n){b(t,n());var e=!1;return{tick:function(){if(e)throw new Error("can't tick disposed ticker");E(t),b(t,n())},stop:function(){if(e)throw new Error("ticker already disposed");E(t)}}}function A(t,n){return{control:n,parent:t,parentReactor:null,dependentReactors:[],_state:ct,active:!1,_type:vt,uid:f(),reacting:!1,stopping:!1,yielding:!1}}function j(t){if(t.active){if(t.stopping)throw Error(wt);try{for(t.stopping=!0;t.dependentReactors.length;){var n=t.dependentReactors.pop();j(n)}}finally{c(t.parent._children,t),t.parentReactor&&q(t),t.active=!1,t.stopping=!1}t.control.onStop&&t.control.onStop()}}function V(t){if(!t.active){o(t.parent._children,t),t.active=!0,t.parent._get();var n=kt.length;n>0&&(t.parentReactor=kt[n-1],o(t.parentReactor.dependentReactors,t)),t.control.onStart&&t.control.onStart()}}function q(t){t.parentReactor&&(c(t.parentReactor.dependentReactors,t),t.parentReactor=null)}function C(t){if(t.yielding)throw Error(wt);if(t.active&&t._state===ot){if(null!==t.parentReactor)try{t.yielding=!0,C(t.parentReactor)}finally{t.yielding=!1}if(t.active){var n=t.parent,e=n._state; | ||
if((e===ot||e===at||e===st||e===rt)&&n._get(),e=n._state,e===ut)t._state=ct;else{if(e!==it)throw new Error("invalid parent state: "+e);D(t)}}}}function D(t){if(!t.control.react)throw new Error("No reactor function available.");t._state=ct;try{t.reacting=!0,kt.push(t),t.control.react(t.parent._get())}finally{kt.pop(),t.reacting=!1}}function S(){this._type=vt}function N(t,n){if(t._base)throw new Error("This reactor has already been initialized");return t._base=A(n,t),t}function G(t){this._type=vt,this.react=t}function I(t){return n(new S,t)}function z(t,n){var e={derive:function(n,e,r,i,u){var a=this;switch(arguments.length){case 0:return a;case 1:return t.derivation(function(){return n(a.get())});case 2:return t.derivation(function(){return n(a.get(),t.unpack(e))});case 3:return t.derivation(function(){return n(a.get(),t.unpack(e),t.unpack(r))});case 4:return t.derivation(function(){return n(a.get(),t.unpack(e),t.unpack(r),t.unpack(i))});case 5:return t.derivation(function(){return n(a.get(),t.unpack(e),t.unpack(r),t.unpack(i),t.unpack(u))});default:var o=[a].concat(l(arguments,1));return t.derivation(function(){return n.apply(null,o.map(t.unpack))})}},reactor:function(t){if("function"==typeof t)return N(new G(t),this);if(t instanceof S)return N(t,this);if(t&&t.react)return N(I(t),this);throw new Error("Unrecognized type for reactor "+t)},react:function(t){return this.reactor(t).start().force()},get:function(){return g(this),this._get()},is:function(e){return t.lift(n.equals)(this,e)},and:function(n){return this.derive(function(e){return e&&t.unpack(n)})},or:function(n){return this.derive(function(e){return e||t.unpack(n)})},then:function(n,e){return this.derive(function(r){return t.unpack(r?n:e)})},mThen:function(n,e){return this.derive(function(r){return t.unpack(h(r)?n:e)})},mOr:function(t){return this.mThen(this,t)},mDerive:function(){return this.mThen(this.derive.apply(this,arguments))},mAnd:function(t){return this.mThen(t,this)},not:function(){return this.derive(function(t){return!t})}};return e["switch"]=function(){ | ||
var e=arguments;return this.derive(function(r){var i;for(i=0;e.length-1>i;i+=2)if(n.equals(r,t.unpack(e[i])))return t.unpack(e[i+1]);return i===e.length-1?t.unpack(e[i]):void 0})},e}function Q(t,n){return{_clone:function(){return t.derivation(this._deriver)},_forceGet:function(){var t,e=this,r=d(function(){var t=e._deriver();e._state=n.equals(t,e._value)?ut:it,e._value=t});for(t=this._parents.length;t--;){var i=this._parents[t];s(r,i)||c(i._children,this)}for(this._parents=r,t=r.length;t--;)o(r[t]._children,this)},_get:function(){var t,e;t:switch(this._state){case rt:case at:this._forceGet();break;case ot:for(t=0;this._parents.length>t;t++){e=this._parents[t];var r=e._state;if((r===ot||r===at||r===st)&&e._get(),r=e._state,r===it){this._forceGet();break t}if(r!==ct&&r!==ut)throw new Error("invalid parent mode: "+r)}this._state=ut;break;case st:var i=[];for(t=0;this._parents.length>t;t++){var u=this._parents[t],a=u[1];if(e=u[0],!n.equals(e._get(),a)){this._parents=[],this._forceGet();break t}i.push(e)}for(t=i.length;t--;)o(i[t]._children,this);this._parents=i,this._state=ut}return this._value}}}function L(t,n){return t._children=[],t._parents=[],t._deriver=n,t._state=rt,t._type=ht,t._value=et,t}function B(t,n){return{swap:function(t){var n=l(arguments,0);return n[0]=this.get(),this.set(t.apply(null,n))},lens:function(n){return t.lens(this,n)}}}function F(t,n){return{_clone:function(){return t.lens(this._parent,{get:this._getter,set:this._setter})},set:function(t){return this._parent.set(this._setter(this._parent._get(),t)),this}}}function M(t,n,e){return t._getter=e.get,t._setter=e.set,t._parent=n,t._type=pt,t}function P(t){for(var n=t.length;n--;)C(t[n])}function U(){this.inTxnValues={},this.reactorQueue=[]}function W(t,n){var e=t.inTxnValues[n._uid];return e?e[1]:n._value}function H(t,n,e){t.inTxnValues[n._uid]=[n,e],p(n,t.reactorQueue)}function J(t,n){return{_clone:function(){return t.atom(this._value)},withValidator:function(t){if(null===t)return this._clone();if("function"==typeof t){var n=this._clone(),e=this._validator; | ||
return n._validator=e?function(n){return t(n)&&e(n)}:t,n}throw new Error(".withValidator expects function or null")},validate:function(){this._validate(this.get())},_validate:function(t){var n=this._validator&&this._validator(t);if(this._validator&&n!==!0)throw new Error("Failed validation with value: '"+t+"'. Validator returned '"+n+"' ")},set:function(t){if(this._validate(t),!n.equals(t,this._value))if(this._state=it,k(mt))H(m(mt),this,t);else{this._value=t;var e=[];p(this,e),P(e),v(this)}return this},_get:function(){return k(mt)?W(m(mt),this):this._value}}}function K(t,n){return t._uid=f(),t._children=[],t._state=ct,t._value=n,t._type=lt,t}function X(t){O(mt,new U,t)}function Y(t){return function(){var n,e=l(arguments,0),r=this;return X(function(){n=t.apply(r,e)}),n}}function Z(){Tt?Tt.refCount++:(Tt=R(mt,function(){return new U}),Tt.refCount=1);var t=!1;return{tick:function(){if(t)throw new Error("tyring to use ticker after release");Tt.tick()},release:function(){if(t)throw new Error("ticker already released");0===--Tt.refCount&&(Tt.stop(),Tt=null),t=!0}}}function $(t){function e(t){var n=l(arguments,1);return i.derivation(function(){for(var e="",r=0;t.length>r;r++)e+=t[r],n.length>r&&(e+=i.unpack(n[r]));return e})}function r(t){if(i.isDerivable(t))return t.get();if(t instanceof Array)return t.map(r);if(t.constructor===Object){for(var n={},e=tt(t),u=e.length;u--;){var a=e[u];n[a]=r(t[a])}return n}return t}t=n({},Et,t||{});var i={transact:X,defaultEquals:a,transaction:Y,ticker:Z,Reactor:S,isAtom:function(t){return t&&(t._type===lt||t._type===pt)},isDerivable:function(t){return t&&(t._type===lt||t._type===pt||t._type===ht)},isDerivation:function(t){return t&&(t._type===ht||t._type===pt)},isLensed:function(t){return t&&t._type===pt},isReactor:function(t){return t&&t._type===vt}},u=z(i,t),o=B(i,t),c=n({},o,u,J(i,t)),s=n({},u,Q(i,t)),f=n({},o,s,F(i,t));return i.atom=function(t){return K(Object.create(c),t)},i.swap=function(t,n){var e=l(arguments,1);return e[0]=t.get(),t.set(n.apply(null,e))},i.derivation=function(t){ | ||
return L(Object.create(s),t)},i.derive=function(t){if(t instanceof Array)return e.apply(null,arguments);if(arguments.length>0)return u.derive.apply(t,l(arguments,1));throw new Error("Wrong arity for derive. Expecting 1+ args")},i.mDerive=function(t){return u.mDerive.apply(t,l(arguments,1))},i.lens=function(t,n){var e=Object.create(f);return M(L(e,function(){return n.get(t.get())}),t,n)},i.unpack=function(t){return i.isDerivable(t)?t.get():t},i.lift=function(t){return function(){var n=arguments,e=this;return i.derivation(function(){return t.apply(e,Array.prototype.map.call(n,i.unpack))})}},i.set=function(t,n){return t.set(n)},i.get=function(t){return t.get()},i.struct=function(t){if(t.constructor===Object||t instanceof Array)return i.derivation(function(){return r(t)});throw new Error("`struct` expects plain Object or Array")},i.destruct=function(t){for(var n=arguments,e=[],r=1;n.length>r;r++)e.push(i.lookup(t,n[r]));return e},i.lookup=function(t,n){return i.derivation(function(){return i.unpack(t)[i.unpack(n)]})},i.ifThenElse=function(t,n,e){return t.then(n,e)},i.ifThenElse=function(t,n,e){return i.derivation(function(){return i.unpack(i.unpack(t)?n:e)})},i.mIfThenElse=function(t,n,e){return i.derivation(function(){var r=i.unpack(t);return i.unpack(h(r)?n:e)})},i.or=function(){var t=arguments;return i.derivation(function(){for(var n,e=0;t.length>e&&!(n=i.unpack(t[e]));e++);return n})},i.mOr=function(){var t=arguments;return i.derivation(function(){for(var n,e=0;t.length>e&&(n=i.unpack(t[e]),!h(n));e++);return n})},i.and=function(){var t=arguments;return i.derivation(function(){for(var n,e=0;t.length>e&&(n=i.unpack(t[e]),n);e++);return n})},i.mAnd=function(){var t=arguments;return i.derivation(function(){for(var n,e=0;t.length>e&&(n=i.unpack(t[e]),h(n));e++);return n})},i.not=function(t){return t.derive(function(t){return!t})},i.switchCase=function(t){return u["switch"].apply(t,l(arguments,1))},i}var tt=Object.keys,nt=0,et=Object.freeze({equals:function(){return!1}}),rt=0,it=1,ut=2,at=3,ot=4,ct=5,st=6,ft=[],lt="ATOM",ht="DERIVATION",pt="LENS",vt="REACTION",_t=0,dt=1,gt=3,yt={},wt="Cyclical Reactor Dependency! Not allowed!",kt=[]; | ||
n(S.prototype,{start:function(){return V(this._base),this},stop:function(){return j(this._base),this},force:function(){return D(this._base),this},isActive:function(){return this._base.active},orphan:function(){return q(this._base),this}}),n(G.prototype,S.prototype);var mt=w(),bt={push:function(){}};n(U.prototype,{onCommit:function(){var t,n,e=tt(this.inTxnValues);if(k(mt))for(t=e.length;t--;)n=this.inTxnValues[e[t]],n[0].set(n[1]);else{for(t=e.length;t--;)n=this.inTxnValues[e[t]],n[0]._value=n[1],p(n[0],bt);for(P(this.reactorQueue),t=e.length;t--;)v(this.inTxnValues[e[t]][0])}},onAbort:function(){if(!k(mt))for(var t=tt(this.inTxnValues),n=t.length;n--;)_(this.inTxnValues[t[n]][0])}});var Tt=null,Et={equals:a};n(t,$()),t.withEquality=function(t){return $({equals:t})},t["default"]=t}); | ||
!function(t,n){"use strict";t&&"function"==typeof t.define&&t.define.amd?t.define(["exports"],n):n("undefined"!=typeof exports?exports:t.Derivable={})}(this,function(t){"use strict";function n(t){for(var n=1;n<arguments.length;n++)for(var e=arguments[n],r=nt(e),i=r.length;i--;){var u=r[i];t[u]=e[u]}return t}function e(t){return Object.prototype.toString.call(t).slice(8,-1)}function r(t,n){return t===n?0!==t||1/t===1/n:t!==t&&n!==n}function i(t,n){return Object.hasOwnProperty.call(n,t)}function u(t,n,a,o){var c=e(t);if(c!==e(n))return!1;if("Boolean"===c||"Number"===c||"String"===c)return"object"==typeof t?"object"==typeof n&&r(t.valueOf(),n.valueOf()):r(t,n);if(r(t,n))return!0;if("RegExp"===c)return t.source===n.source&&t.global===n.global&&t.ignoreCase===n.ignoreCase&&t.multiline===n.multiline&&t.sticky===n.sticky&&t.unicode===n.unicode;if(Object(t)===t){if("Date"===c&&t.getTime()!==n.getTime())return!1;var s=nt(t);if(s.length!==nt(n).length)return!1;for(var f=a.length-1;f>=0;){if(a[f]===t)return o[f]===n;f-=1}for(a[a.length]=t,o[o.length]=n,f=s.length-1;f>=0;){var l=s[f];if(!i(l,n)||!u(n[l],t[l],a,o))return!1;f-=1}return a.pop(),o.pop(),!0}return!1}function a(t,n){return t&&"function"==typeof t.equals?t.equals(n):u(t,n,[],[])}function o(t,n){var e=t.indexOf(n);0>e&&t.push(n)}function c(t,n){var e=t.indexOf(n);e>=0&&t.splice(e,1)}function s(t,n){return t.indexOf(n)>=0}function f(){return et++}function l(t,n){return Array.prototype.slice.call(t,n)}function h(t){return null!==t&&void 0!==t}function p(t,n){if(t._type===_t){if(t.reacting)throw new Error("Cycle detected! Don't do this!");n.push(t)}else for(var e=t._children.length;e--;){var r=t._children[e];r._state!==ct&&(r._state=ct,p(r,n))}}function v(t){var n;switch(t._state){case ut:case at:for(n=t._children.length;n--;){var e=t._children[n];v(e),e._state!==st&&t._children.splice(n,1)}t._state=st;break;case ct:if(t._type===_t)t._state=st;else{var r=[];for(n=t._parents.length;n--;){var i=t._parents[n];if(i._state!==at){t._state=ot;break}r.push([i,i._value])}t._state!==ot&&(t._state=ft, | ||
t._parents=r)}break;case st:case ot:case ft:break;default:throw new Error("can't sweep state "+t._state)}}function _(t){var n=!1;switch(t._type){case ht:t._state=st,n=!0;break;case pt:case vt:t._state=it,t._value=rt,n=!0;break;case _t:t._state=st,n=!1}if(n){for(var e=t._children.length;e--;)_(t._children[e]);t._children=[]}}function d(t){return lt.push([]),t(),lt.pop()}function g(t){lt.length>0&&o(lt[lt.length-1],t)}function y(){throw wt}function w(){return{currentTxn:null}}function k(t){return null!==t.currentTxn}function m(t){return t.currentTxn}function b(t,n){n._parent=t.currentTxn,n._state=dt,t.currentTxn=n}function E(t,n){var e=t.currentTxn;if(t.currentTxn=e._parent,e._state!==dt)throw new Error("unexpected state: "+e._state);n(e)}function T(t){E(t,function(t){t._state=gt,t.onCommit&&t.onCommit()})}function x(t){E(t,function(t){t._state=yt,t.onAbort&&t.onAbort()})}function O(t,n,e){b(t,n);try{e(y)}catch(r){if(x(t),r!==wt)throw r;return}T(t)}function R(t,n){b(t,n());var e=!1;return{tick:function(){if(e)throw new Error("can't tick disposed ticker");T(t),b(t,n())},stop:function(){if(e)throw new Error("ticker already disposed");T(t)}}}function A(t,n){return{control:n,parent:t,parentReactor:null,dependentReactors:[],_state:st,active:!1,_type:_t,uid:f(),reacting:!1,stopping:!1,yielding:!1}}function j(t){if(t.active){if(t.stopping)throw Error(kt);try{for(t.stopping=!0;t.dependentReactors.length;){var n=t.dependentReactors.pop();j(n)}}finally{c(t.parent._children,t),t.parentReactor&&q(t),t.active=!1,t.stopping=!1}t.control.onStop&&t.control.onStop()}}function V(t){if(!t.active){o(t.parent._children,t),t.active=!0,t.parent._get();var n=mt.length;n>0&&(t.parentReactor=mt[n-1],o(t.parentReactor.dependentReactors,t)),t.control.onStart&&t.control.onStart()}}function q(t){t.parentReactor&&(c(t.parentReactor.dependentReactors,t),t.parentReactor=null)}function C(t,n){q(n),t.active?(n.parentReactor=t,o(t.dependentReactors,n)):j(n)}function D(t){if(t.yielding)throw Error(kt);if(t.active&&t._state===ct){if(null!==t.parentReactor)try{ | ||
t.yielding=!0,D(t.parentReactor)}finally{t.yielding=!1}if(t.active){var n=t.parent,e=n._state;if((e===ct||e===ot||e===ft||e===it)&&n._get(),e=n._state,e===at)t._state=st;else{if(e!==ut)throw new Error("invalid parent state: "+e);S(t)}}}}function S(t){if(!t.control.react)throw new Error("No reactor function available.");t._state=st;try{t.reacting=!0,mt.push(t),t.control.react(t.parent._get())}finally{mt.pop(),t.reacting=!1}}function N(){this._type=_t}function G(t,n){if(t._base)throw new Error("This reactor has already been initialized");return t._base=A(n,t),t}function I(t){this._type=_t,this.react=t}function z(t){return n(new N,t)}function Q(t,n){var e={derive:function(n,e,r,i,u){var a=this;switch(arguments.length){case 0:return a;case 1:return t.derivation(function(){return n(a.get())});case 2:return t.derivation(function(){return n(a.get(),t.unpack(e))});case 3:return t.derivation(function(){return n(a.get(),t.unpack(e),t.unpack(r))});case 4:return t.derivation(function(){return n(a.get(),t.unpack(e),t.unpack(r),t.unpack(i))});case 5:return t.derivation(function(){return n(a.get(),t.unpack(e),t.unpack(r),t.unpack(i),t.unpack(u))});default:var o=[a].concat(l(arguments,1));return t.derivation(function(){return n.apply(null,o.map(t.unpack))})}},reactor:function(t){if("function"==typeof t)return G(new I(t),this);if(t instanceof N)return G(t,this);if(t&&t.react)return G(z(t),this);throw new Error("Unrecognized type for reactor "+t)},react:function(t){return this.reactor(t).start().force()},get:function(){return g(this),this._get()},is:function(e){return t.lift(n.equals)(this,e)},and:function(n){return this.derive(function(e){return e&&t.unpack(n)})},or:function(n){return this.derive(function(e){return e||t.unpack(n)})},then:function(n,e){return this.derive(function(r){return t.unpack(r?n:e)})},mThen:function(n,e){return this.derive(function(r){return t.unpack(h(r)?n:e)})},mOr:function(t){return this.mThen(this,t)},mDerive:function(){return this.mThen(this.derive.apply(this,arguments))},mAnd:function(t){return this.mThen(t,this); | ||
},not:function(){return this.derive(function(t){return!t})}};return e["switch"]=function(){var e=arguments;return this.derive(function(r){var i;for(i=0;e.length-1>i;i+=2)if(n.equals(r,t.unpack(e[i])))return t.unpack(e[i+1]);return i===e.length-1?t.unpack(e[i]):void 0})},e}function L(t,n){return{_clone:function(){return t.derivation(this._deriver)},_forceGet:function(){var t,e=this,r=d(function(){var t=e._deriver();e._state=n.equals(t,e._value)?at:ut,e._value=t});for(t=this._parents.length;t--;){var i=this._parents[t];s(r,i)||c(i._children,this)}for(this._parents=r,t=r.length;t--;)o(r[t]._children,this)},_get:function(){var t,e;t:switch(this._state){case it:case ot:this._forceGet();break;case ct:for(t=0;this._parents.length>t;t++){e=this._parents[t];var r=e._state;if((r===ct||r===ot||r===ft)&&e._get(),r=e._state,r===ut){this._forceGet();break t}if(r!==st&&r!==at)throw new Error("invalid parent mode: "+r)}this._state=at;break;case ft:var i=[];for(t=0;this._parents.length>t;t++){var u=this._parents[t],a=u[1];if(e=u[0],!n.equals(e._get(),a)){this._parents=[],this._forceGet();break t}i.push(e)}for(t=i.length;t--;)o(i[t]._children,this);this._parents=i,this._state=at}return this._value}}}function B(t,n){return t._children=[],t._parents=[],t._deriver=n,t._state=it,t._type=pt,t._value=rt,t}function F(t,n){return{swap:function(t){var n=l(arguments,0);return n[0]=this.get(),this.set(t.apply(null,n))},lens:function(n){return t.lens(this,n)}}}function M(t,n){return{_clone:function(){return t.lens(this._parent,{get:this._getter,set:this._setter})},set:function(t){return this._parent.set(this._setter(this._parent._get(),t)),this}}}function P(t,n,e){return t._getter=e.get,t._setter=e.set,t._parent=n,t._type=vt,t}function U(t){for(var n=t.length;n--;)D(t[n])}function W(){this.inTxnValues={},this.reactorQueue=[]}function H(t,n){var e=t.inTxnValues[n._uid];return e?e[1]:n._value}function J(t,n,e){t.inTxnValues[n._uid]=[n,e],p(n,t.reactorQueue)}function K(t,n){return{_clone:function(){return t.atom(this._value)},withValidator:function(t){ | ||
if(null===t)return this._clone();if("function"==typeof t){var n=this._clone(),e=this._validator;return n._validator=e?function(n){return t(n)&&e(n)}:t,n}throw new Error(".withValidator expects function or null")},validate:function(){this._validate(this.get())},_validate:function(t){var n=this._validator&&this._validator(t);if(this._validator&&n!==!0)throw new Error("Failed validation with value: '"+t+"'. Validator returned '"+n+"' ")},set:function(t){if(this._validate(t),!n.equals(t,this._value))if(this._state=ut,k(bt))J(m(bt),this,t);else{this._value=t;var e=[];p(this,e),U(e),v(this)}return this},_get:function(){return k(bt)?H(m(bt),this):this._value}}}function X(t,n){return t._uid=f(),t._children=[],t._state=st,t._value=n,t._type=ht,t}function Y(t){O(bt,new W,t)}function Z(t){return function(){var n,e=l(arguments,0),r=this;return Y(function(){n=t.apply(r,e)}),n}}function $(){Tt?Tt.refCount++:(Tt=R(bt,function(){return new W}),Tt.refCount=1);var t=!1;return{tick:function(){if(t)throw new Error("tyring to use ticker after release");Tt.tick()},release:function(){if(t)throw new Error("ticker already released");0===--Tt.refCount&&(Tt.stop(),Tt=null),t=!0}}}function tt(t){function e(t){var n=l(arguments,1);return i.derivation(function(){for(var e="",r=0;t.length>r;r++)e+=t[r],n.length>r&&(e+=i.unpack(n[r]));return e})}function r(t){if(i.isDerivable(t))return t.get();if(t instanceof Array)return t.map(r);if(t.constructor===Object){for(var n={},e=nt(t),u=e.length;u--;){var a=e[u];n[a]=r(t[a])}return n}return t}t=n({},xt,t||{});var i={transact:Y,defaultEquals:a,transaction:Z,ticker:$,Reactor:N,isAtom:function(t){return t&&(t._type===ht||t._type===vt)},isDerivable:function(t){return t&&(t._type===ht||t._type===vt||t._type===pt)},isDerivation:function(t){return t&&(t._type===pt||t._type===vt)},isLensed:function(t){return t&&t._type===vt},isReactor:function(t){return t&&t._type===_t}},u=Q(i,t),o=F(i,t),c=n({},o,u,K(i,t)),s=n({},u,L(i,t)),f=n({},o,s,M(i,t));return i.atom=function(t){return X(Object.create(c),t)},i.swap=function(t,n){ | ||
var e=l(arguments,1);return e[0]=t.get(),t.set(n.apply(null,e))},i.derivation=function(t){return B(Object.create(s),t)},i.derive=function(t){if(t instanceof Array)return e.apply(null,arguments);if(arguments.length>0)return u.derive.apply(t,l(arguments,1));throw new Error("Wrong arity for derive. Expecting 1+ args")},i.mDerive=function(t){return u.mDerive.apply(t,l(arguments,1))},i.lens=function(t,n){var e=Object.create(f);return P(B(e,function(){return n.get(t.get())}),t,n)},i.unpack=function(t){return i.isDerivable(t)?t.get():t},i.lift=function(t){return function(){var n=arguments,e=this;return i.derivation(function(){return t.apply(e,Array.prototype.map.call(n,i.unpack))})}},i.set=function(t,n){return t.set(n)},i.get=function(t){return t.get()},i.struct=function(t){if(t.constructor===Object||t instanceof Array)return i.derivation(function(){return r(t)});throw new Error("`struct` expects plain Object or Array")},i.destruct=function(t){for(var n=arguments,e=[],r=1;n.length>r;r++)e.push(i.lookup(t,n[r]));return e},i.lookup=function(t,n){return i.derivation(function(){return i.unpack(t)[i.unpack(n)]})},i.ifThenElse=function(t,n,e){return t.then(n,e)},i.ifThenElse=function(t,n,e){return i.derivation(function(){return i.unpack(i.unpack(t)?n:e)})},i.mIfThenElse=function(t,n,e){return i.derivation(function(){var r=i.unpack(t);return i.unpack(h(r)?n:e)})},i.or=function(){var t=arguments;return i.derivation(function(){for(var n,e=0;t.length>e&&!(n=i.unpack(t[e]));e++);return n})},i.mOr=function(){var t=arguments;return i.derivation(function(){for(var n,e=0;t.length>e&&(n=i.unpack(t[e]),!h(n));e++);return n})},i.and=function(){var t=arguments;return i.derivation(function(){for(var n,e=0;t.length>e&&(n=i.unpack(t[e]),n);e++);return n})},i.mAnd=function(){var t=arguments;return i.derivation(function(){for(var n,e=0;t.length>e&&(n=i.unpack(t[e]),h(n));e++);return n})},i.not=function(t){return t.derive(function(t){return!t})},i.switchCase=function(t){return u["switch"].apply(t,l(arguments,1))},i}var nt=Object.keys,et=0,rt=Object.freeze({ | ||
equals:function(){return!1}}),it=0,ut=1,at=2,ot=3,ct=4,st=5,ft=6,lt=[],ht="ATOM",pt="DERIVATION",vt="LENS",_t="REACTION",dt=0,gt=1,yt=3,wt={},kt="Cyclical Reactor Dependency! Not allowed!",mt=[];n(N.prototype,{start:function(){return V(this._base),this},stop:function(){return j(this._base),this},force:function(){return S(this._base),this},isActive:function(){return this._base.active},orphan:function(){return q(this._base),this},adopt:function(t){if(t._type!==_t)throw Error("reactors can only adopt reactors");return C(this._base,t._base),this}}),n(I.prototype,N.prototype);var bt=w(),Et={push:function(){}};n(W.prototype,{onCommit:function(){var t,n,e=nt(this.inTxnValues);if(k(bt))for(t=e.length;t--;)n=this.inTxnValues[e[t]],n[0].set(n[1]);else{for(t=e.length;t--;)n=this.inTxnValues[e[t]],n[0]._value=n[1],p(n[0],Et);for(U(this.reactorQueue),t=e.length;t--;)v(this.inTxnValues[e[t]][0])}},onAbort:function(){if(!k(bt))for(var t=nt(this.inTxnValues),n=t.length;n--;)_(this.inTxnValues[t[n]][0])}});var Tt=null,xt={equals:a};n(t,tt()),t.withEquality=function(t){return tt({equals:t})},t["default"]=t}); | ||
//# sourceMappingURL=derivable.min.js.map |
{ | ||
"name": "derivable", | ||
"version": "0.9.0", | ||
"version": "0.9.1", | ||
"description": "Functional Reactive State for JavaScript & TypeScript", | ||
@@ -5,0 +5,0 @@ "author": "David Sheldrick", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
273137
1260