Comparing version 0.9.3 to 0.10.0
@@ -0,1 +1,11 @@ | ||
## 0.10.0 | ||
#### Debug Mode | ||
Due to inversion of control, the stack traces you get when your derivations or reactors throw errors can be totally unhelpful. This pull request solves that issue by enabling JS Errors to be created (but not thrown) when derivations are instantiated in order to capture the stack trace at the point of instantiation. Then if a derivation throws an error, its instantiation stack trace is logged so we can easily identify exactly which derivation threw the error, and which derivations the error propagated up through. | ||
Creating errors is quite expensive, and can cause noticeable slowdown if there are enough derivations being instantiated, so this mode can be toggled on/off for dev/prod respectively. It is off by default. | ||
See the top-level setDebugMode function. | ||
## 0.9.3 | ||
@@ -2,0 +12,0 @@ |
@@ -152,2 +152,4 @@ /** | ||
function setDebugMode(debugMode: boolean): void; | ||
export interface Ticker { | ||
@@ -154,0 +156,0 @@ |
@@ -76,2 +76,7 @@ // UMD loader | ||
var util_DEBUG_MODE = false; | ||
function util_setDebugMode(val) { | ||
util_DEBUG_MODE = !!val; | ||
} | ||
// node modes | ||
@@ -190,5 +195,10 @@ var gc_NEW = 0, | ||
function parents_capturingParents(f) { | ||
var i = parentsStack.length; | ||
parentsStack.push([]); | ||
f(); | ||
return parentsStack.pop(); | ||
try { | ||
f(); | ||
return parentsStack[i]; | ||
} finally { | ||
parentsStack.pop(); | ||
} | ||
} | ||
@@ -290,3 +300,3 @@ | ||
function reactorBase (parent, control) { | ||
return { | ||
var base = { | ||
control: control, // the actual object the user gets | ||
@@ -303,3 +313,7 @@ parent: parent, // the parent derivable | ||
yielding: false, // whether or not letting parentReactor react first | ||
}; | ||
if (util_DEBUG_MODE) { | ||
base.stack = Error().stack; | ||
} | ||
return base; | ||
} | ||
@@ -408,3 +422,12 @@ var cycleMsg = "Cyclical Reactor Dependency! Not allowed!"; | ||
parentReactorStack.push(base); | ||
base.control.react(base.parent._get()); | ||
if (!util_DEBUG_MODE) { | ||
base.control.react(base.parent._get()); | ||
} else { | ||
try { | ||
base.control.react(base.parent._get()); | ||
} catch (e) { | ||
console.error(base.stack); | ||
throw e; | ||
} | ||
} | ||
} finally { | ||
@@ -609,3 +632,13 @@ parentReactorStack.pop(); | ||
var newParents = parents_capturingParents(function () { | ||
var newState = that._deriver(); | ||
var newState; | ||
if (!util_DEBUG_MODE) { | ||
newState = that._deriver(); | ||
} else { | ||
try { | ||
newState = that._deriver(); | ||
} catch (e) { | ||
console.error(that._stack); | ||
throw e; | ||
} | ||
} | ||
that._state = opts.equals(newState, that._value) ? gc_UNCHANGED : gc_CHANGED; | ||
@@ -694,2 +727,7 @@ that._value = newState; | ||
obj._value = util_unique; | ||
if (util_DEBUG_MODE) { | ||
obj._stack = Error().stack; | ||
} | ||
return obj; | ||
@@ -928,2 +966,3 @@ } | ||
defaultEquals: util_equals, | ||
setDebugMode: util_setDebugMode, | ||
transaction: atom_transaction, | ||
@@ -930,0 +969,0 @@ ticker: atom_ticker, |
@@ -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=Z(e),i=r.length;i--;){var a=r[i];t[a]=e[a]}return t}function e(t,n){return t===n?0!==t||1/t===1/n:t!==t&&n!==n}function r(t,n){return e(t,n)||t&&"function"==typeof t.equals&&t.equals(n)}function i(t,n){var e=t.indexOf(n);0>e&&t.push(n)}function a(t,n){var e=t.indexOf(n);e>=0&&t.splice(e,1)}function u(t,n){return t.indexOf(n)>=0}function o(){return $++}function c(t,n){return Array.prototype.slice.call(t,n)}function s(t){return null!==t&&void 0!==t}function f(t,n){if(t._type===ht){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!==at&&(r._state=at,f(r,n))}}function l(t){var n;switch(t._state){case et:case rt:for(n=t._children.length;n--;){var e=t._children[n];l(e),e._state!==ut&&t._children.splice(n,1)}t._state=ut;break;case at:if(t._type===ht)t._state=ut;else{var r=[];for(n=t._parents.length;n--;){var i=t._parents[n];if(i._state!==rt){t._state=it;break}r.push([i,i._value])}t._state!==it&&(t._state=ot,t._parents=r)}break;case ut:case it:case ot:break;default:throw new Error("can't sweep state "+t._state)}}function h(t){var n=!1;switch(t._type){case st:t._state=ut,n=!0;break;case ft:case lt:t._state=nt,t._value=tt,n=!0;break;case ht:t._state=ut,n=!1}if(n){for(var e=t._children.length;e--;)h(t._children[e]);t._children=[]}}function p(t){return ct.push([]),t(),ct.pop()}function _(t){ct.length>0&&i(ct[ct.length-1],t)}function v(){throw dt}function d(){return{currentTxn:null}}function g(t){return null!==t.currentTxn}function y(t){return t.currentTxn}function w(t,n){n._parent=t.currentTxn,n._state=pt,t.currentTxn=n}function k(t,n){var e=t.currentTxn;if(t.currentTxn=e._parent,e._state!==pt)throw new Error("unexpected state: "+e._state);n(e)}function m(t){k(t,function(t){t._state=_t, | ||
t.onCommit&&t.onCommit()})}function b(t){k(t,function(t){t._state=vt,t.onAbort&&t.onAbort()})}function E(t,n,e){w(t,n);try{e(v)}catch(r){if(b(t),r!==dt)throw r;return}m(t)}function T(t,n){w(t,n());var e=!1;return{tick:function(){if(e)throw new Error("can't tick disposed ticker");m(t),w(t,n())},stop:function(){if(e)throw new Error("ticker already disposed");m(t)}}}function x(t,n){return{control:n,parent:t,parentReactor:null,dependentReactors:[],_state:ut,active:!1,_type:ht,uid:o(),reacting:!1,stopping:!1,yielding:!1}}function R(t){if(t.active){if(t.stopping)throw Error(gt);try{for(t.stopping=!0;t.dependentReactors.length;){var n=t.dependentReactors.pop();R(n)}}finally{a(t.parent._children,t),t.parentReactor&&O(t),t.active=!1,t.stopping=!1}t.control.onStop&&t.control.onStop()}}function A(t){if(!t.active){i(t.parent._children,t),t.active=!0,t.parent._get();var n=yt.length;n>0&&(t.parentReactor=yt[n-1],i(t.parentReactor.dependentReactors,t)),t.control.onStart&&t.control.onStart()}}function O(t){t.parentReactor&&(a(t.parentReactor.dependentReactors,t),t.parentReactor=null)}function V(t,n){O(n),t.active?(n.parentReactor=t,i(t.dependentReactors,n)):R(n)}function q(t){if(t.yielding)throw Error(gt);if(t.active&&t._state===at){if(null!==t.parentReactor)try{t.yielding=!0,q(t.parentReactor)}finally{t.yielding=!1}if(t.active){var n=t.parent,e=n._state;if((e===at||e===it||e===ot||e===nt)&&n._get(),e=n._state,e===rt)t._state=ut;else{if(e!==et)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=ut;try{t.reacting=!0,yt.push(t),t.control.react(t.parent._get())}finally{yt.pop(),t.reacting=!1}}function C(){this._type=ht}function j(t,n){if(t._base)throw new Error("This reactor has already been initialized");return t._base=x(n,t),t}function N(t){this._type=ht,this.react=t}function S(t){return n(new C,t)}function G(t,n){var e={derive:function(n,e,r,i,a){var u=this;switch(arguments.length){case 0:return u;case 1:return t.derivation(function(){return n(u.get()); | ||
});case 2:return t.derivation(function(){return n(u.get(),t.unpack(e))});case 3:return t.derivation(function(){return n(u.get(),t.unpack(e),t.unpack(r))});case 4:return t.derivation(function(){return n(u.get(),t.unpack(e),t.unpack(r),t.unpack(i))});case 5:return t.derivation(function(){return n(u.get(),t.unpack(e),t.unpack(r),t.unpack(i),t.unpack(a))});default:var o=[u].concat(c(arguments,1));return t.derivation(function(){return n.apply(null,o.map(t.unpack))})}},reactor:function(t){if("function"==typeof t)return j(new N(t),this);if(t instanceof C)return j(t,this);if(t&&t.react)return j(S(t),this);throw new Error("Unrecognized type for reactor "+t)},react:function(t){return this.reactor(t).start().force()},get:function(){return _(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(s(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 I(t,n){return{_clone:function(){return t.derivation(this._deriver)},_forceGet:function(){var t,e=this,r=p(function(){var t=e._deriver();e._state=n.equals(t,e._value)?rt:et,e._value=t});for(t=this._parents.length;t--;){var o=this._parents[t];u(r,o)||a(o._children,this)}for(this._parents=r,t=r.length;t--;)i(r[t]._children,this)},_get:function(){var t,e;t:switch(this._state){case nt:case it:this._forceGet();break;case at:for(t=0;this._parents.length>t;t++){e=this._parents[t];var r=e._state;if((r===at||r===it||r===ot)&&e._get(), | ||
r=e._state,r===et){this._forceGet();break t}if(r!==ut&&r!==rt)throw new Error("invalid parent mode: "+r)}this._state=rt;break;case ot:var a=[];for(t=0;this._parents.length>t;t++){var u=this._parents[t],o=u[1];if(e=u[0],!n.equals(e._get(),o)){this._parents=[],this._forceGet();break t}a.push(e)}for(t=a.length;t--;)i(a[t]._children,this);this._parents=a,this._state=rt}return this._value}}}function z(t,n){return t._children=[],t._parents=[],t._deriver=n,t._state=nt,t._type=ft,t._value=tt,t}function Q(t,n){return{swap:function(t){var n=c(arguments,0);return n[0]=this.get(),this.set(t.apply(null,n))},lens:function(n){return t.lens(this,n)}}}function L(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 F(t,n,e){return t._getter=e.get,t._setter=e.set,t._parent=n,t._type=lt,t}function M(t){for(var n=t.length;n--;)q(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 B(t,n,e){t.inTxnValues[n._uid]=[n,e],f(n,t.reactorQueue)}function H(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=et,g(wt))B(y(wt),this,t);else{this._value=t;var e=[];f(this,e),M(e),l(this)}return this},_get:function(){return g(wt)?W(y(wt),this):this._value}}}function J(t,n){return t._uid=o(),t._children=[],t._state=ut,t._value=n,t._type=st,t}function K(t){E(wt,new U,t)}function P(t){return function(){ | ||
var n,e=c(arguments,0),r=this;return K(function(){n=t.apply(r,e)}),n}}function X(){mt?mt.refCount++:(mt=T(wt,function(){return new U}),mt.refCount=1);var t=!1;return{tick:function(){if(t)throw new Error("tyring to use ticker after release");mt.tick()},release:function(){if(t)throw new Error("ticker already released");0===--mt.refCount&&(mt.stop(),mt=null),t=!0}}}function Y(t){function e(t){var n=c(arguments,1);return a.derivation(function(){for(var e="",r=0;t.length>r;r++)e+=t[r],n.length>r&&(e+=a.unpack(n[r]));return e})}function i(t){if(a.isDerivable(t))return t.get();if(t instanceof Array)return t.map(i);if(t.constructor===Object){for(var n={},e=Z(t),r=e.length;r--;){var u=e[r];n[u]=i(t[u])}return n}return t}t=n({},bt,t||{});var a={transact:K,defaultEquals:r,transaction:P,ticker:X,Reactor:C,isAtom:function(t){return t&&(t._type===st||t._type===lt)},isDerivable:function(t){return t&&(t._type===st||t._type===lt||t._type===ft)},isDerivation:function(t){return t&&(t._type===ft||t._type===lt)},isLensed:function(t){return t&&t._type===lt},isReactor:function(t){return t&&t._type===ht}},u=G(a,t),o=Q(a,t),f=n({},o,u,H(a,t)),l=n({},u,I(a,t)),h=n({},o,l,L(a,t));return a.atom=function(t){return J(Object.create(f),t)},a.swap=function(t,n){var e=c(arguments,1);return e[0]=t.get(),t.set(n.apply(null,e))},a.derivation=function(t){return z(Object.create(l),t)},a.derive=function(t){if(t instanceof Array)return e.apply(null,arguments);if(arguments.length>0)return u.derive.apply(t,c(arguments,1));throw new Error("Wrong arity for derive. Expecting 1+ args")},a.mDerive=function(t){return u.mDerive.apply(t,c(arguments,1))},a.lens=function(t,n){var e=Object.create(h);return F(z(e,function(){return n.get(t.get())}),t,n)},a.unpack=function(t){return a.isDerivable(t)?t.get():t},a.lift=function(t){return function(){var n=arguments,e=this;return a.derivation(function(){return t.apply(e,Array.prototype.map.call(n,a.unpack))})}},a.set=function(t,n){return t.set(n)},a.get=function(t){return t.get()},a.struct=function(t){if(t.constructor===Object||t instanceof Array)return a.derivation(function(){ | ||
return i(t)});throw new Error("`struct` expects plain Object or Array")},a.destruct=function(t){for(var n=arguments,e=[],r=1;n.length>r;r++)e.push(a.lookup(t,n[r]));return e},a.lookup=function(t,n){return a.derivation(function(){return a.unpack(t)[a.unpack(n)]})},a.ifThenElse=function(t,n,e){return t.then(n,e)},a.ifThenElse=function(t,n,e){return a.derivation(function(){return a.unpack(a.unpack(t)?n:e)})},a.mIfThenElse=function(t,n,e){return a.derivation(function(){var r=a.unpack(t);return a.unpack(s(r)?n:e)})},a.or=function(){var t=arguments;return a.derivation(function(){for(var n,e=0;t.length>e&&!(n=a.unpack(t[e]));e++);return n})},a.mOr=function(){var t=arguments;return a.derivation(function(){for(var n,e=0;t.length>e&&(n=a.unpack(t[e]),!s(n));e++);return n})},a.and=function(){var t=arguments;return a.derivation(function(){for(var n,e=0;t.length>e&&(n=a.unpack(t[e]),n);e++);return n})},a.mAnd=function(){var t=arguments;return a.derivation(function(){for(var n,e=0;t.length>e&&(n=a.unpack(t[e]),s(n));e++);return n})},a.not=function(t){return t.derive(function(t){return!t})},a.switchCase=function(t){return u["switch"].apply(t,c(arguments,1))},a}var Z=Object.keys,$=0,tt=Object.freeze({equals:function(){return!1}}),nt=0,et=1,rt=2,it=3,at=4,ut=5,ot=6,ct=[],st="ATOM",ft="DERIVATION",lt="LENS",ht="REACTION",pt=0,_t=1,vt=3,dt={},gt="Cyclical Reactor Dependency! Not allowed!",yt=[];n(C.prototype,{start:function(){return A(this._base),this},stop:function(){return R(this._base),this},force:function(){return D(this._base),this},isActive:function(){return this._base.active},orphan:function(){return O(this._base),this},adopt:function(t){if(t._type!==ht)throw Error("reactors can only adopt reactors");return V(this._base,t._base),this}}),n(N.prototype,C.prototype);var wt=d(),kt={push:function(){}};n(U.prototype,{onCommit:function(){var t,n,e=Z(this.inTxnValues);if(g(wt))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],f(n[0],kt);for(M(this.reactorQueue), | ||
t=e.length;t--;)l(this.inTxnValues[e[t]][0])}},onAbort:function(){if(!g(wt))for(var t=Z(this.inTxnValues),n=t.length;n--;)h(this.inTxnValues[t[n]][0])}});var mt=null,bt={equals:r};n(t,Y()),t.withEquality=function(t){return Y({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=$(e),i=r.length;i--;){var a=r[i];t[a]=e[a]}return t}function e(t,n){return t===n?0!==t||1/t===1/n:t!==t&&n!==n}function r(t,n){return e(t,n)||t&&"function"==typeof t.equals&&t.equals(n)}function i(t,n){var e=t.indexOf(n);0>e&&t.push(n)}function a(t,n){var e=t.indexOf(n);e>=0&&t.splice(e,1)}function u(t,n){return t.indexOf(n)>=0}function o(){return tt++}function c(t,n){return Array.prototype.slice.call(t,n)}function s(t){return null!==t&&void 0!==t}function f(t){et=!!t}function l(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!==ot&&(r._state=ot,l(r,n))}}function h(t){var n;switch(t._state){case it:case at:for(n=t._children.length;n--;){var e=t._children[n];h(e),e._state!==ct&&t._children.splice(n,1)}t._state=ct;break;case ot:if(t._type===_t)t._state=ct;else{var r=[];for(n=t._parents.length;n--;){var i=t._parents[n];if(i._state!==at){t._state=ut;break}r.push([i,i._value])}t._state!==ut&&(t._state=st,t._parents=r)}break;case ct:case ut:case st:break;default:throw new Error("can't sweep state "+t._state)}}function p(t){var n=!1;switch(t._type){case lt:t._state=ct,n=!0;break;case ht:case pt:t._state=rt,t._value=nt,n=!0;break;case _t:t._state=ct,n=!1}if(n){for(var e=t._children.length;e--;)p(t._children[e]);t._children=[]}}function _(t){var n=ft.length;ft.push([]);try{return t(),ft[n]}finally{ft.pop()}}function v(t){ft.length>0&&i(ft[ft.length-1],t)}function d(){throw yt}function g(){return{currentTxn:null}}function y(t){return null!==t.currentTxn}function k(t){return t.currentTxn}function w(t,n){n._parent=t.currentTxn,n._state=vt,t.currentTxn=n}function m(t,n){var e=t.currentTxn;if(t.currentTxn=e._parent,e._state!==vt)throw new Error("unexpected state: "+e._state); | ||
n(e)}function b(t){m(t,function(t){t._state=dt,t.onCommit&&t.onCommit()})}function E(t){m(t,function(t){t._state=gt,t.onAbort&&t.onAbort()})}function T(t,n,e){w(t,n);try{e(d)}catch(r){if(E(t),r!==yt)throw r;return}b(t)}function x(t,n){w(t,n());var e=!1;return{tick:function(){if(e)throw new Error("can't tick disposed ticker");b(t),w(t,n())},stop:function(){if(e)throw new Error("ticker already disposed");b(t)}}}function R(t,n){var e={control:n,parent:t,parentReactor:null,dependentReactors:[],_state:ct,active:!1,_type:_t,uid:o(),reacting:!1,stopping:!1,yielding:!1};return et&&(e.stack=Error().stack),e}function A(t){if(t.active){if(t.stopping)throw Error(kt);try{for(t.stopping=!0;t.dependentReactors.length;){var n=t.dependentReactors.pop();A(n)}}finally{a(t.parent._children,t),t.parentReactor&&V(t),t.active=!1,t.stopping=!1}t.control.onStop&&t.control.onStop()}}function O(t){if(!t.active){i(t.parent._children,t),t.active=!0,t.parent._get();var n=wt.length;n>0&&(t.parentReactor=wt[n-1],i(t.parentReactor.dependentReactors,t)),t.control.onStart&&t.control.onStart()}}function V(t){t.parentReactor&&(a(t.parentReactor.dependentReactors,t),t.parentReactor=null)}function q(t,n){V(n),t.active?(n.parentReactor=t,i(t.dependentReactors,n)):A(n)}function D(t){if(t.yielding)throw Error(kt);if(t.active&&t._state===ot){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===ot||e===ut||e===st||e===rt)&&n._get(),e=n._state,e===at)t._state=ct;else{if(e!==it)throw new Error("invalid parent state: "+e);C(t)}}}}function C(t){if(!t.control.react)throw new Error("No reactor function available.");t._state=ct;try{if(t.reacting=!0,wt.push(t),et)try{t.control.react(t.parent._get())}catch(n){throw console.error(t.stack),n}else t.control.react(t.parent._get())}finally{wt.pop(),t.reacting=!1}}function j(){this._type=_t}function N(t,n){if(t._base)throw new Error("This reactor has already been initialized");return t._base=R(n,t),t}function S(t){this._type=_t,this.react=t}function G(t){ | ||
return n(new j,t)}function I(t,n){var e={derive:function(n,e,r,i,a){var u=this;switch(arguments.length){case 0:return u;case 1:return t.derivation(function(){return n(u.get())});case 2:return t.derivation(function(){return n(u.get(),t.unpack(e))});case 3:return t.derivation(function(){return n(u.get(),t.unpack(e),t.unpack(r))});case 4:return t.derivation(function(){return n(u.get(),t.unpack(e),t.unpack(r),t.unpack(i))});case 5:return t.derivation(function(){return n(u.get(),t.unpack(e),t.unpack(r),t.unpack(i),t.unpack(a))});default:var o=[u].concat(c(arguments,1));return t.derivation(function(){return n.apply(null,o.map(t.unpack))})}},reactor:function(t){if("function"==typeof t)return N(new S(t),this);if(t instanceof j)return N(t,this);if(t&&t.react)return N(G(t),this);throw new Error("Unrecognized type for reactor "+t)},react:function(t){return this.reactor(t).start().force()},get:function(){return v(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(s(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 z(t,n){return{_clone:function(){return t.derivation(this._deriver)},_forceGet:function(){var t,e=this,r=_(function(){var t;if(et)try{t=e._deriver()}catch(r){throw console.error(e._stack),r}else t=e._deriver();e._state=n.equals(t,e._value)?at:it,e._value=t});for(t=this._parents.length;t--;){var o=this._parents[t];u(r,o)||a(o._children,this); | ||
}for(this._parents=r,t=r.length;t--;)i(r[t]._children,this)},_get:function(){var t,e;t:switch(this._state){case rt:case ut: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===ut||r===st)&&e._get(),r=e._state,r===it){this._forceGet();break t}if(r!==ct&&r!==at)throw new Error("invalid parent mode: "+r)}this._state=at;break;case st:var a=[];for(t=0;this._parents.length>t;t++){var u=this._parents[t],o=u[1];if(e=u[0],!n.equals(e._get(),o)){this._parents=[],this._forceGet();break t}a.push(e)}for(t=a.length;t--;)i(a[t]._children,this);this._parents=a,this._state=at}return this._value}}}function Q(t,n){return t._children=[],t._parents=[],t._deriver=n,t._state=rt,t._type=ht,t._value=nt,et&&(t._stack=Error().stack),t}function L(t,n){return{swap:function(t){var n=c(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 F(t,n,e){return t._getter=e.get,t._setter=e.set,t._parent=n,t._type=pt,t}function U(t){for(var n=t.length;n--;)D(t[n])}function W(){this.inTxnValues={},this.reactorQueue=[]}function B(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],l(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, | ||
y(mt))H(k(mt),this,t);else{this._value=t;var e=[];l(this,e),U(e),h(this)}return this},_get:function(){return y(mt)?B(k(mt),this):this._value}}}function K(t,n){return t._uid=o(),t._children=[],t._state=ct,t._value=n,t._type=lt,t}function P(t){T(mt,new W,t)}function X(t){return function(){var n,e=c(arguments,0),r=this;return P(function(){n=t.apply(r,e)}),n}}function Y(){Et?Et.refCount++:(Et=x(mt,function(){return new W}),Et.refCount=1);var t=!1;return{tick:function(){if(t)throw new Error("tyring to use ticker after release");Et.tick()},release:function(){if(t)throw new Error("ticker already released");0===--Et.refCount&&(Et.stop(),Et=null),t=!0}}}function Z(t){function e(t){var n=c(arguments,1);return a.derivation(function(){for(var e="",r=0;t.length>r;r++)e+=t[r],n.length>r&&(e+=a.unpack(n[r]));return e})}function i(t){if(a.isDerivable(t))return t.get();if(t instanceof Array)return t.map(i);if(t.constructor===Object){for(var n={},e=$(t),r=e.length;r--;){var u=e[r];n[u]=i(t[u])}return n}return t}t=n({},Tt,t||{});var a={transact:P,defaultEquals:r,setDebugMode:f,transaction:X,ticker:Y,Reactor:j,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===_t}},u=I(a,t),o=L(a,t),l=n({},o,u,J(a,t)),h=n({},u,z(a,t)),p=n({},o,h,M(a,t));return a.atom=function(t){return K(Object.create(l),t)},a.swap=function(t,n){var e=c(arguments,1);return e[0]=t.get(),t.set(n.apply(null,e))},a.derivation=function(t){return Q(Object.create(h),t)},a.derive=function(t){if(t instanceof Array)return e.apply(null,arguments);if(arguments.length>0)return u.derive.apply(t,c(arguments,1));throw new Error("Wrong arity for derive. Expecting 1+ args")},a.mDerive=function(t){return u.mDerive.apply(t,c(arguments,1))},a.lens=function(t,n){var e=Object.create(p);return F(Q(e,function(){return n.get(t.get())}),t,n)},a.unpack=function(t){ | ||
return a.isDerivable(t)?t.get():t},a.lift=function(t){return function(){var n=arguments,e=this;return a.derivation(function(){return t.apply(e,Array.prototype.map.call(n,a.unpack))})}},a.set=function(t,n){return t.set(n)},a.get=function(t){return t.get()},a.struct=function(t){if(t.constructor===Object||t instanceof Array)return a.derivation(function(){return i(t)});throw new Error("`struct` expects plain Object or Array")},a.destruct=function(t){for(var n=arguments,e=[],r=1;n.length>r;r++)e.push(a.lookup(t,n[r]));return e},a.lookup=function(t,n){return a.derivation(function(){return a.unpack(t)[a.unpack(n)]})},a.ifThenElse=function(t,n,e){return t.then(n,e)},a.ifThenElse=function(t,n,e){return a.derivation(function(){return a.unpack(a.unpack(t)?n:e)})},a.mIfThenElse=function(t,n,e){return a.derivation(function(){var r=a.unpack(t);return a.unpack(s(r)?n:e)})},a.or=function(){var t=arguments;return a.derivation(function(){for(var n,e=0;t.length>e&&!(n=a.unpack(t[e]));e++);return n})},a.mOr=function(){var t=arguments;return a.derivation(function(){for(var n,e=0;t.length>e&&(n=a.unpack(t[e]),!s(n));e++);return n})},a.and=function(){var t=arguments;return a.derivation(function(){for(var n,e=0;t.length>e&&(n=a.unpack(t[e]),n);e++);return n})},a.mAnd=function(){var t=arguments;return a.derivation(function(){for(var n,e=0;t.length>e&&(n=a.unpack(t[e]),s(n));e++);return n})},a.not=function(t){return t.derive(function(t){return!t})},a.switchCase=function(t){return u["switch"].apply(t,c(arguments,1))},a}var $=Object.keys,tt=0,nt=Object.freeze({equals:function(){return!1}}),et=!1,rt=0,it=1,at=2,ut=3,ot=4,ct=5,st=6,ft=[],lt="ATOM",ht="DERIVATION",pt="LENS",_t="REACTION",vt=0,dt=1,gt=3,yt={},kt="Cyclical Reactor Dependency! Not allowed!",wt=[];n(j.prototype,{start:function(){return O(this._base),this},stop:function(){return A(this._base),this},force:function(){return C(this._base),this},isActive:function(){return this._base.active},orphan:function(){return V(this._base),this},adopt:function(t){if(t._type!==_t)throw Error("reactors can only adopt reactors"); | ||
return q(this._base,t._base),this}}),n(S.prototype,j.prototype);var mt=g(),bt={push:function(){}};n(W.prototype,{onCommit:function(){var t,n,e=$(this.inTxnValues);if(y(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],l(n[0],bt);for(U(this.reactorQueue),t=e.length;t--;)h(this.inTxnValues[e[t]][0])}},onAbort:function(){if(!y(mt))for(var t=$(this.inTxnValues),n=t.length;n--;)p(this.inTxnValues[t[n]][0])}});var Et=null,Tt={equals:r};n(t,Z()),t.withEquality=function(t){return Z({equals:t})},t["default"]=t}); | ||
//# sourceMappingURL=derivable.min.js.map |
{ | ||
"name": "derivable", | ||
"version": "0.9.3", | ||
"version": "0.10.0", | ||
"description": "Functional Reactive State for JavaScript & TypeScript", | ||
@@ -5,0 +5,0 @@ "author": "David Sheldrick", |
@@ -113,4 +113,2 @@ <h1 align="center">DerivableJS</h1> | ||
Another drawback, a side-effect of the laziness, is that stack traces can be rather opaque when your reactions throw errors. There should be ways to mitigate this for debugging purposes, but I haven't thought about it much yet. | ||
A final potential drawback is that DerivableJS requires one to think and design in terms of pure functions and immutable data being lazily computed, which I think takes a little while to get comfortable with coming directly from an OO background. | ||
@@ -125,12 +123,17 @@ | ||
##### Examples (wip) | ||
If you want to get a really good feel for what DerivableJS can do, I recommend checking out the [Routing Walkthrough](https://github.com/ds300/derivablejs/tree/master/examples/routing/README.md), which is presented in TypeScript to aid readability. | ||
##### Debugging | ||
Others: | ||
Due to inversion of control, the stack traces you get when your derivations throw errors can be totally unhelpful. There is a nice way to solve this problem for dev time. See [setDebugMode](https://ds300.github.com/derivablejs/#derivable-setDebugMode) for more info. | ||
- [TodoMVC w/React](https://ds300.github.com/derivablejs/examples/todo) ([source](https://github.com/ds300/derivablejs/tree/master/examples/todo)) | ||
- [Null/Error propagation](https://github.com/ds300/derivablejs/tree/master/examples/maybe/README.md) | ||
- [Mapping over collections with caching](https://github.com/ds300/derivablejs/tree/master/examples/caching/README.md) | ||
- [Example of how to do History](https://github.com/ds300/derivablejs/tree/master/examples/history) | ||
##### Examples (very wip) | ||
The best example of writing good code with Derivables right now is the [talk demo](https://github.com/ds300/derivables-talk-demo), which is presented as a 'diff tutorial' and should be read from the initial commit. | ||
The next best is the [routing walkthrough](https://github.com/ds300/derivablejs/tree/master/examples/routing/README.md) | ||
And there are a few others [here](https://github.com/ds300/derivablejs/tree/master/examples/) too. | ||
More coming! | ||
##### npm | ||
@@ -137,0 +140,0 @@ Available as `derivable`. |
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
266763
1236
203