Comparing version 1.0.9 to 1.0.10
@@ -5,2 +5,7 @@ # Change Log | ||
<a name="1.0.10"></a> | ||
## [1.0.10](https://github.com/zerkalica/lom_atom/compare/v1.0.9...v1.0.10) (2017-08-22) | ||
<a name="1.0.9"></a> | ||
@@ -7,0 +12,0 @@ ## [1.0.9](https://github.com/zerkalica/lom_atom/compare/v1.0.8...v1.0.9) (2017-08-14) |
@@ -27,3 +27,19 @@ // eslint-disable-line | ||
var createClass = function () { | ||
function defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
} | ||
} | ||
return function (Constructor, protoProps, staticProps) { | ||
if (protoProps) defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
}; | ||
}(); | ||
@@ -128,3 +144,2 @@ | ||
if (this.status !== ATOM_STATUS_DESTROYED) { | ||
// console.log('destroy', this.field) | ||
if (this._masters) { | ||
@@ -138,8 +153,4 @@ this._masters.forEach(disleadThis, this); | ||
var host = this.host; | ||
this._context.destroyHost(this); | ||
if (host !== undefined) { | ||
this._context.destroyHost(this); | ||
} | ||
this.cached = undefined; | ||
@@ -156,3 +167,3 @@ this.status = ATOM_STATUS_DESTROYED; | ||
Atom.prototype.get = function get(force) { | ||
Atom.prototype.get = function get$$1(force) { | ||
if (force || this._context.force) { | ||
@@ -172,9 +183,7 @@ this._context.force = false; | ||
if (!slaves) { | ||
// console.log('unreap', this.field) | ||
this._context.unreap(this); | ||
slaves = this._slaves = new Set(); | ||
} // console.log('add slave', slave.field, 'to master', this.field) | ||
} | ||
slaves.add(slave); | ||
@@ -187,3 +196,3 @@ slave.addMaster(this); | ||
Atom.prototype.set = function set(v, force) { | ||
Atom.prototype.set = function set$$1(v, force) { | ||
var normalized = this._normalize(v, this.cached); | ||
@@ -199,3 +208,3 @@ | ||
var context = this._context; // console.log('set', this.field, 'value', normalized) | ||
var context = this._context; | ||
@@ -207,2 +216,3 @@ if (!force && !context.force || normalized instanceof Error) { | ||
this.cached = normalized instanceof Error ? createMock(normalized) : normalized; | ||
context.newValue(this, oldValue, normalized); | ||
@@ -212,4 +222,2 @@ if (this._slaves) { | ||
} | ||
context.newValue(this, oldValue, normalized); | ||
} else { | ||
@@ -272,7 +280,7 @@ this.obsolete(); | ||
this._context.newValue(this, oldValue, newValue, true); | ||
if (this._slaves) { | ||
this._slaves.forEach(obsoleteSlave); | ||
} | ||
this._context.newValue(this, oldValue, newValue); | ||
} | ||
@@ -286,7 +294,6 @@ }; | ||
if (slaves.size === 1) { | ||
this._slaves = null; // console.log('reap (slaves === null)', this.field) | ||
this._slaves = null; | ||
this._context.proposeToReap(this); | ||
} else { | ||
// console.log('delete slave', slave.field, 'from', this.field) | ||
slaves.delete(slave); | ||
@@ -301,3 +308,2 @@ } | ||
} else { | ||
// console.log('pull', this.field) | ||
this._context.proposeToPull(this); | ||
@@ -335,2 +341,10 @@ } | ||
createClass(Atom, [{ | ||
key: "displayName", | ||
get: function get$$1() { | ||
var hc = this.host.constructor; | ||
var k = this.key; | ||
return (this.host.displayName || (hc ? String(hc.displayName || hc.name) : '')) + '.' + this.field + (k ? '(' + (typeof k === 'function' ? k.displayName || k.name : String(k)) + ')' : ''); | ||
} | ||
}]); | ||
return Atom; | ||
@@ -374,5 +388,39 @@ }(); | ||
var BaseLogger = function () { | ||
function BaseLogger() {} | ||
BaseLogger.prototype.status = function status(_status, atom) {}; | ||
BaseLogger.prototype.error = function error(atom, err) {}; | ||
BaseLogger.prototype.newValue = function newValue(atom, from, to, isActualize) {}; | ||
return BaseLogger; | ||
}(); | ||
var ConsoleLogger = function (_BaseLogger) { | ||
inheritsLoose(ConsoleLogger, _BaseLogger); | ||
function ConsoleLogger() { | ||
return _BaseLogger.apply(this, arguments) || this; | ||
} | ||
ConsoleLogger.prototype.status = function status(_status2, atom) { | ||
console.log(_status2, atom.displayName); | ||
}; | ||
ConsoleLogger.prototype.error = function error(atom, err) { | ||
console.log('error', atom.displayName, err); | ||
}; | ||
ConsoleLogger.prototype.newValue = function newValue(atom, from, to, isActualize) { | ||
console.log(isActualize ? 'actualize' : 'cacheSet', atom.displayName, 'from', from, 'to', to); | ||
}; | ||
return ConsoleLogger; | ||
}(BaseLogger); | ||
var Context = function () { | ||
function Context() { | ||
var _this = this; | ||
var _this2 = this; | ||
@@ -388,6 +436,6 @@ this.last = null; | ||
this.__run = function () { | ||
if (_this._scheduled) { | ||
_this._scheduled = false; | ||
if (_this2._scheduled) { | ||
_this2._scheduled = false; | ||
_this._run(); | ||
_this2._run(); | ||
} | ||
@@ -400,2 +448,9 @@ }; | ||
Context.prototype.hasAtom = function hasAtom(host, key) { | ||
// return host[getKey(key) + '@'] !== undefined | ||
var map = this._atomMap.get(host); | ||
return map !== undefined && map.has(getKey(key)); | ||
}; | ||
Context.prototype.getAtom = function getAtom(field, host, key, normalize, isComponent) { | ||
@@ -416,3 +471,3 @@ var k = key === undefined ? field : getKey(key); | ||
atom = new Atom(field, host, this, key, normalize, isComponent); | ||
map.set(k, atom); // host[k + '@'] = atom | ||
map.set(k, atom); // host[k + '@'] = (atom: any) | ||
} | ||
@@ -424,8 +479,15 @@ | ||
Context.prototype.destroyHost = function destroyHost(atom) { | ||
var host = atom.host; // host[(atom.key === undefined ? atom.field : getKey(atom.key)) + '@'] = undefined | ||
// | ||
// if (host._destroy !== undefined) { | ||
var host = atom.host; // const k = atom.key === undefined ? atom.field : getKey(atom.key) | ||
// host[k + '@'] = (undefined: any) | ||
// if (host._destroyProp !== undefined) { | ||
// host._destroyProp(atom.key === undefined ? atom.field : atom.key, atom.cached) | ||
// } | ||
// if (host._destroy !== undefined && atom.key === undefined) { | ||
// host._destroy() | ||
// } | ||
if (this._logger) { | ||
this._logger.status('destroy', atom); | ||
} | ||
var map = this._atomMap.get(host); | ||
@@ -454,16 +516,21 @@ | ||
Context.prototype.newValue = function newValue(atom, from, to) { | ||
if (this._logger) { | ||
if (to instanceof AtomWait) { | ||
this._logger.pulling(atom); | ||
} else if (to instanceof Error) { | ||
this._logger.error(atom, to); | ||
} else { | ||
this._logger.newValue(atom, from, to); | ||
} | ||
Context.prototype.newValue = function newValue(atom, from, to, isActualize) { | ||
if (!this._logger) { | ||
return; | ||
} | ||
if (to instanceof AtomWait) { | ||
this._logger.status('waiting', atom); | ||
} else if (to instanceof Error) { | ||
this._logger.error(atom, to); | ||
} else { | ||
this._logger.newValue(atom, from, to, isActualize); | ||
} | ||
}; | ||
Context.prototype.proposeToPull = function proposeToPull(atom) { | ||
// this.logger.pull(atom) | ||
if (this._logger) { | ||
this._logger.status('proposeToPull', atom); | ||
} | ||
this._updating.push(atom); | ||
@@ -475,3 +542,6 @@ | ||
Context.prototype.proposeToReap = function proposeToReap(atom) { | ||
// this.logger.reap(atom) | ||
if (this._logger) { | ||
this._logger.status('proposeToReap', atom); | ||
} | ||
this._reaping.add(atom); | ||
@@ -483,3 +553,2 @@ | ||
Context.prototype.unreap = function unreap(atom) { | ||
// this.logger.unreap(atom) | ||
this._reaping.delete(atom); | ||
@@ -608,2 +677,7 @@ }; | ||
proto[handlerKey] = handler; | ||
proto[handlerKey + '?'] = function (rawKey) { | ||
return defaultContext.hasAtom(this, rawKey); | ||
}; | ||
return { | ||
@@ -781,3 +855,3 @@ enumerable: descr.enumerable, | ||
export { Atom, mem, memkey, detached, force, action, defaultContext }; | ||
export { Atom, mem, memkey, detached, force, action, defaultContext, BaseLogger, ConsoleLogger }; | ||
//# sourceMappingURL=lom_atom.es.js.map |
@@ -1,2 +0,2 @@ | ||
"use strict";function createMock(t){return new Proxy(t,throwOnAccess)}function defaultNormalize(t,e){if(t===e)return t;if(t instanceof Array&&e instanceof Array&&t.length===e.length){for(var o=0;o<t.length;o++)if(t[o]!==e[o])return t;return e}return t}function checkSlave(t){t.check()}function obsoleteSlave(t){t.obsolete()}function disleadThis(t){t.dislead(this)}function actualizeMaster(t){this.status===ATOM_STATUS_CHECKING&&t.actualize()}function reap(t,e,o){o.delete(t),t.slaves||t.destroyed(!0)}function getKey(t){return"string"==typeof t||"number"==typeof t?t:t?"function"==typeof t?(t.__id=t.__id||++lastId,t.__id):"object"===_typeof(t)?Object.keys(t).sort().map(function(e){return e+":"+JSON.stringify(t[e])}).join("."):JSON.stringify(t):0}function memMethod(t,e,o,i,n){var s=e+"$";if(void 0===o.value)throw new TypeError(e+" is not an function (next?: V)");return t[s]=o.value,{enumerable:o.enumerable,configurable:o.configurable,value:function(t,e){return defaultContext.getAtom(s,this,void 0,i,n).value(t,e)}}}function createGetSetHandler(t,e){return function(o){return void 0===o?t.call(this):(e.call(this,o),o)}}function createValueHandler(t){return function(e){return void 0===e&&void 0!==t?t.call(this):e}}function memProp(t,e,o,i){var n=e+"$";if(void 0===t[n]){t[n]=void 0===o.get&&void 0===o.set?createValueHandler(o.initializer):createGetSetHandler(o.get,o.set);return{enumerable:o.enumerable,configurable:o.configurable,get:function(){return defaultContext.getAtom(n,this,void 0,i).get()},set:function(t){defaultContext.getAtom(n,this,void 0,i).set(t)}}}}function memkeyProp(t,e,o,i){var n=o.value;if(void 0===n)throw new TypeError(e+" is not an function (rawKey: K, next?: V)");var s=e+"$";return t[s]=n,{enumerable:o.enumerable,configurable:o.configurable,value:function(t,e,o){return defaultContext.getAtom(s,this,t,i).value(e,o)}}}function memkey(){if(3===arguments.length)return memkeyProp(arguments[0],arguments[1],arguments[2]);var t=arguments[0];return function(e,o,i){return memkeyProp(e,o,i,t)}}function forceGet(){return defaultContext.force=!0,this}function force(t,e,o){return{enumerable:o.enumerable,configurable:o.configurable,get:forceGet}}function detached(t,e,o){return memMethod(t,e,o,void 0,!0)}function createActionMethod(t,e,o){function i(){var i=void 0;switch(o.beginTransaction(),arguments.length){case 0:i=t[e]();break;case 1:i=t[e](arguments[0]);break;case 2:i=t[e](arguments[0],arguments[1]);break;case 3:i=t[e](arguments[0],arguments[1],arguments[2]);break;case 4:i=t[e](arguments[0],arguments[1],arguments[2],arguments[3]);break;case 5:i=t[e](arguments[0],arguments[1],arguments[2],arguments[3],arguments[4]);break;default:i=t[e].apply(t,arguments)}return o.endTransaction(),i}return i.displayName=e,i}function createActionFn(t,e,o){function i(){var e=void 0;switch(o.beginTransaction(),arguments.length){case 0:e=t();break;case 1:e=t(arguments[0]);break;case 2:e=t(arguments[0],arguments[1]);break;case 3:e=t(arguments[0],arguments[1],arguments[2]);break;case 4:e=t(arguments[0],arguments[1],arguments[2],arguments[3]);break;case 5:e=t(arguments[0],arguments[1],arguments[2],arguments[3],arguments[4]);break;default:e=t.apply(null,arguments)}return o.endTransaction(),e}return i.displayName=e||t.displayName||t.name,i}function actionMethod(t,e,o,i){var n=e+"$";if(void 0===o.value)throw new TypeError(e+" is not an function (next?: V)");t[n]=o.value;var s=!1;return{enumerable:o.enumerable,configurable:o.configurable,get:function(){if(s)return this[n];s=!0;var t=createActionMethod(this,n,i);return Object.defineProperty(this,e,{configurable:!0,get:function(){return t}}),s=!1,t}}}function action(){return 3===arguments.length?actionMethod(arguments[0],arguments[1],arguments[2],defaultContext):createActionFn(arguments[0],arguments[1],defaultContext)}function mem(){if(3===arguments.length)return void 0===arguments[2].value?memProp(arguments[0],arguments[1],arguments[2]):memMethod(arguments[0],arguments[1],arguments[2]);var t=arguments[0];return function(e,o,i){return void 0===i.value?memProp(e,o,i,t):memMethod(e,o,i,t)}}Object.defineProperty(exports,"__esModule",{value:!0});var ATOM_STATUS_DESTROYED=0,ATOM_STATUS_OBSOLETE=1,ATOM_STATUS_CHECKING=2,ATOM_STATUS_PULLING=3,ATOM_STATUS_ACTUAL=4,catchedId=Symbol("lom_atom_catched"),_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},inheritsLoose=function(t,e){t.prototype=Object.create(e.prototype),t.prototype.constructor=t,t.__proto__=e},throwOnAccess={get:function(t){throw t.valueOf()},ownKeys:function(t){throw t.valueOf()}},AtomWait=function(t){function e(){var e,o=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"Wait...";return e=t.call(this,o)||this,e.__proto__=new.target.prototype,e[catchedId]=!0,e}return inheritsLoose(e,t),e}(Error),Atom=function(){function t(t,e,o,i,n,s){this.status=ATOM_STATUS_OBSOLETE,this.cached=void 0,this._masters=null,this._slaves=null,this.field=t,this.key=i,this.host=e,this.isComponent=s||!1,this._normalize=n||defaultNormalize,this._context=o}return t.prototype.destroyed=function(t){return void 0===t?this.status===ATOM_STATUS_DESTROYED:!!t&&(this.status!==ATOM_STATUS_DESTROYED&&(this._masters&&(this._masters.forEach(disleadThis,this),this._masters=null),this._checkSlaves(),void 0!==this.host&&this._context.destroyHost(this),this.cached=void 0,this.status=ATOM_STATUS_DESTROYED,this.key=void 0),!0)},t.prototype.get=function(t){t||this._context.force?(this._context.force=!1,this._pullPush(void 0,!0)):this.actualize();var e=this._context.last;if(e&&(!e.isComponent||!this.isComponent)){var o=this._slaves;o||(this._context.unreap(this),o=this._slaves=new Set),o.add(e),e.addMaster(this)}return this.cached},t.prototype.set=function(t,e){var o=this._normalize(t,this.cached);if(this.cached===o)return o;if(void 0===o)return this.cached;var i=this._context;if(!e&&!i.force||o instanceof Error){i.force=!1,this.status=ATOM_STATUS_ACTUAL;var n=this.cached;this.cached=o instanceof Error?createMock(o):o,this._slaves&&this._slaves.forEach(obsoleteSlave),i.newValue(this,n,o)}else this.obsolete(),this.actualize(o);return this.cached},t.prototype.actualize=function(t){this.status!==ATOM_STATUS_ACTUAL&&(this.status===ATOM_STATUS_CHECKING&&(this._masters&&this._masters.forEach(actualizeMaster,this),this.status===ATOM_STATUS_CHECKING&&(this.status=ATOM_STATUS_ACTUAL)),this.status!==ATOM_STATUS_ACTUAL&&this._pullPush(t))},t.prototype._pullPush=function(t,e){this._masters&&this._masters.forEach(disleadThis,this);var o=void 0;this.status=ATOM_STATUS_PULLING;var i=this._context,n=i.last;i.last=this;try{o=this._normalize(void 0===this.key?this.host[this.field](t,e,this.cached):this.host[this.field](this.key,t,e,this.cached),this.cached)}catch(t){void 0===t[catchedId]&&(t[catchedId]=!0,console.error(t.stack||t)),o=createMock(t)}if(i.last=n,this.status=ATOM_STATUS_ACTUAL,void 0!==o&&this.cached!==o){var s=this.cached;this.cached=o,this._slaves&&this._slaves.forEach(obsoleteSlave),this._context.newValue(this,s,o)}},t.prototype.dislead=function(t){var e=this._slaves;e&&(1===e.size?(this._slaves=null,this._context.proposeToReap(this)):e.delete(t))},t.prototype._checkSlaves=function(){this._slaves?this._slaves.forEach(checkSlave):this._context.proposeToPull(this)},t.prototype.check=function(){this.status===ATOM_STATUS_ACTUAL&&(this.status=ATOM_STATUS_CHECKING,this._checkSlaves())},t.prototype.obsolete=function(){this.status!==ATOM_STATUS_OBSOLETE&&(this.status=ATOM_STATUS_OBSOLETE,this._checkSlaves())},t.prototype.addMaster=function(t){this._masters||(this._masters=new Set),this._masters.add(t)},t.prototype.value=function(t,e){return void 0===t?this.get(e):this.set(t,e)},t}(),scheduleNative="function"==typeof requestAnimationFrame?function(t){return requestAnimationFrame(t)}:function(t){return setTimeout(t,16)},lastId=0,Context=function(){function t(){var t=this;this.last=null,this.force=!1,this._logger=null,this._updating=[],this._reaping=new Set,this._atomMap=new WeakMap,this._scheduled=!1,this.__run=function(){t._scheduled&&(t._scheduled=!1,t._run())},this._start=0,this._pendCount=0}return t.prototype.getAtom=function(t,e,o,i,n){var s=void 0===o?t:getKey(o),r=this._atomMap.get(e);void 0===r&&(r=new Map,this._atomMap.set(e,r));var a=r.get(s);return void 0===a&&(a=new Atom(t,e,this,o,i,n),r.set(s,a)),a},t.prototype.destroyHost=function(t){var e=t.host,o=this._atomMap.get(e);void 0!==o&&(void 0!==e._destroyProp&&e._destroyProp(void 0===t.key?t.field:t.key,t.cached),o.delete(void 0===t.key?t.field:getKey(t.key)),0===o.size&&(void 0!==e._destroy&&e._destroy(),this._atomMap.delete(e)))},t.prototype.setLogger=function(t){this._logger=t},t.prototype.newValue=function(t,e,o){this._logger&&(o instanceof AtomWait?this._logger.pulling(t):o instanceof Error?this._logger.error(t,o):this._logger.newValue(t,e,o))},t.prototype.proposeToPull=function(t){this._updating.push(t),this._schedule()},t.prototype.proposeToReap=function(t){this._reaping.add(t),this._schedule()},t.prototype.unreap=function(t){this._reaping.delete(t)},t.prototype._schedule=function(){this._scheduled||(scheduleNative(this.__run),this._scheduled=!0)},t.prototype._run=function(){this._schedule();var t=this._reaping,e=this._updating,o=this._start;do{for(var i=e.length,n=o;n<i;n++){this._start=n;var s=e[n];t.has(s)||s.destroyed()||s.actualize()}o=i}while(e.length>o);for(e.length=0,this._start=0;t.size>0;)t.forEach(reap);this._scheduled=!1,this._pendCount=0},t.prototype.beginTransaction=function(){this._pendCount++},t.prototype.endTransaction=function(){1===this._pendCount?this._run():this._pendCount--},t}(),defaultContext=new Context;mem.Wait=AtomWait,mem.key=memkey,mem.detached=detached,exports.Atom=Atom,exports.mem=mem,exports.memkey=memkey,exports.detached=detached,exports.force=force,exports.action=action,exports.defaultContext=defaultContext; | ||
"use strict";function createMock(t){return new Proxy(t,throwOnAccess)}function defaultNormalize(t,e){if(t===e)return t;if(t instanceof Array&&e instanceof Array&&t.length===e.length){for(var o=0;o<t.length;o++)if(t[o]!==e[o])return t;return e}return t}function checkSlave(t){t.check()}function obsoleteSlave(t){t.obsolete()}function disleadThis(t){t.dislead(this)}function actualizeMaster(t){this.status===ATOM_STATUS_CHECKING&&t.actualize()}function reap(t,e,o){o.delete(t),t.slaves||t.destroyed(!0)}function getKey(t){return"string"==typeof t||"number"==typeof t?t:t?"function"==typeof t?(t.__id=t.__id||++lastId,t.__id):"object"===_typeof(t)?Object.keys(t).sort().map(function(e){return e+":"+JSON.stringify(t[e])}).join("."):JSON.stringify(t):0}function memMethod(t,e,o,n,i){var s=e+"$";if(void 0===o.value)throw new TypeError(e+" is not an function (next?: V)");return t[s]=o.value,{enumerable:o.enumerable,configurable:o.configurable,value:function(t,e){return defaultContext.getAtom(s,this,void 0,n,i).value(t,e)}}}function createGetSetHandler(t,e){return function(o){return void 0===o?t.call(this):(e.call(this,o),o)}}function createValueHandler(t){return function(e){return void 0===e&&void 0!==t?t.call(this):e}}function memProp(t,e,o,n){var i=e+"$";if(void 0===t[i]){t[i]=void 0===o.get&&void 0===o.set?createValueHandler(o.initializer):createGetSetHandler(o.get,o.set);return{enumerable:o.enumerable,configurable:o.configurable,get:function(){return defaultContext.getAtom(i,this,void 0,n).get()},set:function(t){defaultContext.getAtom(i,this,void 0,n).set(t)}}}}function memkeyProp(t,e,o,n){var i=o.value;if(void 0===i)throw new TypeError(e+" is not an function (rawKey: K, next?: V)");var s=e+"$";return t[s]=i,t[s+"?"]=function(t){return defaultContext.hasAtom(this,t)},{enumerable:o.enumerable,configurable:o.configurable,value:function(t,e,o){return defaultContext.getAtom(s,this,t,n).value(e,o)}}}function memkey(){if(3===arguments.length)return memkeyProp(arguments[0],arguments[1],arguments[2]);var t=arguments[0];return function(e,o,n){return memkeyProp(e,o,n,t)}}function forceGet(){return defaultContext.force=!0,this}function force(t,e,o){return{enumerable:o.enumerable,configurable:o.configurable,get:forceGet}}function detached(t,e,o){return memMethod(t,e,o,void 0,!0)}function createActionMethod(t,e,o){function n(){var n=void 0;switch(o.beginTransaction(),arguments.length){case 0:n=t[e]();break;case 1:n=t[e](arguments[0]);break;case 2:n=t[e](arguments[0],arguments[1]);break;case 3:n=t[e](arguments[0],arguments[1],arguments[2]);break;case 4:n=t[e](arguments[0],arguments[1],arguments[2],arguments[3]);break;case 5:n=t[e](arguments[0],arguments[1],arguments[2],arguments[3],arguments[4]);break;default:n=t[e].apply(t,arguments)}return o.endTransaction(),n}return n.displayName=e,n}function createActionFn(t,e,o){function n(){var e=void 0;switch(o.beginTransaction(),arguments.length){case 0:e=t();break;case 1:e=t(arguments[0]);break;case 2:e=t(arguments[0],arguments[1]);break;case 3:e=t(arguments[0],arguments[1],arguments[2]);break;case 4:e=t(arguments[0],arguments[1],arguments[2],arguments[3]);break;case 5:e=t(arguments[0],arguments[1],arguments[2],arguments[3],arguments[4]);break;default:e=t.apply(null,arguments)}return o.endTransaction(),e}return n.displayName=e||t.displayName||t.name,n}function actionMethod(t,e,o,n){var i=e+"$";if(void 0===o.value)throw new TypeError(e+" is not an function (next?: V)");t[i]=o.value;var s=!1;return{enumerable:o.enumerable,configurable:o.configurable,get:function(){if(s)return this[i];s=!0;var t=createActionMethod(this,i,n);return Object.defineProperty(this,e,{configurable:!0,get:function(){return t}}),s=!1,t}}}function action(){return 3===arguments.length?actionMethod(arguments[0],arguments[1],arguments[2],defaultContext):createActionFn(arguments[0],arguments[1],defaultContext)}function mem(){if(3===arguments.length)return void 0===arguments[2].value?memProp(arguments[0],arguments[1],arguments[2]):memMethod(arguments[0],arguments[1],arguments[2]);var t=arguments[0];return function(e,o,n){return void 0===n.value?memProp(e,o,n,t):memMethod(e,o,n,t)}}Object.defineProperty(exports,"__esModule",{value:!0});var ATOM_STATUS_DESTROYED=0,ATOM_STATUS_OBSOLETE=1,ATOM_STATUS_CHECKING=2,ATOM_STATUS_PULLING=3,ATOM_STATUS_ACTUAL=4,catchedId=Symbol("lom_atom_catched"),_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},createClass=function(){function t(t,e){for(var o=0;o<e.length;o++){var n=e[o];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}return function(e,o,n){return o&&t(e.prototype,o),n&&t(e,n),e}}(),inheritsLoose=function(t,e){t.prototype=Object.create(e.prototype),t.prototype.constructor=t,t.__proto__=e},throwOnAccess={get:function(t){throw t.valueOf()},ownKeys:function(t){throw t.valueOf()}},AtomWait=function(t){function e(){var e,o=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"Wait...";return e=t.call(this,o)||this,e.__proto__=new.target.prototype,e[catchedId]=!0,e}return inheritsLoose(e,t),e}(Error),Atom=function(){function t(t,e,o,n,i,s){this.status=ATOM_STATUS_OBSOLETE,this.cached=void 0,this._masters=null,this._slaves=null,this.field=t,this.key=n,this.host=e,this.isComponent=s||!1,this._normalize=i||defaultNormalize,this._context=o}return t.prototype.destroyed=function(t){return void 0===t?this.status===ATOM_STATUS_DESTROYED:!!t&&(this.status!==ATOM_STATUS_DESTROYED&&(this._masters&&(this._masters.forEach(disleadThis,this),this._masters=null),this._checkSlaves(),this._context.destroyHost(this),this.cached=void 0,this.status=ATOM_STATUS_DESTROYED,this.key=void 0),!0)},t.prototype.get=function(t){t||this._context.force?(this._context.force=!1,this._pullPush(void 0,!0)):this.actualize();var e=this._context.last;if(e&&(!e.isComponent||!this.isComponent)){var o=this._slaves;o||(this._context.unreap(this),o=this._slaves=new Set),o.add(e),e.addMaster(this)}return this.cached},t.prototype.set=function(t,e){var o=this._normalize(t,this.cached);if(this.cached===o)return o;if(void 0===o)return this.cached;var n=this._context;if(!e&&!n.force||o instanceof Error){n.force=!1,this.status=ATOM_STATUS_ACTUAL;var i=this.cached;this.cached=o instanceof Error?createMock(o):o,n.newValue(this,i,o),this._slaves&&this._slaves.forEach(obsoleteSlave)}else this.obsolete(),this.actualize(o);return this.cached},t.prototype.actualize=function(t){this.status!==ATOM_STATUS_ACTUAL&&(this.status===ATOM_STATUS_CHECKING&&(this._masters&&this._masters.forEach(actualizeMaster,this),this.status===ATOM_STATUS_CHECKING&&(this.status=ATOM_STATUS_ACTUAL)),this.status!==ATOM_STATUS_ACTUAL&&this._pullPush(t))},t.prototype._pullPush=function(t,e){this._masters&&this._masters.forEach(disleadThis,this);var o=void 0;this.status=ATOM_STATUS_PULLING;var n=this._context,i=n.last;n.last=this;try{o=this._normalize(void 0===this.key?this.host[this.field](t,e,this.cached):this.host[this.field](this.key,t,e,this.cached),this.cached)}catch(t){void 0===t[catchedId]&&(t[catchedId]=!0,console.error(t.stack||t)),o=createMock(t)}if(n.last=i,this.status=ATOM_STATUS_ACTUAL,void 0!==o&&this.cached!==o){var s=this.cached;this.cached=o,this._context.newValue(this,s,o,!0),this._slaves&&this._slaves.forEach(obsoleteSlave)}},t.prototype.dislead=function(t){var e=this._slaves;e&&(1===e.size?(this._slaves=null,this._context.proposeToReap(this)):e.delete(t))},t.prototype._checkSlaves=function(){this._slaves?this._slaves.forEach(checkSlave):this._context.proposeToPull(this)},t.prototype.check=function(){this.status===ATOM_STATUS_ACTUAL&&(this.status=ATOM_STATUS_CHECKING,this._checkSlaves())},t.prototype.obsolete=function(){this.status!==ATOM_STATUS_OBSOLETE&&(this.status=ATOM_STATUS_OBSOLETE,this._checkSlaves())},t.prototype.addMaster=function(t){this._masters||(this._masters=new Set),this._masters.add(t)},t.prototype.value=function(t,e){return void 0===t?this.get(e):this.set(t,e)},createClass(t,[{key:"displayName",get:function(){var t=this.host.constructor,e=this.key;return(this.host.displayName||(t?String(t.displayName||t.name):""))+"."+this.field+(e?"("+("function"==typeof e?e.displayName||e.name:String(e))+")":"")}}]),t}(),scheduleNative="function"==typeof requestAnimationFrame?function(t){return requestAnimationFrame(t)}:function(t){return setTimeout(t,16)},lastId=0,BaseLogger=function(){function t(){}return t.prototype.status=function(t,e){},t.prototype.error=function(t,e){},t.prototype.newValue=function(t,e,o,n){},t}(),ConsoleLogger=function(t){function e(){return t.apply(this,arguments)||this}return inheritsLoose(e,t),e.prototype.status=function(t,e){console.log(t,e.displayName)},e.prototype.error=function(t,e){console.log("error",t.displayName,e)},e.prototype.newValue=function(t,e,o,n){console.log(n?"actualize":"cacheSet",t.displayName,"from",e,"to",o)},e}(BaseLogger),Context=function(){function t(){var t=this;this.last=null,this.force=!1,this._logger=null,this._updating=[],this._reaping=new Set,this._atomMap=new WeakMap,this._scheduled=!1,this.__run=function(){t._scheduled&&(t._scheduled=!1,t._run())},this._start=0,this._pendCount=0}return t.prototype.hasAtom=function(t,e){var o=this._atomMap.get(t);return void 0!==o&&o.has(getKey(e))},t.prototype.getAtom=function(t,e,o,n,i){var s=void 0===o?t:getKey(o),r=this._atomMap.get(e);void 0===r&&(r=new Map,this._atomMap.set(e,r));var a=r.get(s);return void 0===a&&(a=new Atom(t,e,this,o,n,i),r.set(s,a)),a},t.prototype.destroyHost=function(t){var e=t.host;this._logger&&this._logger.status("destroy",t);var o=this._atomMap.get(e);void 0!==o&&(void 0!==e._destroyProp&&e._destroyProp(void 0===t.key?t.field:t.key,t.cached),o.delete(void 0===t.key?t.field:getKey(t.key)),0===o.size&&(void 0!==e._destroy&&e._destroy(),this._atomMap.delete(e)))},t.prototype.setLogger=function(t){this._logger=t},t.prototype.newValue=function(t,e,o,n){this._logger&&(o instanceof AtomWait?this._logger.status("waiting",t):o instanceof Error?this._logger.error(t,o):this._logger.newValue(t,e,o,n))},t.prototype.proposeToPull=function(t){this._logger&&this._logger.status("proposeToPull",t),this._updating.push(t),this._schedule()},t.prototype.proposeToReap=function(t){this._logger&&this._logger.status("proposeToReap",t),this._reaping.add(t),this._schedule()},t.prototype.unreap=function(t){this._reaping.delete(t)},t.prototype._schedule=function(){this._scheduled||(scheduleNative(this.__run),this._scheduled=!0)},t.prototype._run=function(){this._schedule();var t=this._reaping,e=this._updating,o=this._start;do{for(var n=e.length,i=o;i<n;i++){this._start=i;var s=e[i];t.has(s)||s.destroyed()||s.actualize()}o=n}while(e.length>o);for(e.length=0,this._start=0;t.size>0;)t.forEach(reap);this._scheduled=!1,this._pendCount=0},t.prototype.beginTransaction=function(){this._pendCount++},t.prototype.endTransaction=function(){1===this._pendCount?this._run():this._pendCount--},t}(),defaultContext=new Context;mem.Wait=AtomWait,mem.key=memkey,mem.detached=detached,exports.Atom=Atom,exports.mem=mem,exports.memkey=memkey,exports.detached=detached,exports.force=force,exports.action=action,exports.defaultContext=defaultContext,exports.BaseLogger=BaseLogger,exports.ConsoleLogger=ConsoleLogger; | ||
//# sourceMappingURL=lom_atom.js.map |
@@ -1,2 +0,2 @@ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(t.lom_atom={})}(this,function(t){"use strict";function e(t){return new Proxy(t,T)}function i(t,e){if(t===e)return t;if(t instanceof Array&&e instanceof Array&&t.length===e.length){for(var i=0;i<t.length;i++)if(t[i]!==e[i])return t;return e}return t}function n(t){t.check()}function o(t){t.obsolete()}function s(t){t.dislead(this)}function r(t){this.status===w&&t.actualize()}function a(t,e,i){i.delete(t),t.slaves||t.destroyed(!0)}function u(t){return"string"==typeof t||"number"==typeof t?t:t?"function"==typeof t?(t.__id=t.__id||++P,t.__id):"object"===S(t)?Object.keys(t).sort().map(function(e){return e+":"+JSON.stringify(t[e])}).join("."):JSON.stringify(t):0}function h(t,e,i,n,o){var s=e+"$";if(void 0===i.value)throw new TypeError(e+" is not an function (next?: V)");return t[s]=i.value,{enumerable:i.enumerable,configurable:i.configurable,value:function(t,e){return C.getAtom(s,this,void 0,n,o).value(t,e)}}}function c(t,e){return function(i){return void 0===i?t.call(this):(e.call(this,i),i)}}function l(t){return function(e){return void 0===e&&void 0!==t?t.call(this):e}}function f(t,e,i,n){var o=e+"$";if(void 0===t[o]){t[o]=void 0===i.get&&void 0===i.set?l(i.initializer):c(i.get,i.set);return{enumerable:i.enumerable,configurable:i.configurable,get:function(){return C.getAtom(o,this,void 0,n).get()},set:function(t){C.getAtom(o,this,void 0,n).set(t)}}}}function d(t,e,i,n){var o=i.value;if(void 0===o)throw new TypeError(e+" is not an function (rawKey: K, next?: V)");var s=e+"$";return t[s]=o,{enumerable:i.enumerable,configurable:i.configurable,value:function(t,e,i){return C.getAtom(s,this,t,n).value(e,i)}}}function p(){if(3===arguments.length)return d(arguments[0],arguments[1],arguments[2]);var t=arguments[0];return function(e,i,n){return d(e,i,n,t)}}function _(){return C.force=!0,this}function v(t,e,i){return h(t,e,i,void 0,!0)}function y(t,e,i){function n(){var n=void 0;switch(i.beginTransaction(),arguments.length){case 0:n=t[e]();break;case 1:n=t[e](arguments[0]);break;case 2:n=t[e](arguments[0],arguments[1]);break;case 3:n=t[e](arguments[0],arguments[1],arguments[2]);break;case 4:n=t[e](arguments[0],arguments[1],arguments[2],arguments[3]);break;case 5:n=t[e](arguments[0],arguments[1],arguments[2],arguments[3],arguments[4]);break;default:n=t[e].apply(t,arguments)}return i.endTransaction(),n}return n.displayName=e,n}function g(t,e,i){function n(){var e=void 0;switch(i.beginTransaction(),arguments.length){case 0:e=t();break;case 1:e=t(arguments[0]);break;case 2:e=t(arguments[0],arguments[1]);break;case 3:e=t(arguments[0],arguments[1],arguments[2]);break;case 4:e=t(arguments[0],arguments[1],arguments[2],arguments[3]);break;case 5:e=t(arguments[0],arguments[1],arguments[2],arguments[3],arguments[4]);break;default:e=t.apply(null,arguments)}return i.endTransaction(),e}return n.displayName=e||t.displayName||t.name,n}function m(t,e,i,n){var o=e+"$";if(void 0===i.value)throw new TypeError(e+" is not an function (next?: V)");t[o]=i.value;var s=!1;return{enumerable:i.enumerable,configurable:i.configurable,get:function(){if(s)return this[o];s=!0;var t=y(this,o,n);return Object.defineProperty(this,e,{configurable:!0,get:function(){return t}}),s=!1,t}}}function b(){if(3===arguments.length)return void 0===arguments[2].value?f(arguments[0],arguments[1],arguments[2]):h(arguments[0],arguments[1],arguments[2]);var t=arguments[0];return function(e,i,n){return void 0===n.value?f(e,i,n,t):h(e,i,n,t)}}var k=1,w=2,x=Symbol("lom_atom_catched"),S="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},E=function(t,e){t.prototype=Object.create(e.prototype),t.prototype.constructor=t,t.__proto__=e},T={get:function(t){throw t.valueOf()},ownKeys:function(t){throw t.valueOf()}},z=function(t){function e(){var e,i=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"Wait...";return e=t.call(this,i)||this,e.__proto__=new.target.prototype,e[x]=!0,e}return E(e,t),e}(Error),A=function(){function t(t,e,n,o,s,r){this.status=k,this.cached=void 0,this._masters=null,this._slaves=null,this.field=t,this.key=o,this.host=e,this.isComponent=r||!1,this._normalize=s||i,this._context=n}return t.prototype.destroyed=function(t){return void 0===t?0===this.status:!!t&&(0!==this.status&&(this._masters&&(this._masters.forEach(s,this),this._masters=null),this._checkSlaves(),void 0!==this.host&&this._context.destroyHost(this),this.cached=void 0,this.status=0,this.key=void 0),!0)},t.prototype.get=function(t){t||this._context.force?(this._context.force=!1,this._pullPush(void 0,!0)):this.actualize();var e=this._context.last;if(e&&(!e.isComponent||!this.isComponent)){var i=this._slaves;i||(this._context.unreap(this),i=this._slaves=new Set),i.add(e),e.addMaster(this)}return this.cached},t.prototype.set=function(t,i){var n=this._normalize(t,this.cached);if(this.cached===n)return n;if(void 0===n)return this.cached;var s=this._context;if(!i&&!s.force||n instanceof Error){s.force=!1,this.status=4;var r=this.cached;this.cached=n instanceof Error?e(n):n,this._slaves&&this._slaves.forEach(o),s.newValue(this,r,n)}else this.obsolete(),this.actualize(n);return this.cached},t.prototype.actualize=function(t){4!==this.status&&(this.status===w&&(this._masters&&this._masters.forEach(r,this),this.status===w&&(this.status=4)),4!==this.status&&this._pullPush(t))},t.prototype._pullPush=function(t,i){this._masters&&this._masters.forEach(s,this);var n=void 0;this.status=3;var r=this._context,a=r.last;r.last=this;try{n=this._normalize(void 0===this.key?this.host[this.field](t,i,this.cached):this.host[this.field](this.key,t,i,this.cached),this.cached)}catch(t){void 0===t[x]&&(t[x]=!0,console.error(t.stack||t)),n=e(t)}if(r.last=a,this.status=4,void 0!==n&&this.cached!==n){var u=this.cached;this.cached=n,this._slaves&&this._slaves.forEach(o),this._context.newValue(this,u,n)}},t.prototype.dislead=function(t){var e=this._slaves;e&&(1===e.size?(this._slaves=null,this._context.proposeToReap(this)):e.delete(t))},t.prototype._checkSlaves=function(){this._slaves?this._slaves.forEach(n):this._context.proposeToPull(this)},t.prototype.check=function(){4===this.status&&(this.status=w,this._checkSlaves())},t.prototype.obsolete=function(){this.status!==k&&(this.status=k,this._checkSlaves())},t.prototype.addMaster=function(t){this._masters||(this._masters=new Set),this._masters.add(t)},t.prototype.value=function(t,e){return void 0===t?this.get(e):this.set(t,e)},t}(),M="function"==typeof requestAnimationFrame?function(t){return requestAnimationFrame(t)}:function(t){return setTimeout(t,16)},P=0,C=new(function(){function t(){var t=this;this.last=null,this.force=!1,this._logger=null,this._updating=[],this._reaping=new Set,this._atomMap=new WeakMap,this._scheduled=!1,this.__run=function(){t._scheduled&&(t._scheduled=!1,t._run())},this._start=0,this._pendCount=0}return t.prototype.getAtom=function(t,e,i,n,o){var s=void 0===i?t:u(i),r=this._atomMap.get(e);void 0===r&&(r=new Map,this._atomMap.set(e,r));var a=r.get(s);return void 0===a&&(a=new A(t,e,this,i,n,o),r.set(s,a)),a},t.prototype.destroyHost=function(t){var e=t.host,i=this._atomMap.get(e);void 0!==i&&(void 0!==e._destroyProp&&e._destroyProp(void 0===t.key?t.field:t.key,t.cached),i.delete(void 0===t.key?t.field:u(t.key)),0===i.size&&(void 0!==e._destroy&&e._destroy(),this._atomMap.delete(e)))},t.prototype.setLogger=function(t){this._logger=t},t.prototype.newValue=function(t,e,i){this._logger&&(i instanceof z?this._logger.pulling(t):i instanceof Error?this._logger.error(t,i):this._logger.newValue(t,e,i))},t.prototype.proposeToPull=function(t){this._updating.push(t),this._schedule()},t.prototype.proposeToReap=function(t){this._reaping.add(t),this._schedule()},t.prototype.unreap=function(t){this._reaping.delete(t)},t.prototype._schedule=function(){this._scheduled||(M(this.__run),this._scheduled=!0)},t.prototype._run=function(){this._schedule();var t=this._reaping,e=this._updating,i=this._start;do{for(var n=e.length,o=i;o<n;o++){this._start=o;var s=e[o];t.has(s)||s.destroyed()||s.actualize()}i=n}while(e.length>i);for(e.length=0,this._start=0;t.size>0;)t.forEach(a);this._scheduled=!1,this._pendCount=0},t.prototype.beginTransaction=function(){this._pendCount++},t.prototype.endTransaction=function(){1===this._pendCount?this._run():this._pendCount--},t}());b.Wait=z,b.key=p,b.detached=v,t.Atom=A,t.mem=b,t.memkey=p,t.detached=v,t.force=function(t,e,i){return{enumerable:i.enumerable,configurable:i.configurable,get:_}},t.action=function(){return 3===arguments.length?m(arguments[0],arguments[1],arguments[2],C):g(arguments[0],arguments[1],C)},t.defaultContext=C,Object.defineProperty(t,"__esModule",{value:!0})}); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(t.lom_atom={})}(this,function(t){"use strict";function e(t){return new Proxy(t,z)}function o(t,e){if(t===e)return t;if(t instanceof Array&&e instanceof Array&&t.length===e.length){for(var o=0;o<t.length;o++)if(t[o]!==e[o])return t;return e}return t}function n(t){t.check()}function i(t){t.obsolete()}function s(t){t.dislead(this)}function r(t){this.status===w&&t.actualize()}function a(t,e,o){o.delete(t),t.slaves||t.destroyed(!0)}function u(t){return"string"==typeof t||"number"==typeof t?t:t?"function"==typeof t?(t.__id=t.__id||++M,t.__id):"object"===S(t)?Object.keys(t).sort().map(function(e){return e+":"+JSON.stringify(t[e])}).join("."):JSON.stringify(t):0}function c(t,e,o,n,i){var s=e+"$";if(void 0===o.value)throw new TypeError(e+" is not an function (next?: V)");return t[s]=o.value,{enumerable:o.enumerable,configurable:o.configurable,value:function(t,e){return V.getAtom(s,this,void 0,n,i).value(t,e)}}}function h(t,e){return function(o){return void 0===o?t.call(this):(e.call(this,o),o)}}function l(t){return function(e){return void 0===e&&void 0!==t?t.call(this):e}}function f(t,e,o,n){var i=e+"$";if(void 0===t[i]){t[i]=void 0===o.get&&void 0===o.set?l(o.initializer):h(o.get,o.set);return{enumerable:o.enumerable,configurable:o.configurable,get:function(){return V.getAtom(i,this,void 0,n).get()},set:function(t){V.getAtom(i,this,void 0,n).set(t)}}}}function p(t,e,o,n){var i=o.value;if(void 0===i)throw new TypeError(e+" is not an function (rawKey: K, next?: V)");var s=e+"$";return t[s]=i,t[s+"?"]=function(t){return V.hasAtom(this,t)},{enumerable:o.enumerable,configurable:o.configurable,value:function(t,e,o){return V.getAtom(s,this,t,n).value(e,o)}}}function d(){if(3===arguments.length)return p(arguments[0],arguments[1],arguments[2]);var t=arguments[0];return function(e,o,n){return p(e,o,n,t)}}function _(){return V.force=!0,this}function y(t,e,o){return c(t,e,o,void 0,!0)}function v(t,e,o){function n(){var n=void 0;switch(o.beginTransaction(),arguments.length){case 0:n=t[e]();break;case 1:n=t[e](arguments[0]);break;case 2:n=t[e](arguments[0],arguments[1]);break;case 3:n=t[e](arguments[0],arguments[1],arguments[2]);break;case 4:n=t[e](arguments[0],arguments[1],arguments[2],arguments[3]);break;case 5:n=t[e](arguments[0],arguments[1],arguments[2],arguments[3],arguments[4]);break;default:n=t[e].apply(t,arguments)}return o.endTransaction(),n}return n.displayName=e,n}function g(t,e,o){function n(){var e=void 0;switch(o.beginTransaction(),arguments.length){case 0:e=t();break;case 1:e=t(arguments[0]);break;case 2:e=t(arguments[0],arguments[1]);break;case 3:e=t(arguments[0],arguments[1],arguments[2]);break;case 4:e=t(arguments[0],arguments[1],arguments[2],arguments[3]);break;case 5:e=t(arguments[0],arguments[1],arguments[2],arguments[3],arguments[4]);break;default:e=t.apply(null,arguments)}return o.endTransaction(),e}return n.displayName=e||t.displayName||t.name,n}function m(t,e,o,n){var i=e+"$";if(void 0===o.value)throw new TypeError(e+" is not an function (next?: V)");t[i]=o.value;var s=!1;return{enumerable:o.enumerable,configurable:o.configurable,get:function(){if(s)return this[i];s=!0;var t=v(this,i,n);return Object.defineProperty(this,e,{configurable:!0,get:function(){return t}}),s=!1,t}}}function b(){if(3===arguments.length)return void 0===arguments[2].value?f(arguments[0],arguments[1],arguments[2]):c(arguments[0],arguments[1],arguments[2]);var t=arguments[0];return function(e,o,n){return void 0===n.value?f(e,o,n,t):c(e,o,n,t)}}var k=1,w=2,x=Symbol("lom_atom_catched"),S="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},T=function(){function t(t,e){for(var o=0;o<e.length;o++){var n=e[o];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}return function(e,o,n){return o&&t(e.prototype,o),n&&t(e,n),e}}(),E=function(t,e){t.prototype=Object.create(e.prototype),t.prototype.constructor=t,t.__proto__=e},z={get:function(t){throw t.valueOf()},ownKeys:function(t){throw t.valueOf()}},A=function(t){function e(){var e,o=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"Wait...";return e=t.call(this,o)||this,e.__proto__=new.target.prototype,e[x]=!0,e}return E(e,t),e}(Error),N=function(){function t(t,e,n,i,s,r){this.status=k,this.cached=void 0,this._masters=null,this._slaves=null,this.field=t,this.key=i,this.host=e,this.isComponent=r||!1,this._normalize=s||o,this._context=n}return t.prototype.destroyed=function(t){return void 0===t?0===this.status:!!t&&(0!==this.status&&(this._masters&&(this._masters.forEach(s,this),this._masters=null),this._checkSlaves(),this._context.destroyHost(this),this.cached=void 0,this.status=0,this.key=void 0),!0)},t.prototype.get=function(t){t||this._context.force?(this._context.force=!1,this._pullPush(void 0,!0)):this.actualize();var e=this._context.last;if(e&&(!e.isComponent||!this.isComponent)){var o=this._slaves;o||(this._context.unreap(this),o=this._slaves=new Set),o.add(e),e.addMaster(this)}return this.cached},t.prototype.set=function(t,o){var n=this._normalize(t,this.cached);if(this.cached===n)return n;if(void 0===n)return this.cached;var s=this._context;if(!o&&!s.force||n instanceof Error){s.force=!1,this.status=4;var r=this.cached;this.cached=n instanceof Error?e(n):n,s.newValue(this,r,n),this._slaves&&this._slaves.forEach(i)}else this.obsolete(),this.actualize(n);return this.cached},t.prototype.actualize=function(t){4!==this.status&&(this.status===w&&(this._masters&&this._masters.forEach(r,this),this.status===w&&(this.status=4)),4!==this.status&&this._pullPush(t))},t.prototype._pullPush=function(t,o){this._masters&&this._masters.forEach(s,this);var n=void 0;this.status=3;var r=this._context,a=r.last;r.last=this;try{n=this._normalize(void 0===this.key?this.host[this.field](t,o,this.cached):this.host[this.field](this.key,t,o,this.cached),this.cached)}catch(t){void 0===t[x]&&(t[x]=!0,console.error(t.stack||t)),n=e(t)}if(r.last=a,this.status=4,void 0!==n&&this.cached!==n){var u=this.cached;this.cached=n,this._context.newValue(this,u,n,!0),this._slaves&&this._slaves.forEach(i)}},t.prototype.dislead=function(t){var e=this._slaves;e&&(1===e.size?(this._slaves=null,this._context.proposeToReap(this)):e.delete(t))},t.prototype._checkSlaves=function(){this._slaves?this._slaves.forEach(n):this._context.proposeToPull(this)},t.prototype.check=function(){4===this.status&&(this.status=w,this._checkSlaves())},t.prototype.obsolete=function(){this.status!==k&&(this.status=k,this._checkSlaves())},t.prototype.addMaster=function(t){this._masters||(this._masters=new Set),this._masters.add(t)},t.prototype.value=function(t,e){return void 0===t?this.get(e):this.set(t,e)},T(t,[{key:"displayName",get:function(){var t=this.host.constructor,e=this.key;return(this.host.displayName||(t?String(t.displayName||t.name):""))+"."+this.field+(e?"("+("function"==typeof e?e.displayName||e.name:String(e))+")":"")}}]),t}(),P="function"==typeof requestAnimationFrame?function(t){return requestAnimationFrame(t)}:function(t){return setTimeout(t,16)},M=0,C=function(){function t(){}return t.prototype.status=function(t,e){},t.prototype.error=function(t,e){},t.prototype.newValue=function(t,e,o,n){},t}(),O=function(t){function e(){return t.apply(this,arguments)||this}return E(e,t),e.prototype.status=function(t,e){console.log(t,e.displayName)},e.prototype.error=function(t,e){console.log("error",t.displayName,e)},e.prototype.newValue=function(t,e,o,n){console.log(n?"actualize":"cacheSet",t.displayName,"from",e,"to",o)},e}(C),V=new(function(){function t(){var t=this;this.last=null,this.force=!1,this._logger=null,this._updating=[],this._reaping=new Set,this._atomMap=new WeakMap,this._scheduled=!1,this.__run=function(){t._scheduled&&(t._scheduled=!1,t._run())},this._start=0,this._pendCount=0}return t.prototype.hasAtom=function(t,e){var o=this._atomMap.get(t);return void 0!==o&&o.has(u(e))},t.prototype.getAtom=function(t,e,o,n,i){var s=void 0===o?t:u(o),r=this._atomMap.get(e);void 0===r&&(r=new Map,this._atomMap.set(e,r));var a=r.get(s);return void 0===a&&(a=new N(t,e,this,o,n,i),r.set(s,a)),a},t.prototype.destroyHost=function(t){var e=t.host;this._logger&&this._logger.status("destroy",t);var o=this._atomMap.get(e);void 0!==o&&(void 0!==e._destroyProp&&e._destroyProp(void 0===t.key?t.field:t.key,t.cached),o.delete(void 0===t.key?t.field:u(t.key)),0===o.size&&(void 0!==e._destroy&&e._destroy(),this._atomMap.delete(e)))},t.prototype.setLogger=function(t){this._logger=t},t.prototype.newValue=function(t,e,o,n){this._logger&&(o instanceof A?this._logger.status("waiting",t):o instanceof Error?this._logger.error(t,o):this._logger.newValue(t,e,o,n))},t.prototype.proposeToPull=function(t){this._logger&&this._logger.status("proposeToPull",t),this._updating.push(t),this._schedule()},t.prototype.proposeToReap=function(t){this._logger&&this._logger.status("proposeToReap",t),this._reaping.add(t),this._schedule()},t.prototype.unreap=function(t){this._reaping.delete(t)},t.prototype._schedule=function(){this._scheduled||(P(this.__run),this._scheduled=!0)},t.prototype._run=function(){this._schedule();var t=this._reaping,e=this._updating,o=this._start;do{for(var n=e.length,i=o;i<n;i++){this._start=i;var s=e[i];t.has(s)||s.destroyed()||s.actualize()}o=n}while(e.length>o);for(e.length=0,this._start=0;t.size>0;)t.forEach(a);this._scheduled=!1,this._pendCount=0},t.prototype.beginTransaction=function(){this._pendCount++},t.prototype.endTransaction=function(){1===this._pendCount?this._run():this._pendCount--},t}());b.Wait=A,b.key=d,b.detached=y,t.Atom=N,t.mem=b,t.memkey=d,t.detached=y,t.force=function(t,e,o){return{enumerable:o.enumerable,configurable:o.configurable,get:_}},t.action=function(){return 3===arguments.length?m(arguments[0],arguments[1],arguments[2],V):g(arguments[0],arguments[1],V)},t.defaultContext=V,t.BaseLogger=C,t.ConsoleLogger=O,Object.defineProperty(t,"__esModule",{value:!0})}); | ||
//# sourceMappingURL=lom_atom.umd.js.map |
{ | ||
"name": "lom_atom", | ||
"version": "1.0.9", | ||
"version": "1.0.10", | ||
"description": "Observable state management", | ||
@@ -5,0 +5,0 @@ "publishConfig": { |
@@ -67,2 +67,15 @@ // @flow | ||
get displayName(): string { | ||
const hc = this.host.constructor | ||
const k = this.key | ||
return (this.host.displayName || (hc ? String(hc.displayName || hc.name) : '')) | ||
+ '.' | ||
+ this.field | ||
+ (k | ||
? ('(' + (typeof k === 'function' ? (k.displayName || k.name) : String(k)) + ')') | ||
: '' | ||
) | ||
} | ||
destroyed(isDestroyed?: boolean): boolean { | ||
@@ -75,3 +88,2 @@ if (isDestroyed === undefined) { | ||
if (this.status !== ATOM_STATUS_DESTROYED) { | ||
// console.log('destroy', this.field) | ||
if (this._masters) { | ||
@@ -82,6 +94,3 @@ this._masters.forEach(disleadThis, this) | ||
this._checkSlaves() | ||
const host = this.host | ||
if (host !== undefined) { | ||
this._context.destroyHost(this) | ||
} | ||
this._context.destroyHost(this) | ||
this.cached = undefined | ||
@@ -110,7 +119,5 @@ this.status = ATOM_STATUS_DESTROYED | ||
if (!slaves) { | ||
// console.log('unreap', this.field) | ||
this._context.unreap(this) | ||
slaves = this._slaves = new Set() | ||
} | ||
// console.log('add slave', slave.field, 'to master', this.field) | ||
slaves.add(slave) | ||
@@ -133,3 +140,2 @@ slave.addMaster(this) | ||
const context = this._context | ||
// console.log('set', this.field, 'value', normalized) | ||
if ((!force && !context.force) || normalized instanceof Error) { | ||
@@ -142,6 +148,7 @@ context.force = false | ||
: normalized | ||
context.newValue(this, oldValue, normalized) | ||
if (this._slaves) { | ||
this._slaves.forEach(obsoleteSlave) | ||
} | ||
context.newValue(this, oldValue, normalized) | ||
} else { | ||
@@ -208,6 +215,7 @@ this.obsolete() | ||
this.cached = newValue | ||
this._context.newValue(this, oldValue, newValue, true) | ||
if (this._slaves) { | ||
this._slaves.forEach(obsoleteSlave) | ||
} | ||
this._context.newValue(this, oldValue, newValue) | ||
} | ||
@@ -221,6 +229,4 @@ } | ||
this._slaves = null | ||
// console.log('reap (slaves === null)', this.field) | ||
this._context.proposeToReap(this) | ||
} else { | ||
// console.log('delete slave', slave.field, 'from', this.field) | ||
slaves.delete(slave) | ||
@@ -235,3 +241,2 @@ } | ||
} else { | ||
// console.log('pull', this.field) | ||
this._context.proposeToPull(this) | ||
@@ -238,0 +243,0 @@ } |
@@ -10,3 +10,4 @@ // @flow | ||
IContext, | ||
ILogger | ||
ILogger, | ||
ILoggerStatus | ||
} from './interfaces' | ||
@@ -48,2 +49,22 @@ import {AtomWait} from './utils' | ||
export class BaseLogger implements ILogger { | ||
status(status: ILoggerStatus, atom: IAtom<*>): void {} | ||
error<V>(atom: IAtom<V>, err: Error): void {} | ||
newValue<V>(atom: IAtom<V>, from?: V | Error, to: V, isActualize?: boolean): void {} | ||
} | ||
export class ConsoleLogger extends BaseLogger implements ILogger { | ||
status(status: ILoggerStatus, atom: IAtom<*>): void { | ||
console.log(status, atom.displayName) | ||
} | ||
error<V>(atom: IAtom<V>, err: Error): void { | ||
console.log('error', atom.displayName, err) | ||
} | ||
newValue<V>(atom: IAtom<V>, from?: V | Error, to: V, isActualize?: boolean): void { | ||
console.log(isActualize ? 'actualize' : 'cacheSet', atom.displayName, 'from', from, 'to', to) | ||
} | ||
} | ||
export default class Context implements IContext { | ||
@@ -59,2 +80,8 @@ last: ?IAtomInt = null | ||
hasAtom(host: IAtomHost, key: mixed): boolean { | ||
// return host[getKey(key) + '@'] !== undefined | ||
const map = this._atomMap.get(host) | ||
return map !== undefined && map.has(getKey(key)) | ||
} | ||
getAtom<V>( | ||
@@ -68,2 +95,3 @@ field: string, | ||
const k = key === undefined ? field : getKey(key) | ||
let map = this._atomMap.get(host) | ||
@@ -77,6 +105,7 @@ if (map === undefined) { | ||
// let atom: IAtom<V> | void = host[k + '@'] | ||
if (atom === undefined) { | ||
atom = new Atom(field, host, this, key, normalize, isComponent) | ||
map.set(k, atom) | ||
// host[k + '@'] = atom | ||
// host[k + '@'] = (atom: any) | ||
} | ||
@@ -90,8 +119,15 @@ | ||
// host[(atom.key === undefined ? atom.field : getKey(atom.key)) + '@'] = undefined | ||
// | ||
// if (host._destroy !== undefined) { | ||
// const k = atom.key === undefined ? atom.field : getKey(atom.key) | ||
// host[k + '@'] = (undefined: any) | ||
// if (host._destroyProp !== undefined) { | ||
// host._destroyProp(atom.key === undefined ? atom.field : atom.key, atom.cached) | ||
// } | ||
// if (host._destroy !== undefined && atom.key === undefined) { | ||
// host._destroy() | ||
// } | ||
if (this._logger) { | ||
this._logger.status('destroy', atom) | ||
} | ||
const map = this._atomMap.get(host) | ||
@@ -117,16 +153,19 @@ if (map !== undefined) { | ||
newValue<V>(atom: IAtom<V>, from?: V | Error, to: V | Error) { | ||
if (this._logger) { | ||
if (to instanceof AtomWait) { | ||
this._logger.pulling(atom) | ||
} else if (to instanceof Error) { | ||
this._logger.error(atom, to) | ||
} else { | ||
this._logger.newValue(atom, from, to) | ||
} | ||
newValue<V>(atom: IAtom<V>, from?: V | Error, to: V | Error, isActualize?: boolean) { | ||
if (!this._logger) { | ||
return | ||
} | ||
if (to instanceof AtomWait) { | ||
this._logger.status('waiting', atom) | ||
} else if (to instanceof Error) { | ||
this._logger.error(atom, to) | ||
} else { | ||
this._logger.newValue(atom, from, to, isActualize) | ||
} | ||
} | ||
proposeToPull(atom: IAtomInt) { | ||
// this.logger.pull(atom) | ||
if (this._logger) { | ||
this._logger.status('proposeToPull', atom) | ||
} | ||
this._updating.push(atom) | ||
@@ -137,3 +176,5 @@ this._schedule() | ||
proposeToReap(atom: IAtomInt) { | ||
// this.logger.reap(atom) | ||
if (this._logger) { | ||
this._logger.status('proposeToReap', atom) | ||
} | ||
this._reaping.add(atom) | ||
@@ -144,3 +185,2 @@ this._schedule() | ||
unreap(atom: IAtomInt) { | ||
// this.logger.unreap(atom) | ||
this._reaping.delete(atom) | ||
@@ -147,0 +187,0 @@ } |
@@ -5,3 +5,3 @@ // @flow | ||
export {default as mem, memkey, detached, force, action} from './mem' | ||
export {defaultContext} from './Context' | ||
export {defaultContext, BaseLogger, ConsoleLogger} from './Context' | ||
@@ -14,3 +14,5 @@ export type { | ||
IAtomHandler, | ||
NamesOf | ||
NamesOf, | ||
ILoggerStatus, | ||
ILogger | ||
} from './interfaces' |
@@ -7,6 +7,8 @@ // @flow | ||
export type ILoggerStatus = 'destroy' | 'waiting' | 'proposeToReap' | 'proposeToPull' | ||
export interface ILogger { | ||
pulling(atom: IAtom<*>): void; | ||
status(status: ILoggerStatus, atom: IAtom<*>): void; | ||
error<V>(atom: IAtom<V>, err: Error): void; | ||
newValue<V>(atom: IAtom<V>, from?: V | Error, to: V): void; | ||
newValue<V>(atom: IAtom<V>, from?: V | Error, to: V, isActualize?: boolean): void; | ||
} | ||
@@ -25,6 +27,6 @@ | ||
): IAtom<V>; | ||
hasAtom(host: IAtomHost, key: mixed): boolean; | ||
destroyHost(atom: IAtomInt): void; | ||
newValue<V>(t: IAtom<V>, from?: V | Error, to: V | Error): void; | ||
newValue<V>(t: IAtom<V>, from?: V | Error, to: V | Error, isActualize?: boolean): void; | ||
setLogger(logger: ILogger): void; | ||
@@ -51,3 +53,4 @@ proposeToPull(atom: IAtomInt): void; | ||
status: IAtomStatus; | ||
field: string; | ||
+field: string; | ||
+displayName: string; | ||
get(force?: boolean): V; | ||
@@ -78,2 +81,3 @@ set(v: V | Error, force?: boolean): V; | ||
export interface IAtomHost { | ||
displayName?: string; | ||
[key: string]: IAtomHandler<*, *>; | ||
@@ -80,0 +84,0 @@ _destroyProp?: (key: mixed, value: mixed | void) => void; |
@@ -102,2 +102,5 @@ // @flow | ||
proto[handlerKey] = handler | ||
proto[handlerKey + '?'] = function(rawKey: K) { | ||
return defaultContext.hasAtom(this, rawKey) | ||
} | ||
@@ -104,0 +107,0 @@ return { |
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
201638
1761