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 1.0.0-beta9 to 2.0.0-beta.0

dist/derivable.js.flow

8

CHANGELOG.md

@@ -0,1 +1,9 @@

## 1.0.0
- Renamed `lens` to `proxy`, and `isLensed` to `isProxy`
- `derivation` was renamed to `derive`.
- `derive` has a more flexible call signature, where all extra arguments will be unpacked and used as arguments to the function.
- Removed the `lift` method. Instead of `lift(f)`, you can instead do `derive.bind(null, f)`.
- Renamed `swap` to `update`
## 0.12.1

@@ -2,0 +10,0 @@

45

dist/derivable.d.ts

@@ -24,5 +24,5 @@ /**

react(f: (value: T) => void, options?: Lifecycle): void;
react(f: (value: T) => void, options?: Lifecycle<T>): void;
mReact(f: (value: T) => void, options?: Lifecycle): void;
mReact(f: (value: T) => void, options?: Lifecycle<T>): void;

@@ -56,8 +56,8 @@ get(): T;

swap<E>(f: (value: T, ...args: any[]) => E, ...args: any[]): Atom<E>;
update<E>(f: (value: T, ...args: any[]) => E, ...args: any[]): Atom<E>;
lens<E>(lens: Lens<T, E>): Atom<E>;
proxy<E>(proxy: Proxy<T, E>): Atom<E>;
}
export interface Lens<ParentType, ChildType> {
export interface Proxy<ParentType, ChildType> {

@@ -69,3 +69,3 @@ get(source: ParentType): ChildType;

export interface CompositeLens<T> {
export interface CompositeProxy<T> {

@@ -77,9 +77,9 @@ get(): T;

export interface Lifecycle {
export interface Lifecycle<T> {
from?: ((() => boolean) | Derivable<boolean>);
from?: (((d: Derivable<T>) => boolean) | Derivable<boolean>);
when?: ((() => boolean) | Derivable<boolean>);
when?: (((d: Derivable<T>) => boolean) | Derivable<boolean>);
until?: ((() => boolean) | Derivable<boolean>);
until?: (((d: Derivable<T>) => boolean) | Derivable<boolean>);

@@ -93,6 +93,21 @@ skipFirst?: boolean;

function derivation<T>(f: () => T): Derivable<T>;
function derive<T>(f: () => T): Derivable<T>;
function lens<T>(lens: CompositeLens<T>): Atom<T>;
function derive<T, A>(f: (a: A) => T, a: A | Derivable<A>): Derivable<T>;
function derive<T, A, B>(f: (a: A, b: B) => T, a: A | Derivable<A>, b: B | Derivable<B>): Derivable<T>;
function derive<T, A, B, C>(f: (a: A, b: B, c: C) => T, a: A | Derivable<A>,
b: B | Derivable<B>, c: C | Derivable<C>): Derivable<T>;
function derive<T, A, B, C, D>(f: (a: A, b: B, c: C, d: D) => T, a: A | Derivable<A>,
b: B | Derivable<B>, c: C | Derivable<C>, d: D | Derivable<D>): Derivable<T>;
function derive<T>(f: (...args: any[])=> T, ...args: any[]): Derivable<T>
function derive(sections: string[], ...values: any[]): Derivable<string>
function proxy<T>(proxy: CompositeProxy<T>): Atom<T>;
function transact(f: () => void): void;

@@ -110,6 +125,2 @@

function lift(f: (...args: any[]) => any): (...args: Derivable<any>[]) => Derivable<any>;
function lift<A, B, E>(f: (a: A, b: B) => E): (a: (A | Derivable<A>), b: (B | Derivable<B>)) => Derivable<E>;
function lift<A, B, C, E>(f: (a: A, b: B, c: C) => E): (a: (A | Derivable<A>), b: (B | Derivable<B>), c: (C | Derivable<C>)) => Derivable<E>;
function isAtom(obj: any): boolean;

@@ -121,3 +132,3 @@

function isLensed(obj: any): boolean;
function isProxy(obj: any): boolean;

@@ -124,0 +135,0 @@ function derive(strings: string[], ...things: any[]): Derivable<string>;

@@ -75,3 +75,3 @@ 'use strict';

var DERIVATION = "DERIVATION";
var LENS = "LENS";
var PROXY = "PROXY";
var REACTOR = "REACTOR";

@@ -83,15 +83,15 @@

x._type === ATOM ||
x._type === LENS);
x._type === PROXY);
}
function isAtom$1 (x) {
return x && (x._type === ATOM || x._type === LENS);
return x && (x._type === ATOM || x._type === PROXY);
}
function isDerivation$1 (x) {
return x && (x._type === DERIVATION || x._type === LENS);
return x && (x._type === DERIVATION || x._type === PROXY);
}
function isLensed$1 (x) {
return x && x._type === LENS;
function isProxy$1 (x) {
return x && x._type === PROXY;
}

@@ -165,3 +165,3 @@

case DERIVATION:
case LENS:
case PROXY:
if (child._state !== UNKNOWN) {

@@ -329,2 +329,32 @@ child._state = UNKNOWN;

/**
* dereferences a thing if it is dereferencable, otherwise just returns it.
*/
function _unpack (thing) {
if (isDerivable$1(thing)) {
return thing.get();
} else {
return thing;
}
};
function deepUnpack (thing) {
if (isDerivable$1(thing)) {
return thing.get();
} else if (thing instanceof Array) {
return thing.map(deepUnpack);
} else if (thing.constructor === Object) {
var result = {};
var keys$$ = keys(thing);
for (var i = keys$$.length; i--;) {
var prop = keys$$[i];
result[prop] = deepUnpack(thing[prop]);
}
return result;
} else {
return thing;
}
};
function Derivation (deriver) {

@@ -346,3 +376,3 @@ this._deriver = deriver;

_clone: function () {
return setEquals(_derivation(this._deriver), this._equals);
return setEquals(_derive(this._deriver), this._equals);
},

@@ -445,4 +475,49 @@

function _derivation (deriver) {
return new Derivation(deriver);
function _derive (f, a, b, c, d) {
if (f instanceof Array) {
// Template string tag for derivable strings
var args = slice(arguments, 1);
return _derive(function () {
var s = "";
for (var i=0; i < f.length; i++) {
s += f[i];
if (i < args.length) {
s += _unpack(args[i]);
}
}
return s;
});
} else {
switch (arguments.length) {
case 0:
throw new Error('derive takes at least one argument');
case 1:
return new Derivation(f);
case 2:
return new Derivation(function () {
return f(_unpack(a));
});
case 3:
return new Derivation(function () {
return f(_unpack(a), _unpack(b));
});
case 4:
return new Derivation(function () {
return f(_unpack(a), _unpack(b), _unpack(c));
});
case 5:
return new Derivation(function () {
return f(_unpack(a),
_unpack(b),
_unpack(c),
_unpack(d));
});
default:
var args = slice(arguments, 1);
return new Derivation(function () {
return f.apply(null, args.map(_unpack));
});
}
}
}

@@ -533,5 +608,5 @@

if (typeof fOrD === 'function') {
return _derivation(fOrD);
return _derive(function () { return fOrD(derivable); });
} else if (typeof fOrD === 'boolean') {
return _derivation(function () { return fOrD; });
return _derive(function () { return fOrD; });
} else {

@@ -564,3 +639,3 @@ throw Error('react ' + name + ' condition must be derivable, got: ' + JSON.stringify(fOrD));

var $whenUntil = _derivation(function () {
var $whenUntil = _derive(function () {
return {

@@ -645,11 +720,11 @@ until: $until.get(),

function Lens (descriptor) {
function Proxy (descriptor) {
Derivation.call(this, descriptor.get);
this._lensDescriptor = descriptor;
this._type = LENS;
this._proxyMapping = descriptor;
this._type = PROXY;
}
assign(Lens.prototype, Derivation.prototype, {
assign(Proxy.prototype, Derivation.prototype, {
_clone: function () {
return setEquals(new Lens(this._lensDescriptor), this._equals);
return setEquals(new Proxy(this._proxyMapping), this._equals);
},

@@ -660,3 +735,3 @@

atomically$1(function () {
that._lensDescriptor.set(value);
that._proxyMapping.set(value);
});

@@ -667,4 +742,4 @@ return this;

function lens$2 (descriptor) {
return new Lens(descriptor);
function proxy$2 (descriptor) {
return new Proxy(descriptor);
}

@@ -679,72 +754,14 @@

var isAtom$2 = isAtom$1;
var isLensed$2 = isLensed$1;
var isProxy$2 = isProxy$1;
var isDerivation$2 = isDerivation$1;
var derivation$1 = _derivation;
var derive$1 = _derive;
var atom$1 = atom$2;
var atomic$2 = atomic$1;
var atomically$2 = atomically$1;
var lens$1 = lens$2;
var proxy$1 = proxy$2;
var unpack$1 = _unpack;
/**
* Template string tag for derivable strings
*/
function derive$1 (parts) {
var args = slice(arguments, 1);
return derivation$1(function () {
var s = "";
for (var i=0; i < parts.length; i++) {
s += parts[i];
if (i < args.length) {
s += unpack$1(args[i]);
}
}
return s;
});
};
/**
* dereferences a thing if it is dereferencable, otherwise just returns it.
*/
function unpack$1 (thing) {
if (isDerivable$2(thing)) {
return thing.get();
} else {
return thing;
}
};
/**
* lifts a non-monadic function to work on derivables
*/
function lift$1 (f) {
return function () {
var args = arguments;
var that = this;
return derivation$1(function () {
return f.apply(that, Array.prototype.map.call(args, unpack$1));
});
};
};
function deepUnpack (thing) {
if (isDerivable$2(thing)) {
return thing.get();
} else if (thing instanceof Array) {
return thing.map(deepUnpack);
} else if (thing.constructor === Object) {
var result = {};
var keys$$ = keys(thing);
for (var i = keys$$.length; i--;) {
var prop = keys$$[i];
result[prop] = deepUnpack(thing[prop]);
}
return result;
} else {
return thing;
}
}
function struct$1 (arg) {
if (arg.constructor === Object || arg instanceof Array) {
return derivation$1(function () {
return derive$1(function () {
return deepUnpack(arg);

@@ -780,3 +797,3 @@ });

var args = arguments;
return derivation$1(function () {
return derive$1(function () {
var val;

@@ -809,12 +826,10 @@ for (var i = 0; i < args.length; i++) {

isAtom: isAtom$2,
isLensed: isLensed$2,
isProxy: isProxy$2,
isDerivation: isDerivation$2,
derivation: derivation$1,
derive: derive$1,
atom: atom$1,
atomic: atomic$2,
atomically: atomically$2,
lens: lens$1,
derive: derive$1,
proxy: proxy$1,
unpack: unpack$1,
lift: lift$1,
struct: struct$1,

@@ -842,9 +857,7 @@ wrapPreviousState: wrapPreviousState$1,

case 'function':
return derivation$1(function () {
return f(that.get());
});
return derive$1(f, that);
case 'string':
case 'number':
return derivation$1(function () {
return that.get()[unpack$1(f)];
return derive$1(function () {
return that.get()[f];
});

@@ -857,7 +870,7 @@ default:

} else if (f instanceof RegExp) {
return derivation$1(function () {
return derive$1(function () {
return that.get().match(f);
});
} else if (isDerivable$1(f)) {
return derivation$1(function () {
return derive$1(function () {
var deriver = f.get();

@@ -882,31 +895,14 @@ var thing = that.get();

}
}
}
case 2:
return derivation$1(function () {
return f(that.get(), unpack$1(a));
});
return derive$1(f, that, a);
case 3:
return derivation$1(function () {
return f(that.get(), unpack$1(a), unpack$1(b));
});
return derive$1(f, that, a, b);
case 4:
return derivation$1(function () {
return f(that.get(),
unpack$1(a),
unpack$1(b),
unpack$1(c));
});
return derive$1(f, that, a, b, c);
case 5:
return derivation$1(function () {
return f(that.get(),
unpack$1(a),
unpack$1(b),
unpack$1(c),
unpack$1(d));
});
return derive$1(f, that, a, b, c, d);
default:
var args = ([that]).concat(slice(arguments, 1));
return derivation$1(function () {
return f.apply(null, args.map(unpack$1));
});
var args = ([f, that]).concat(slice(arguments, 1));
return derive$1.apply(null, args);
}

@@ -924,3 +920,3 @@ },

if (typeof when === 'function' || when === false) {
when = derivation$1(when);
when = derive$1(when);
} else if (!isDerivable$1(when)) {

@@ -937,3 +933,3 @@ throw new Error('when condition must be bool, function, or derivable');

return this.derive(function (x) {
return that.__equals(x, unpack$1(other));
return that.__equals(x, _unpack(other));
});

@@ -943,7 +939,7 @@ },

and: function (other) {
return this.derive(function (x) {return x && unpack$1(other);});
return this.derive(function (x) {return x && _unpack(other);});
},
or: function (other) {
return this.derive(function (x) {return x || unpack$1(other);});
return this.derive(function (x) {return x || _unpack(other);});
},

@@ -953,3 +949,3 @@

return this.derive(function (x) {
return unpack$1(x ? thenClause : elseClause);
return _unpack(x ? thenClause : elseClause);
});

@@ -960,3 +956,3 @@ },

return this.derive(function (x) {
return unpack$1(some(x) ? thenClause : elseClause);
return _unpack(some(x) ? thenClause : elseClause);
});

@@ -1009,8 +1005,8 @@ },

for (i = 0; i < args.length-1; i+=2) {
if (that.__equals(x, unpack$1(args[i]))) {
return unpack$1(args[i+1]);
if (that.__equals(x, _unpack(args[i]))) {
return _unpack(args[i+1]);
}
}
if (i === args.length - 1) {
return unpack$1(args[i]);
return _unpack(args[i]);
}

@@ -1021,3 +1017,3 @@ });

var mutablePrototype = {
swap: function (f) {
update: function (f) {
var args = slice(arguments, 0);

@@ -1027,10 +1023,10 @@ args[0] = this.get();

},
lens: function (monoLensDescriptor) {
proxy: function (monoProxyMapping) {
var that = this;
return new Lens({
return new Proxy({
get: function () {
return monoLensDescriptor.get(that.get());
return monoProxyMapping.get(that.get());
},
set: function (val) {
that.set(monoLensDescriptor.set(that.get(), val));
that.set(monoProxyMapping.set(that.get(), val));
}

@@ -1042,3 +1038,3 @@ });

assign(Derivation.prototype, derivablePrototype);
assign(Lens.prototype, derivablePrototype, mutablePrototype);
assign(Proxy.prototype, derivablePrototype, mutablePrototype);
assign(Atom.prototype, derivablePrototype, mutablePrototype);

@@ -1054,12 +1050,10 @@

var isAtom = isAtom$2;
var isLensed = isLensed$2;
var isProxy = isProxy$2;
var isDerivation = isDerivation$2;
var derivation = derivation$1;
var atom = atom$1;
var atomic = atomic$2;
var atomically = atomically$2;
var lens = lens$1;
var proxy = proxy$1;
var derive = derive$1;
var unpack = unpack$1;
var lift = lift$1;
var struct = struct$1;

@@ -1080,12 +1074,10 @@ var wrapPreviousState = wrapPreviousState$1;

exports.isAtom = isAtom;
exports.isLensed = isLensed;
exports.isProxy = isProxy;
exports.isDerivation = isDerivation;
exports.derivation = derivation;
exports.derive = derive;
exports.atom = atom;
exports.atomic = atomic;
exports.atomically = atomically;
exports.lens = lens;
exports.derive = derive;
exports.proxy = proxy;
exports.unpack = unpack;
exports.lift = lift;
exports.struct = struct;

@@ -1092,0 +1084,0 @@ exports.wrapPreviousState = wrapPreviousState;

@@ -1,8 +0,8 @@

"use strict";function t(t){for(var e=1;e<arguments.length;e++)for(var n=arguments[e],r=Y(n||{}),i=r.length;i--;){var o=r[i];t[o]=n[o]}return t}function e(t,e){return t===e?0!==t||1/t===1/e:t!==t&&e!==e}function n(t,n){return e(t,n)||t&&"function"==typeof t.equals&&t.equals(n)}function r(t,e){var n=t.indexOf(e);n<0&&t.push(e)}function i(t,e){var n=t.indexOf(e);n>=0&&t.splice(n,1)}function o(){return Z++}function s(t,e){return Array.prototype.slice.call(t,e)}function u(t){return null!==t&&void 0!==t}function a(t){tt=!!t}function c(t,e){return t._equals=e,t}function f(t){return t&&(t._type===nt||t._type===et||t._type===rt)}function h(t){return t&&(t._type===et||t._type===rt)}function l(t){return t&&(t._type===nt||t._type===rt)}function p(t){return t&&t._type===rt}function _(t,e){ct.push({parents:e,offset:0,child:t}),ft=t}function v(){return ct[ct.length-1]}function d(){ct.pop(),ft=0===ct.length?null:ct[ct.length-1].child}function g(t){if(null!==ft){var e=ct[ct.length-1];if(e.parents[e.offset]===t)e.offset++;else{var n=e.parents.indexOf(t);if(n===-1)void 0!==ft&&r(t._activeChildren,ft),e.offset===e.parents.length?e.parents.push(t):(e.parents.push(e.parents[e.offset]),e.parents[e.offset]=t),e.offset++;else if(n>e.offset){var i=e.parents[n];e.parents[n]=e.parents[e.offset],e.parents[e.offset]=i,e.offset++}}}}function y(t,e){for(var n=0,r=t._activeChildren.length;n<r;n++){var i=t._activeChildren[n];switch(i._type){case nt:case rt:i._state!==ot&&(i._state=ot,y(i,e));break;case it:e.push(i)}}}function m(t){for(var e=0,n=t.length;e<n;e++){var r=t[e];if(r._reacting)throw new Error("Synchronous cyclical reactions disallowed. Use setImmediate.");r._maybeReact()}}function w(){throw ht}function x(t){this.parent=t,this.id2originalValue={},this.modifiedAtoms=[]}function b(t){null!==lt&&(t._id in lt.id2originalValue||(lt.modifiedAtoms.push(t),lt.id2originalValue[t._id]=t._value))}function E(){return null!==lt}function k(t){D();try{t.call(null,w)}catch(e){if(C(),e!==ht)throw e;return}R()}function A(t){E()?t():k(t)}function q(t){return function(){
var e,n=s(arguments,0),r=this;return k(function(){e=t.apply(r,n)}),e}}function O(t){return function(){var e,n=s(arguments,0),r=this;return A(function(){e=t.apply(r,n)}),e}}function D(){lt=new x(lt)}function R(){var t=lt;if(lt=t.parent,null===lt){var e=[];t.modifiedAtoms.forEach(function(n){n.__equals(n._value,t.id2originalValue[n._id])?n._state=ut:(n._state=st,y(n,e))}),m(e),t.modifiedAtoms.forEach(function(t){t._state=ut})}}function C(){var t=lt;lt=t.parent,t.modifiedAtoms.forEach(function(e){e._value=t.id2originalValue[e._id],e._state=ut,y(e,[])})}function T(){0===pt&&D(),pt++;var t=!1;return{tick:function(){if(t)throw new Error("trying to use ticker after release");R(),D()},reset:function(){if(t)throw new Error("trying to use ticker after release");C(),D()},release:function(){if(t)throw new Error("ticker already released");pt--,t=!0,0===pt&&R()}}}function j(t){this._deriver=t,this._parents=null,this._type=nt,this._value=$,this._equals=null,this._activeChildren=[],this._state=at,tt&&(this.stack=Error().stack)}function V(t,e){if(i(t._activeChildren,e),0===t._activeChildren.length&&null!=t._parents){for(var n=t._parents.length,r=0;r<n;r++)V(t._parents[r],t);t._parents=null,t._state=at}}function S(t){return new j(t)}function M(t,e,n){this._parent=t,this.react=e,this._governor=n||null,this._active=!1,this._reacting=!1,this._type=it,tt&&(this.stack=Error().stack)}function I(e,n,r){function i(t,e){if(!f(t)){if("function"==typeof t)return S(t);if("boolean"==typeof t)return S(function(){return t});throw Error("react "+e+" condition must be derivable, got: "+JSON.stringify(t))}return t}if("function"!=typeof n)throw Error("the first argument to .react must be a function");r=t({once:!1,from:!0,until:!1,when:!0,skipFirst:!1},r);var o=r.skipFirst,s=new M(e,function(t){o?o=!1:(n(t),r.once&&(this.stop(),h.stop()))}),u=i(r.until,"until"),a=i(r.when,"when"),c=S(function(){return{until:u.get(),when:a.get()}}),h=new M(c,function(t){t.until?(s.stop(),this.stop()):t.when?s._active||s.start().force():s._active&&s.stop()});s._governor=h;
var l=i(r.from,"from"),p=new M(l,function(t){t&&(h.start().force(),this.stop())});p.start().force()}function L(t){return this._id=o(),this._activeChildren=[],this._value=t,this._state=ut,this._type=et,this._equals=null,this}function N(t){return new L(t)}function P(t){j.call(this,t.get),this._lensDescriptor=t,this._type=rt}function z(t){return new P(t)}function F(t){var e=s(arguments,1);return Et(function(){for(var n="",r=0;r<t.length;r++)n+=t[r],r<e.length&&(n+=J(e[r]));return n})}function J(t){return mt(t)?t.get():t}function U(t){return function(){var e=arguments,n=this;return Et(function(){return t.apply(n,Array.prototype.map.call(e,J))})}}function B(t){if(mt(t))return t.get();if(t instanceof Array)return t.map(B);if(t.constructor===Object){for(var e={},n=Y(t),r=n.length;r--;){var i=n[r];e[i]=B(t[i])}return e}return t}function G(t){if(t.constructor===Object||t instanceof Array)return Et(function(){return B(t)});throw new Error("`struct` expects plain Object or Array")}function H(t,e){var n=e;return function(e){var r=t.call(this,e,n);return n=e,r}}function K(t){var e=[];_(void 0,e);try{t()}finally{d()}return e}function Q(t){return function(){var e=arguments;return Et(function(){for(var n,r=0;r<e.length&&(n=J(e[r]),!t(n));r++);return n})}}function W(t){return t}function X(t){return function(e){return!t(e)}}Object.defineProperty(exports,"__esModule",{value:!0});var Y=Object.keys,Z=0,$=Object.freeze({equals:function(){return!1}}),tt=!1,et="ATOM",nt="DERIVATION",rt="LENS",it="REACTOR",ot=0,st=1,ut=2,at=3,ct=[],ft=null,ht={},lt=null,pt=0;t(j.prototype,{_clone:function(){return c(S(this._deriver),this._equals)},_forceEval:function(){var t,e=this,n=null;try{if(null===this._parents&&(this._parents=[]),_(this,this._parents),tt)try{n=e._deriver()}catch(r){throw console.error(e.stack),r}else n=e._deriver();t=v().offset}finally{d()}this._state=this.__equals(n,this._value)?ut:st;for(var i=t,o=this._parents.length;i<o;i++){var s=this._parents[i];V(s,this),this._parents[i]=null}this._parents.length=t,this._value=n},_update:function(){
if(null===this._parents)this._forceEval();else if(this._state===ot){for(var t=this._parents.length,e=0;e<t;e++){var n=this._parents[e];if(n._state===ot&&n._update(),n._state===st){this._forceEval();break}}this._state===ot&&(this._state=ut)}},get:function(){if(g(this),this._activeChildren.length>0)this._update();else{_(void 0,[]);try{this._value=this._deriver()}finally{d()}}return this._value}}),t(M.prototype,{start:function(){return this._active=!0,r(this._parent._activeChildren,this),this._parent.get(),this},_force:function(t){try{this._reacting=!0,this.react(t)}catch(e){throw tt&&console.error(this.stack),e}finally{this._reacting=!1}},force:function(){return this._force(this._parent.get()),this},_maybeReact:function(){if(!this._reacting&&this._active&&(null!==this._governor&&this._governor._maybeReact(),this._active)){var t=this._parent.get();this._parent._state===st&&this._force(t)}},stop:function(){return V(this._parent,this),this._active=!1,this}}),t(L.prototype,{_clone:function(){return c(N(this._value),this._equals)},set:function(t){b(this);var e=this._value;if(this._value=t,!E()&&!this.__equals(t,e))try{this._state=st;var n=[];y(this,n),m(n)}finally{this._state=ut}},get:function(){return g(this),this._value}}),t(P.prototype,j.prototype,{_clone:function(){return c(new P(this._lensDescriptor),this._equals)},set:function(t){var e=this;return A(function(){e._lensDescriptor.set(t)}),this}});var _t=M,vt=k,dt=a,gt=q,yt=T,mt=f,wt=h,xt=p,bt=l,Et=S,kt=N,At=O,qt=A,Ot=z,Dt=Q(W),Rt=Q(u),Ct=Q(X(W)),Tt=Q(X(u)),jt=Object.freeze({__Reactor:_t,transact:vt,setDebugMode:dt,transaction:gt,ticker:yt,isDerivable:mt,isAtom:wt,isLensed:xt,isDerivation:bt,derivation:Et,atom:kt,atomic:At,atomically:qt,lens:Ot,derive:F,unpack:J,lift:U,struct:G,wrapPreviousState:H,captureDereferences:K,or:Dt,mOr:Rt,and:Ct,mAnd:Tt}),Vt={derive:function(t,e,n,r,i){var o=this;switch(arguments.length){case 0:throw new Error(".derive takes at least one argument");case 1:switch(typeof t){case"function":return Et(function(){return t(o.get())});case"string":case"number":
return Et(function(){return o.get()[J(t)]});default:if(t instanceof Array)return t.map(function(t){return o.derive(t)});if(t instanceof RegExp)return Et(function(){return o.get().match(t)});if(f(t))return Et(function(){var e=t.get(),n=o.get();switch(typeof e){case"function":return e(n);case"string":case"number":return n[e];default:if(e instanceof RegExp)return n.match(e);throw Error("type error")}});throw Error("type error")}case 2:return Et(function(){return t(o.get(),J(e))});case 3:return Et(function(){return t(o.get(),J(e),J(n))});case 4:return Et(function(){return t(o.get(),J(e),J(n),J(r))});case 5:return Et(function(){return t(o.get(),J(e),J(n),J(r),J(i))});default:var u=[o].concat(s(arguments,1));return Et(function(){return t.apply(null,u.map(J))})}},react:function(t,e){I(this,t,e)},mReact:function(e,n){var r=this.mThen(!0,!1);if(n&&"when"in n&&n.when!==!0){var i=n.when;if("function"==typeof i||i===!1)i=Et(i);else if(!f(i))throw new Error("when condition must be bool, function, or derivable");r=i.and(r)}return this.react(e,t({},n,{when:r}))},is:function(t){var e=this;return this.derive(function(n){return e.__equals(n,J(t))})},and:function(t){return this.derive(function(e){return e&&J(t)})},or:function(t){return this.derive(function(e){return e||J(t)})},then:function(t,e){return this.derive(function(n){return J(n?t:e)})},mThen:function(t,e){return this.derive(function(n){return J(u(n)?t:e)})},mOr:function(t){return this.mThen(this,t)},mDerive:function(t){if(1===arguments.length&&t instanceof Array){var e=this;return t.map(function(t){return e.mDerive(t)})}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})},withEquality:function(t){if(t){if("function"!=typeof t)throw new Error("equals must be function")}else t=null;return c(this._clone(),t)},__equals:function(t,e){return(this._equals||n)(t,e)}};Vt["switch"]=function(){var t=arguments,e=this;return this.derive(function(n){var r;for(r=0;r<t.length-1;r+=2)if(e.__equals(n,J(t[r])))return J(t[r+1]);
if(r===t.length-1)return J(t[r])})};var St={swap:function(t){var e=s(arguments,0);return e[0]=this.get(),this.set(t.apply(null,e))},lens:function(t){var e=this;return new P({get:function(){return t.get(e.get())},set:function(n){e.set(t.set(e.get(),n))}})}};t(j.prototype,Vt),t(P.prototype,Vt,St),t(L.prototype,Vt,St);var Mt=_t,It=vt,Lt=dt,Nt=gt,Pt=yt,zt=mt,Ft=wt,Jt=xt,Ut=bt,Bt=Et,Gt=kt,Ht=At,Kt=qt,Qt=Ot,Wt=F,Xt=J,Yt=U,Zt=G,$t=H,te=K,ee=Dt,ne=Rt,re=Ct,ie=Tt;exports.__Reactor=Mt,exports.transact=It,exports.setDebugMode=Lt,exports.transaction=Nt,exports.ticker=Pt,exports.isDerivable=zt,exports.isAtom=Ft,exports.isLensed=Jt,exports.isDerivation=Ut,exports.derivation=Bt,exports.atom=Gt,exports.atomic=Ht,exports.atomically=Kt,exports.lens=Qt,exports.derive=Wt,exports.unpack=Xt,exports.lift=Yt,exports.struct=Zt,exports.wrapPreviousState=$t,exports.captureDereferences=te,exports.or=ee,exports.mOr=ne,exports.and=re,exports.mAnd=ie,exports["default"]=jt;
"use strict";function t(t){for(var e=1;e<arguments.length;e++)for(var n=arguments[e],r=L(n||{}),i=r.length;i--;){var o=r[i];t[o]=n[o]}return t}function e(t,e){return t===e?0!==t||1/t==1/e:t!==t&&e!==e}function n(t,n){return e(t,n)||t&&"function"==typeof t.equals&&t.equals(n)}function r(t,e){t.indexOf(e)<0&&t.push(e)}function i(t,e){var n=t.indexOf(e);n>=0&&t.splice(n,1)}function o(){return Q++}function s(t,e){return Array.prototype.slice.call(t,e)}function u(t){return null!==t&&void 0!==t}function a(t){Z=!!t}function c(t,e){return t._equals=e,t}function f(t){return t&&(t._type===tt||t._type===$||t._type===et)}function h(t){return t&&(t._type===$||t._type===et)}function l(t){return t&&(t._type===tt||t._type===et)}function p(t){return t&&t._type===et}function _(t,e){ut.push({parents:e,offset:0,child:t}),at=t}function v(){return ut[ut.length-1]}function d(){ut.pop(),at=0===ut.length?null:ut[ut.length-1].child}function y(t){if(null!==at){var e=ut[ut.length-1];if(e.parents[e.offset]===t)e.offset++;else{var n=e.parents.indexOf(t);if(-1===n)void 0!==at&&r(t._activeChildren,at),e.offset===e.parents.length?e.parents.push(t):(e.parents.push(e.parents[e.offset]),e.parents[e.offset]=t),e.offset++;else if(n>e.offset){var i=e.parents[n];e.parents[n]=e.parents[e.offset],e.parents[e.offset]=i,e.offset++}}}}function g(t,e){for(var n=0,r=t._activeChildren.length;n<r;n++){var i=t._activeChildren[n];switch(i._type){case tt:case et:i._state!==rt&&(i._state=rt,g(i,e));break;case nt:e.push(i)}}}function w(t){for(var e=0,n=t.length;e<n;e++){var r=t[e];if(r._reacting)throw new Error("Synchronous cyclical reactions disallowed. Use setImmediate.");r._maybeReact()}}function m(){throw ct}function x(t){this.parent=t,this.id2originalValue={},this.modifiedAtoms=[]}function b(t){null!==ft&&(t._id in ft.id2originalValue||(ft.modifiedAtoms.push(t),ft.id2originalValue[t._id]=t._value))}function E(){return null!==ft}function k(t){R();try{t.call(null,m)}catch(t){if(C(),t!==ct)throw t;return}D()}function A(t){E()?t():k(t)}function q(t){
return function(){var e,n=s(arguments,0),r=this;return k(function(){e=t.apply(r,n)}),e}}function O(t){return function(){var e,n=s(arguments,0),r=this;return A(function(){e=t.apply(r,n)}),e}}function R(){ft=new x(ft)}function D(){var t=ft;if(null===(ft=t.parent)){var e=[];t.modifiedAtoms.forEach(function(n){n.__equals(n._value,t.id2originalValue[n._id])?n._state=ot:(n._state=it,g(n,e))}),w(e),t.modifiedAtoms.forEach(function(t){t._state=ot})}}function C(){var t=ft;ft=t.parent,t.modifiedAtoms.forEach(function(e){e._value=t.id2originalValue[e._id],e._state=ot,g(e,[])})}function T(){0===ht&&R(),ht++;var t=!1;return{tick:function(){if(t)throw new Error("trying to use ticker after release");D(),R()},reset:function(){if(t)throw new Error("trying to use ticker after release");C(),R()},release:function(){if(t)throw new Error("ticker already released");ht--,t=!0,0===ht&&D()}}}function j(t){return f(t)?t.get():t}function M(t){if(f(t))return t.get();if(t instanceof Array)return t.map(M);if(t.constructor===Object){for(var e={},n=L(t),r=n.length;r--;){var i=n[r];e[i]=M(t[i])}return e}return t}function P(t){this._deriver=t,this._parents=null,this._type=tt,this._value=W,this._equals=null,this._activeChildren=[],this._state=st,Z&&(this.stack=Error().stack)}function V(t,e){if(i(t._activeChildren,e),0===t._activeChildren.length&&null!=t._parents){for(var n=t._parents.length,r=0;r<n;r++)V(t._parents[r],t);t._parents=null,t._state=st}}function S(t,e,n,r,i){if(t instanceof Array){var o=s(arguments,1);return S(function(){for(var e="",n=0;n<t.length;n++)e+=t[n],n<o.length&&(e+=j(o[n]));return e})}switch(arguments.length){case 0:throw new Error("derive takes at least one argument");case 1:return new P(t);case 2:return new P(function(){return t(j(e))});case 3:return new P(function(){return t(j(e),j(n))});case 4:return new P(function(){return t(j(e),j(n),j(r))});case 5:return new P(function(){return t(j(e),j(n),j(r),j(i))});default:var o=s(arguments,1);return new P(function(){return t.apply(null,o.map(j))})}}function I(t,e,n){
this._parent=t,this.react=e,this._governor=n||null,this._active=!1,this._reacting=!1,this._type=nt,Z&&(this.stack=Error().stack)}function z(e,n,r){function i(t,n){if(!f(t)){if("function"==typeof t)return S(function(){return t(e)});if("boolean"==typeof t)return S(function(){return t});throw Error("react "+n+" condition must be derivable, got: "+JSON.stringify(t))}return t}if("function"!=typeof n)throw Error("the first argument to .react must be a function");r=t({once:!1,from:!0,until:!1,when:!0,skipFirst:!1},r);var o=r.skipFirst,s=new I(e,function(t){o?o=!1:(n(t),r.once&&(this.stop(),h.stop()))}),u=i(r.until,"until"),a=i(r.when,"when"),c=S(function(){return{until:u.get(),when:a.get()}}),h=new I(c,function(t){t.until?(s.stop(),this.stop()):t.when?s._active||s.start().force():s._active&&s.stop()});s._governor=h,new I(i(r.from,"from"),function(t){t&&(h.start().force(),this.stop())}).start().force()}function F(t){return this._id=o(),this._activeChildren=[],this._value=t,this._state=ot,this._type=$,this._equals=null,this}function N(t){return new F(t)}function J(t){P.call(this,t.get),this._proxyMapping=t,this._type=et}function U(t){return new J(t)}function X(t){if(t.constructor===Object||t instanceof Array)return xt(function(){return M(t)});throw new Error("`struct` expects plain Object or Array")}function Y(t,e){var n=e;return function(e){var r=t.call(this,e,n);return n=e,r}}function B(t){var e=[];_(void 0,e);try{t()}finally{d()}return e}function G(t){return function(){var e=arguments;return xt(function(){for(var n,r=0;r<e.length&&(n=qt(e[r]),!t(n));r++);return n})}}function H(t){return t}function K(t){return function(e){return!t(e)}}Object.defineProperty(exports,"__esModule",{value:!0});var L=Object.keys,Q=0,W=Object.freeze({equals:function(){return!1}}),Z=!1,$="ATOM",tt="DERIVATION",et="PROXY",nt="REACTOR",rt=0,it=1,ot=2,st=3,ut=[],at=null,ct={},ft=null,ht=0;t(P.prototype,{_clone:function(){return c(S(this._deriver),this._equals)},_forceEval:function(){var t,e=this,n=null;try{
if(null===this._parents&&(this._parents=[]),_(this,this._parents),Z)try{n=e._deriver()}catch(t){throw console.error(e.stack),t}else n=e._deriver();t=v().offset}finally{d()}this._state=this.__equals(n,this._value)?ot:it;for(var r=t,i=this._parents.length;r<i;r++){V(this._parents[r],this),this._parents[r]=null}this._parents.length=t,this._value=n},_update:function(){if(null===this._parents)this._forceEval();else if(this._state===rt){for(var t=this._parents.length,e=0;e<t;e++){var n=this._parents[e];if(n._state===rt&&n._update(),n._state===it){this._forceEval();break}}this._state===rt&&(this._state=ot)}},get:function(){if(y(this),this._activeChildren.length>0)this._update();else{_(void 0,[]);try{this._value=this._deriver()}finally{d()}}return this._value}}),t(I.prototype,{start:function(){return this._active=!0,r(this._parent._activeChildren,this),this._parent.get(),this},_force:function(t){try{this._reacting=!0,this.react(t)}catch(t){throw Z&&console.error(this.stack),t}finally{this._reacting=!1}},force:function(){return this._force(this._parent.get()),this},_maybeReact:function(){if(!this._reacting&&this._active&&(null!==this._governor&&this._governor._maybeReact(),this._active)){var t=this._parent.get();this._parent._state===it&&this._force(t)}},stop:function(){return V(this._parent,this),this._active=!1,this}}),t(F.prototype,{_clone:function(){return c(N(this._value),this._equals)},set:function(t){b(this);var e=this._value;if(this._value=t,!E()&&!this.__equals(t,e))try{this._state=it;var n=[];g(this,n),w(n)}finally{this._state=ot}},get:function(){return y(this),this._value}}),t(J.prototype,P.prototype,{_clone:function(){return c(new J(this._proxyMapping),this._equals)},set:function(t){var e=this;return A(function(){e._proxyMapping.set(t)}),this}});var lt=I,pt=k,_t=a,vt=q,dt=T,yt=f,gt=h,wt=p,mt=l,xt=S,bt=N,Et=O,kt=A,At=U,qt=j,Ot=G(H),Rt=G(u),Dt=G(K(H)),Ct=G(K(u)),Tt=Object.freeze({__Reactor:lt,transact:pt,setDebugMode:_t,transaction:vt,ticker:dt,isDerivable:yt,isAtom:gt,isProxy:wt,isDerivation:mt,derive:xt,
atom:bt,atomic:Et,atomically:kt,proxy:At,unpack:qt,struct:X,wrapPreviousState:Y,captureDereferences:B,or:Ot,mOr:Rt,and:Dt,mAnd:Ct}),jt={derive:function(t,e,n,r,i){var o=this;switch(arguments.length){case 0:throw new Error(".derive takes at least one argument");case 1:switch(typeof t){case"function":return xt(t,o);case"string":case"number":return xt(function(){return o.get()[t]});default:if(t instanceof Array)return t.map(function(t){return o.derive(t)});if(t instanceof RegExp)return xt(function(){return o.get().match(t)});if(f(t))return xt(function(){var e=t.get(),n=o.get();switch(typeof e){case"function":return e(n);case"string":case"number":return n[e];default:if(e instanceof RegExp)return n.match(e);throw Error("type error")}});throw Error("type error")}case 2:return xt(t,o,e);case 3:return xt(t,o,e,n);case 4:return xt(t,o,e,n,r);case 5:return xt(t,o,e,n,r,i);default:var u=[t,o].concat(s(arguments,1));return xt.apply(null,u)}},react:function(t,e){z(this,t,e)},mReact:function(e,n){var r=this.mThen(!0,!1);if(n&&"when"in n&&!0!==n.when){var i=n.when;if("function"==typeof i||!1===i)i=xt(i);else if(!f(i))throw new Error("when condition must be bool, function, or derivable");r=i.and(r)}return this.react(e,t({},n,{when:r}))},is:function(t){var e=this;return this.derive(function(n){return e.__equals(n,j(t))})},and:function(t){return this.derive(function(e){return e&&j(t)})},or:function(t){return this.derive(function(e){return e||j(t)})},then:function(t,e){return this.derive(function(n){return j(n?t:e)})},mThen:function(t,e){return this.derive(function(n){return j(u(n)?t:e)})},mOr:function(t){return this.mThen(this,t)},mDerive:function(t){if(1===arguments.length&&t instanceof Array){var e=this;return t.map(function(t){return e.mDerive(t)})}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})},withEquality:function(t){if(t){if("function"!=typeof t)throw new Error("equals must be function")}else t=null
;return c(this._clone(),t)},__equals:function(t,e){return(this._equals||n)(t,e)}};jt.switch=function(){var t=arguments,e=this;return this.derive(function(n){var r;for(r=0;r<t.length-1;r+=2)if(e.__equals(n,j(t[r])))return j(t[r+1]);if(r===t.length-1)return j(t[r])})};var Mt={update:function(t){var e=s(arguments,0);return e[0]=this.get(),this.set(t.apply(null,e))},proxy:function(t){var e=this;return new J({get:function(){return t.get(e.get())},set:function(n){e.set(t.set(e.get(),n))}})}};t(P.prototype,jt),t(J.prototype,jt,Mt),t(F.prototype,jt,Mt);var Pt=lt,Vt=pt,St=_t,It=vt,zt=dt,Ft=yt,Nt=gt,Jt=wt,Ut=mt,Xt=bt,Yt=Et,Bt=kt,Gt=At,Ht=xt,Kt=qt,Lt=X,Qt=Y,Wt=B,Zt=Ot,$t=Rt,te=Dt,ee=Ct;exports.__Reactor=Pt,exports.transact=Vt,exports.setDebugMode=St,exports.transaction=It,exports.ticker=zt,exports.isDerivable=Ft,exports.isAtom=Nt,exports.isProxy=Jt,exports.isDerivation=Ut,exports.derive=Ht,exports.atom=Xt,exports.atomic=Yt,exports.atomically=Bt,exports.proxy=Gt,exports.unpack=Kt,exports.struct=Lt,exports.wrapPreviousState=Qt,exports.captureDereferences=Wt,exports.or=Zt,exports.mOr=$t,exports.and=te,exports.mAnd=ee,exports.default=Tt;
//# sourceMappingURL=dist/derivable.min.js.map
//# sourceMappingURL=derivable.min.js.map

@@ -86,3 +86,3 @@ // UMD loader

var DERIVATION = "DERIVATION";
var LENS = "LENS";
var PROXY = "PROXY";
var REACTOR = "REACTOR";

@@ -94,15 +94,15 @@

x._type === ATOM ||
x._type === LENS);
x._type === PROXY);
}
function isAtom$1 (x) {
return x && (x._type === ATOM || x._type === LENS);
return x && (x._type === ATOM || x._type === PROXY);
}
function isDerivation$1 (x) {
return x && (x._type === DERIVATION || x._type === LENS);
return x && (x._type === DERIVATION || x._type === PROXY);
}
function isLensed$1 (x) {
return x && x._type === LENS;
function isProxy$1 (x) {
return x && x._type === PROXY;
}

@@ -176,3 +176,3 @@

case DERIVATION:
case LENS:
case PROXY:
if (child._state !== UNKNOWN) {

@@ -340,2 +340,32 @@ child._state = UNKNOWN;

/**
* dereferences a thing if it is dereferencable, otherwise just returns it.
*/
function _unpack (thing) {
if (isDerivable$1(thing)) {
return thing.get();
} else {
return thing;
}
};
function deepUnpack (thing) {
if (isDerivable$1(thing)) {
return thing.get();
} else if (thing instanceof Array) {
return thing.map(deepUnpack);
} else if (thing.constructor === Object) {
var result = {};
var keys$$ = keys(thing);
for (var i = keys$$.length; i--;) {
var prop = keys$$[i];
result[prop] = deepUnpack(thing[prop]);
}
return result;
} else {
return thing;
}
};
function Derivation (deriver) {

@@ -357,3 +387,3 @@ this._deriver = deriver;

_clone: function () {
return setEquals(_derivation(this._deriver), this._equals);
return setEquals(_derive(this._deriver), this._equals);
},

@@ -456,4 +486,49 @@

function _derivation (deriver) {
return new Derivation(deriver);
function _derive (f, a, b, c, d) {
if (f instanceof Array) {
// Template string tag for derivable strings
var args = slice(arguments, 1);
return _derive(function () {
var s = "";
for (var i=0; i < f.length; i++) {
s += f[i];
if (i < args.length) {
s += _unpack(args[i]);
}
}
return s;
});
} else {
switch (arguments.length) {
case 0:
throw new Error('derive takes at least one argument');
case 1:
return new Derivation(f);
case 2:
return new Derivation(function () {
return f(_unpack(a));
});
case 3:
return new Derivation(function () {
return f(_unpack(a), _unpack(b));
});
case 4:
return new Derivation(function () {
return f(_unpack(a), _unpack(b), _unpack(c));
});
case 5:
return new Derivation(function () {
return f(_unpack(a),
_unpack(b),
_unpack(c),
_unpack(d));
});
default:
var args = slice(arguments, 1);
return new Derivation(function () {
return f.apply(null, args.map(_unpack));
});
}
}
}

@@ -544,5 +619,5 @@

if (typeof fOrD === 'function') {
return _derivation(fOrD);
return _derive(function () { return fOrD(derivable); });
} else if (typeof fOrD === 'boolean') {
return _derivation(function () { return fOrD; });
return _derive(function () { return fOrD; });
} else {

@@ -575,3 +650,3 @@ throw Error('react ' + name + ' condition must be derivable, got: ' + JSON.stringify(fOrD));

var $whenUntil = _derivation(function () {
var $whenUntil = _derive(function () {
return {

@@ -656,11 +731,11 @@ until: $until.get(),

function Lens (descriptor) {
function Proxy (descriptor) {
Derivation.call(this, descriptor.get);
this._lensDescriptor = descriptor;
this._type = LENS;
this._proxyMapping = descriptor;
this._type = PROXY;
}
assign(Lens.prototype, Derivation.prototype, {
assign(Proxy.prototype, Derivation.prototype, {
_clone: function () {
return setEquals(new Lens(this._lensDescriptor), this._equals);
return setEquals(new Proxy(this._proxyMapping), this._equals);
},

@@ -671,3 +746,3 @@

atomically$1(function () {
that._lensDescriptor.set(value);
that._proxyMapping.set(value);
});

@@ -678,4 +753,4 @@ return this;

function lens$2 (descriptor) {
return new Lens(descriptor);
function proxy$2 (descriptor) {
return new Proxy(descriptor);
}

@@ -690,72 +765,14 @@

var isAtom$2 = isAtom$1;
var isLensed$2 = isLensed$1;
var isProxy$2 = isProxy$1;
var isDerivation$2 = isDerivation$1;
var derivation$1 = _derivation;
var derive$1 = _derive;
var atom$1 = atom$2;
var atomic$2 = atomic$1;
var atomically$2 = atomically$1;
var lens$1 = lens$2;
var proxy$1 = proxy$2;
var unpack$1 = _unpack;
/**
* Template string tag for derivable strings
*/
function derive$1 (parts) {
var args = slice(arguments, 1);
return derivation$1(function () {
var s = "";
for (var i=0; i < parts.length; i++) {
s += parts[i];
if (i < args.length) {
s += unpack$1(args[i]);
}
}
return s;
});
};
/**
* dereferences a thing if it is dereferencable, otherwise just returns it.
*/
function unpack$1 (thing) {
if (isDerivable$2(thing)) {
return thing.get();
} else {
return thing;
}
};
/**
* lifts a non-monadic function to work on derivables
*/
function lift$1 (f) {
return function () {
var args = arguments;
var that = this;
return derivation$1(function () {
return f.apply(that, Array.prototype.map.call(args, unpack$1));
});
};
};
function deepUnpack (thing) {
if (isDerivable$2(thing)) {
return thing.get();
} else if (thing instanceof Array) {
return thing.map(deepUnpack);
} else if (thing.constructor === Object) {
var result = {};
var keys$$ = keys(thing);
for (var i = keys$$.length; i--;) {
var prop = keys$$[i];
result[prop] = deepUnpack(thing[prop]);
}
return result;
} else {
return thing;
}
}
function struct$1 (arg) {
if (arg.constructor === Object || arg instanceof Array) {
return derivation$1(function () {
return derive$1(function () {
return deepUnpack(arg);

@@ -791,3 +808,3 @@ });

var args = arguments;
return derivation$1(function () {
return derive$1(function () {
var val;

@@ -820,12 +837,10 @@ for (var i = 0; i < args.length; i++) {

isAtom: isAtom$2,
isLensed: isLensed$2,
isProxy: isProxy$2,
isDerivation: isDerivation$2,
derivation: derivation$1,
derive: derive$1,
atom: atom$1,
atomic: atomic$2,
atomically: atomically$2,
lens: lens$1,
derive: derive$1,
proxy: proxy$1,
unpack: unpack$1,
lift: lift$1,
struct: struct$1,

@@ -853,9 +868,7 @@ wrapPreviousState: wrapPreviousState$1,

case 'function':
return derivation$1(function () {
return f(that.get());
});
return derive$1(f, that);
case 'string':
case 'number':
return derivation$1(function () {
return that.get()[unpack$1(f)];
return derive$1(function () {
return that.get()[f];
});

@@ -868,7 +881,7 @@ default:

} else if (f instanceof RegExp) {
return derivation$1(function () {
return derive$1(function () {
return that.get().match(f);
});
} else if (isDerivable$1(f)) {
return derivation$1(function () {
return derive$1(function () {
var deriver = f.get();

@@ -893,31 +906,14 @@ var thing = that.get();

}
}
}
case 2:
return derivation$1(function () {
return f(that.get(), unpack$1(a));
});
return derive$1(f, that, a);
case 3:
return derivation$1(function () {
return f(that.get(), unpack$1(a), unpack$1(b));
});
return derive$1(f, that, a, b);
case 4:
return derivation$1(function () {
return f(that.get(),
unpack$1(a),
unpack$1(b),
unpack$1(c));
});
return derive$1(f, that, a, b, c);
case 5:
return derivation$1(function () {
return f(that.get(),
unpack$1(a),
unpack$1(b),
unpack$1(c),
unpack$1(d));
});
return derive$1(f, that, a, b, c, d);
default:
var args = ([that]).concat(slice(arguments, 1));
return derivation$1(function () {
return f.apply(null, args.map(unpack$1));
});
var args = ([f, that]).concat(slice(arguments, 1));
return derive$1.apply(null, args);
}

@@ -935,3 +931,3 @@ },

if (typeof when === 'function' || when === false) {
when = derivation$1(when);
when = derive$1(when);
} else if (!isDerivable$1(when)) {

@@ -948,3 +944,3 @@ throw new Error('when condition must be bool, function, or derivable');

return this.derive(function (x) {
return that.__equals(x, unpack$1(other));
return that.__equals(x, _unpack(other));
});

@@ -954,7 +950,7 @@ },

and: function (other) {
return this.derive(function (x) {return x && unpack$1(other);});
return this.derive(function (x) {return x && _unpack(other);});
},
or: function (other) {
return this.derive(function (x) {return x || unpack$1(other);});
return this.derive(function (x) {return x || _unpack(other);});
},

@@ -964,3 +960,3 @@

return this.derive(function (x) {
return unpack$1(x ? thenClause : elseClause);
return _unpack(x ? thenClause : elseClause);
});

@@ -971,3 +967,3 @@ },

return this.derive(function (x) {
return unpack$1(some(x) ? thenClause : elseClause);
return _unpack(some(x) ? thenClause : elseClause);
});

@@ -1020,8 +1016,8 @@ },

for (i = 0; i < args.length-1; i+=2) {
if (that.__equals(x, unpack$1(args[i]))) {
return unpack$1(args[i+1]);
if (that.__equals(x, _unpack(args[i]))) {
return _unpack(args[i+1]);
}
}
if (i === args.length - 1) {
return unpack$1(args[i]);
return _unpack(args[i]);
}

@@ -1032,3 +1028,3 @@ });

var mutablePrototype = {
swap: function (f) {
update: function (f) {
var args = slice(arguments, 0);

@@ -1038,10 +1034,10 @@ args[0] = this.get();

},
lens: function (monoLensDescriptor) {
proxy: function (monoProxyMapping) {
var that = this;
return new Lens({
return new Proxy({
get: function () {
return monoLensDescriptor.get(that.get());
return monoProxyMapping.get(that.get());
},
set: function (val) {
that.set(monoLensDescriptor.set(that.get(), val));
that.set(monoProxyMapping.set(that.get(), val));
}

@@ -1053,3 +1049,3 @@ });

assign(Derivation.prototype, derivablePrototype);
assign(Lens.prototype, derivablePrototype, mutablePrototype);
assign(Proxy.prototype, derivablePrototype, mutablePrototype);
assign(Atom.prototype, derivablePrototype, mutablePrototype);

@@ -1065,12 +1061,10 @@

var isAtom = isAtom$2;
var isLensed = isLensed$2;
var isProxy = isProxy$2;
var isDerivation = isDerivation$2;
var derivation = derivation$1;
var atom = atom$1;
var atomic = atomic$2;
var atomically = atomically$2;
var lens = lens$1;
var proxy = proxy$1;
var derive = derive$1;
var unpack = unpack$1;
var lift = lift$1;
var struct = struct$1;

@@ -1091,12 +1085,10 @@ var wrapPreviousState = wrapPreviousState$1;

exports.isAtom = isAtom;
exports.isLensed = isLensed;
exports.isProxy = isProxy;
exports.isDerivation = isDerivation;
exports.derivation = derivation;
exports.derive = derive;
exports.atom = atom;
exports.atomic = atomic;
exports.atomically = atomically;
exports.lens = lens;
exports.derive = derive;
exports.proxy = proxy;
exports.unpack = unpack;
exports.lift = lift;
exports.struct = struct;

@@ -1103,0 +1095,0 @@ exports.wrapPreviousState = wrapPreviousState;

@@ -1,8 +0,8 @@

!function(t,e){"use strict";t&&"function"==typeof t.define&&t.define.amd?t.define(["exports"],e):e("undefined"!=typeof exports?exports:t.Derivable={})}(this,function(t){"use strict";function e(t){for(var e=1;e<arguments.length;e++)for(var n=arguments[e],r=Z(n||{}),i=r.length;i--;){var u=r[i];t[u]=n[u]}return t}function n(t,e){return t===e?0!==t||1/t===1/e:t!==t&&e!==e}function r(t,e){return n(t,e)||t&&"function"==typeof t.equals&&t.equals(e)}function i(t,e){var n=t.indexOf(e);n<0&&t.push(e)}function u(t,e){var n=t.indexOf(e);n>=0&&t.splice(n,1)}function o(){return $++}function s(t,e){return Array.prototype.slice.call(t,e)}function a(t){return null!==t&&void 0!==t}function c(t){et=!!t}function f(t,e){return t._equals=e,t}function h(t){return t&&(t._type===rt||t._type===nt||t._type===it)}function l(t){return t&&(t._type===nt||t._type===it)}function _(t){return t&&(t._type===rt||t._type===it)}function p(t){return t&&t._type===it}function v(t,e){ft.push({parents:e,offset:0,child:t}),ht=t}function d(){return ft[ft.length-1]}function y(){ft.pop(),ht=0===ft.length?null:ft[ft.length-1].child}function g(t){if(null!==ht){var e=ft[ft.length-1];if(e.parents[e.offset]===t)e.offset++;else{var n=e.parents.indexOf(t);if(n===-1)void 0!==ht&&i(t._activeChildren,ht),e.offset===e.parents.length?e.parents.push(t):(e.parents.push(e.parents[e.offset]),e.parents[e.offset]=t),e.offset++;else if(n>e.offset){var r=e.parents[n];e.parents[n]=e.parents[e.offset],e.parents[e.offset]=r,e.offset++}}}}function m(t,e){for(var n=0,r=t._activeChildren.length;n<r;n++){var i=t._activeChildren[n];switch(i._type){case rt:case it:i._state!==ot&&(i._state=ot,m(i,e));break;case ut:e.push(i)}}}function w(t){for(var e=0,n=t.length;e<n;e++){var r=t[e];if(r._reacting)throw new Error("Synchronous cyclical reactions disallowed. Use setImmediate.");r._maybeReact()}}function b(){throw lt}function E(t){this.parent=t,this.id2originalValue={},this.modifiedAtoms=[]}function k(t){null!==_t&&(t._id in _t.id2originalValue||(_t.modifiedAtoms.push(t),_t.id2originalValue[t._id]=t._value));
}function A(){return null!==_t}function q(t){C();try{t.call(null,b)}catch(e){if(T(),e!==lt)throw e;return}x()}function O(t){A()?t():q(t)}function D(t){return function(){var e,n=s(arguments,0),r=this;return q(function(){e=t.apply(r,n)}),e}}function R(t){return function(){var e,n=s(arguments,0),r=this;return O(function(){e=t.apply(r,n)}),e}}function C(){_t=new E(_t)}function x(){var t=_t;if(_t=t.parent,null===_t){var e=[];t.modifiedAtoms.forEach(function(n){n.__equals(n._value,t.id2originalValue[n._id])?n._state=at:(n._state=st,m(n,e))}),w(e),t.modifiedAtoms.forEach(function(t){t._state=at})}}function T(){var t=_t;_t=t.parent,t.modifiedAtoms.forEach(function(e){e._value=t.id2originalValue[e._id],e._state=at,m(e,[])})}function j(){0===pt&&C(),pt++;var t=!1;return{tick:function(){if(t)throw new Error("trying to use ticker after release");x(),C()},reset:function(){if(t)throw new Error("trying to use ticker after release");T(),C()},release:function(){if(t)throw new Error("ticker already released");pt--,t=!0,0===pt&&x()}}}function V(t){this._deriver=t,this._parents=null,this._type=rt,this._value=tt,this._equals=null,this._activeChildren=[],this._state=ct,et&&(this.stack=Error().stack)}function S(t,e){if(u(t._activeChildren,e),0===t._activeChildren.length&&null!=t._parents){for(var n=t._parents.length,r=0;r<n;r++)S(t._parents[r],t);t._parents=null,t._state=ct}}function M(t){return new V(t)}function I(t,e,n){this._parent=t,this.react=e,this._governor=n||null,this._active=!1,this._reacting=!1,this._type=ut,et&&(this.stack=Error().stack)}function L(t,n,r){function i(t,e){if(!h(t)){if("function"==typeof t)return M(t);if("boolean"==typeof t)return M(function(){return t});throw Error("react "+e+" condition must be derivable, got: "+JSON.stringify(t))}return t}if("function"!=typeof n)throw Error("the first argument to .react must be a function");r=e({once:!1,from:!0,until:!1,when:!0,skipFirst:!1},r);var u=r.skipFirst,o=new I(t,function(t){u?u=!1:(n(t),r.once&&(this.stop(),f.stop()))}),s=i(r.until,"until"),a=i(r.when,"when"),c=M(function(){
return{until:s.get(),when:a.get()}}),f=new I(c,function(t){t.until?(o.stop(),this.stop()):t.when?o._active||o.start().force():o._active&&o.stop()});o._governor=f;var l=i(r.from,"from"),_=new I(l,function(t){t&&(f.start().force(),this.stop())});_.start().force()}function N(t){return this._id=o(),this._activeChildren=[],this._value=t,this._state=at,this._type=nt,this._equals=null,this}function P(t){return new N(t)}function z(t){V.call(this,t.get),this._lensDescriptor=t,this._type=it}function F(t){return new z(t)}function J(t){var e=s(arguments,1);return At(function(){for(var n="",r=0;r<t.length;r++)n+=t[r],r<e.length&&(n+=U(e[r]));return n})}function U(t){return wt(t)?t.get():t}function B(t){return function(){var e=arguments,n=this;return At(function(){return t.apply(n,Array.prototype.map.call(e,U))})}}function G(t){if(wt(t))return t.get();if(t instanceof Array)return t.map(G);if(t.constructor===Object){for(var e={},n=Z(t),r=n.length;r--;){var i=n[r];e[i]=G(t[i])}return e}return t}function H(t){if(t.constructor===Object||t instanceof Array)return At(function(){return G(t)});throw new Error("`struct` expects plain Object or Array")}function K(t,e){var n=e;return function(e){var r=t.call(this,e,n);return n=e,r}}function Q(t){var e=[];v(void 0,e);try{t()}finally{y()}return e}function W(t){return function(){var e=arguments;return At(function(){for(var n,r=0;r<e.length&&(n=U(e[r]),!t(n));r++);return n})}}function X(t){return t}function Y(t){return function(e){return!t(e)}}Object.defineProperty(t,"__esModule",{value:!0});var Z=Object.keys,$=0,tt=Object.freeze({equals:function(){return!1}}),et=!1,nt="ATOM",rt="DERIVATION",it="LENS",ut="REACTOR",ot=0,st=1,at=2,ct=3,ft=[],ht=null,lt={},_t=null,pt=0;e(V.prototype,{_clone:function(){return f(M(this._deriver),this._equals)},_forceEval:function(){var t,e=this,n=null;try{if(null===this._parents&&(this._parents=[]),v(this,this._parents),et)try{n=e._deriver()}catch(r){throw console.error(e.stack),r}else n=e._deriver();t=d().offset}finally{y()}this._state=this.__equals(n,this._value)?at:st;
for(var i=t,u=this._parents.length;i<u;i++){var o=this._parents[i];S(o,this),this._parents[i]=null}this._parents.length=t,this._value=n},_update:function(){if(null===this._parents)this._forceEval();else if(this._state===ot){for(var t=this._parents.length,e=0;e<t;e++){var n=this._parents[e];if(n._state===ot&&n._update(),n._state===st){this._forceEval();break}}this._state===ot&&(this._state=at)}},get:function(){if(g(this),this._activeChildren.length>0)this._update();else{v(void 0,[]);try{this._value=this._deriver()}finally{y()}}return this._value}}),e(I.prototype,{start:function(){return this._active=!0,i(this._parent._activeChildren,this),this._parent.get(),this},_force:function(t){try{this._reacting=!0,this.react(t)}catch(e){throw et&&console.error(this.stack),e}finally{this._reacting=!1}},force:function(){return this._force(this._parent.get()),this},_maybeReact:function(){if(!this._reacting&&this._active&&(null!==this._governor&&this._governor._maybeReact(),this._active)){var t=this._parent.get();this._parent._state===st&&this._force(t)}},stop:function(){return S(this._parent,this),this._active=!1,this}}),e(N.prototype,{_clone:function(){return f(P(this._value),this._equals)},set:function(t){k(this);var e=this._value;if(this._value=t,!A()&&!this.__equals(t,e))try{this._state=st;var n=[];m(this,n),w(n)}finally{this._state=at}},get:function(){return g(this),this._value}}),e(z.prototype,V.prototype,{_clone:function(){return f(new z(this._lensDescriptor),this._equals)},set:function(t){var e=this;return O(function(){e._lensDescriptor.set(t)}),this}});var vt=I,dt=q,yt=c,gt=D,mt=j,wt=h,bt=l,Et=p,kt=_,At=M,qt=P,Ot=R,Dt=O,Rt=F,Ct=W(X),xt=W(a),Tt=W(Y(X)),jt=W(Y(a)),Vt=Object.freeze({__Reactor:vt,transact:dt,setDebugMode:yt,transaction:gt,ticker:mt,isDerivable:wt,isAtom:bt,isLensed:Et,isDerivation:kt,derivation:At,atom:qt,atomic:Ot,atomically:Dt,lens:Rt,derive:J,unpack:U,lift:B,struct:H,wrapPreviousState:K,captureDereferences:Q,or:Ct,mOr:xt,and:Tt,mAnd:jt}),St={derive:function(t,e,n,r,i){var u=this;switch(arguments.length){
case 0:throw new Error(".derive takes at least one argument");case 1:switch(typeof t){case"function":return At(function(){return t(u.get())});case"string":case"number":return At(function(){return u.get()[U(t)]});default:if(t instanceof Array)return t.map(function(t){return u.derive(t)});if(t instanceof RegExp)return At(function(){return u.get().match(t)});if(h(t))return At(function(){var e=t.get(),n=u.get();switch(typeof e){case"function":return e(n);case"string":case"number":return n[e];default:if(e instanceof RegExp)return n.match(e);throw Error("type error")}});throw Error("type error")}case 2:return At(function(){return t(u.get(),U(e))});case 3:return At(function(){return t(u.get(),U(e),U(n))});case 4:return At(function(){return t(u.get(),U(e),U(n),U(r))});case 5:return At(function(){return t(u.get(),U(e),U(n),U(r),U(i))});default:var o=[u].concat(s(arguments,1));return At(function(){return t.apply(null,o.map(U))})}},react:function(t,e){L(this,t,e)},mReact:function(t,n){var r=this.mThen(!0,!1);if(n&&"when"in n&&n.when!==!0){var i=n.when;if("function"==typeof i||i===!1)i=At(i);else if(!h(i))throw new Error("when condition must be bool, function, or derivable");r=i.and(r)}return this.react(t,e({},n,{when:r}))},is:function(t){var e=this;return this.derive(function(n){return e.__equals(n,U(t))})},and:function(t){return this.derive(function(e){return e&&U(t)})},or:function(t){return this.derive(function(e){return e||U(t)})},then:function(t,e){return this.derive(function(n){return U(n?t:e)})},mThen:function(t,e){return this.derive(function(n){return U(a(n)?t:e)})},mOr:function(t){return this.mThen(this,t)},mDerive:function(t){if(1===arguments.length&&t instanceof Array){var e=this;return t.map(function(t){return e.mDerive(t)})}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})},withEquality:function(t){if(t){if("function"!=typeof t)throw new Error("equals must be function")}else t=null;return f(this._clone(),t)},__equals:function(t,e){
return(this._equals||r)(t,e)}};St["switch"]=function(){var t=arguments,e=this;return this.derive(function(n){var r;for(r=0;r<t.length-1;r+=2)if(e.__equals(n,U(t[r])))return U(t[r+1]);if(r===t.length-1)return U(t[r])})};var Mt={swap:function(t){var e=s(arguments,0);return e[0]=this.get(),this.set(t.apply(null,e))},lens:function(t){var e=this;return new z({get:function(){return t.get(e.get())},set:function(n){e.set(t.set(e.get(),n))}})}};e(V.prototype,St),e(z.prototype,St,Mt),e(N.prototype,St,Mt);var It=vt,Lt=dt,Nt=yt,Pt=gt,zt=mt,Ft=wt,Jt=bt,Ut=Et,Bt=kt,Gt=At,Ht=qt,Kt=Ot,Qt=Dt,Wt=Rt,Xt=J,Yt=U,Zt=B,$t=H,te=K,ee=Q,ne=Ct,re=xt,ie=Tt,ue=jt;t.__Reactor=It,t.transact=Lt,t.setDebugMode=Nt,t.transaction=Pt,t.ticker=zt,t.isDerivable=Ft,t.isAtom=Jt,t.isLensed=Ut,t.isDerivation=Bt,t.derivation=Gt,t.atom=Ht,t.atomic=Kt,t.atomically=Qt,t.lens=Wt,t.derive=Xt,t.unpack=Yt,t.lift=Zt,t.struct=$t,t.wrapPreviousState=te,t.captureDereferences=ee,t.or=ne,t.mOr=re,t.and=ie,t.mAnd=ue,t["default"]=Vt});
!function(t,e){"use strict";t&&"function"==typeof t.define&&t.define.amd?t.define(["exports"],e):e("undefined"!=typeof exports?exports:t.Derivable={})}(this,function(t){"use strict";function e(t){for(var e=1;e<arguments.length;e++)for(var n=arguments[e],r=Q(n||{}),i=r.length;i--;){var u=r[i];t[u]=n[u]}return t}function n(t,e){return t===e?0!==t||1/t==1/e:t!==t&&e!==e}function r(t,e){return n(t,e)||t&&"function"==typeof t.equals&&t.equals(e)}function i(t,e){t.indexOf(e)<0&&t.push(e)}function u(t,e){var n=t.indexOf(e);n>=0&&t.splice(n,1)}function o(){return W++}function s(t,e){return Array.prototype.slice.call(t,e)}function a(t){return null!==t&&void 0!==t}function c(t){$=!!t}function f(t,e){return t._equals=e,t}function h(t){return t&&(t._type===et||t._type===tt||t._type===nt)}function l(t){return t&&(t._type===tt||t._type===nt)}function _(t){return t&&(t._type===et||t._type===nt)}function p(t){return t&&t._type===nt}function v(t,e){at.push({parents:e,offset:0,child:t}),ct=t}function d(){return at[at.length-1]}function y(){at.pop(),ct=0===at.length?null:at[at.length-1].child}function g(t){if(null!==ct){var e=at[at.length-1];if(e.parents[e.offset]===t)e.offset++;else{var n=e.parents.indexOf(t);if(-1===n)void 0!==ct&&i(t._activeChildren,ct),e.offset===e.parents.length?e.parents.push(t):(e.parents.push(e.parents[e.offset]),e.parents[e.offset]=t),e.offset++;else if(n>e.offset){var r=e.parents[n];e.parents[n]=e.parents[e.offset],e.parents[e.offset]=r,e.offset++}}}}function w(t,e){for(var n=0,r=t._activeChildren.length;n<r;n++){var i=t._activeChildren[n];switch(i._type){case et:case nt:i._state!==it&&(i._state=it,w(i,e));break;case rt:e.push(i)}}}function m(t){for(var e=0,n=t.length;e<n;e++){var r=t[e];if(r._reacting)throw new Error("Synchronous cyclical reactions disallowed. Use setImmediate.");r._maybeReact()}}function b(){throw ft}function E(t){this.parent=t,this.id2originalValue={},this.modifiedAtoms=[]}function k(t){null!==ht&&(t._id in ht.id2originalValue||(ht.modifiedAtoms.push(t),
ht.id2originalValue[t._id]=t._value))}function A(){return null!==ht}function q(t){R();try{t.call(null,b)}catch(t){if(T(),t!==ft)throw t;return}C()}function O(t){A()?t():q(t)}function x(t){return function(){var e,n=s(arguments,0),r=this;return q(function(){e=t.apply(r,n)}),e}}function D(t){return function(){var e,n=s(arguments,0),r=this;return O(function(){e=t.apply(r,n)}),e}}function R(){ht=new E(ht)}function C(){var t=ht;if(null===(ht=t.parent)){var e=[];t.modifiedAtoms.forEach(function(n){n.__equals(n._value,t.id2originalValue[n._id])?n._state=ot:(n._state=ut,w(n,e))}),m(e),t.modifiedAtoms.forEach(function(t){t._state=ot})}}function T(){var t=ht;ht=t.parent,t.modifiedAtoms.forEach(function(e){e._value=t.id2originalValue[e._id],e._state=ot,w(e,[])})}function j(){0===lt&&R(),lt++;var t=!1;return{tick:function(){if(t)throw new Error("trying to use ticker after release");C(),R()},reset:function(){if(t)throw new Error("trying to use ticker after release");T(),R()},release:function(){if(t)throw new Error("ticker already released");lt--,t=!0,0===lt&&C()}}}function M(t){return h(t)?t.get():t}function P(t){if(h(t))return t.get();if(t instanceof Array)return t.map(P);if(t.constructor===Object){for(var e={},n=Q(t),r=n.length;r--;){var i=n[r];e[i]=P(t[i])}return e}return t}function V(t){this._deriver=t,this._parents=null,this._type=et,this._value=Z,this._equals=null,this._activeChildren=[],this._state=st,$&&(this.stack=Error().stack)}function S(t,e){if(u(t._activeChildren,e),0===t._activeChildren.length&&null!=t._parents){for(var n=t._parents.length,r=0;r<n;r++)S(t._parents[r],t);t._parents=null,t._state=st}}function I(t,e,n,r,i){if(t instanceof Array){var u=s(arguments,1);return I(function(){for(var e="",n=0;n<t.length;n++)e+=t[n],n<u.length&&(e+=M(u[n]));return e})}switch(arguments.length){case 0:throw new Error("derive takes at least one argument");case 1:return new V(t);case 2:return new V(function(){return t(M(e))});case 3:return new V(function(){return t(M(e),M(n))});case 4:return new V(function(){
return t(M(e),M(n),M(r))});case 5:return new V(function(){return t(M(e),M(n),M(r),M(i))});default:var u=s(arguments,1);return new V(function(){return t.apply(null,u.map(M))})}}function z(t,e,n){this._parent=t,this.react=e,this._governor=n||null,this._active=!1,this._reacting=!1,this._type=rt,$&&(this.stack=Error().stack)}function F(t,n,r){function i(e,n){if(!h(e)){if("function"==typeof e)return I(function(){return e(t)});if("boolean"==typeof e)return I(function(){return e});throw Error("react "+n+" condition must be derivable, got: "+JSON.stringify(e))}return e}if("function"!=typeof n)throw Error("the first argument to .react must be a function");r=e({once:!1,from:!0,until:!1,when:!0,skipFirst:!1},r);var u=r.skipFirst,o=new z(t,function(t){u?u=!1:(n(t),r.once&&(this.stop(),f.stop()))}),s=i(r.until,"until"),a=i(r.when,"when"),c=I(function(){return{until:s.get(),when:a.get()}}),f=new z(c,function(t){t.until?(o.stop(),this.stop()):t.when?o._active||o.start().force():o._active&&o.stop()});o._governor=f,new z(i(r.from,"from"),function(t){t&&(f.start().force(),this.stop())}).start().force()}function N(t){return this._id=o(),this._activeChildren=[],this._value=t,this._state=ot,this._type=tt,this._equals=null,this}function J(t){return new N(t)}function U(t){V.call(this,t.get),this._proxyMapping=t,this._type=nt}function X(t){return new U(t)}function Y(t){if(t.constructor===Object||t instanceof Array)return Et(function(){return P(t)});throw new Error("`struct` expects plain Object or Array")}function B(t,e){var n=e;return function(e){var r=t.call(this,e,n);return n=e,r}}function G(t){var e=[];v(void 0,e);try{t()}finally{y()}return e}function H(t){return function(){var e=arguments;return Et(function(){for(var n,r=0;r<e.length&&(n=xt(e[r]),!t(n));r++);return n})}}function K(t){return t}function L(t){return function(e){return!t(e)}}Object.defineProperty(t,"__esModule",{value:!0});var Q=Object.keys,W=0,Z=Object.freeze({equals:function(){return!1}
}),$=!1,tt="ATOM",et="DERIVATION",nt="PROXY",rt="REACTOR",it=0,ut=1,ot=2,st=3,at=[],ct=null,ft={},ht=null,lt=0;e(V.prototype,{_clone:function(){return f(I(this._deriver),this._equals)},_forceEval:function(){var t,e=this,n=null;try{if(null===this._parents&&(this._parents=[]),v(this,this._parents),$)try{n=e._deriver()}catch(t){throw console.error(e.stack),t}else n=e._deriver();t=d().offset}finally{y()}this._state=this.__equals(n,this._value)?ot:ut;for(var r=t,i=this._parents.length;r<i;r++){S(this._parents[r],this),this._parents[r]=null}this._parents.length=t,this._value=n},_update:function(){if(null===this._parents)this._forceEval();else if(this._state===it){for(var t=this._parents.length,e=0;e<t;e++){var n=this._parents[e];if(n._state===it&&n._update(),n._state===ut){this._forceEval();break}}this._state===it&&(this._state=ot)}},get:function(){if(g(this),this._activeChildren.length>0)this._update();else{v(void 0,[]);try{this._value=this._deriver()}finally{y()}}return this._value}}),e(z.prototype,{start:function(){return this._active=!0,i(this._parent._activeChildren,this),this._parent.get(),this},_force:function(t){try{this._reacting=!0,this.react(t)}catch(t){throw $&&console.error(this.stack),t}finally{this._reacting=!1}},force:function(){return this._force(this._parent.get()),this},_maybeReact:function(){if(!this._reacting&&this._active&&(null!==this._governor&&this._governor._maybeReact(),this._active)){var t=this._parent.get();this._parent._state===ut&&this._force(t)}},stop:function(){return S(this._parent,this),this._active=!1,this}}),e(N.prototype,{_clone:function(){return f(J(this._value),this._equals)},set:function(t){k(this);var e=this._value;if(this._value=t,!A()&&!this.__equals(t,e))try{this._state=ut;var n=[];w(this,n),m(n)}finally{this._state=ot}},get:function(){return g(this),this._value}}),e(U.prototype,V.prototype,{_clone:function(){return f(new U(this._proxyMapping),this._equals)},set:function(t){var e=this;return O(function(){e._proxyMapping.set(t)}),this}})
;var _t=z,pt=q,vt=c,dt=x,yt=j,gt=h,wt=l,mt=p,bt=_,Et=I,kt=J,At=D,qt=O,Ot=X,xt=M,Dt=H(K),Rt=H(a),Ct=H(L(K)),Tt=H(L(a)),jt=Object.freeze({__Reactor:_t,transact:pt,setDebugMode:vt,transaction:dt,ticker:yt,isDerivable:gt,isAtom:wt,isProxy:mt,isDerivation:bt,derive:Et,atom:kt,atomic:At,atomically:qt,proxy:Ot,unpack:xt,struct:Y,wrapPreviousState:B,captureDereferences:G,or:Dt,mOr:Rt,and:Ct,mAnd:Tt}),Mt={derive:function(t,e,n,r,i){var u=this;switch(arguments.length){case 0:throw new Error(".derive takes at least one argument");case 1:switch(typeof t){case"function":return Et(t,u);case"string":case"number":return Et(function(){return u.get()[t]});default:if(t instanceof Array)return t.map(function(t){return u.derive(t)});if(t instanceof RegExp)return Et(function(){return u.get().match(t)});if(h(t))return Et(function(){var e=t.get(),n=u.get();switch(typeof e){case"function":return e(n);case"string":case"number":return n[e];default:if(e instanceof RegExp)return n.match(e);throw Error("type error")}});throw Error("type error")}case 2:return Et(t,u,e);case 3:return Et(t,u,e,n);case 4:return Et(t,u,e,n,r);case 5:return Et(t,u,e,n,r,i);default:var o=[t,u].concat(s(arguments,1));return Et.apply(null,o)}},react:function(t,e){F(this,t,e)},mReact:function(t,n){var r=this.mThen(!0,!1);if(n&&"when"in n&&!0!==n.when){var i=n.when;if("function"==typeof i||!1===i)i=Et(i);else if(!h(i))throw new Error("when condition must be bool, function, or derivable");r=i.and(r)}return this.react(t,e({},n,{when:r}))},is:function(t){var e=this;return this.derive(function(n){return e.__equals(n,M(t))})},and:function(t){return this.derive(function(e){return e&&M(t)})},or:function(t){return this.derive(function(e){return e||M(t)})},then:function(t,e){return this.derive(function(n){return M(n?t:e)})},mThen:function(t,e){return this.derive(function(n){return M(a(n)?t:e)})},mOr:function(t){return this.mThen(this,t)},mDerive:function(t){if(1===arguments.length&&t instanceof Array){var e=this;return t.map(function(t){return e.mDerive(t)})}
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})},withEquality:function(t){if(t){if("function"!=typeof t)throw new Error("equals must be function")}else t=null;return f(this._clone(),t)},__equals:function(t,e){return(this._equals||r)(t,e)}};Mt.switch=function(){var t=arguments,e=this;return this.derive(function(n){var r;for(r=0;r<t.length-1;r+=2)if(e.__equals(n,M(t[r])))return M(t[r+1]);if(r===t.length-1)return M(t[r])})};var Pt={update:function(t){var e=s(arguments,0);return e[0]=this.get(),this.set(t.apply(null,e))},proxy:function(t){var e=this;return new U({get:function(){return t.get(e.get())},set:function(n){e.set(t.set(e.get(),n))}})}};e(V.prototype,Mt),e(U.prototype,Mt,Pt),e(N.prototype,Mt,Pt);var Vt=_t,St=pt,It=vt,zt=dt,Ft=yt,Nt=gt,Jt=wt,Ut=mt,Xt=bt,Yt=kt,Bt=At,Gt=qt,Ht=Ot,Kt=Et,Lt=xt,Qt=Y,Wt=B,Zt=G,$t=Dt,te=Rt,ee=Ct,ne=Tt;t.__Reactor=Vt,t.transact=St,t.setDebugMode=It,t.transaction=zt,t.ticker=Ft,t.isDerivable=Nt,t.isAtom=Jt,t.isProxy=Ut,t.isDerivation=Xt,t.derive=Kt,t.atom=Yt,t.atomic=Bt,t.atomically=Gt,t.proxy=Ht,t.unpack=Lt,t.struct=Qt,t.wrapPreviousState=Wt,t.captureDereferences=Zt,t.or=$t,t.mOr=te,t.and=ee,t.mAnd=ne,t.default=jt});
//# sourceMappingURL=dist/derivable.umd.min.js.map
//# sourceMappingURL=derivable.umd.min.js.map
{
"name": "derivable",
"version": "1.0.0-beta9",
"version": "2.0.0-beta.0",
"description": "Functional Reactive State for JavaScript & TypeScript",

@@ -10,8 +10,9 @@ "author": "David Sheldrick",

"scripts": {
"build": "node scripts/build.js",
"test": "mocha --recursive",
"prepublish": "npm run build",
"build": "node scripts/build.js && cp derivable.d.ts derivable.js.flow dist/",
"test": "jest",
"ci:test": "jest && (cd test_flow && npm install && npm test)",
"bench": "node scripts/bench.js",
"coverage": "./scripts/coverage.sh && istanbul report --include=coverage-final.json text",
"show-coverage": "./scripts/coverage.sh && istanbul report --include=coverage-final.json html && open coverage/index.html",
"report-coverage": "./scripts/coverage.sh && istanbul report --include=coverage-final.json lcov && cat coverage/lcov.info | coveralls",
"coverage": "jest --coverage",
"report-coverage": "jest --coverage && cat ./coverage/lcov.info | coveralls",
"stats": "node scripts/stats.js",

@@ -38,3 +39,5 @@ "docs": "node scripts/make-docs.js",

],
"dependencies": {},
"jest": {
"testRegex": ".*_test.js$"
},
"devDependencies": {

@@ -51,6 +54,8 @@ "babel": "^6.5.2",

"immutable": "^3.7.4",
"jest": "^19.0.2",
"mobx": "^2.3.3",
"mocha": "^2.2.5",
"mocha-istanbul": "^0.2.0",
"np": "^2.16.0",
"promise": "^7.1.1",
"rollup": "^0.26.1",
"source-map": "^0.5.6",
"source-map-support": "^0.3.2",

@@ -57,0 +62,0 @@ "uglify-js": "^2.4.24"

@@ -7,3 +7,3 @@ <h1 align="center">DerivableJS</h1>

Derivables are an Observable-like state container with superpowers. Think [MobX](https://github.com/mobxjs/mobx) distilled to a potent essence, served with two heaped spoonfuls of extra performance, a garnish of side effects innovation, and a healthy side-salad of immutability.
Derivables are an Observable-like state container with superpowers. Think [MobX](https://github.com/mobxjs/mobx) distilled to a potent essence, served with two heaped tablespoons of extra performance, a garnish of declarative effects management, and a healthy side-salad of immutability.

@@ -16,9 +16,10 @@ <!-- START doctoc generated TOC please keep comment here to allow auto update -->

- [Reactors](#reactors)
- [Key differences with MobX](#key-differences-with-mobx)
- [API / Documentation](#api--documentation)
- [Usage](#usage)
- [With React](#with-react)
- [With Redux](#with-redux)
- [With Immutable](#with-immutable)
- [Debugging](#debugging)
- [Examples (very wip)](#examples-very-wip)
- [Examples](#examples)
- [Browser](#browser)
- [Equality Woes](#equality-woes)
- [Contributing](#contributing)

@@ -30,3 +31,2 @@ - [Inspiration <3](#inspiration-3)

## Quick start

@@ -52,12 +52,16 @@

<em>N.B. The dollar-sign prefix is just a convention I personally use to create a syntactic distinction between ordinary values and derivable values.</em>
<em>N.B. The dollar-sign prefix is just a simple convention I use to create a visual distinction between ordinary values and derivable values.</em>
- **Derivations**
Derivations represent pure (as in 'pure function') transformation of values held in atoms. You can create them with the `.derive` method, which is a bit like the `.map` method of Arrays and Observables.
Derivations are declarative transformations of values held in atoms. You can create them with the `derive` function.
```javascript
import {derive} from 'derivable';
const cyber = word => word.toUpperCase().split('').join(' ');
const $cyberName = $Name.derive(cyber);
const $cyberName = derive(() =>
cyber($Name.get())
);

@@ -71,10 +75,8 @@ $cyberName.get(); // 'W I L L I A M'

Derivations cannot be modified directly with `.set`, but change in accordance with their dependencies, of which there may be many. Here is an example with two dependencies which uses the fundamental `derivation` constructor function:
Unlike atoms, derivations cannot be modified in-place with a `.set` method. Their values change only when one or more of the values that they depend upon change. Here is an example with two dependencies.
```javascript
import {derivation} from 'derivable';
const $Transformer = atom(cyber);
const $transformedName = derivation(() =>
const $transformedName = derive(() =>
$Transformer.get()($Name.get())

@@ -96,3 +98,3 @@ );

`derivation` takes a function of zero arguments which should
`derive` takes a function of zero arguments which should
dereference one or more Derivables to compute the new derived value. DerivableJS then sneakily monitors who

@@ -108,6 +110,6 @@ is dereferencing who to infer the parent-child relationships.

```javascript
import {atom, derivation, transact} from 'derivable'
import {atom, derive, transact} from 'derivable'
// global application state
const $Name = atom("World"); // the name of the user
const $Name = atom("World"); // the name of the user
const $CountryCode = atom("en"); // for i18n

@@ -125,4 +127,6 @@

// derive a greeting message based on the user's name and country.
const $greeting = $CountryCode.derive(cc => greetings[cc]);
const $message = derivation(() =>
const $greeting = derive(() =>
greetings[$CountryCode.get()]
);
const $message = derive(() =>
`${$greeting.get()}, ${$name.get()}!`

@@ -162,48 +166,68 @@ );

## Usage
## Key differences with MobX
DerivableJS is becoming fairly mature, and has been used for serious stuff in production with very few issues. I think it is safe to consider it beta quality at this point.
- Smaller API surface area.
If your app is non-trivial, use [Immutable](https://facebook.github.io/immutable-js/).
There are far fewer *kinds of thing* in DerivableJS, and therefore fewer
things to learn and fewer surprising exceptions and spooky corners. This
reduces noise and enhances one's ability to grok the concepts and wield the
tools on offer. It also shrinks the set of tools
on offer, but maybe that's not a bad thing:
### With React
> It seems that perfection is attained not when there is nothing more to add, but when there is nothing more to remove.
[react-derivable](https://github.com/jevakallio/react-derivable) is where it's at.
<em>- Antoie de Saint Exupéry</em>
### With Redux
- No transparent dereferencing and assignment.
DerivableJS can be used as a kind-of replacement for reselect, by just doing something like this:
It is always necessary to call `.get` on derivables to find out what's inside, and you always have to call `.set` on atoms to change what's inside.
This provides a consistent semantic and visual
distinction between ordinary values and derivable values.
```javascript
const $Store = atom(null);
- No observable map and array types.
myReduxStore.subscribe(() => $Store.set(myReduxStore.getState()));
```
So you probably have to use something extra like Immutable or [icepick](https://github.com/aearly/icepick) to deal with collections. Not great if you're just out to get
shit done fast, but the benefits of immutable
collections become more and more valuable as projects
mature and grow in scope.
and then you derive all your derived state from $Store, rather than
- More subtle control over reactors
### Debugging
DerivableJS has a tidy and flexible declarative system for defining when reactors should start and stop. This
is rather nice to use for managing many kinds of side effects.
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.
- Speed
### Examples (very wip)
DerivableJS is finely tuned, and propagates change
significantly faster than MobX. \[link to benchmark-results.html forthcoming\]
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.
## API / Documentation
The next best is the [routing walkthrough](https://github.com/ds300/derivablejs/tree/master/examples/routing/README.md)
[Over Here](https://ds300.github.io/derivablejs)
I've also implemented a solution to @staltz's [flux challenge](https://github.com/staltz/flux-challenge/tree/master/submissions/ds300).
## Usage
There is a proper gitbook tutorial on the way!
DerivableJS is fairly mature, and has been used enough in production by various people to be considered a solid beta-quality piece of kit.
### Browser
Either with browserify/webpack/common-js-bundler-du-jour or build as umd bundle with `npm run build -- --umd`
### With React
### Equality Woes
JavaScript is entirely whack when it comes to equality. People do [crazy jazz](https://github.com/ramda/ramda/blob/v0.16.0/src/internal/_equals.js) trying to figure out if some stuff is the same as some other stuff.
The fantastic project [react-derivable](https://github.com/andreypopp/react-derivable) lets you use
derivables in your render method, providing seamless interop with component-local state and props.
If the data you're threading through DerivableJS needs its own notion of equality, make sure it has a sensible `.equals` method and everything will be fine.
### With Immutable
If you're using a data library with some custom non-standard mechanism for doing equality checks (e.g. mori), then you'll need to re-initialize DerivableJS with a custom equality function.
DerivableJS works spiffingly with [Immmutable](https://github.com/facebook/immutable), which
is practically required if your app deals with medium-to-large collections.
### Debugging
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.
### Examples
[See here](https://github.com/ds300/derivablejs/tree/master/examples)
### Browser
Either with browserify/webpack/common-js-bundler-du-jour, or clone the repo, run `npm install && npm run build`, then grab the UMD bundle from `dist/derivable.umd[.min].js` (source maps are also available).
```javascript

@@ -210,0 +234,0 @@ import { withEquality } from 'derivable'

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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