Comparing version 2.0.0-beta.0 to 2.0.0-beta.1
@@ -7,29 +7,8 @@ 'use strict'; | ||
function assign (obj) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var other = arguments[i]; | ||
var ks = keys(other || {}); | ||
for (var j = ks.length; j--;) { | ||
var prop = ks[j]; | ||
obj[prop] = other[prop]; | ||
} | ||
} | ||
return obj; | ||
} | ||
var assign = Object.assign; | ||
function _is(a, b) { | ||
// SameValue algorithm | ||
if (a === b) { // Steps 1-5, 7-10 | ||
// Steps 6.b-6.e: +0 != -0 | ||
return a !== 0 || 1 / a === 1 / b; | ||
} else { | ||
// Step 6.a: NaN == NaN | ||
return a !== a && b !== b; | ||
} | ||
function equals (a, b) { | ||
return Object.is(a, b) || (a && typeof a.equals === 'function' && a.equals(b)); | ||
} | ||
function equals (a, b) { | ||
return _is(a, b) || (a && typeof a.equals === 'function' && a.equals(b)); | ||
}; | ||
function addToArray (a, b) { | ||
@@ -40,3 +19,3 @@ var i = a.indexOf(b); | ||
} | ||
}; | ||
} | ||
@@ -48,3 +27,3 @@ function removeFromArray (a, b) { | ||
} | ||
}; | ||
} | ||
@@ -54,7 +33,7 @@ var _nextId = 0; | ||
return _nextId++; | ||
}; | ||
} | ||
function slice (a, i) { | ||
return Array.prototype.slice.call(a, i); | ||
}; | ||
} | ||
@@ -65,3 +44,3 @@ var unique = Object.freeze({equals: function () { return false; }}); | ||
return (x !== null) && (x !== void 0); | ||
}; | ||
} | ||
@@ -71,3 +50,3 @@ var DEBUG_MODE = false; | ||
DEBUG_MODE = !!val; | ||
}; | ||
} | ||
@@ -77,3 +56,3 @@ function setEquals (derivable, equals) { | ||
return derivable; | ||
}; | ||
} | ||
@@ -163,3 +142,3 @@ var ATOM = "ATOM"; | ||
} | ||
}; | ||
} | ||
@@ -220,3 +199,3 @@ function mark (node, reactors) { | ||
return currentCtx !== null; | ||
}; | ||
} | ||
@@ -236,3 +215,3 @@ function transact$1 (f) { | ||
commitTransaction(); | ||
}; | ||
} | ||
@@ -257,3 +236,3 @@ function atomically$1 (f) { | ||
}; | ||
}; | ||
} | ||
@@ -335,3 +314,3 @@ function atomic$1 (f) { | ||
}; | ||
}; | ||
} | ||
@@ -342,3 +321,3 @@ /** | ||
function _unpack (thing) { | ||
function unpack$1 (thing) { | ||
if (isDerivable$1(thing)) { | ||
@@ -349,3 +328,3 @@ return thing.get(); | ||
} | ||
}; | ||
} | ||
@@ -359,5 +338,5 @@ function deepUnpack (thing) { | ||
var result = {}; | ||
var keys$$ = keys(thing); | ||
for (var i = keys$$.length; i--;) { | ||
var prop = keys$$[i]; | ||
var keys$$1 = keys(thing); | ||
for (var i = keys$$1.length; i--;) { | ||
var prop = keys$$1[i]; | ||
result[prop] = deepUnpack(thing[prop]); | ||
@@ -369,3 +348,3 @@ } | ||
} | ||
}; | ||
} | ||
@@ -384,7 +363,7 @@ function Derivation (deriver) { | ||
} | ||
}; | ||
} | ||
assign(Derivation.prototype, { | ||
_clone: function () { | ||
return setEquals(_derive(this._deriver), this._equals); | ||
return setEquals(derive$1(this._deriver), this._equals); | ||
}, | ||
@@ -487,7 +466,7 @@ | ||
function _derive (f, a, b, c, d) { | ||
function derive$1 (f, a, b, c, d) { | ||
if (f instanceof Array) { | ||
// Template string tag for derivable strings | ||
var args = slice(arguments, 1); | ||
return _derive(function () { | ||
return derive$1(function () { | ||
var s = ""; | ||
@@ -497,3 +476,3 @@ for (var i=0; i < f.length; i++) { | ||
if (i < args.length) { | ||
s += _unpack(args[i]); | ||
s += unpack$1(args[i]); | ||
} | ||
@@ -512,18 +491,18 @@ } | ||
return new Derivation(function () { | ||
return f(_unpack(a)); | ||
return f(unpack$1(a)); | ||
}); | ||
case 3: | ||
return new Derivation(function () { | ||
return f(_unpack(a), _unpack(b)); | ||
return f(unpack$1(a), unpack$1(b)); | ||
}); | ||
case 4: | ||
return new Derivation(function () { | ||
return f(_unpack(a), _unpack(b), _unpack(c)); | ||
return f(unpack$1(a), unpack$1(b), unpack$1(c)); | ||
}); | ||
case 5: | ||
return new Derivation(function () { | ||
return f(_unpack(a), | ||
_unpack(b), | ||
_unpack(c), | ||
_unpack(d)); | ||
return f(unpack$1(a), | ||
unpack$1(b), | ||
unpack$1(c), | ||
unpack$1(d)); | ||
}); | ||
@@ -533,3 +512,3 @@ default: | ||
return new Derivation(function () { | ||
return f.apply(null, args.map(_unpack)); | ||
return f.apply(null, args.map(unpack$1)); | ||
}); | ||
@@ -623,5 +602,5 @@ } | ||
if (typeof fOrD === 'function') { | ||
return _derive(function () { return fOrD(derivable); }); | ||
return derive$1(function () { return fOrD(derivable); }); | ||
} else if (typeof fOrD === 'boolean') { | ||
return _derive(function () { return fOrD; }); | ||
return derive$1(function () { return fOrD; }); | ||
} else { | ||
@@ -654,3 +633,3 @@ throw Error('react ' + name + ' condition must be derivable, got: ' + JSON.stringify(fOrD)); | ||
var $whenUntil = _derive(function () { | ||
var $whenUntil = derive$1(function () { | ||
return { | ||
@@ -690,2 +669,6 @@ until: $until.get(), | ||
var global$1 = typeof window === 'object' ? window : typeof global=== 'object' ? global : {}; | ||
var devtoolsHook = global$1.__DERIVABLE_DEVTOOLS_HOOK__; | ||
function Atom (value) { | ||
@@ -699,3 +682,3 @@ this._id = nextId(); | ||
return this; | ||
}; | ||
} | ||
@@ -728,2 +711,5 @@ assign(Atom.prototype, { | ||
get: function () { | ||
if (typeof devtoolsHook === 'function') { | ||
devtoolsHook('captureAtom', this); | ||
} | ||
maybeCaptureParent(this); | ||
@@ -762,2 +748,30 @@ return this._value; | ||
function andOrFn (breakOn) { | ||
return function () { | ||
var args = arguments; | ||
return derive$1(function () { | ||
var val; | ||
for (var i = 0; i < args.length; i++) { | ||
val = unpack$1(args[i]); | ||
if (breakOn(val)) { | ||
break; | ||
} | ||
} | ||
return val; | ||
}); | ||
}; | ||
} | ||
function identity (x) { return x; } | ||
function complement (f) { return function (x) { return !f(x); }; } | ||
var or$1 = andOrFn(identity); | ||
var mOr$1 = andOrFn(some); | ||
var and$1 = andOrFn(complement(identity)); | ||
var mAnd$1 = andOrFn(complement(some)); | ||
var __Reactor$1 = Reactor; | ||
@@ -772,3 +786,3 @@ var transact$2 = transact$1; | ||
var isDerivation$2 = isDerivation$1; | ||
var derive$1 = _derive; | ||
var derive$2 = derive$1; | ||
var atom$1 = atom$2; | ||
@@ -778,7 +792,7 @@ var atomic$2 = atomic$1; | ||
var proxy$1 = proxy$2; | ||
var unpack$1 = _unpack; | ||
var unpack$2 = unpack$1; | ||
function struct$1 (arg) { | ||
if (arg.constructor === Object || arg instanceof Array) { | ||
return derive$1(function () { | ||
return derive$2(function () { | ||
return deepUnpack(arg); | ||
@@ -789,3 +803,3 @@ }); | ||
} | ||
}; | ||
} | ||
@@ -812,48 +826,26 @@ function wrapPreviousState$1 (f, init) { | ||
function andOrFn (breakOn) { | ||
return function () { | ||
var args = arguments; | ||
return derive$1(function () { | ||
var val; | ||
for (var i = 0; i < args.length; i++) { | ||
val = unpack$1(args[i]); | ||
if (breakOn(val)) { | ||
break; | ||
} | ||
} | ||
return val; | ||
}); | ||
}; | ||
} | ||
function identity (x) { return x; } | ||
function complement (f) { return function (x) { return !f(x); }; } | ||
var or$1 = andOrFn(identity); | ||
var mOr$1 = andOrFn(some); | ||
var and$1 = andOrFn(complement(identity)); | ||
var mAnd$1 = andOrFn(complement(some)); | ||
var derivable = Object.freeze({ | ||
__Reactor: __Reactor$1, | ||
transact: transact$2, | ||
setDebugMode: setDebugMode$2, | ||
transaction: transaction$2, | ||
ticker: ticker$2, | ||
isDerivable: isDerivable$2, | ||
isAtom: isAtom$2, | ||
isProxy: isProxy$2, | ||
isDerivation: isDerivation$2, | ||
derive: derive$1, | ||
atom: atom$1, | ||
atomic: atomic$2, | ||
atomically: atomically$2, | ||
proxy: proxy$1, | ||
unpack: unpack$1, | ||
struct: struct$1, | ||
wrapPreviousState: wrapPreviousState$1, | ||
captureDereferences: captureDereferences$1, | ||
or: or$1, | ||
mOr: mOr$1, | ||
and: and$1, | ||
mAnd: mAnd$1 | ||
var derivable$1 = Object.freeze({ | ||
__Reactor: __Reactor$1, | ||
transact: transact$2, | ||
setDebugMode: setDebugMode$2, | ||
transaction: transaction$2, | ||
ticker: ticker$2, | ||
isDerivable: isDerivable$2, | ||
isAtom: isAtom$2, | ||
isProxy: isProxy$2, | ||
isDerivation: isDerivation$2, | ||
derive: derive$2, | ||
atom: atom$1, | ||
atomic: atomic$2, | ||
atomically: atomically$2, | ||
proxy: proxy$1, | ||
unpack: unpack$2, | ||
struct: struct$1, | ||
wrapPreviousState: wrapPreviousState$1, | ||
captureDereferences: captureDereferences$1, | ||
or: or$1, | ||
mOr: mOr$1, | ||
and: and$1, | ||
mAnd: mAnd$1 | ||
}); | ||
@@ -874,6 +866,6 @@ | ||
case 'function': | ||
return derive$1(f, that); | ||
return derive$2(f, that); | ||
case 'string': | ||
case 'number': | ||
return derive$1(function () { | ||
return derive$2(function () { | ||
return that.get()[f]; | ||
@@ -887,7 +879,7 @@ }); | ||
} else if (f instanceof RegExp) { | ||
return derive$1(function () { | ||
return derive$2(function () { | ||
return that.get().match(f); | ||
}); | ||
} else if (isDerivable$1(f)) { | ||
return derive$1(function () { | ||
return derive$2(function () { | ||
var deriver = f.get(); | ||
@@ -914,12 +906,12 @@ var thing = that.get(); | ||
case 2: | ||
return derive$1(f, that, a); | ||
return derive$2(f, that, a); | ||
case 3: | ||
return derive$1(f, that, a, b); | ||
return derive$2(f, that, a, b); | ||
case 4: | ||
return derive$1(f, that, a, b, c); | ||
return derive$2(f, that, a, b, c); | ||
case 5: | ||
return derive$1(f, that, a, b, c, d); | ||
return derive$2(f, that, a, b, c, d); | ||
default: | ||
var args = ([f, that]).concat(slice(arguments, 1)); | ||
return derive$1.apply(null, args); | ||
return derive$2.apply(null, args); | ||
} | ||
@@ -937,3 +929,3 @@ }, | ||
if (typeof when === 'function' || when === false) { | ||
when = derive$1(when); | ||
when = derive$2(when); | ||
} else if (!isDerivable$1(when)) { | ||
@@ -948,19 +940,12 @@ throw new Error('when condition must be bool, function, or derivable'); | ||
is: function (other) { | ||
var that = this; | ||
return this.derive(function (x) { | ||
return that.__equals(x, _unpack(other)); | ||
var x = this; | ||
return derive$2(function () { | ||
return x.__equals(x.get(), unpack$1(other)); | ||
}); | ||
}, | ||
and: function (other) { | ||
return this.derive(function (x) {return x && _unpack(other);}); | ||
}, | ||
or: function (other) { | ||
return this.derive(function (x) {return x || _unpack(other);}); | ||
}, | ||
then: function (thenClause, elseClause) { | ||
return this.derive(function (x) { | ||
return _unpack(x ? thenClause : elseClause); | ||
var x = this; | ||
return derive$2(function () { | ||
return unpack$1(x.get() ? thenClause : elseClause); | ||
}); | ||
@@ -970,11 +955,24 @@ }, | ||
mThen: function (thenClause, elseClause) { | ||
return this.derive(function (x) { | ||
return _unpack(some(x) ? thenClause : elseClause); | ||
var x = this; | ||
return derive$2(function () { | ||
return unpack$1(some(x.get()) ? thenClause : elseClause); | ||
}); | ||
}, | ||
or: function (other) { | ||
return or$1(this, other); | ||
}, | ||
mOr: function (other) { | ||
return this.mThen(this, other); | ||
return mOr$1(this, other); | ||
}, | ||
and: function (other) { | ||
return and$1(this, other); | ||
}, | ||
mAnd: function (other) { | ||
return mAnd$1(this, other); | ||
}, | ||
mDerive: function (arg) { | ||
@@ -989,20 +987,17 @@ if (arguments.length === 1 && arg instanceof Array) { | ||
mAnd: function (other) { | ||
return this.mThen(other, this); | ||
}, | ||
not: function () { | ||
return this.derive(function (x) { return !x; }); | ||
var x = this; | ||
return derive$2(function () { return !x.get(); }); | ||
}, | ||
withEquality: function (equals) { | ||
if (equals) { | ||
if (typeof equals !== 'function') { | ||
withEquality: function (equals$$1) { | ||
if (equals$$1) { | ||
if (typeof equals$$1 !== 'function') { | ||
throw new Error('equals must be function'); | ||
} | ||
} else { | ||
equals = null; | ||
equals$$1 = null; | ||
} | ||
return setEquals(this._clone(), equals); | ||
return setEquals(this._clone(), equals$$1); | ||
}, | ||
@@ -1021,8 +1016,8 @@ | ||
for (i = 0; i < args.length-1; i+=2) { | ||
if (that.__equals(x, _unpack(args[i]))) { | ||
return _unpack(args[i+1]); | ||
if (that.__equals(x, unpack$1(args[i]))) { | ||
return unpack$1(args[i+1]); | ||
} | ||
} | ||
if (i === args.length - 1) { | ||
return _unpack(args[i]); | ||
return unpack$1(args[i]); | ||
} | ||
@@ -1055,2 +1050,6 @@ }); | ||
if (global$1.__DERIVABLE_INIT_FLAG__) { | ||
console.warn('Multiple instances of derivable have been initialized on the same page'); | ||
} | ||
global$1.__DERIVABLE_INIT_FLAG__ = true; | ||
@@ -1066,2 +1065,3 @@ var __Reactor = __Reactor$1; | ||
var isDerivation = isDerivation$2; | ||
var derive = derive$2; | ||
var atom = atom$1; | ||
@@ -1071,4 +1071,3 @@ var atomic = atomic$2; | ||
var proxy = proxy$1; | ||
var derive = derive$1; | ||
var unpack = unpack$1; | ||
var unpack = unpack$2; | ||
var struct = struct$1; | ||
@@ -1104,3 +1103,3 @@ var wrapPreviousState = wrapPreviousState$1; | ||
exports.mAnd = mAnd; | ||
exports['default'] = derivable; | ||
//# sourceMappingURL=derivable.js.map | ||
exports['default'] = derivable$1; | ||
//# sourceMappingURL=derivable.js.map |
@@ -1,45 +0,15 @@ | ||
// UMD loader | ||
(function (global, factory) { | ||
"use strict"; | ||
if (global && typeof global.define === "function" && global.define.amd) { | ||
global.define(["exports"], factory); | ||
} else if (typeof exports !== "undefined") { | ||
factory(exports); | ||
} else { | ||
factory(global.Derivable = {}); | ||
} | ||
})(this, function (exports) { | ||
'use strict'; | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
typeof define === 'function' && define.amd ? define(['exports'], factory) : | ||
(factory((global.Derivable = {}))); | ||
}(this, (function (exports) { 'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
var keys = Object.keys; | ||
function assign (obj) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var other = arguments[i]; | ||
var ks = keys(other || {}); | ||
for (var j = ks.length; j--;) { | ||
var prop = ks[j]; | ||
obj[prop] = other[prop]; | ||
} | ||
} | ||
return obj; | ||
} | ||
var assign = Object.assign; | ||
function _is(a, b) { | ||
// SameValue algorithm | ||
if (a === b) { // Steps 1-5, 7-10 | ||
// Steps 6.b-6.e: +0 != -0 | ||
return a !== 0 || 1 / a === 1 / b; | ||
} else { | ||
// Step 6.a: NaN == NaN | ||
return a !== a && b !== b; | ||
} | ||
function equals (a, b) { | ||
return Object.is(a, b) || (a && typeof a.equals === 'function' && a.equals(b)); | ||
} | ||
function equals (a, b) { | ||
return _is(a, b) || (a && typeof a.equals === 'function' && a.equals(b)); | ||
}; | ||
function addToArray (a, b) { | ||
@@ -50,3 +20,3 @@ var i = a.indexOf(b); | ||
} | ||
}; | ||
} | ||
@@ -58,3 +28,3 @@ function removeFromArray (a, b) { | ||
} | ||
}; | ||
} | ||
@@ -64,7 +34,7 @@ var _nextId = 0; | ||
return _nextId++; | ||
}; | ||
} | ||
function slice (a, i) { | ||
return Array.prototype.slice.call(a, i); | ||
}; | ||
} | ||
@@ -75,3 +45,3 @@ var unique = Object.freeze({equals: function () { return false; }}); | ||
return (x !== null) && (x !== void 0); | ||
}; | ||
} | ||
@@ -81,3 +51,3 @@ var DEBUG_MODE = false; | ||
DEBUG_MODE = !!val; | ||
}; | ||
} | ||
@@ -87,3 +57,3 @@ function setEquals (derivable, equals) { | ||
return derivable; | ||
}; | ||
} | ||
@@ -173,3 +143,3 @@ var ATOM = "ATOM"; | ||
} | ||
}; | ||
} | ||
@@ -230,3 +200,3 @@ function mark (node, reactors) { | ||
return currentCtx !== null; | ||
}; | ||
} | ||
@@ -246,3 +216,3 @@ function transact$1 (f) { | ||
commitTransaction(); | ||
}; | ||
} | ||
@@ -267,3 +237,3 @@ function atomically$1 (f) { | ||
}; | ||
}; | ||
} | ||
@@ -345,3 +315,3 @@ function atomic$1 (f) { | ||
}; | ||
}; | ||
} | ||
@@ -352,3 +322,3 @@ /** | ||
function _unpack (thing) { | ||
function unpack$1 (thing) { | ||
if (isDerivable$1(thing)) { | ||
@@ -359,3 +329,3 @@ return thing.get(); | ||
} | ||
}; | ||
} | ||
@@ -369,5 +339,5 @@ function deepUnpack (thing) { | ||
var result = {}; | ||
var keys$$ = keys(thing); | ||
for (var i = keys$$.length; i--;) { | ||
var prop = keys$$[i]; | ||
var keys$$1 = keys(thing); | ||
for (var i = keys$$1.length; i--;) { | ||
var prop = keys$$1[i]; | ||
result[prop] = deepUnpack(thing[prop]); | ||
@@ -379,3 +349,3 @@ } | ||
} | ||
}; | ||
} | ||
@@ -394,7 +364,7 @@ function Derivation (deriver) { | ||
} | ||
}; | ||
} | ||
assign(Derivation.prototype, { | ||
_clone: function () { | ||
return setEquals(_derive(this._deriver), this._equals); | ||
return setEquals(derive$1(this._deriver), this._equals); | ||
}, | ||
@@ -497,7 +467,7 @@ | ||
function _derive (f, a, b, c, d) { | ||
function derive$1 (f, a, b, c, d) { | ||
if (f instanceof Array) { | ||
// Template string tag for derivable strings | ||
var args = slice(arguments, 1); | ||
return _derive(function () { | ||
return derive$1(function () { | ||
var s = ""; | ||
@@ -507,3 +477,3 @@ for (var i=0; i < f.length; i++) { | ||
if (i < args.length) { | ||
s += _unpack(args[i]); | ||
s += unpack$1(args[i]); | ||
} | ||
@@ -522,18 +492,18 @@ } | ||
return new Derivation(function () { | ||
return f(_unpack(a)); | ||
return f(unpack$1(a)); | ||
}); | ||
case 3: | ||
return new Derivation(function () { | ||
return f(_unpack(a), _unpack(b)); | ||
return f(unpack$1(a), unpack$1(b)); | ||
}); | ||
case 4: | ||
return new Derivation(function () { | ||
return f(_unpack(a), _unpack(b), _unpack(c)); | ||
return f(unpack$1(a), unpack$1(b), unpack$1(c)); | ||
}); | ||
case 5: | ||
return new Derivation(function () { | ||
return f(_unpack(a), | ||
_unpack(b), | ||
_unpack(c), | ||
_unpack(d)); | ||
return f(unpack$1(a), | ||
unpack$1(b), | ||
unpack$1(c), | ||
unpack$1(d)); | ||
}); | ||
@@ -543,3 +513,3 @@ default: | ||
return new Derivation(function () { | ||
return f.apply(null, args.map(_unpack)); | ||
return f.apply(null, args.map(unpack$1)); | ||
}); | ||
@@ -633,5 +603,5 @@ } | ||
if (typeof fOrD === 'function') { | ||
return _derive(function () { return fOrD(derivable); }); | ||
return derive$1(function () { return fOrD(derivable); }); | ||
} else if (typeof fOrD === 'boolean') { | ||
return _derive(function () { return fOrD; }); | ||
return derive$1(function () { return fOrD; }); | ||
} else { | ||
@@ -664,3 +634,3 @@ throw Error('react ' + name + ' condition must be derivable, got: ' + JSON.stringify(fOrD)); | ||
var $whenUntil = _derive(function () { | ||
var $whenUntil = derive$1(function () { | ||
return { | ||
@@ -700,2 +670,6 @@ until: $until.get(), | ||
var global$1 = typeof window === 'object' ? window : typeof global=== 'object' ? global : {}; | ||
var devtoolsHook = global$1.__DERIVABLE_DEVTOOLS_HOOK__; | ||
function Atom (value) { | ||
@@ -709,3 +683,3 @@ this._id = nextId(); | ||
return this; | ||
}; | ||
} | ||
@@ -738,2 +712,5 @@ assign(Atom.prototype, { | ||
get: function () { | ||
if (typeof devtoolsHook === 'function') { | ||
devtoolsHook('captureAtom', this); | ||
} | ||
maybeCaptureParent(this); | ||
@@ -772,2 +749,30 @@ return this._value; | ||
function andOrFn (breakOn) { | ||
return function () { | ||
var args = arguments; | ||
return derive$1(function () { | ||
var val; | ||
for (var i = 0; i < args.length; i++) { | ||
val = unpack$1(args[i]); | ||
if (breakOn(val)) { | ||
break; | ||
} | ||
} | ||
return val; | ||
}); | ||
}; | ||
} | ||
function identity (x) { return x; } | ||
function complement (f) { return function (x) { return !f(x); }; } | ||
var or$1 = andOrFn(identity); | ||
var mOr$1 = andOrFn(some); | ||
var and$1 = andOrFn(complement(identity)); | ||
var mAnd$1 = andOrFn(complement(some)); | ||
var __Reactor$1 = Reactor; | ||
@@ -782,3 +787,3 @@ var transact$2 = transact$1; | ||
var isDerivation$2 = isDerivation$1; | ||
var derive$1 = _derive; | ||
var derive$2 = derive$1; | ||
var atom$1 = atom$2; | ||
@@ -788,7 +793,7 @@ var atomic$2 = atomic$1; | ||
var proxy$1 = proxy$2; | ||
var unpack$1 = _unpack; | ||
var unpack$2 = unpack$1; | ||
function struct$1 (arg) { | ||
if (arg.constructor === Object || arg instanceof Array) { | ||
return derive$1(function () { | ||
return derive$2(function () { | ||
return deepUnpack(arg); | ||
@@ -799,3 +804,3 @@ }); | ||
} | ||
}; | ||
} | ||
@@ -822,48 +827,26 @@ function wrapPreviousState$1 (f, init) { | ||
function andOrFn (breakOn) { | ||
return function () { | ||
var args = arguments; | ||
return derive$1(function () { | ||
var val; | ||
for (var i = 0; i < args.length; i++) { | ||
val = unpack$1(args[i]); | ||
if (breakOn(val)) { | ||
break; | ||
} | ||
} | ||
return val; | ||
}); | ||
}; | ||
} | ||
function identity (x) { return x; } | ||
function complement (f) { return function (x) { return !f(x); }; } | ||
var or$1 = andOrFn(identity); | ||
var mOr$1 = andOrFn(some); | ||
var and$1 = andOrFn(complement(identity)); | ||
var mAnd$1 = andOrFn(complement(some)); | ||
var derivable = Object.freeze({ | ||
__Reactor: __Reactor$1, | ||
transact: transact$2, | ||
setDebugMode: setDebugMode$2, | ||
transaction: transaction$2, | ||
ticker: ticker$2, | ||
isDerivable: isDerivable$2, | ||
isAtom: isAtom$2, | ||
isProxy: isProxy$2, | ||
isDerivation: isDerivation$2, | ||
derive: derive$1, | ||
atom: atom$1, | ||
atomic: atomic$2, | ||
atomically: atomically$2, | ||
proxy: proxy$1, | ||
unpack: unpack$1, | ||
struct: struct$1, | ||
wrapPreviousState: wrapPreviousState$1, | ||
captureDereferences: captureDereferences$1, | ||
or: or$1, | ||
mOr: mOr$1, | ||
and: and$1, | ||
mAnd: mAnd$1 | ||
var derivable$1 = Object.freeze({ | ||
__Reactor: __Reactor$1, | ||
transact: transact$2, | ||
setDebugMode: setDebugMode$2, | ||
transaction: transaction$2, | ||
ticker: ticker$2, | ||
isDerivable: isDerivable$2, | ||
isAtom: isAtom$2, | ||
isProxy: isProxy$2, | ||
isDerivation: isDerivation$2, | ||
derive: derive$2, | ||
atom: atom$1, | ||
atomic: atomic$2, | ||
atomically: atomically$2, | ||
proxy: proxy$1, | ||
unpack: unpack$2, | ||
struct: struct$1, | ||
wrapPreviousState: wrapPreviousState$1, | ||
captureDereferences: captureDereferences$1, | ||
or: or$1, | ||
mOr: mOr$1, | ||
and: and$1, | ||
mAnd: mAnd$1 | ||
}); | ||
@@ -884,6 +867,6 @@ | ||
case 'function': | ||
return derive$1(f, that); | ||
return derive$2(f, that); | ||
case 'string': | ||
case 'number': | ||
return derive$1(function () { | ||
return derive$2(function () { | ||
return that.get()[f]; | ||
@@ -897,7 +880,7 @@ }); | ||
} else if (f instanceof RegExp) { | ||
return derive$1(function () { | ||
return derive$2(function () { | ||
return that.get().match(f); | ||
}); | ||
} else if (isDerivable$1(f)) { | ||
return derive$1(function () { | ||
return derive$2(function () { | ||
var deriver = f.get(); | ||
@@ -924,12 +907,12 @@ var thing = that.get(); | ||
case 2: | ||
return derive$1(f, that, a); | ||
return derive$2(f, that, a); | ||
case 3: | ||
return derive$1(f, that, a, b); | ||
return derive$2(f, that, a, b); | ||
case 4: | ||
return derive$1(f, that, a, b, c); | ||
return derive$2(f, that, a, b, c); | ||
case 5: | ||
return derive$1(f, that, a, b, c, d); | ||
return derive$2(f, that, a, b, c, d); | ||
default: | ||
var args = ([f, that]).concat(slice(arguments, 1)); | ||
return derive$1.apply(null, args); | ||
return derive$2.apply(null, args); | ||
} | ||
@@ -947,3 +930,3 @@ }, | ||
if (typeof when === 'function' || when === false) { | ||
when = derive$1(when); | ||
when = derive$2(when); | ||
} else if (!isDerivable$1(when)) { | ||
@@ -958,19 +941,12 @@ throw new Error('when condition must be bool, function, or derivable'); | ||
is: function (other) { | ||
var that = this; | ||
return this.derive(function (x) { | ||
return that.__equals(x, _unpack(other)); | ||
var x = this; | ||
return derive$2(function () { | ||
return x.__equals(x.get(), unpack$1(other)); | ||
}); | ||
}, | ||
and: function (other) { | ||
return this.derive(function (x) {return x && _unpack(other);}); | ||
}, | ||
or: function (other) { | ||
return this.derive(function (x) {return x || _unpack(other);}); | ||
}, | ||
then: function (thenClause, elseClause) { | ||
return this.derive(function (x) { | ||
return _unpack(x ? thenClause : elseClause); | ||
var x = this; | ||
return derive$2(function () { | ||
return unpack$1(x.get() ? thenClause : elseClause); | ||
}); | ||
@@ -980,11 +956,24 @@ }, | ||
mThen: function (thenClause, elseClause) { | ||
return this.derive(function (x) { | ||
return _unpack(some(x) ? thenClause : elseClause); | ||
var x = this; | ||
return derive$2(function () { | ||
return unpack$1(some(x.get()) ? thenClause : elseClause); | ||
}); | ||
}, | ||
or: function (other) { | ||
return or$1(this, other); | ||
}, | ||
mOr: function (other) { | ||
return this.mThen(this, other); | ||
return mOr$1(this, other); | ||
}, | ||
and: function (other) { | ||
return and$1(this, other); | ||
}, | ||
mAnd: function (other) { | ||
return mAnd$1(this, other); | ||
}, | ||
mDerive: function (arg) { | ||
@@ -999,20 +988,17 @@ if (arguments.length === 1 && arg instanceof Array) { | ||
mAnd: function (other) { | ||
return this.mThen(other, this); | ||
}, | ||
not: function () { | ||
return this.derive(function (x) { return !x; }); | ||
var x = this; | ||
return derive$2(function () { return !x.get(); }); | ||
}, | ||
withEquality: function (equals) { | ||
if (equals) { | ||
if (typeof equals !== 'function') { | ||
withEquality: function (equals$$1) { | ||
if (equals$$1) { | ||
if (typeof equals$$1 !== 'function') { | ||
throw new Error('equals must be function'); | ||
} | ||
} else { | ||
equals = null; | ||
equals$$1 = null; | ||
} | ||
return setEquals(this._clone(), equals); | ||
return setEquals(this._clone(), equals$$1); | ||
}, | ||
@@ -1031,8 +1017,8 @@ | ||
for (i = 0; i < args.length-1; i+=2) { | ||
if (that.__equals(x, _unpack(args[i]))) { | ||
return _unpack(args[i+1]); | ||
if (that.__equals(x, unpack$1(args[i]))) { | ||
return unpack$1(args[i+1]); | ||
} | ||
} | ||
if (i === args.length - 1) { | ||
return _unpack(args[i]); | ||
return unpack$1(args[i]); | ||
} | ||
@@ -1065,2 +1051,6 @@ }); | ||
if (global$1.__DERIVABLE_INIT_FLAG__) { | ||
console.warn('Multiple instances of derivable have been initialized on the same page'); | ||
} | ||
global$1.__DERIVABLE_INIT_FLAG__ = true; | ||
@@ -1076,2 +1066,3 @@ var __Reactor = __Reactor$1; | ||
var isDerivation = isDerivation$2; | ||
var derive = derive$2; | ||
var atom = atom$1; | ||
@@ -1081,4 +1072,3 @@ var atomic = atomic$2; | ||
var proxy = proxy$1; | ||
var derive = derive$1; | ||
var unpack = unpack$1; | ||
var unpack = unpack$2; | ||
var struct = struct$1; | ||
@@ -1114,4 +1104,7 @@ var wrapPreviousState = wrapPreviousState$1; | ||
exports.mAnd = mAnd; | ||
exports['default'] = derivable;}); | ||
exports['default'] = derivable$1; | ||
//# sourceMappingURL=derivable.umd.js.map | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
}))); | ||
//# sourceMappingURL=derivable.umd.js.map |
@@ -1,8 +0,7 @@ | ||
!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 | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(t.Derivable={})}(this,function(t){"use strict";function e(t,e){t.indexOf(e)<0&&t.push(e)}function n(t,e){return Array.prototype.slice.call(t,e)}function r(t){return null!==t&&void 0!==t}function i(t,e){return t._equals=e,t}function o(t){return t&&(t._type===S||t._type===P||t._type===F)}function s(t,e){J.push({parents:e,offset:0,child:t}),K=t}function u(){J.pop(),K=0===J.length?null:J[J.length-1].child}function a(t){if(null!==K){var n=J[J.length-1];if(n.parents[n.offset]===t)n.offset++;else{var r=n.parents.indexOf(t);if(-1===r)void 0!==K&&e(t._activeChildren,K),n.offset===n.parents.length?n.parents.push(t):(n.parents.push(n.parents[n.offset]),n.parents[n.offset]=t),n.offset++;else if(r>n.offset){var i=n.parents[r];n.parents[r]=n.parents[n.offset],n.parents[n.offset]=i,n.offset++}}}}function c(t,e){for(var n=0,r=t._activeChildren.length;n<r;n++){var i=t._activeChildren[n];switch(i._type){case S:case F:i._state!==z&&(i._state=z,c(i,e));break;case N:e.push(i)}}}function f(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 h(){return null!==X}function l(t){p();try{t.call(null,function(){throw U})}catch(t){if(d(),t!==U)throw t;return}v()}function _(t){h()?t():l(t)}function p(){X=new function(t){this.parent=t,this.id2originalValue={},this.modifiedAtoms=[]}(X)}function v(){var t=X;if(null===(X=t.parent)){var e=[];t.modifiedAtoms.forEach(function(n){n.__equals(n._value,t.id2originalValue[n._id])?n._state=G:(n._state=B,c(n,e))}),f(e),t.modifiedAtoms.forEach(function(t){t._state=G})}}function d(){var t=X;X=t.parent,t.modifiedAtoms.forEach(function(e){e._value=t.id2originalValue[e._id],e._state=G,c(e,[])})}function y(t){return o(t)?t.get():t}function g(t){if(o(t))return t.get();if(t instanceof Array)return t.map(g);if(t.constructor===Object){ | ||
for(var e={},n=I(t),r=n.length;r--;){var i=n[r];e[i]=g(t[i])}return e}return t}function w(t){this._deriver=t,this._parents=null,this._type=S,this._value=M,this._equals=null,this._activeChildren=[],this._state=H,L&&(this.stack=Error().stack)}function m(t,e){if(function(t,e){var n=t.indexOf(e);n>=0&&t.splice(n,1)}(t._activeChildren,e),0===t._activeChildren.length&&null!=t._parents){for(var n=t._parents.length,r=0;r<n;r++)m(t._parents[r],t);t._parents=null,t._state=H}}function b(t,e,r,i,o){if(t instanceof Array){s=n(arguments,1);return b(function(){for(var e="",n=0;n<t.length;n++)e+=t[n],n<s.length&&(e+=y(s[n]));return e})}switch(arguments.length){case 0:throw new Error("derive takes at least one argument");case 1:return new w(t);case 2:return new w(function(){return t(y(e))});case 3:return new w(function(){return t(y(e),y(r))});case 4:return new w(function(){return t(y(e),y(r),y(i))});case 5:return new w(function(){return t(y(e),y(r),y(i),y(o))});default:var s=n(arguments,1);return new w(function(){return t.apply(null,s.map(y))})}}function E(t,e,n){this._parent=t,this.react=e,this._governor=n||null,this._active=!1,this._reacting=!1,this._type=N,L&&(this.stack=Error().stack)}function A(t){return this._id=T++,this._activeChildren=[],this._value=t,this._state=G,this._type=P,this._equals=null,this}function O(t){return new A(t)}function k(t){w.call(this,t.get),this._proxyMapping=t,this._type=F}function q(t){return function(){var e=arguments;return b(function(){for(var n,r=0;r<e.length&&(n=y(e[r]),!t(n));r++);return n})}}function x(t){return t}function D(t){return function(e){return!t(e)}}function R(t){if(t.constructor===Object||t instanceof Array)return ht(function(){return g(t)});throw new Error("`struct` expects plain Object or Array")}function j(t,e){var n=e;return function(e){var r=t.call(this,e,n);return n=e,r}}function C(t){var e=[];s(void 0,e);try{t()}finally{u()}return e}var I=Object.keys,V=Object.assign,T=0,M=Object.freeze({equals:function(){return!1} | ||
}),L=!1,P="ATOM",S="DERIVATION",F="PROXY",N="REACTOR",z=0,B=1,G=2,H=3,J=[],K=null,U={},X=null,Y=0;V(w.prototype,{_clone:function(){return i(b(this._deriver),this._equals)},_forceEval:function(){var t,e=this,n=null;try{if(null===this._parents&&(this._parents=[]),s(this,this._parents),L)try{n=e._deriver()}catch(t){throw console.error(e.stack),t}else n=e._deriver();t=J[J.length-1].offset}finally{u()}this._state=this.__equals(n,this._value)?G:B;for(var r=t,i=this._parents.length;r<i;r++){m(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===z){for(var t=this._parents.length,e=0;e<t;e++){var n=this._parents[e];if(n._state===z&&n._update(),n._state===B){this._forceEval();break}}this._state===z&&(this._state=G)}},get:function(){if(a(this),this._activeChildren.length>0)this._update();else{s(void 0,[]);try{this._value=this._deriver()}finally{u()}}return this._value}}),V(E.prototype,{start:function(){return this._active=!0,e(this._parent._activeChildren,this),this._parent.get(),this},_force:function(t){try{this._reacting=!0,this.react(t)}catch(t){throw L&&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===B&&this._force(t)}},stop:function(){return m(this._parent,this),this._active=!1,this}});var Q="object"==typeof window?window:"object"==typeof global?global:{},W=Q.__DERIVABLE_DEVTOOLS_HOOK__;V(A.prototype,{_clone:function(){return i(O(this._value),this._equals)},set:function(t){!function(t){null!==X&&(t._id in X.id2originalValue||(X.modifiedAtoms.push(t),X.id2originalValue[t._id]=t._value))}(this);var e=this._value;if(this._value=t,!h()&&!this.__equals(t,e))try{this._state=B;var n=[];c(this,n),f(n)}finally{this._state=G}},get:function(){ | ||
return"function"==typeof W&&W("captureAtom",this),a(this),this._value}}),V(k.prototype,w.prototype,{_clone:function(){return i(new k(this._proxyMapping),this._equals)},set:function(t){var e=this;return _(function(){e._proxyMapping.set(t)}),this}});var Z=q(x),$=q(r),tt=q(D(x)),et=q(D(r)),nt=E,rt=l,it=function(t){L=!!t},ot=function(t){return function(){var e,r=n(arguments,0),i=this;return l(function(){e=t.apply(i,r)}),e}},st=function(){0===Y&&p(),Y++;var t=!1;return{tick:function(){if(t)throw new Error("trying to use ticker after release");v(),p()},reset:function(){if(t)throw new Error("trying to use ticker after release");d(),p()},release:function(){if(t)throw new Error("ticker already released");t=!0,0==--Y&&v()}}},ut=o,at=function(t){return t&&(t._type===P||t._type===F)},ct=function(t){return t&&t._type===F},ft=function(t){return t&&(t._type===S||t._type===F)},ht=b,lt=O,_t=function(t){return function(){var e,r=n(arguments,0),i=this;return _(function(){e=t.apply(i,r)}),e}},pt=_,vt=function(t){return new k(t)},dt=y,yt=Object.freeze({__Reactor:nt,transact:rt,setDebugMode:it,transaction:ot,ticker:st,isDerivable:ut,isAtom:at,isProxy:ct,isDerivation:ft,derive:ht,atom:lt,atomic:_t,atomically:pt,proxy:vt,unpack:dt,struct:R,wrapPreviousState:j,captureDereferences:C,or:Z,mOr:$,and:tt,mAnd:et}),gt={derive:function(t,e,r,i,s){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 ht(t,u);case"string":case"number":return ht(function(){return u.get()[t]});default:if(t instanceof Array)return t.map(function(t){return u.derive(t)});if(t instanceof RegExp)return ht(function(){return u.get().match(t)});if(o(t))return ht(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 ht(t,u,e);case 3:return ht(t,u,e,r);case 4:return ht(t,u,e,r,i);case 5:return ht(t,u,e,r,i,s) | ||
;default:var a=[t,u].concat(n(arguments,1));return ht.apply(null,a)}},react:function(t,e){!function(t,e,n){function r(e,n){if(!o(e)){if("function"==typeof e)return b(function(){return e(t)});if("boolean"==typeof e)return b(function(){return e});throw Error("react "+n+" condition must be derivable, got: "+JSON.stringify(e))}return e}if("function"!=typeof e)throw Error("the first argument to .react must be a function");var i=(n=V({once:!1,from:!0,until:!1,when:!0,skipFirst:!1},n)).skipFirst,s=new E(t,function(t){i?i=!1:(e(t),n.once&&(this.stop(),c.stop()))}),u=r(n.until,"until"),a=r(n.when,"when"),c=new E(b(function(){return{until:u.get(),when:a.get()}}),function(t){t.until?(s.stop(),this.stop()):t.when?s._active||s.start().force():s._active&&s.stop()});s._governor=c,new E(r(n.from,"from"),function(t){t&&(c.start().force(),this.stop())}).start().force()}(this,t,e)},mReact:function(t,e){var n=this.mThen(!0,!1);if(e&&"when"in e&&!0!==e.when){var r=e.when;if("function"==typeof r||!1===r)r=ht(r);else if(!o(r))throw new Error("when condition must be bool, function, or derivable");n=r.and(n)}return this.react(t,V({},e,{when:n}))},is:function(t){var e=this;return ht(function(){return e.__equals(e.get(),y(t))})},then:function(t,e){var n=this;return ht(function(){return y(n.get()?t:e)})},mThen:function(t,e){var n=this;return ht(function(){return y(r(n.get())?t:e)})},or:function(t){return Z(this,t)},mOr:function(t){return $(this,t)},and:function(t){return tt(this,t)},mAnd:function(t){return et(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))},not:function(){var t=this;return ht(function(){return!t.get()})},withEquality:function(t){if(t){if("function"!=typeof t)throw new Error("equals must be function")}else t=null;return i(this._clone(),t)},__equals:function(t,e){return(this._equals||function(t,e){return Object.is(t,e)||t&&"function"==typeof t.equals&&t.equals(e)})(t,e)}} | ||
;gt.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,y(t[r])))return y(t[r+1]);if(r===t.length-1)return y(t[r])})};var wt={update:function(t){var e=n(arguments,0);return e[0]=this.get(),this.set(t.apply(null,e))},proxy:function(t){var e=this;return new k({get:function(){return t.get(e.get())},set:function(n){e.set(t.set(e.get(),n))}})}};V(w.prototype,gt),V(k.prototype,gt,wt),V(A.prototype,gt,wt),Q.__DERIVABLE_INIT_FLAG__&&console.warn("Multiple instances of derivable have been initialized on the same page"),Q.__DERIVABLE_INIT_FLAG__=!0;var mt=rt,bt=it,Et=ot,At=st,Ot=ut,kt=at,qt=ct,xt=ft,Dt=ht,Rt=lt,jt=_t,Ct=pt,It=vt,Vt=dt,Tt=R,Mt=j,Lt=C,Pt=Z,St=$,Ft=tt,Nt=et;t.__Reactor=nt,t.transact=mt,t.setDebugMode=bt,t.transaction=Et,t.ticker=At,t.isDerivable=Ot,t.isAtom=kt,t.isProxy=qt,t.isDerivation=xt,t.derive=Dt,t.atom=Rt,t.atomic=jt,t.atomically=Ct,t.proxy=It,t.unpack=Vt,t.struct=Tt,t.wrapPreviousState=Mt,t.captureDereferences=Lt,t.or=Pt,t.mOr=St,t.and=Ft,t.mAnd=Nt,t.default=yt,Object.defineProperty(t,"__esModule",{value:!0})}); | ||
//# sourceMappingURL=derivable.umd.min.js.map |
{ | ||
"name": "derivable", | ||
"version": "2.0.0-beta.0", | ||
"version": "2.0.0-beta.1", | ||
"description": "Functional Reactive State for JavaScript & TypeScript", | ||
@@ -11,5 +11,6 @@ "author": "David Sheldrick", | ||
"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)", | ||
"build": "rollup -c && cross-env MINIFY=true rollup -c && cp derivable.d.ts derivable.js.flow dist/", | ||
"test": "npm run build && jest && npm run lint", | ||
"lint": "eslint src", | ||
"ci:test": "npm run test && (cd test_flow && npm install && npm test)", | ||
"bench": "node scripts/bench.js", | ||
@@ -19,8 +20,8 @@ "coverage": "jest --coverage", | ||
"stats": "node scripts/stats.js", | ||
"docs": "node scripts/make-docs.js", | ||
"toc": "doctoc README.md", | ||
"clean": "rm -rf dist", | ||
"all": "npm run clean && npm run build && npm run test && npm run coverage && npm run docs && npm run stats && npm run bench && npm run toc" | ||
"all": "npm run clean && npm run build && npm run test && npm run coverage && npm run stats && npm run bench && npm run toc" | ||
}, | ||
"main": "dist/derivable.js", | ||
"module": "dist/derivable.es.js", | ||
"typings": "dist/derivable.d.ts", | ||
@@ -40,2 +41,11 @@ "typescript": { | ||
], | ||
"babel": { | ||
"env": { | ||
"test": { | ||
"plugins": [ | ||
"transform-es2015-modules-commonjs" | ||
] | ||
} | ||
} | ||
}, | ||
"jest": { | ||
@@ -45,4 +55,3 @@ "testRegex": ".*_test.js$" | ||
"devDependencies": { | ||
"babel": "^6.5.2", | ||
"babel-preset-es2015": "^6.6.0", | ||
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.0", | ||
"babel-register": "^6.7.2", | ||
@@ -54,12 +63,13 @@ "benchmark": "^2.1.0", | ||
"coveralls": "^2.11.9", | ||
"cross-env": "^5.1.1", | ||
"doctoc": "^1.0.0", | ||
"eslint": "^4.10.0", | ||
"eslint-plugin-es5": "^1.1.0", | ||
"immutable": "^3.7.4", | ||
"jest": "^19.0.2", | ||
"jest": "^21.2.1", | ||
"mobx": "^2.3.3", | ||
"np": "^2.16.0", | ||
"promise": "^7.1.1", | ||
"rollup": "^0.26.1", | ||
"source-map": "^0.5.6", | ||
"source-map-support": "^0.3.2", | ||
"uglify-js": "^2.4.24" | ||
"rollup": "^0.51.1", | ||
"rollup-plugin-uglify": "^2.0.1", | ||
"source-map-support": "^0.3.2" | ||
}, | ||
@@ -66,0 +76,0 @@ "repository": { |
@@ -210,3 +210,3 @@ <h1 align="center">DerivableJS</h1> | ||
DerivableJS works spiffingly with [Immmutable](https://github.com/facebook/immutable), which | ||
DerivableJS works spiffingly with [Immutable](https://github.com/facebook/immutable), which | ||
is practically required if your app deals with medium-to-large collections. | ||
@@ -213,0 +213,0 @@ |
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
397805
16
2974