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.11.0 to 0.12.0

228

CHANGELOG.md

@@ -0,1 +1,227 @@

## 0.12.0
### Highlights
- `Derivable#derive` does more stuff (destructuring, property/index lookup, regex matching)
- `Derivable#react` now provides options for declarative lifecycle control.
- Composite Lenses
- Fine-grained Equality checks
- Removed a bunch of API cruft.
### New Stuff
#### Declarative Reactor Lifecycle Control
Prior to this release, the lifecycles of Reactors were controlled imperatively using the`Reactor#start` and `Reactor#stop` methods. This was somewhat verbose and, more importantly, went against the grain of this whole declarative/reactive thing we have going here.
Now there is a better way: providing a lifetime configuration object as a second argument to the `.react` method. Here are the options:
```javascript
interface Lifecycle {
from?: (() => boolean) | Derivable<boolean>;
when?: (() => boolean) | Derivable<boolean>;
until?: (() => boolean) | Derivable<boolean>;
skipFirst?: boolean;
once?: boolean;
onStart?: () => void;
onStop?: () => void;
}
```
`from` determines this initialization time of the reactor. i.e. when the given derivable becomes truthy, the reactor is initialized.
`when` causes `.start` and `.stop` to be called when the given derivable becomes truthy and falsey respectively (but not before the reactor has been initialized, and not after it has been killed).
`until` causes the reactor to be killed when the given derivable becomes truthy.
`skipFirst` causes the first invocation of the reactor (after it has been initialized, and when `when` is truthy) to be ignored. This is typically used if the state of the world at the time of declaration is such that invoking the reactor would be redundant or harmful.
`once` causes the reactor to be killed immediately following its first invocation.
`onStart` and `onStop` are the same lifecycle hooks that were previously provided.
Example usage:
```typescript
const n = atom(0);
n.react(n => console.log(`n is ${n}`), {
from: () => n.get() > 0, // start when n > 0
when: () => n.get() %2 === 0, // only react when n is even
until: () => n.get() => 5 // stop when n >= 5
});
// ... no output
n.set(1);
// ... no output (n is odd)
n.set(2);
// $> n is 2
n.set(3);
// ... no output (n is odd)
n.set(4);
// $> n is 4
n.set(5);
// ... no output (reactor was killed)
n.set(4);
// ... no output (reactors don't come back from the dead)
```
#### `Derivable#derive` new capabilities
- RegExp matching
```javascript
const string = atom('hello world');
const firstLetters = string.derive(/\b\w/g);
firstLetters.get();
// => ['h', 'w']
```
- Property/Index lookup
```javascript
const obj = atom({foo: 'FOO!'});
const foo = obj.derive('foo');
foo.get();
// => 'FOO!'
const arr = atom(['one', 'two']);
const first = arr.derive(0);
first.get();
// => 'one'
```
- Destructuring
```javascript
const string = atom('hello world')
const [len, upper, firstChar, words] = string.derive([
'length', s => s.toUpperCase(), 0, /\w+/g
]);
```
Also note that these work with derivable versions of the arguments:
```javascript
const arr = atom(['one', 'two', 'three']);
const idx = atom(0);
const item = arr.derive(idx);
item.get();
// => 'one'
idx.set(1);
item.get();
// => 'two'
```
#### Composite Lenses
Previously 'lensed atoms' could only have one underlying atom. It is now possible to lens over an arbitrary number of atoms using the new `CompositeLens` interface:
```typescript
type CompositeLens<T> = {
// no-arg getter uses lexical closure to deref and combine atoms
get: () => T,
// one-arg setter to tease apart the value being set and push it
// up to the atoms manually
// runs in an implicit transaction.
set: (value: T) => void
}
```
Instances of which may be passed to a new 1-arity version of the top-level `lens` function to create lensed atoms:
```typescript
const $FirstName = atom('John');
const $LastName = atom('Steinbeck');
const $Name = lens({
get: () => $FirstName.get() + ' ' + $LastName.get(),
set: (val) => {
const [first, last] = val.split(' ');
$FirstName.set(first);
$LastName.set(last);
}
});
$Name.get(); // => 'John Steinbeck'
$Name.set('James Joyce').
$LastName.get(); // => 'Joyce'
```
#### Fine-grained Equality Control
Because JS has no standard way to override equality comparisons, DerivableJS makes it possible to inject equality-checking logic at the module level using the top-level function [`withEquality`](http://ds300.github.io/derivablejs/#derivable-withEquality) which returns a new instance of DerivableJS using the given equality-checking function.
It is now also possible to do this on a per-derivable basis.
The new `Derivable#withEquality` method creates a clone of a derivable, which new derivable uses the given equality-checking function. It looks like this:
```javascript
import { equals } from 'ramda'
const $Person = atom({name: "Steve"}).withEquality(equals);
$Person.react(({name}) => console.log(`name is ${name}`));
// $> name is Steve
$Person.set({name: "Steve"});
// ... no output (this would print the name again
// if using DerivableJS's standard equality function
// which only does strict-equality (===) checks if no .equals
// method is present on the arguments being compared)
```
#### `atomic`/`atomically`
These new top-level functions are identical to `transaction`/`transact` respectively except that they do not create new (nested) transactions if already in a transaction. This is almost always the desired behaviour, unless you want to gracefully abort transactions.
### Breaking changes:
##### The `Derivable#react` method:
- no longer returns a Reactor.
- only accepts functions as the first argument.
- does not bind the given function to the context of the resultant reactor.
You can get the old behaviour by converting
```javascript
d.react(f);
```
to
```javascript
d.reactor(f).start().force();
```
Although it is recommended to switch to using the new declarative lifecycle stuffs if possible.
##### The `Derivable#reactWhen` method was removed
Use `$d.react(r, {when: $when})`.
##### Dependent reactors are no longer stopped automatically when their governors are.
That was a silly idea...
##### The following top-level functions were removed due to cruftiness:
- `derive` (except the tagged template string version, that's still there). Use the `Derivable#derive` method instead.
- `mDerive`. Use the `Derivable#mDerive` method instead.
- 2+ arity version of `lens`. Use the `Derivable#lens` method instead.
- `lookup`. Use the `Derivable#derive(string|number)` method instead.
- `destruct`. Use the `Derivable#derive([string|number])` method instead.
- `ifThenElse`. Use the `Derivable#then` method instead.
- `mIfThenElse`. Use the `Derivable#mThen` method instead.
- `not`. Use the `Derivable#not` method instead.
- `switchCase`. Use the `Derivable#switch` method instead.
- `get`. Use the `Derivable#get` method instead.
- `set`. Use the `Atom#set` method instead.
- `swap`. Use the `Atom#swap` method instead.
## 0.11.0

@@ -42,3 +268,3 @@

It seems like the typescript compiler now figures out how to get the typings
for an npm module by interrogating the "typings" field in its project.json. It
for an npm module by interrogating the "typings" field in its package.json. It
also seems like .d.ts files are now expected to explicitly declare an export.

@@ -45,0 +271,0 @@

106

dist/derivable.d.ts

@@ -10,20 +10,18 @@ /**

derive<E>(f: (value: T) => E): Derivable<E>;
derive<A, E>(f: (value: T, a: A) => E, a: A): Derivable<E>;
derive<A, E>(f: (value: T, a: A) => E, a: Derivable<A>): Derivable<E>;
derive(prop: (string | Derivable<string>)): Derivable<any>;
derive(index: (number | Derivable<number>)): Derivable<any>;
derive(re: (RegExp | Derivable<RegExp>)): Derivable<string[]>;
derive<E>(f: Derivable<(value: T) => E>): Derivable<E>;
derive(args: any[]): Derivable<any>[];
derive<A, E>(f: (value: T, a: A) => E, a: (A | Derivable<A>)): Derivable<E>;
derive<A, B, E>(f: (value: T, a: A, b: B) => E, a: (A | Derivable<A>), b: (B | Derivable<B>)): Derivable<E>;
derive<E>(f: (value: T, ...args: any[]) => E, ...args: any[]): Derivable<E>;
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<A, E>(f: (value: T, a: A) => E, a: (A | Derivable<A>)): Derivable<E>;
mDerive<A, B, E>(f: (value: T, a: A, b: B) => E, a: (A | Derivable<A>), b: (B | Derivable<B>)): Derivable<E>;
mDerive<E>(f: (value: T, ...args: any[]) => E, ...args: any[]): Derivable<E>;
reactor(r: Reactor<T>): Reactor<T>;
reactor(f: (value: T) => void): Reactor<T>;
react(f: (value: T) => void, options?: Lifecycle): void;
react(r: Reactor<T>): Reactor<T>;
react(f: (value: T) => void): Reactor<T>;
reactWhen(cond: Derivable<any>, f: (value: T) => void): Reactor<T>;
reactWhen(cond: Derivable<any>, r: Reactor<T>): Reactor<T>;
get(): T;

@@ -48,2 +46,7 @@

switch(...args: any[]): Derivable<any>;
withEquality(equals: (a: any, b: any) => boolean): this;
reactor(r: Reactor<T>): Reactor<T>;
reactor(f: (value: T) => void): Reactor<T>;
}

@@ -67,2 +70,26 @@

export interface CompositeLens<T> {
get(): T;
set(value: T): void;
}
export interface Lifecycle {
from?: ((() => boolean) | Derivable<boolean>);
when?: ((() => boolean) | Derivable<boolean>);
until?: ((() => boolean) | Derivable<boolean>);
skipFirst?: boolean;
once?: boolean;
onStart?: () => void;
onStop?: () => void;
}
export class Reactor<T> {

@@ -93,17 +120,6 @@

function swap<A, B>(atom: Atom<A>, f: (a: A, ...args: any[]) => B, ...args: any[]): B;
function derivation<T>(f: () => T): Derivable<T>;
function derive<I, O>(d: Derivable<I>, f: (v: I) => O): Derivable<O>;
function derive<I, O, A>(d: Derivable<I>, f: (v: I, a: A) => O, a: A): Derivable<O>;
function derive<I, O, A>(d: Derivable<I>, f: (v: I, a: A) => O, a: Derivable<A>): Derivable<O>;
function derive<I, O>(d: Derivable<I>, f: (v: I, ...args: any[]) => O, ...args: any[]): Derivable<O>;
function derive(strings: string[], ...things: any[]): Derivable<string>;
function lens<T>(lens: CompositeLens<T>): Atom<T>;
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;

@@ -113,33 +129,13 @@

function unpack(obj: any): any;
function atomically(f: () => void): void;
function atomic(f: (...args: any[]) => any): (...args: any[]) => any;
function struct(obj: any): Derivable<any>;
function lookup(obj: Derivable<any>, key: any): Derivable<any>;
function unpack(obj: any): any;
function destruct(obj: Derivable<any>, ...keys: any[]): Derivable<any>[];
function ifThenElse(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>;
function switchCase(d: Derivable<any>, ...args: any[]): Derivable<any>;
function get<T>(d: Derivable<T>): T;
function set<A, B>(a: Atom<A>, v: B): Atom<B>;
function lens<A, B>(atom: Atom<A>, lens: Lens<A, B>): Atom<B>;
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>;

@@ -156,2 +152,12 @@ function isAtom(obj: any): boolean;

function derive(strings: string[], ...things: any[]): Derivable<string>;
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 withEquality(equals: (a: any, b: any) => boolean): any;

@@ -158,0 +164,0 @@

@@ -81,2 +81,7 @@ // UMD loader

function util_setEquals(derivable, equals) {
derivable._equals = equals;
return derivable;
}
// node modes

@@ -309,3 +314,2 @@ var gc_NEW = 0,

reacting: false, // whether or not reaction function being invoked
stopping: false,
yielding: false, // whether or not letting parentReactor react first

@@ -322,19 +326,7 @@ };

if (base.active) {
if (base.stopping) {
throw Error(cycleMsg);
util_removeFromArray(base.parent._children, base);
if (base.parentReactor) {
orphan(base);
}
try {
base.stopping = true;
while (base.dependentReactors.length) {
var dr = base.dependentReactors.pop();
stop(dr);
}
} finally {
util_removeFromArray(base.parent._children, base);
if (base.parentReactor) {
orphan(base);
}
base.active = false;
base.stopping = false;
}
base.active = false;
base.control.onStop && base.control.onStop();

@@ -355,3 +347,2 @@ }

base.parentReactor = parentReactorStack[len - 1];
util_addToArray(base.parentReactor.dependentReactors, base);
}

@@ -365,3 +356,2 @@

if (base.parentReactor) {
util_removeFromArray(base.parentReactor.dependentReactors, base);
base.parentReactor = null;

@@ -372,9 +362,3 @@ }

function adopt (parentBase, childBase) {
orphan(childBase);
if (parentBase.active) {
childBase.parentReactor = parentBase;
util_addToArray(parentBase.dependentReactors, childBase);
} else {
stop(childBase);
}
childBase.parentReactor = parentBase;
}

@@ -509,5 +493,45 @@

case 1:
return D.derivation(function () {
return f(that.get());
});
switch (typeof f) {
case 'function':
return D.derivation(function () {
return f(that.get());
});
case 'string':
case 'number':
return D.derivation(function () {
return that.get()[D.unpack(f)];
});
default:
if (f instanceof Array) {
return f.map(function (x) {
return that.derive(x);
});
} else if (f instanceof RegExp) {
return D.derivation(function () {
return that.get().match(f);
});
} else if (D.isDerivable(f)) {
return D.derivation(function () {
const deriver = f.get();
const thing = that.get();
switch (typeof deriver) {
case 'function':
return deriver(thing);
case 'string':
case 'number':
return thing[deriver];
default:
if (deriver instanceof RegExp) {
return thing.match(deriver);
} else {
throw Error('type error');
}
}
return that.get()[D.unpack(f)];
});
} else {
throw Error('type error');
}
}
break;
case 2:

@@ -558,17 +582,74 @@ return D.derivation(function () {

react: function (f) {
return this.reactor(f).start().force();
},
react: function (f, opts) {
if (typeof f !== 'function') {
throw Error('the first argument to .react must be a function');
}
reactWhen: function (cond, f) {
var result = this.reactor(f);
// cast cond to boolean
cond.derive(function (c) { return !!c; }).react(function (cond) {
if (cond) {
result.start().force();
} else {
result.stop();
opts = Object.assign({
once: false,
from: true,
until: false,
when: true,
skipFirst: false,
}, opts);
// coerce fn or bool to derivable<bool>
function condDerivable(fOrD, name) {
if (!D.isDerivable(fOrD)) {
if (typeof fOrD === 'function') {
fOrD = D.derivation(fOrD);
} else if (typeof fOrD === 'boolean') {
fOrD = D.atom(fOrD);
} else {
throw Error('react ' + name + ' condition must be derivable');
}
}
return fOrD.derive(function (x) { return !!x; });
}
// wrap reactor so f doesn't get a .this context, and to allow
// stopping after one reaction if desired.
var reactor = this.reactor({
react: function (val) {
if (opts.skipFirst) {
opts.skipFirst = false;
} else {
f(val);
if (opts.once) {
this.stop();
controller.stop();
}
}
},
onStart: opts.onStart,
onStop: opts.onStop
});
return result;
// listen to when and until conditions, starting and stopping the
// reactor as appropriate, and stopping this controller when until
// condition becomes true
var controller = D.struct({
until: condDerivable(opts.until, 'until'),
when: condDerivable(opts.when, 'when')
}).reactor(function (conds) {
if (conds.until) {
reactor.stop();
this.stop();
} else if (conds.when) {
if (!reactor.isActive()) {
reactor.start().force();
}
} else if (reactor.isActive()) {
reactor.stop();
}
});
// listen to from condition, starting the reactor controller
// when appropriate
condDerivable(opts.from, 'from').reactor(function (from) {
if (from) {
controller.start().force();
this.stop();
}
}).start().force();
},

@@ -609,4 +690,9 @@

mDerive: function () {
return this.mThen(this.derive.apply(this, arguments));
mDerive: function (arg) {
if (arguments.length === 1 && arg instanceof Array) {
var that = this;
return arg.map(function (a) { return that.mDerive(a); });
} else {
return this.mThen(this.derive.apply(this, arguments));
}
},

@@ -621,3 +707,16 @@

},
withEquality: function (equals) {
if (equals) {
if (typeof equals !== 'function') {
throw new Error('equals must be function');
}
} else {
equals = null;
}
return util_setEquals(this._clone(), equals);
},
};
x.switch = function () {

@@ -637,2 +736,3 @@ var args = arguments;

};
return x;

@@ -644,3 +744,3 @@ }

_clone: function () {
return D.derivation(this._deriver);
return util_setEquals(D.derivation(this._deriver), this._equals);
},

@@ -663,3 +763,4 @@

}
that._state = opts.equals(newState, that._value) ? gc_UNCHANGED : gc_CHANGED;
var equals = that._equals || opts.equals;
that._state = equals(newState, that._value) ? gc_UNCHANGED : gc_CHANGED;
that._value = newState;

@@ -747,2 +848,3 @@ });

obj._value = util_unique;
obj._equals = null;

@@ -763,4 +865,12 @@ if (util_DEBUG_MODE) {

},
lens: function (lensDescriptor) {
return D.lens(this, lensDescriptor);
lens: function (monoLensDescriptor) {
var that = this;
return D.lens({
get: function () {
return monoLensDescriptor.get(that.get());
},
set: function (val) {
that.set(monoLensDescriptor.set(that.get(), val));
}
});
}

@@ -773,10 +883,10 @@ }

_clone: function () {
return D.lens(this._parent, {
get: this._getter,
set: this._setter
});
return util_setEquals(D.lens(this._lensDescriptor), this._equals);
},
set: function (value) {
this._parent.set(this._setter(this._parent._get(), value));
var that = this;
D.atomically(function () {
that._lensDescriptor.set(value);
});
return this;

@@ -787,6 +897,4 @@ }

function lens_construct(derivation, parent, descriptor) {
derivation._getter = descriptor.get;
derivation._setter = descriptor.set;
derivation._parent = parent;
function lens_construct(derivation, descriptor) {
derivation._lensDescriptor = descriptor;
derivation._type = types_LENS;

@@ -805,2 +913,6 @@

function atom_inTxn () {
return transactions_inTransaction(TXN_CTX)
}
var NOOP_ARRAY = {push: function () {}};

@@ -831,3 +943,3 @@

var keys = util_keys(this.inTxnValues);
if (transactions_inTransaction(TXN_CTX)) {
if (atom_inTxn()) {
// push in-txn vals up to current txn

@@ -856,3 +968,3 @@ for (i = keys.length; i--;) {

onAbort: function () {
if (!transactions_inTransaction(TXN_CTX)) {
if (!atom_inTxn()) {
var keys = util_keys(this.inTxnValues);

@@ -870,3 +982,3 @@ for (var i = keys.length; i--;) {

_clone: function () {
return D.atom(this._value);
return util_setEquals(D.atom(this._value), this._equals);
},

@@ -906,6 +1018,7 @@

this._validate(value);
if (!opts.equals(value, this._value)) {
var equals = this._equals || opts.equals;
if (!equals(value, this._value)) {
this._state = gc_CHANGED;
if (transactions_inTransaction(TXN_CTX)) {
if (atom_inTxn()) {
setState(transactions_currentTransaction(TXN_CTX), this, value);

@@ -925,3 +1038,3 @@ } else {

_get: function () {
if (transactions_inTransaction(TXN_CTX)) {
if (atom_inTxn()) {
return getState(transactions_currentTransaction(TXN_CTX), this);

@@ -940,2 +1053,3 @@ }

atom._type = types_ATOM;
atom._equals = null;
return atom;

@@ -1040,10 +1154,24 @@ }

/**
* Sets the e's state to be f applied to e's current state and args
* Returns a copy of f which runs atomically
*/
D.swap = function (atom, f) {
var args = util_slice(arguments, 1);
args[0] = atom.get();
return atom.set(f.apply(null, args));
D.atomic = function (f) {
return function () {
var result;
var that = this;
var args = arguments;
D.atomically(function () {
result = f.apply(that, args);
});
return result;
}
};
D.atomically = function (f) {
if (atom_inTxn()) {
f();
} else {
D.transact(f);
}
};
D.derivation = function (f) {

@@ -1054,15 +1182,5 @@ return derivation_construct(Object.create(Derivation), f);

/**
* Creates a new derivation. Can also be used as a template string tag.
* Template string tag for derivable strings
*/
D.derive = function (a) {
if (a instanceof Array) {
return deriveString.apply(null, arguments);
} else if (arguments.length > 0) {
return Derivable.derive.apply(a, util_slice(arguments, 1));
} else {
throw new Error("Wrong arity for derive. Expecting 1+ args");
}
};
function deriveString (parts) {
D.derive = function (parts) {
var args = util_slice(arguments, 1);

@@ -1079,6 +1197,2 @@ return D.derivation(function () {

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

@@ -1089,10 +1203,5 @@

*/
D.lens = function (parent, descriptor) {
var lens = Object.create(Lens);
D.lens = function (descriptor) {
return lens_construct(
derivation_construct(
lens,
function () { return descriptor.get(parent.get()); }
),
parent,
derivation_construct(Object.create(Lens), descriptor.get),
descriptor

@@ -1126,13 +1235,2 @@ );

/**
* sets a to v, returning v
*/
D.set = function (a, v) {
return a.set(v);
};
D.get = function (d) {
return d.get();
};
function deepUnpack (thing) {

@@ -1166,98 +1264,24 @@ if (D.isDerivable(thing)) {

D.destruct = function (arg) {
var args = arguments;
var result = [];
for (var i = 1; i < args.length; i++) {
result.push(D.lookup(arg, args[i]));
function andOrFn (breakOn) {
return function () {
var args = arguments;
return D.derivation(function () {
var val;
for (var i = 0; i < args.length; i++) {
val = D.unpack(args[i]);
if (breakOn(val)) {
break;
}
}
return val;
});
}
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) };
D.ifThenElse = function (testValue, thenClause, elseClause) {
return D.derivation(function () {
return D.unpack(
D.unpack(testValue) ? thenClause : elseClause
);
});
}
function identity (x) { return x; }
function complement (f) { return function (x) { return !f(x); }}
D.or = andOrFn(identity);
D.mOr = andOrFn(util_some);
D.and = andOrFn(complement(identity));
D.mAnd = andOrFn(complement(util_some));
D.mIfThenElse = function (testValue, thenClause, elseClause) {
return D.derivation(function () {
var x = D.unpack(testValue);
return D.unpack(
util_some(x) ? thenClause : elseClause
);
});
};
D.or = function () {
var args = arguments;
return D.derivation(function () {
var val;
for (var i = 0; i < args.length; i++) {
val = D.unpack(args[i]);
if (val) {
break;
}
}
return val;
});
};
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 () {
var args = arguments;
return D.derivation(function () {
var val;
for (var i = 0; i < args.length; i++) {
val = D.unpack(args[i]);
if (!val) {
break;
}
}
return val;
});
};
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; }); };
D.switchCase = function (x) {
return Derivable.switch.apply(x, util_slice(arguments, 1));
};
return D;

@@ -1264,0 +1288,0 @@ }

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

!function(t,n){"use strict";t&&"function"==typeof t.define&&t.define.amd?t.define(["exports"],n):n("undefined"!=typeof exports?exports:t.Derivable={})}(this,function(t){"use strict";function n(t){for(var n=1;n<arguments.length;n++)for(var e=arguments[n],r=$(e),i=r.length;i--;){var a=r[i];t[a]=e[a]}return t}function e(t,n){return t===n?0!==t||1/t===1/n:t!==t&&n!==n}function r(t,n){return e(t,n)||t&&"function"==typeof t.equals&&t.equals(n)}function i(t,n){var e=t.indexOf(n);0>e&&t.push(n)}function a(t,n){var e=t.indexOf(n);e>=0&&t.splice(e,1)}function u(t,n){return t.indexOf(n)>=0}function o(){return tt++}function c(t,n){return Array.prototype.slice.call(t,n)}function s(t){return null!==t&&void 0!==t}function f(t){et=!!t}function l(t,n){if(t._type===_t){if(t.reacting)throw new Error("Cycle detected! Don't do this!");n.push(t)}else for(var e=t._children.length;e--;){var r=t._children[e];r._state!==ot&&(r._state=ot,l(r,n))}}function h(t){var n;switch(t._state){case it:case at:for(n=t._children.length;n--;){var e=t._children[n];h(e),e._state!==ct&&t._children.splice(n,1)}t._state=ct;break;case ot:if(t._type===_t)t._state=ct;else{var r=[];for(n=t._parents.length;n--;){var i=t._parents[n];if(i._state!==at){t._state=ut;break}r.push([i,i._value])}t._state!==ut&&(t._state=st,t._parents=r)}break;case ct:case ut:case st:break;default:throw new Error("can't sweep state "+t._state)}}function p(t){var n=!1;switch(t._type){case lt:t._state=ct,n=!0;break;case ht:case pt:t._state=rt,t._value=nt,n=!0;break;case _t:t._state=ct,n=!1}if(n){for(var e=t._children.length;e--;)p(t._children[e]);t._children=[]}}function _(t){var n=ft.length;ft.push([]);try{return t(),ft[n]}finally{ft.pop()}}function v(t){ft.length>0&&i(ft[ft.length-1],t)}function d(){throw yt}function g(){return{currentTxn:null}}function y(t){return null!==t.currentTxn}function k(t){return t.currentTxn}function w(t,n){n._parent=t.currentTxn,n._state=vt,t.currentTxn=n}function m(t,n){var e=t.currentTxn;if(t.currentTxn=e._parent,e._state!==vt)throw new Error("unexpected state: "+e._state);
n(e)}function b(t){m(t,function(t){t._state=dt,t.onCommit&&t.onCommit()})}function E(t){m(t,function(t){t._state=gt,t.onAbort&&t.onAbort()})}function T(t,n,e){w(t,n);try{e(d)}catch(r){if(E(t),r!==yt)throw r;return}b(t)}function x(t,n){w(t,n());var e=!1;return{tick:function(){if(e)throw new Error("can't tick disposed ticker");b(t),w(t,n())},stop:function(){if(e)throw new Error("ticker already disposed");b(t)}}}function R(t,n){var e={control:n,parent:t,parentReactor:null,dependentReactors:[],_state:ct,active:!1,_type:_t,uid:o(),reacting:!1,stopping:!1,yielding:!1};return et&&(e.stack=Error().stack),e}function A(t){if(t.active){if(t.stopping)throw Error(kt);try{for(t.stopping=!0;t.dependentReactors.length;){var n=t.dependentReactors.pop();A(n)}}finally{a(t.parent._children,t),t.parentReactor&&V(t),t.active=!1,t.stopping=!1}t.control.onStop&&t.control.onStop()}}function O(t){if(!t.active){i(t.parent._children,t),t.active=!0,t.parent._get();var n=wt.length;n>0&&(t.parentReactor=wt[n-1],i(t.parentReactor.dependentReactors,t)),t.control.onStart&&t.control.onStart()}}function V(t){t.parentReactor&&(a(t.parentReactor.dependentReactors,t),t.parentReactor=null)}function q(t,n){V(n),t.active?(n.parentReactor=t,i(t.dependentReactors,n)):A(n)}function D(t){if(t.yielding)throw Error(kt);if(t.active&&t._state===ot){if(null!==t.parentReactor)try{t.yielding=!0,D(t.parentReactor)}finally{t.yielding=!1}if(t.active){var n=t.parent,e=n._state;if((e===ot||e===ut||e===st||e===rt)&&n._get(),e=n._state,e===at)t._state=ct;else{if(e!==it)throw new Error("invalid parent state: "+e);C(t)}}}}function C(t){if(!t.control.react)throw new Error("No reactor function available.");t._state=ct;try{if(t.reacting=!0,wt.push(t),et)try{t.control.react(t.parent._get())}catch(n){throw console.error(t.stack),n}else t.control.react(t.parent._get())}finally{wt.pop(),t.reacting=!1}}function j(){this._type=_t}function N(t,n){if(t._base)throw new Error("This reactor has already been initialized");return t._base=R(n,t),t}function S(t){this._type=_t,this.react=t}function G(t){
return n(new j,t)}function I(t,n){var e={derive:function(n,e,r,i,a){var u=this;switch(arguments.length){case 0:return u;case 1:return t.derivation(function(){return n(u.get())});case 2:return t.derivation(function(){return n(u.get(),t.unpack(e))});case 3:return t.derivation(function(){return n(u.get(),t.unpack(e),t.unpack(r))});case 4:return t.derivation(function(){return n(u.get(),t.unpack(e),t.unpack(r),t.unpack(i))});case 5:return t.derivation(function(){return n(u.get(),t.unpack(e),t.unpack(r),t.unpack(i),t.unpack(a))});default:var o=[u].concat(c(arguments,1));return t.derivation(function(){return n.apply(null,o.map(t.unpack))})}},reactor:function(t){if("function"==typeof t)return N(new S(t),this);if(t instanceof j)return N(t,this);if(t&&t.react)return N(G(t),this);throw new Error("Unrecognized type for reactor "+t)},react:function(t){return this.reactor(t).start().force()},reactWhen:function(t,n){var e=this.reactor(n);return t.derive(function(t){return!!t}).react(function(t){t?e.start().force():e.stop()}),e},get:function(){return v(this),this._get()},is:function(e){return t.lift(n.equals)(this,e)},and:function(n){return this.derive(function(e){return e&&t.unpack(n)})},or:function(n){return this.derive(function(e){return e||t.unpack(n)})},then:function(n,e){return this.derive(function(r){return t.unpack(r?n:e)})},mThen:function(n,e){return this.derive(function(r){return t.unpack(s(r)?n:e)})},mOr:function(t){return this.mThen(this,t)},mDerive:function(){return this.mThen(this.derive.apply(this,arguments))},mAnd:function(t){return this.mThen(t,this)},not:function(){return this.derive(function(t){return!t})}};return e["switch"]=function(){var e=arguments;return this.derive(function(r){var i;for(i=0;e.length-1>i;i+=2)if(n.equals(r,t.unpack(e[i])))return t.unpack(e[i+1]);return i===e.length-1?t.unpack(e[i]):void 0})},e}function z(t,n){return{_clone:function(){return t.derivation(this._deriver)},_forceGet:function(){var t,e=this,r=_(function(){var t;if(et)try{t=e._deriver()}catch(r){throw console.error(e._stack),r}else t=e._deriver();
e._state=n.equals(t,e._value)?at:it,e._value=t});for(t=this._parents.length;t--;){var o=this._parents[t];u(r,o)||a(o._children,this)}for(this._parents=r,t=r.length;t--;)i(r[t]._children,this)},_get:function(){var t,e;t:switch(this._state){case rt:case ut:this._forceGet();break;case ot:for(t=0;this._parents.length>t;t++){e=this._parents[t];var r=e._state;if((r===ot||r===ut||r===st)&&e._get(),r=e._state,r===it){this._forceGet();break t}if(r!==ct&&r!==at)throw new Error("invalid parent mode: "+r)}this._state=at;break;case st:var a=[];for(t=0;this._parents.length>t;t++){var u=this._parents[t],o=u[1];if(e=u[0],!n.equals(e._get(),o)){this._parents=[],this._forceGet();break t}a.push(e)}for(t=a.length;t--;)i(a[t]._children,this);this._parents=a,this._state=at}return this._value}}}function Q(t,n){return t._children=[],t._parents=[],t._deriver=n,t._state=rt,t._type=ht,t._value=nt,et&&(t._stack=Error().stack),t}function L(t,n){return{swap:function(t){var n=c(arguments,0);return n[0]=this.get(),this.set(t.apply(null,n))},lens:function(n){return t.lens(this,n)}}}function M(t,n){return{_clone:function(){return t.lens(this._parent,{get:this._getter,set:this._setter})},set:function(t){return this._parent.set(this._setter(this._parent._get(),t)),this}}}function W(t,n,e){return t._getter=e.get,t._setter=e.set,t._parent=n,t._type=pt,t}function F(t){for(var n=t.length;n--;)D(t[n])}function U(){this.inTxnValues={},this.reactorQueue=[]}function B(t,n){var e=t.inTxnValues[n._uid];return e?e[1]:n._value}function H(t,n,e){t.inTxnValues[n._uid]=[n,e],l(n,t.reactorQueue)}function J(t,n){return{_clone:function(){return t.atom(this._value)},withValidator:function(t){if(null===t)return this._clone();if("function"==typeof t){var n=this._clone(),e=this._validator;return n._validator=e?function(n){return t(n)&&e(n)}:t,n}throw new Error(".withValidator expects function or null")},validate:function(){this._validate(this.get())},_validate:function(t){var n=this._validator&&this._validator(t);if(this._validator&&n!==!0)throw new Error("Failed validation with value: '"+t+"'. Validator returned '"+n+"' ");
},set:function(t){if(this._validate(t),!n.equals(t,this._value))if(this._state=it,y(mt))H(k(mt),this,t);else{this._value=t;var e=[];l(this,e),F(e),h(this)}return this},_get:function(){return y(mt)?B(k(mt),this):this._value}}}function K(t,n){return t._uid=o(),t._children=[],t._state=ct,t._value=n,t._type=lt,t}function P(t){T(mt,new U,t)}function X(t){return function(){var n,e=c(arguments,0),r=this;return P(function(){n=t.apply(r,e)}),n}}function Y(){Et?Et.refCount++:(Et=x(mt,function(){return new U}),Et.refCount=1);var t=!1;return{tick:function(){if(t)throw new Error("tyring to use ticker after release");Et.tick()},release:function(){if(t)throw new Error("ticker already released");0===--Et.refCount&&(Et.stop(),Et=null),t=!0}}}function Z(t){function e(t){var n=c(arguments,1);return a.derivation(function(){for(var e="",r=0;t.length>r;r++)e+=t[r],n.length>r&&(e+=a.unpack(n[r]));return e})}function i(t){if(a.isDerivable(t))return t.get();if(t instanceof Array)return t.map(i);if(t.constructor===Object){for(var n={},e=$(t),r=e.length;r--;){var u=e[r];n[u]=i(t[u])}return n}return t}t=n({},Tt,t||{});var a={transact:P,defaultEquals:r,setDebugMode:f,transaction:X,ticker:Y,Reactor:j,isAtom:function(t){return t&&(t._type===lt||t._type===pt)},isDerivable:function(t){return t&&(t._type===lt||t._type===pt||t._type===ht)},isDerivation:function(t){return t&&(t._type===ht||t._type===pt)},isLensed:function(t){return t&&t._type===pt},isReactor:function(t){return t&&t._type===_t}},u=I(a,t),o=L(a,t),l=n({},o,u,J(a,t)),h=n({},u,z(a,t)),p=n({},o,h,M(a,t));return a.atom=function(t){return K(Object.create(l),t)},a.swap=function(t,n){var e=c(arguments,1);return e[0]=t.get(),t.set(n.apply(null,e))},a.derivation=function(t){return Q(Object.create(h),t)},a.derive=function(t){if(t instanceof Array)return e.apply(null,arguments);if(arguments.length>0)return u.derive.apply(t,c(arguments,1));throw new Error("Wrong arity for derive. Expecting 1+ args")},a.mDerive=function(t){return u.mDerive.apply(t,c(arguments,1))},a.lens=function(t,n){var e=Object.create(p);
return W(Q(e,function(){return n.get(t.get())}),t,n)},a.unpack=function(t){return a.isDerivable(t)?t.get():t},a.lift=function(t){return function(){var n=arguments,e=this;return a.derivation(function(){return t.apply(e,Array.prototype.map.call(n,a.unpack))})}},a.set=function(t,n){return t.set(n)},a.get=function(t){return t.get()},a.struct=function(t){if(t.constructor===Object||t instanceof Array)return a.derivation(function(){return i(t)});throw new Error("`struct` expects plain Object or Array")},a.destruct=function(t){for(var n=arguments,e=[],r=1;n.length>r;r++)e.push(a.lookup(t,n[r]));return e},a.lookup=function(t,n){return a.derivation(function(){return a.unpack(t)[a.unpack(n)]})},a.ifThenElse=function(t,n,e){return t.then(n,e)},a.ifThenElse=function(t,n,e){return a.derivation(function(){return a.unpack(a.unpack(t)?n:e)})},a.mIfThenElse=function(t,n,e){return a.derivation(function(){var r=a.unpack(t);return a.unpack(s(r)?n:e)})},a.or=function(){var t=arguments;return a.derivation(function(){for(var n,e=0;t.length>e&&!(n=a.unpack(t[e]));e++);return n})},a.mOr=function(){var t=arguments;return a.derivation(function(){for(var n,e=0;t.length>e&&(n=a.unpack(t[e]),!s(n));e++);return n})},a.and=function(){var t=arguments;return a.derivation(function(){for(var n,e=0;t.length>e&&(n=a.unpack(t[e]),n);e++);return n})},a.mAnd=function(){var t=arguments;return a.derivation(function(){for(var n,e=0;t.length>e&&(n=a.unpack(t[e]),s(n));e++);return n})},a.not=function(t){return t.derive(function(t){return!t})},a.switchCase=function(t){return u["switch"].apply(t,c(arguments,1))},a}var $=Object.keys,tt=0,nt=Object.freeze({equals:function(){return!1}}),et=!1,rt=0,it=1,at=2,ut=3,ot=4,ct=5,st=6,ft=[],lt="ATOM",ht="DERIVATION",pt="LENS",_t="REACTION",vt=0,dt=1,gt=3,yt={},kt="Cyclical Reactor Dependency! Not allowed!",wt=[];n(j.prototype,{start:function(){return O(this._base),this},stop:function(){return A(this._base),this},force:function(){return C(this._base),this},isActive:function(){return this._base.active},orphan:function(){return V(this._base),
this},adopt:function(t){if(t._type!==_t)throw Error("reactors can only adopt reactors");return q(this._base,t._base),this}}),n(S.prototype,j.prototype);var mt=g(),bt={push:function(){}};n(U.prototype,{onCommit:function(){var t,n,e=$(this.inTxnValues);if(y(mt))for(t=e.length;t--;)n=this.inTxnValues[e[t]],n[0].set(n[1]);else{for(t=e.length;t--;)n=this.inTxnValues[e[t]],n[0]._value=n[1],l(n[0],bt);for(F(this.reactorQueue),t=e.length;t--;)h(this.inTxnValues[e[t]][0])}},onAbort:function(){if(!y(mt))for(var t=$(this.inTxnValues),n=t.length;n--;)p(this.inTxnValues[t[n]][0])}});var Et=null,Tt={equals:r};n(t,Z()),t.withEquality=function(t){return Z({equals:t})},t["default"]=t});
!function(t,n){"use strict";t&&"function"==typeof t.define&&t.define.amd?t.define(["exports"],n):n("undefined"!=typeof exports?exports:t.Derivable={})}(this,function(t){"use strict";function n(t){for(var n=1;n<arguments.length;n++)for(var e=arguments[n],r=nt(e),i=r.length;i--;){var a=r[i];t[a]=e[a]}return t}function e(t,n){return t===n?0!==t||1/t===1/n:t!==t&&n!==n}function r(t,n){return e(t,n)||t&&"function"==typeof t.equals&&t.equals(n)}function i(t,n){var e=t.indexOf(n);0>e&&t.push(n)}function a(t,n){var e=t.indexOf(n);e>=0&&t.splice(e,1)}function u(t,n){return t.indexOf(n)>=0}function o(){return et++}function c(t,n){return Array.prototype.slice.call(t,n)}function s(t){return null!==t&&void 0!==t}function f(t){it=!!t}function l(t,n){return t._equals=n,t}function h(t,n){if(t._type===dt){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!==st&&(r._state=st,h(r,n))}}function p(t){var n;switch(t._state){case ut:case ot:for(n=t._children.length;n--;){var e=t._children[n];p(e),e._state!==ft&&t._children.splice(n,1)}t._state=ft;break;case st:if(t._type===dt)t._state=ft;else{var r=[];for(n=t._parents.length;n--;){var i=t._parents[n];if(i._state!==ot){t._state=ct;break}r.push([i,i._value])}t._state!==ct&&(t._state=lt,t._parents=r)}break;case ft:case ct:case lt:break;default:throw new Error("can't sweep state "+t._state)}}function _(t){var n=!1;switch(t._type){case pt:t._state=ft,n=!0;break;case _t:case vt:t._state=at,t._value=rt,n=!0;break;case dt:t._state=ft,n=!1}if(n){for(var e=t._children.length;e--;)_(t._children[e]);t._children=[]}}function v(t){var n=ht.length;ht.push([]);try{return t(),ht[n]}finally{ht.pop()}}function d(t){ht.length>0&&i(ht[ht.length-1],t)}function g(){throw mt}function y(){return{currentTxn:null}}function w(t){return null!==t.currentTxn}function m(t){return t.currentTxn}function k(t,n){n._parent=t.currentTxn,n._state=gt,t.currentTxn=n}function b(t,n){var e=t.currentTxn;if(t.currentTxn=e._parent,e._state!==gt)throw new Error("unexpected state: "+e._state);
n(e)}function E(t){b(t,function(t){t._state=yt,t.onCommit&&t.onCommit()})}function x(t){b(t,function(t){t._state=wt,t.onAbort&&t.onAbort()})}function T(t,n,e){k(t,n);try{e(g)}catch(r){if(x(t),r!==mt)throw r;return}E(t)}function q(t,n){k(t,n());var e=!1;return{tick:function(){if(e)throw new Error("can't tick disposed ticker");E(t),k(t,n())},stop:function(){if(e)throw new Error("ticker already disposed");E(t)}}}function A(t,n){var e={control:n,parent:t,parentReactor:null,dependentReactors:[],_state:ft,active:!1,_type:dt,uid:o(),reacting:!1,yielding:!1};return it&&(e.stack=Error().stack),e}function O(t){t.active&&(a(t.parent._children,t),t.parentReactor&&R(t),t.active=!1,t.control.onStop&&t.control.onStop())}function D(t){if(!t.active){i(t.parent._children,t),t.active=!0,t.parent._get();var n=bt.length;n>0&&(t.parentReactor=bt[n-1]),t.control.onStart&&t.control.onStart()}}function R(t){t.parentReactor&&(t.parentReactor=null)}function V(t,n){n.parentReactor=t}function j(t){if(t.yielding)throw Error(kt);if(t.active&&t._state===st){if(null!==t.parentReactor)try{t.yielding=!0,j(t.parentReactor)}finally{t.yielding=!1}if(t.active){var n=t.parent,e=n._state;if((e===st||e===ct||e===lt||e===at)&&n._get(),e=n._state,e===ot)t._state=ft;else{if(e!==ut)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=ft;try{if(t.reacting=!0,bt.push(t),it)try{t.control.react(t.parent._get())}catch(n){throw console.error(t.stack),n}else t.control.react(t.parent._get())}finally{bt.pop(),t.reacting=!1}}function S(){this._type=dt}function N(t,n){if(t._base)throw new Error("This reactor has already been initialized");return t._base=A(n,t),t}function F(t){this._type=dt,this.react=t}function G(t){return n(new S,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:switch(typeof n){case"function":return t.derivation(function(){return n(u.get())});case"string":case"number":return t.derivation(function(){return u.get()[t.unpack(n)];
});default:if(n instanceof Array)return n.map(function(t){return u.derive(t)});if(n instanceof RegExp)return t.derivation(function(){return u.get().match(n)});if(t.isDerivable(n))return t.derivation(function(){const e=n.get(),r=u.get();switch(typeof e){case"function":return e(r);case"string":case"number":return r[e];default:if(e instanceof RegExp)return r.match(e);throw Error("type error")}return u.get()[t.unpack(n)]});throw Error("type error")}break;case 2:return t.derivation(function(){return n(u.get(),t.unpack(e))});case 3:return t.derivation(function(){return n(u.get(),t.unpack(e),t.unpack(r))});case 4:return t.derivation(function(){return n(u.get(),t.unpack(e),t.unpack(r),t.unpack(i))});case 5:return t.derivation(function(){return n(u.get(),t.unpack(e),t.unpack(r),t.unpack(i),t.unpack(a))});default:var o=[u].concat(c(arguments,1));return t.derivation(function(){return n.apply(null,o.map(t.unpack))})}},reactor:function(t){if("function"==typeof t)return N(new F(t),this);if(t instanceof S)return N(t,this);if(t&&t.react)return N(G(t),this);throw new Error("Unrecognized type for reactor "+t)},react:function(n,e){function r(n,e){if(!t.isDerivable(n))if("function"==typeof n)n=t.derivation(n);else{if("boolean"!=typeof n)throw Error("react "+e+" condition must be derivable");n=t.atom(n)}return n.derive(function(t){return!!t})}if("function"!=typeof n)throw Error("the first argument to .react must be a function");e=Object.assign({once:!1,from:!0,until:!1,when:!0,skipFirst:!1},e);var i=this.reactor({react:function(t){e.skipFirst?e.skipFirst=!1:(n(t),e.once&&(this.stop(),a.stop()))},onStart:e.onStart,onStop:e.onStop}),a=t.struct({until:r(e.until,"until"),when:r(e.when,"when")}).reactor(function(t){t.until?(i.stop(),this.stop()):t.when?i.isActive()||i.start().force():i.isActive()&&i.stop()});r(e.from,"from").reactor(function(t){t&&(a.start().force(),this.stop())}).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)})},mThen:function(n,e){return this.derive(function(r){return t.unpack(s(r)?n:e)})},mOr:function(t){return this.mThen(this,t)},mDerive:function(t){if(1===arguments.length&&t instanceof Array){var n=this;return t.map(function(t){return n.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 l(this._clone(),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 l(t.derivation(this._deriver),this._equals)},_forceGet:function(){var t,e=this,r=v(function(){var t;if(it)try{t=e._deriver()}catch(r){throw console.error(e._stack),r}else t=e._deriver();var i=e._equals||n.equals;e._state=i(t,e._value)?ot:ut,e._value=t});for(t=this._parents.length;t--;){var o=this._parents[t];u(r,o)||a(o._children,this)}for(this._parents=r,t=r.length;t--;)i(r[t]._children,this)},_get:function(){var t,e;t:switch(this._state){case at:case ct:this._forceGet();break;case st:for(t=0;this._parents.length>t;t++){e=this._parents[t];var r=e._state;if((r===st||r===ct||r===lt)&&e._get(),r=e._state,r===ut){this._forceGet();break t}if(r!==ft&&r!==ot)throw new Error("invalid parent mode: "+r)}this._state=ot;break;case lt:var a=[];for(t=0;this._parents.length>t;t++){var u=this._parents[t],o=u[1];if(e=u[0],!n.equals(e._get(),o)){this._parents=[],this._forceGet();break t}a.push(e)}for(t=a.length;t--;)i(a[t]._children,this);this._parents=a,this._state=ot}return this._value}}}function Q(t,n){return t._children=[],t._parents=[],t._deriver=n,t._state=at,t._type=_t,t._value=rt,t._equals=null,
it&&(t._stack=Error().stack),t}function L(t,n){return{swap:function(t){var n=c(arguments,0);return n[0]=this.get(),this.set(t.apply(null,n))},lens:function(n){var e=this;return t.lens({get:function(){return n.get(e.get())},set:function(t){e.set(n.set(e.get(),t))}})}}}function M(t,n){return{_clone:function(){return l(t.lens(this._lensDescriptor),this._equals)},set:function(n){var e=this;return t.atomically(function(){e._lensDescriptor.set(n)}),this}}}function U(t,n){return t._lensDescriptor=n,t._type=vt,t}function B(t){for(var n=t.length;n--;)j(t[n])}function H(){return w(Et)}function J(){this.inTxnValues={},this.reactorQueue=[]}function K(t,n){var e=t.inTxnValues[n._uid];return e?e[1]:n._value}function P(t,n,e){t.inTxnValues[n._uid]=[n,e],h(n,t.reactorQueue)}function W(t,n){return{_clone:function(){return l(t.atom(this._value),this._equals)},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){this._validate(t);var e=this._equals||n.equals;if(!e(t,this._value))if(this._state=ut,H())P(m(Et),this,t);else{this._value=t;var r=[];h(this,r),B(r),p(this)}return this},_get:function(){return H()?K(m(Et),this):this._value}}}function X(t,n){return t._uid=o(),t._children=[],t._state=ft,t._value=n,t._type=pt,t._equals=null,t}function Y(t){T(Et,new J,t)}function Z(t){return function(){var n,e=c(arguments,0),r=this;return Y(function(){n=t.apply(r,e)}),n}}function $(){Tt?Tt.refCount++:(Tt=q(Et,function(){return new J}),Tt.refCount=1);var t=!1;return{tick:function(){if(t)throw new Error("tyring to use ticker after release");Tt.tick()},release:function(){if(t)throw new Error("ticker already released");0===--Tt.refCount&&(Tt.stop(),
Tt=null),t=!0}}}function tt(t){function e(t){if(o.isDerivable(t))return t.get();if(t instanceof Array)return t.map(e);if(t.constructor===Object){for(var n={},r=nt(t),i=r.length;i--;){var a=r[i];n[a]=e(t[a])}return n}return t}function i(t){return function(){var n=arguments;return o.derivation(function(){for(var e,r=0;n.length>r&&(e=o.unpack(n[r]),!t(e));r++);return e})}}function a(t){return t}function u(t){return function(n){return!t(n)}}t=n({},qt,t||{});var o={transact:Y,defaultEquals:r,setDebugMode:f,transaction:Z,ticker:$,Reactor:S,isAtom:function(t){return t&&(t._type===pt||t._type===vt)},isDerivable:function(t){return t&&(t._type===pt||t._type===vt||t._type===_t)},isDerivation:function(t){return t&&(t._type===_t||t._type===vt)},isLensed:function(t){return t&&t._type===vt},isReactor:function(t){return t&&t._type===dt}},l=z(o,t),h=L(o,t),p=n({},h,l,W(o,t)),_=n({},l,I(o,t)),v=n({},h,_,M(o,t));return o.atom=function(t){return X(Object.create(p),t)},o.atomic=function(t){return function(){var n,e=this,r=arguments;return o.atomically(function(){n=t.apply(e,r)}),n}},o.atomically=function(t){H()?t():o.transact(t)},o.derivation=function(t){return Q(Object.create(_),t)},o.derive=function(t){var n=c(arguments,1);return o.derivation(function(){for(var e="",r=0;t.length>r;r++)e+=t[r],n.length>r&&(e+=o.unpack(n[r]));return e})},o.lens=function(t){return U(Q(Object.create(v),t.get),t)},o.unpack=function(t){return o.isDerivable(t)?t.get():t},o.lift=function(t){return function(){var n=arguments,e=this;return o.derivation(function(){return t.apply(e,Array.prototype.map.call(n,o.unpack))})}},o.struct=function(t){if(t.constructor===Object||t instanceof Array)return o.derivation(function(){return e(t)});throw new Error("`struct` expects plain Object or Array")},o.or=i(a),o.mOr=i(s),o.and=i(u(a)),o.mAnd=i(u(s)),o}var nt=Object.keys,et=0,rt=Object.freeze({equals:function(){return!1}}),it=!1,at=0,ut=1,ot=2,ct=3,st=4,ft=5,lt=6,ht=[],pt="ATOM",_t="DERIVATION",vt="LENS",dt="REACTION",gt=0,yt=1,wt=3,mt={},kt="Cyclical Reactor Dependency! Not allowed!",bt=[];
n(S.prototype,{start:function(){return D(this._base),this},stop:function(){return O(this._base),this},force:function(){return C(this._base),this},isActive:function(){return this._base.active},orphan:function(){return R(this._base),this},adopt:function(t){if(t._type!==dt)throw Error("reactors can only adopt reactors");return V(this._base,t._base),this}}),n(F.prototype,S.prototype);var Et=y(),xt={push:function(){}};n(J.prototype,{onCommit:function(){var t,n,e=nt(this.inTxnValues);if(H())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],xt);for(B(this.reactorQueue),t=e.length;t--;)p(this.inTxnValues[e[t]][0])}},onAbort:function(){if(!H())for(var t=nt(this.inTxnValues),n=t.length;n--;)_(this.inTxnValues[t[n]][0])}});var Tt=null,qt={equals:r};n(t,tt()),t.withEquality=function(t){return tt({equals:t})},t["default"]=t});
//# sourceMappingURL=derivable.min.js.map
{
"name": "derivable",
"version": "0.11.0",
"version": "0.12.0",
"description": "Functional Reactive State for JavaScript & TypeScript",

@@ -5,0 +5,0 @@ "author": "David Sheldrick",

@@ -133,2 +133,4 @@ <h1 align="center">DerivableJS</h1>

I've also implemented a solution to @staltz's [flux challenge](https://github.com/staltz/flux-challenge/tree/master/submissions/ds300).
And there are a few others [here](https://github.com/ds300/derivablejs/tree/master/examples/) too.

@@ -135,0 +137,0 @@

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