Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

derivable

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

derivable - npm Package Compare versions

Comparing version 0.8.0 to 0.9.0

CHANGELOG.md

26

dist/derivable.d.ts

@@ -14,2 +14,7 @@ /**

mDerive<E>(f: (value: T) => E): Derivable<E>;
mDerive<A, E>(f: (value: T, a: A) => E, a: A): Derivable<E>;
mDerive<A, E>(f: (value: T, a: A) => E, a: Derivable<A>): Derivable<E>;
mDerive<E>(f: (value: T, ...args: any[]) => E, ...args: any[]): Derivable<E>;
reactor(r: Reactor<T>): Reactor<T>;

@@ -27,7 +32,11 @@ reactor(f: (value: T) => void): Reactor<T>;

mAnd(other: any): Derivable<any>;
or(other: any): Derivable<any>;
mOr(other: any): Derivable<any>;
then(thenD: any, elseD: any): Derivable<any>;
some(thenD: any, elseD: any): Derivable<any>;
mThen(thenD: any, elseD: any): Derivable<any>;

@@ -88,2 +97,7 @@ not(): Derivable<boolean>;

function mDerive<I, O>(d: Derivable<I>, f: (v: I) => O): Derivable<O>;
function mDerive<I, O, A>(d: Derivable<I>, f: (v: I, a: A) => O, a: A): Derivable<O>;
function mDerive<I, O, A>(d: Derivable<I>, f: (v: I, a: A) => O, a: Derivable<A>): Derivable<O>;
function mDerive<I, O>(d: Derivable<I>, f: (v: I, ...args: any[]) => O, ...args: any[]): Derivable<O>;
function transact(f: () => void): void;

@@ -97,10 +111,18 @@

function lookup(obj: Derivable<any>, key: any): Derivable<any>;
function destruct(obj: Derivable<any>, ...keys: any[]): Derivable<any>[];
function ifThenElse(condition: any, thenD: any, elseD: any): Derivable<any>;
function some(condition: any, thenD: any, elseD: any): Derivable<any>;
function mIfThenElse(condition: any, thenD: any, elseD: any): Derivable<any>;
function or(...conditions: any[]): Derivable<any>;
function mOr(...conditions: any[]): Derivable<any>;
function and(...conditions: any[]): Derivable<any>;
function mAnd(...conditions: any[]): Derivable<any>;
function not(d: Derivable<any>): Derivable<boolean>;

@@ -107,0 +129,0 @@

@@ -142,2 +142,6 @@ // UMD loader

function util_some (x) {
return (x !== null) && (x !== void 0);
}
// node modes

@@ -564,2 +568,4 @@ var gc_NEW = 0,

reactor: function (f) {

@@ -604,8 +610,20 @@ if (typeof f === 'function') {

some: function (thenClause, elseClause) {
mThen: function (thenClause, elseClause) {
return this.derive(function (x) {
return D.unpack(x === null || x === (void 0) ? elseClause : thenClause);
return D.unpack(util_some(x) ? thenClause : elseClause);
});
},
mOr: function (other) {
return this.mThen(this, other);
},
mDerive: function () {
return this.mThen(this.derive.apply(this, arguments));
},
mAnd: function (other) {
return this.mThen(other, this);
},
not: function () {

@@ -1041,2 +1059,6 @@ return this.derive(function (x) { return !x; });

D.mDerive = function (a) {
return Derivable.mDerive.apply(a, util_slice(arguments, 1));
};
/**

@@ -1120,2 +1142,17 @@ * creates a new lens

D.destruct = function (arg) {
var args = arguments;
var result = [];
for (var i = 1; i < args.length; i++) {
result.push(D.lookup(arg, args[i]));
}
return result;
};
D.lookup = function (arg, prop) {
return D.derivation(function () {
return D.unpack(arg)[D.unpack(prop)];
})
};
D.ifThenElse = function (a, b, c) { return a.then(b, c) };

@@ -1131,7 +1168,7 @@

D.some = function (testValue, thenClause, elseClause) {
D.mIfThenElse = function (testValue, thenClause, elseClause) {
return D.derivation(function () {
var x = D.unpack(testValue);
return D.unpack(
x === null || x === (void 0) ? elseClause : thenClause
util_some(x) ? thenClause : elseClause
);

@@ -1155,2 +1192,16 @@ });

D.mOr = function () {
var args = arguments;
return D.derivation(function () {
var val;
for (var i = 0; i < args.length; i++) {
val = D.unpack(args[i]);
if (util_some(val)) {
break;
}
}
return val;
});
};
D.and = function () {

@@ -1170,2 +1221,16 @@ var args = arguments;

D.mAnd = function () {
var args = arguments;
return D.derivation(function () {
var val;
for (var i = 0; i < args.length; i++) {
val = D.unpack(args[i]);
if (!util_some(val)) {
break;
}
}
return val;
});
};
D.not = function (x) { return x.derive(function (x) { return !x; }); };

@@ -1172,0 +1237,0 @@

14

dist/derivable.min.js

@@ -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=$(e),i=r.length;i--;){var a=r[i];t[a]=e[a]}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 a(t,n,u,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=$(t);if(s.length!==$(n).length)return!1;for(var f=u.length-1;f>=0;){if(u[f]===t)return o[f]===n;f-=1}for(u[u.length]=t,o[o.length]=n,f=s.length-1;f>=0;){var l=s[f];if(!i(l,n)||!a(n[l],t[l],u,o))return!1;f-=1}return u.pop(),o.pop(),!0}return!1}function u(t,n){return t&&"function"==typeof t.equals?t.equals(n):a(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 tt++}function l(t,n){return Array.prototype.slice.call(t,n)}function h(t,n){if(t._type===pt){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!==ut&&(r._state=ut,h(r,n))}}function p(t){var n;switch(t._state){case rt:case it:for(n=t._children.length;n--;){var e=t._children[n];p(e),e._state!==ot&&t._children.splice(n,1)}t._state=ot;break;case ut:if(t._type===pt)t._state=ot;else{var r=[];for(n=t._parents.length;n--;){var i=t._parents[n];if(i._state!==it){t._state=at;break}r.push([i,i._value])}t._state!==at&&(t._state=ct,t._parents=r)}break;case ot:
case at:case ct:break;default:throw new Error("can't sweep state "+t._state)}}function _(t){var n=!1;switch(t._type){case ft:t._state=ot,n=!0;break;case lt:case ht:t._state=et,t._value=nt,n=!0;break;case pt:t._state=ot,n=!1}if(n){for(var e=t._children.length;e--;)_(t._children[e]);t._children=[]}}function v(t){return st.push([]),t(),st.pop()}function d(t){st.length>0&&o(st[st.length-1],t)}function g(){throw gt}function y(){return{currentTxn:null}}function w(t){return null!==t.currentTxn}function k(t){return t.currentTxn}function b(t,n){n._parent=t.currentTxn,n._state=_t,t.currentTxn=n}function m(t,n){var e=t.currentTxn;if(t.currentTxn=e._parent,e._state!==_t)throw new Error("unexpected state: "+e._state);n(e)}function x(t){m(t,function(t){t._state=vt,t.onCommit&&t.onCommit()})}function E(t){m(t,function(t){t._state=dt,t.onAbort&&t.onAbort()})}function T(t,n,e){b(t,n);try{e(g)}catch(r){if(E(t),r!==gt)throw r;return}x(t)}function O(t,n){b(t,n());var e=!1;return{tick:function(){if(e)throw new Error("can't tick disposed ticker");x(t),b(t,n())},stop:function(){if(e)throw new Error("ticker already disposed");x(t)}}}function R(t,n){return{control:n,parent:t,parentReactor:null,dependentReactors:[],_state:ot,active:!1,_type:pt,uid:f(),reacting:!1,stopping:!1,yielding:!1}}function A(t){if(t.active){if(t.stopping)throw Error(yt);try{for(t.stopping=!0;t.dependentReactors.length;){var n=t.dependentReactors.pop();A(n)}}finally{c(t.parent._children,t),t.parentReactor&&V(t),t.active=!1,t.stopping=!1}t.control.onStop&&t.control.onStop()}}function j(t){if(!t.active){o(t.parent._children,t),t.active=!0,t.parent._get();var n=wt.length;n>0&&(t.parentReactor=wt[n-1],o(t.parentReactor.dependentReactors,t)),t.control.onStart&&t.control.onStart()}}function V(t){t.parentReactor&&(c(t.parentReactor.dependentReactors,t),t.parentReactor=null)}function q(t){if(t.yielding)throw Error(yt);if(t.active&&t._state===ut){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===ut||e===at||e===ct||e===et)&&n._get(),
e=n._state,e===it)t._state=ot;else{if(e!==rt)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=ot;try{t.reacting=!0,wt.push(t),t.control.react(t.parent._get())}finally{wt.pop(),t.reacting=!1}}function D(){this._type=pt}function S(t,n){if(t._base)throw new Error("This reactor has already been initialized");return t._base=R(n,t),t}function N(t){this._type=pt,this.react=t}function G(t){return n(new D,t)}function z(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(l(arguments,1));return t.derivation(function(){return n.apply(null,o.map(t.unpack))})}},reactor:function(t){if("function"==typeof t)return S(new N(t),this);if(t instanceof D)return S(t,this);if(t&&t.react)return S(G(t),this);throw new Error("Unrecognized type for reactor "+t)},react:function(t){return this.reactor(t).start().force()},get:function(){return d(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)})},some:function(n,e){return this.derive(function(r){return t.unpack(null===r||void 0===r?e:n)})},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=v(function(){var t=e._deriver();e._state=n.equals(t,e._value)?it:rt,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 et:case at:this._forceGet();break;case ut:for(t=0;this._parents.length>t;t++){e=this._parents[t];var r=e._state;if((r===ut||r===at||r===ct)&&e._get(),r=e._state,r===rt){this._forceGet();break t}if(r!==ot&&r!==it)throw new Error("invalid parent mode: "+r)}this._state=it;break;case ct:var i=[];for(t=0;this._parents.length>t;t++){var a=this._parents[t],u=a[1];if(e=a[0],!n.equals(e._get(),u)){this._parents=[],this._forceGet();break t}i.push(e)}for(t=i.length;t--;)o(i[t]._children,this);this._parents=i,this._state=it}return this._value}}}function Q(t,n){return t._children=[],t._parents=[],t._deriver=n,t._state=et,t._type=lt,t._value=nt,t}function L(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 B(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=ht,t}function M(t){for(var n=t.length;n--;)q(t[n])}function P(){this.inTxnValues={},this.reactorQueue=[]}function U(t,n){var e=t.inTxnValues[n._uid];return e?e[1]:n._value}function W(t,n,e){t.inTxnValues[n._uid]=[n,e],h(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=rt,w(kt))W(k(kt),this,t);else{this._value=t;var e=[];h(this,e),M(e),p(this)}return this},_get:function(){return w(kt)?U(k(kt),this):this._value}}}function J(t,n){return t._uid=f(),t._children=[],t._state=ot,t._value=n,t._type=ft,t}function K(t){T(kt,new P,t)}function X(t){return function(){var n,e=l(arguments,0),r=this;return K(function(){n=t.apply(r,e)}),n}}function Y(){mt?mt.refCount++:(mt=O(kt,function(){return new P}),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 Z(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=$(t),a=e.length;a--;){var u=e[a];n[u]=r(t[u])}return n}return t}t=n({},xt,t||{});var i={transact:K,defaultEquals:u,transaction:X,ticker:Y,Reactor:D,isAtom:function(t){return t&&(t._type===ft||t._type===ht)},isDerivable:function(t){return t&&(t._type===ft||t._type===ht||t._type===lt)},isDerivation:function(t){return t&&(t._type===lt||t._type===ht)},isLensed:function(t){return t&&t._type===ht},isReactor:function(t){return t&&t._type===pt}},a=z(i,t),o=L(i,t),c=n({},o,a,H(i,t)),s=n({},a,I(i,t)),f=n({},o,s,B(i,t));return i.atom=function(t){return J(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 Q(Object.create(s),t)},i.derive=function(t){if(t instanceof Array)return e.apply(null,arguments);if(arguments.length>0)return a.derive.apply(t,l(arguments,1));throw new Error("Wrong arity for derive. Expecting 1+ args");
},i.lens=function(t,n){var e=Object.create(f);return F(Q(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.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.some=function(t,n,e){return i.derivation(function(){var r=i.unpack(t);return i.unpack(null===r||void 0===r?e:n)})},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.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.not=function(t){return t.derive(function(t){return!t})},i.switchCase=function(t){return a["switch"].apply(t,l(arguments,1))},i}var $=Object.keys,tt=0,nt=Object.freeze({equals:function(){return!1}}),et=0,rt=1,it=2,at=3,ut=4,ot=5,ct=6,st=[],ft="ATOM",lt="DERIVATION",ht="LENS",pt="REACTION",_t=0,vt=1,dt=3,gt={},yt="Cyclical Reactor Dependency! Not allowed!",wt=[];n(D.prototype,{start:function(){return j(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}}),n(N.prototype,D.prototype);var kt=y(),bt={push:function(){}};n(P.prototype,{onCommit:function(){var t,n,e=$(this.inTxnValues);if(w(kt))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],h(n[0],bt);for(M(this.reactorQueue),t=e.length;t--;)p(this.inTxnValues[e[t]][0])}},onAbort:function(){if(!w(kt))for(var t=$(this.inTxnValues),n=t.length;n--;)_(this.inTxnValues[t[n]][0]);
}});var mt=null,xt={equals:u};n(t,Z()),t.withEquality=function(t){return Z({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=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});
//# sourceMappingURL=derivable.min.js.map
{
"name": "derivable",
"version": "0.8.0",
"version": "0.9.0",
"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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc