Comparing version 1.0.2 to 1.0.3
@@ -5,2 +5,7 @@ # Change Log | ||
<a name="1.0.3"></a> | ||
## [1.0.3](https://github.com/zerkalica/lom_atom/compare/v1.0.2...v1.0.3) (2017-07-31) | ||
<a name="1.0.2"></a> | ||
@@ -7,0 +12,0 @@ ## [1.0.2](https://github.com/zerkalica/lom_atom/compare/v1.0.1...v1.0.2) (2017-07-31) |
// eslint-disable-line | ||
var ATOM_STATUS = { | ||
DESTROYED: 0, | ||
OBSOLETE: 1, | ||
CHECKING: 2, | ||
PULLING: 3, | ||
ACTUAL: 4 | ||
}; | ||
var ATOM_STATUS_DESTROYED = 0; | ||
var ATOM_STATUS_OBSOLETE = 1; | ||
var ATOM_STATUS_CHECKING = 2; | ||
var ATOM_STATUS_PULLING = 3; | ||
var ATOM_STATUS_ACTUAL = 4; | ||
var catchedId = Symbol('lom_atom_catched'); | ||
@@ -103,3 +101,3 @@ | ||
function actualizeMaster(master) { | ||
if (this.status === ATOM_STATUS.CHECKING) { | ||
if (this.status === ATOM_STATUS_CHECKING) { | ||
master.actualize(); | ||
@@ -111,2 +109,6 @@ } | ||
function Atom(field, handler, host, isComponent, context, normalize) { | ||
this.status = ATOM_STATUS_OBSOLETE; | ||
this._masters = null; | ||
this._slaves = null; | ||
this._cached = undefined; | ||
this.field = field; | ||
@@ -118,6 +120,2 @@ this._handler = handler; | ||
this._context = context; | ||
this.status = ATOM_STATUS.OBSOLETE; | ||
this._masters = null; | ||
this._slaves = null; | ||
this._cached = undefined; | ||
} | ||
@@ -127,7 +125,7 @@ | ||
if (isDestroyed === undefined) { | ||
return this.status === ATOM_STATUS.DESTROYED; | ||
return this.status === ATOM_STATUS_DESTROYED; | ||
} | ||
if (isDestroyed) { | ||
if (this.status !== ATOM_STATUS.DESTROYED) { | ||
if (this.status !== ATOM_STATUS_DESTROYED) { | ||
// console.log('destroy', this.field) | ||
@@ -143,3 +141,3 @@ if (this._masters) { | ||
this._cached = undefined; | ||
this.status = ATOM_STATUS.DESTROYED; | ||
this.status = ATOM_STATUS_DESTROYED; | ||
var host = this._host; | ||
@@ -201,3 +199,3 @@ | ||
this._context.force = false; | ||
this.status = ATOM_STATUS.ACTUAL; | ||
this.status = ATOM_STATUS_ACTUAL; | ||
@@ -220,7 +218,7 @@ this._context.newValue(this, this._cached, normalized); | ||
Atom.prototype.actualize = function actualize(proposedValue) { | ||
if (this.status === ATOM_STATUS.ACTUAL) { | ||
if (this.status === ATOM_STATUS_ACTUAL) { | ||
return; | ||
} | ||
if (this.status === ATOM_STATUS.CHECKING) { | ||
if (this.status === ATOM_STATUS_CHECKING) { | ||
if (this._masters) { | ||
@@ -230,8 +228,8 @@ this._masters.forEach(actualizeMaster, this); | ||
if (this.status === ATOM_STATUS.CHECKING) { | ||
this.status = ATOM_STATUS.ACTUAL; | ||
if (this.status === ATOM_STATUS_CHECKING) { | ||
this.status = ATOM_STATUS_ACTUAL; | ||
} | ||
} | ||
if (this.status !== ATOM_STATUS.ACTUAL) { | ||
if (this.status !== ATOM_STATUS_ACTUAL) { | ||
this._pullPush(proposedValue); | ||
@@ -247,3 +245,3 @@ } | ||
var newValue = void 0; | ||
this.status = ATOM_STATUS.PULLING; | ||
this.status = ATOM_STATUS_PULLING; | ||
var context = this._context; | ||
@@ -265,3 +263,3 @@ var slave = context.last; | ||
context.last = slave; | ||
this.status = ATOM_STATUS.ACTUAL; | ||
this.status = ATOM_STATUS_ACTUAL; | ||
@@ -304,4 +302,4 @@ if (newValue !== undefined && this._cached !== newValue) { | ||
Atom.prototype.check = function check() { | ||
if (this.status === ATOM_STATUS.ACTUAL) { | ||
this.status = ATOM_STATUS.CHECKING; | ||
if (this.status === ATOM_STATUS_ACTUAL) { | ||
this.status = ATOM_STATUS_CHECKING; | ||
@@ -313,4 +311,4 @@ this._checkSlaves(); | ||
Atom.prototype.obsolete = function obsolete() { | ||
if (this.status !== ATOM_STATUS.OBSOLETE) { | ||
this.status = ATOM_STATUS.OBSOLETE; | ||
if (this.status !== ATOM_STATUS_OBSOLETE) { | ||
this.status = ATOM_STATUS_OBSOLETE; | ||
@@ -491,5 +489,4 @@ this._checkSlaves(); | ||
reaping.forEach(reap); | ||
} // console.log('---------------------------- state changed\n') | ||
} | ||
this._scheduled = false; | ||
@@ -596,10 +593,13 @@ }; | ||
} | ||
function detached(proto, name, descr) { | ||
return memMethod(proto, name, descr, true); | ||
} | ||
function mem(proto, name, descr) { | ||
return descr.value === undefined ? memProp(proto, name, descr) : memMethod(proto, name, descr); | ||
} | ||
function detached(proto, name, descr) { | ||
return memMethod(proto, name, descr, true); | ||
} | ||
mem.Wait = AtomWait; | ||
mem.key = memkey; | ||
mem.detached = detached; | ||
export { Atom, mem, memkey, detached, force, defaultContext, animationFrame, AtomWait, ATOM_STATUS, catchedId }; | ||
export { Atom, mem, memkey, detached, force, defaultContext, animationFrame }; | ||
//# 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.destroyed(!0)}function createKeyedHandler(t,e,o){return function(s,i){return e.call(t,o,s,i)}}function memMethod(t,e,o,s){t[e+"$"]=o.value;var i=o.value;if(void 0===i)throw new TypeError(e+" is not an function (next?: V)");return{enumerable:o.enumerable,configurable:o.configurable,value:function(t,o){return defaultContext.getAtom(this,i,e,s).value(t,o)}}}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){var s=e+"$";if(void 0===t[s]){var i=t[s]=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(this,i,e).get()},set:function(t){defaultContext.getAtom(this,i,e).set(t)}}}}function getKey(t){return t?"object"===_typeof(t)?Object.keys(t).sort().map(function(e){return e+":"+JSON.stringify(t[e])}).join("."):JSON.stringify(t):""}function memkey(t,e,o){var s=o.value;if(void 0===s)throw new TypeError(e+" is not an function (rawKey: K, next?: V)");return t[e+"$"]=s,{enumerable:o.enumerable,configurable:o.configurable,value:function(t,o,i){return defaultContext.getKeyAtom(this,s,"function"==typeof t?t:e+"("+getKey(t)+")").value(o,i)}}}function forceGet(){return defaultContext.force=!0,this}function force(t,e,o){return{enumerable:o.enumerable,configurable:o.configurable,get:forceGet}}function mem(t,e,o){return void 0===o.value?memProp(t,e,o):memMethod(t,e,o)}function detached(t,e,o){return memMethod(t,e,o,!0)}Object.defineProperty(exports,"__esModule",{value:!0});var ATOM_STATUS={DESTROYED:0,OBSOLETE:1,CHECKING:2,PULLING:3,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,s,i,n){this.field=t,this._handler=e,this._host=o,this.isComponent=s||!1,this._normalize=n||defaultNormalize,this._context=i,this.status=ATOM_STATUS.OBSOLETE,this._masters=null,this._slaves=null,this._cached=void 0}return t.prototype.destroyed=function(t){if(void 0===t)return this.status===ATOM_STATUS.DESTROYED;if(t){if(this.status!==ATOM_STATUS.DESTROYED){this._masters&&(this._masters.forEach(disleadThis,this),this._masters=null),this._checkSlaves(),this._cached=void 0,this.status=ATOM_STATUS.DESTROYED;var e=this._host;void 0!==e&&this._context.destroyHost(e,this.field)}return!0}return!1},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);return this._cached===o?o:void 0===o?this._cached:(e||this._context.force||o instanceof Error?(this._context.force=!1,this.status=ATOM_STATUS.ACTUAL,this._context.newValue(this,this._cached,o),this._cached=o instanceof Error?createMock(o):o,this._slaves&&this._slaves.forEach(obsoleteSlave)):(this.obsolete(),this.actualize(o)),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 s=this._context,i=s.last;s.last=this;try{o=this._normalize(void 0===this._host?this._handler(t,e):this._handler.call(this._host,t,e),this._cached)}catch(t){void 0===t[catchedId]&&(t[catchedId]=!0,console.error(t.stack||t)),o=createMock(t)}s.last=i,this.status=ATOM_STATUS.ACTUAL,void 0!==o&&this._cached!==o&&(this._context.newValue(this,this._cached,o),this._cached=o,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)},t}(),animationFrame="function"==typeof requestAnimationFrame?requestAnimationFrame:function(t){return setTimeout(t,0)},Context=function(){function t(){var t=this;this.last=null,this.force=!1,this._logger=null,this._updating=[],this._reaping=new Set,this._scheduled=!1,this._atomMap=new WeakMap,this._run=function(){t._scheduled&&t.run()}}return t.prototype.getKeyAtom=function(t,e,o){var s=this._atomMap.get(t);void 0===s&&(s=new Map,this._atomMap.set(t,s));var i=s.get(o);return void 0===i&&(i=new Atom(o,createKeyedHandler(t,e,o),t,void 0,this),s.set(o,i)),i},t.prototype.getAtom=function(t,e,o,s){var i=this._atomMap.get(t);void 0===i&&(i=new Map,this._atomMap.set(t,i));var n=i.get(o);return void 0===n&&(n=new Atom(o,e,t,s,this),i.set(o,n)),n},t.prototype.destroyHost=function(t,e){var o=this._atomMap.get(t);void 0!==o&&(o.delete(e),0===o.size&&(void 0!==t._destroy&&t._destroy(),this._atomMap.delete(t)))},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||(this._scheduled=!0,animationFrame(this._run))},t.prototype.run=function(){var t=this._reaping,e=this._updating,o=0;do{for(var s=e.length,i=o;i<s;i++){var n=e[i];t.has(n)||n.destroyed()||n.actualize()}o=s}while(e.length>o);for(e.length=0;t.size>0;)t.forEach(reap);this._scheduled=!1},t}(),defaultContext=new Context;exports.Atom=Atom,exports.mem=mem,exports.memkey=memkey,exports.detached=detached,exports.force=force,exports.defaultContext=defaultContext,exports.animationFrame=animationFrame,exports.AtomWait=AtomWait,exports.ATOM_STATUS=ATOM_STATUS,exports.catchedId=catchedId; | ||
"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.destroyed(!0)}function createKeyedHandler(t,e,o){return function(s,i){return e.call(t,o,s,i)}}function memMethod(t,e,o,s){t[e+"$"]=o.value;var i=o.value;if(void 0===i)throw new TypeError(e+" is not an function (next?: V)");return{enumerable:o.enumerable,configurable:o.configurable,value:function(t,o){return defaultContext.getAtom(this,i,e,s).value(t,o)}}}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){var s=e+"$";if(void 0===t[s]){var i=t[s]=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(this,i,e).get()},set:function(t){defaultContext.getAtom(this,i,e).set(t)}}}}function getKey(t){return t?"object"===_typeof(t)?Object.keys(t).sort().map(function(e){return e+":"+JSON.stringify(t[e])}).join("."):JSON.stringify(t):""}function memkey(t,e,o){var s=o.value;if(void 0===s)throw new TypeError(e+" is not an function (rawKey: K, next?: V)");return t[e+"$"]=s,{enumerable:o.enumerable,configurable:o.configurable,value:function(t,o,i){return defaultContext.getKeyAtom(this,s,"function"==typeof t?t:e+"("+getKey(t)+")").value(o,i)}}}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,!0)}function mem(t,e,o){return void 0===o.value?memProp(t,e,o):memMethod(t,e,o)}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,s,i,n){this.status=ATOM_STATUS_OBSOLETE,this._masters=null,this._slaves=null,this._cached=void 0,this.field=t,this._handler=e,this._host=o,this.isComponent=s||!1,this._normalize=n||defaultNormalize,this._context=i}return t.prototype.destroyed=function(t){if(void 0===t)return this.status===ATOM_STATUS_DESTROYED;if(t){if(this.status!==ATOM_STATUS_DESTROYED){this._masters&&(this._masters.forEach(disleadThis,this),this._masters=null),this._checkSlaves(),this._cached=void 0,this.status=ATOM_STATUS_DESTROYED;var e=this._host;void 0!==e&&this._context.destroyHost(e,this.field)}return!0}return!1},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);return this._cached===o?o:void 0===o?this._cached:(e||this._context.force||o instanceof Error?(this._context.force=!1,this.status=ATOM_STATUS_ACTUAL,this._context.newValue(this,this._cached,o),this._cached=o instanceof Error?createMock(o):o,this._slaves&&this._slaves.forEach(obsoleteSlave)):(this.obsolete(),this.actualize(o)),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 s=this._context,i=s.last;s.last=this;try{o=this._normalize(void 0===this._host?this._handler(t,e):this._handler.call(this._host,t,e),this._cached)}catch(t){void 0===t[catchedId]&&(t[catchedId]=!0,console.error(t.stack||t)),o=createMock(t)}s.last=i,this.status=ATOM_STATUS_ACTUAL,void 0!==o&&this._cached!==o&&(this._context.newValue(this,this._cached,o),this._cached=o,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)},t}(),animationFrame="function"==typeof requestAnimationFrame?requestAnimationFrame:function(t){return setTimeout(t,0)},Context=function(){function t(){var t=this;this.last=null,this.force=!1,this._logger=null,this._updating=[],this._reaping=new Set,this._scheduled=!1,this._atomMap=new WeakMap,this._run=function(){t._scheduled&&t.run()}}return t.prototype.getKeyAtom=function(t,e,o){var s=this._atomMap.get(t);void 0===s&&(s=new Map,this._atomMap.set(t,s));var i=s.get(o);return void 0===i&&(i=new Atom(o,createKeyedHandler(t,e,o),t,void 0,this),s.set(o,i)),i},t.prototype.getAtom=function(t,e,o,s){var i=this._atomMap.get(t);void 0===i&&(i=new Map,this._atomMap.set(t,i));var n=i.get(o);return void 0===n&&(n=new Atom(o,e,t,s,this),i.set(o,n)),n},t.prototype.destroyHost=function(t,e){var o=this._atomMap.get(t);void 0!==o&&(o.delete(e),0===o.size&&(void 0!==t._destroy&&t._destroy(),this._atomMap.delete(t)))},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||(this._scheduled=!0,animationFrame(this._run))},t.prototype.run=function(){var t=this._reaping,e=this._updating,o=0;do{for(var s=e.length,i=o;i<s;i++){var n=e[i];t.has(n)||n.destroyed()||n.actualize()}o=s}while(e.length>o);for(e.length=0;t.size>0;)t.forEach(reap);this._scheduled=!1},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.defaultContext=defaultContext,exports.animationFrame=animationFrame; | ||
//# 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,g)}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 i(t){t.check()}function s(t){t.obsolete()}function n(t){t.dislead(this)}function r(t){this.status===d.CHECKING&&t.actualize()}function a(t,e,o){o.delete(t),t.destroyed(!0)}function h(t,e,o){return function(i,s){return e.call(t,o,i,s)}}function u(t,e,o,i){t[e+"$"]=o.value;var s=o.value;if(void 0===s)throw new TypeError(e+" is not an function (next?: V)");return{enumerable:o.enumerable,configurable:o.configurable,value:function(t,o){return S.getAtom(this,s,e,i).value(t,o)}}}function c(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){var i=e+"$";if(void 0===t[i]){var s=t[i]=void 0===o.get&&void 0===o.set?l(o.initializer):c(o.get,o.set);return{enumerable:o.enumerable,configurable:o.configurable,get:function(){return S.getAtom(this,s,e).get()},set:function(t){S.getAtom(this,s,e).set(t)}}}}function _(t){return t?"object"===m(t)?Object.keys(t).sort().map(function(e){return e+":"+JSON.stringify(t[e])}).join("."):JSON.stringify(t):""}function p(){return S.force=!0,this}var d={DESTROYED:0,OBSOLETE:1,CHECKING:2,PULLING:3,ACTUAL:4},v=Symbol("lom_atom_catched"),m="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},y=function(t,e){t.prototype=Object.create(e.prototype),t.prototype.constructor=t,t.__proto__=e},g={get:function(t){throw t.valueOf()},ownKeys:function(t){throw t.valueOf()}},E=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[v]=!0,e}return y(e,t),e}(Error),b=function(){function t(t,e,i,s,n,r){this.field=t,this._handler=e,this._host=i,this.isComponent=s||!1,this._normalize=r||o,this._context=n,this.status=d.OBSOLETE,this._masters=null,this._slaves=null,this._cached=void 0}return t.prototype.destroyed=function(t){if(void 0===t)return this.status===d.DESTROYED;if(t){if(this.status!==d.DESTROYED){this._masters&&(this._masters.forEach(n,this),this._masters=null),this._checkSlaves(),this._cached=void 0,this.status=d.DESTROYED;var e=this._host;void 0!==e&&this._context.destroyHost(e,this.field)}return!0}return!1},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 i=this._normalize(t,this._cached);return this._cached===i?i:void 0===i?this._cached:(o||this._context.force||i instanceof Error?(this._context.force=!1,this.status=d.ACTUAL,this._context.newValue(this,this._cached,i),this._cached=i instanceof Error?e(i):i,this._slaves&&this._slaves.forEach(s)):(this.obsolete(),this.actualize(i)),this._cached)},t.prototype.actualize=function(t){this.status!==d.ACTUAL&&(this.status===d.CHECKING&&(this._masters&&this._masters.forEach(r,this),this.status===d.CHECKING&&(this.status=d.ACTUAL)),this.status!==d.ACTUAL&&this._pullPush(t))},t.prototype._pullPush=function(t,o){this._masters&&this._masters.forEach(n,this);var i=void 0;this.status=d.PULLING;var r=this._context,a=r.last;r.last=this;try{i=this._normalize(void 0===this._host?this._handler(t,o):this._handler.call(this._host,t,o),this._cached)}catch(t){void 0===t[v]&&(t[v]=!0,console.error(t.stack||t)),i=e(t)}r.last=a,this.status=d.ACTUAL,void 0!==i&&this._cached!==i&&(this._context.newValue(this,this._cached,i),this._cached=i,this._slaves&&this._slaves.forEach(s))},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(i):this._context.proposeToPull(this)},t.prototype.check=function(){this.status===d.ACTUAL&&(this.status=d.CHECKING,this._checkSlaves())},t.prototype.obsolete=function(){this.status!==d.OBSOLETE&&(this.status=d.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}(),A="function"==typeof requestAnimationFrame?requestAnimationFrame:function(t){return setTimeout(t,0)},S=new(function(){function t(){var t=this;this.last=null,this.force=!1,this._logger=null,this._updating=[],this._reaping=new Set,this._scheduled=!1,this._atomMap=new WeakMap,this._run=function(){t._scheduled&&t.run()}}return t.prototype.getKeyAtom=function(t,e,o){var i=this._atomMap.get(t);void 0===i&&(i=new Map,this._atomMap.set(t,i));var s=i.get(o);return void 0===s&&(s=new b(o,h(t,e,o),t,void 0,this),i.set(o,s)),s},t.prototype.getAtom=function(t,e,o,i){var s=this._atomMap.get(t);void 0===s&&(s=new Map,this._atomMap.set(t,s));var n=s.get(o);return void 0===n&&(n=new b(o,e,t,i,this),s.set(o,n)),n},t.prototype.destroyHost=function(t,e){var o=this._atomMap.get(t);void 0!==o&&(o.delete(e),0===o.size&&(void 0!==t._destroy&&t._destroy(),this._atomMap.delete(t)))},t.prototype.setLogger=function(t){this._logger=t},t.prototype.newValue=function(t,e,o){this._logger&&(o instanceof E?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||(this._scheduled=!0,A(this._run))},t.prototype.run=function(){var t=this._reaping,e=this._updating,o=0;do{for(var i=e.length,s=o;s<i;s++){var n=e[s];t.has(n)||n.destroyed()||n.actualize()}o=i}while(e.length>o);for(e.length=0;t.size>0;)t.forEach(a);this._scheduled=!1},t}());t.Atom=b,t.mem=function(t,e,o){return void 0===o.value?f(t,e,o):u(t,e,o)},t.memkey=function(t,e,o){var i=o.value;if(void 0===i)throw new TypeError(e+" is not an function (rawKey: K, next?: V)");return t[e+"$"]=i,{enumerable:o.enumerable,configurable:o.configurable,value:function(t,o,s){return S.getKeyAtom(this,i,"function"==typeof t?t:e+"("+_(t)+")").value(o,s)}}},t.detached=function(t,e,o){return u(t,e,o,!0)},t.force=function(t,e,o){return{enumerable:o.enumerable,configurable:o.configurable,get:p}},t.defaultContext=S,t.animationFrame=A,t.AtomWait=E,t.ATOM_STATUS=d,t.catchedId=v,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,S)}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 o(t){t.check()}function s(t){t.obsolete()}function n(t){t.dislead(this)}function r(t){this.status===g&&t.actualize()}function a(t,e,i){i.delete(t),t.destroyed(!0)}function h(t,e,i){return function(o,s){return e.call(t,i,o,s)}}function u(t,e,i,o){t[e+"$"]=i.value;var s=i.value;if(void 0===s)throw new TypeError(e+" is not an function (next?: V)");return{enumerable:i.enumerable,configurable:i.configurable,value:function(t,i){return k.getAtom(this,s,e,o).value(t,i)}}}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){var o=e+"$";if(void 0===t[o]){var s=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 k.getAtom(this,s,e).get()},set:function(t){k.getAtom(this,s,e).set(t)}}}}function _(t){return t?"object"===w(t)?Object.keys(t).sort().map(function(e){return e+":"+JSON.stringify(t[e])}).join("."):JSON.stringify(t):""}function p(t,e,i){var o=i.value;if(void 0===o)throw new TypeError(e+" is not an function (rawKey: K, next?: V)");return t[e+"$"]=o,{enumerable:i.enumerable,configurable:i.configurable,value:function(t,i,s){return k.getKeyAtom(this,o,"function"==typeof t?t:e+"("+_(t)+")").value(i,s)}}}function d(){return k.force=!0,this}function v(t,e,i){return u(t,e,i,!0)}function y(t,e,i){return void 0===i.value?f(t,e,i):u(t,e,i)}var m=1,g=2,b=Symbol("lom_atom_catched"),w="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},x=function(t,e){t.prototype=Object.create(e.prototype),t.prototype.constructor=t,t.__proto__=e},S={get:function(t){throw t.valueOf()},ownKeys:function(t){throw t.valueOf()}},E=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[b]=!0,e}return x(e,t),e}(Error),M=function(){function t(t,e,o,s,n,r){this.status=m,this._masters=null,this._slaves=null,this._cached=void 0,this.field=t,this._handler=e,this._host=o,this.isComponent=s||!1,this._normalize=r||i,this._context=n}return t.prototype.destroyed=function(t){if(void 0===t)return 0===this.status;if(t){if(0!==this.status){this._masters&&(this._masters.forEach(n,this),this._masters=null),this._checkSlaves(),this._cached=void 0,this.status=0;var e=this._host;void 0!==e&&this._context.destroyHost(e,this.field)}return!0}return!1},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 o=this._normalize(t,this._cached);return this._cached===o?o:void 0===o?this._cached:(i||this._context.force||o instanceof Error?(this._context.force=!1,this.status=4,this._context.newValue(this,this._cached,o),this._cached=o instanceof Error?e(o):o,this._slaves&&this._slaves.forEach(s)):(this.obsolete(),this.actualize(o)),this._cached)},t.prototype.actualize=function(t){4!==this.status&&(this.status===g&&(this._masters&&this._masters.forEach(r,this),this.status===g&&(this.status=4)),4!==this.status&&this._pullPush(t))},t.prototype._pullPush=function(t,i){this._masters&&this._masters.forEach(n,this);var o=void 0;this.status=3;var r=this._context,a=r.last;r.last=this;try{o=this._normalize(void 0===this._host?this._handler(t,i):this._handler.call(this._host,t,i),this._cached)}catch(t){void 0===t[b]&&(t[b]=!0,console.error(t.stack||t)),o=e(t)}r.last=a,this.status=4,void 0!==o&&this._cached!==o&&(this._context.newValue(this,this._cached,o),this._cached=o,this._slaves&&this._slaves.forEach(s))},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(o):this._context.proposeToPull(this)},t.prototype.check=function(){4===this.status&&(this.status=g,this._checkSlaves())},t.prototype.obsolete=function(){this.status!==m&&(this.status=m,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}(),z="function"==typeof requestAnimationFrame?requestAnimationFrame:function(t){return setTimeout(t,0)},k=new(function(){function t(){var t=this;this.last=null,this.force=!1,this._logger=null,this._updating=[],this._reaping=new Set,this._scheduled=!1,this._atomMap=new WeakMap,this._run=function(){t._scheduled&&t.run()}}return t.prototype.getKeyAtom=function(t,e,i){var o=this._atomMap.get(t);void 0===o&&(o=new Map,this._atomMap.set(t,o));var s=o.get(i);return void 0===s&&(s=new M(i,h(t,e,i),t,void 0,this),o.set(i,s)),s},t.prototype.getAtom=function(t,e,i,o){var s=this._atomMap.get(t);void 0===s&&(s=new Map,this._atomMap.set(t,s));var n=s.get(i);return void 0===n&&(n=new M(i,e,t,o,this),s.set(i,n)),n},t.prototype.destroyHost=function(t,e){var i=this._atomMap.get(t);void 0!==i&&(i.delete(e),0===i.size&&(void 0!==t._destroy&&t._destroy(),this._atomMap.delete(t)))},t.prototype.setLogger=function(t){this._logger=t},t.prototype.newValue=function(t,e,i){this._logger&&(i instanceof E?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||(this._scheduled=!0,z(this._run))},t.prototype.run=function(){var t=this._reaping,e=this._updating,i=0;do{for(var o=e.length,s=i;s<o;s++){var n=e[s];t.has(n)||n.destroyed()||n.actualize()}i=o}while(e.length>i);for(e.length=0;t.size>0;)t.forEach(a);this._scheduled=!1},t}());y.Wait=E,y.key=p,y.detached=v,t.Atom=M,t.mem=y,t.memkey=p,t.detached=v,t.force=function(t,e,i){return{enumerable:i.enumerable,configurable:i.configurable,get:d}},t.defaultContext=k,t.animationFrame=z,Object.defineProperty(t,"__esModule",{value:!0})}); | ||
//# sourceMappingURL=lom_atom.umd.js.map |
{ | ||
"name": "lom_atom", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Observable state management", | ||
@@ -30,9 +30,11 @@ "publishConfig": { | ||
"state", | ||
"functional", | ||
"immutable", | ||
"hot", | ||
"live", | ||
"replay", | ||
"flux", | ||
"elm" | ||
"mobx", | ||
"observable", | ||
"reactive", | ||
"model", | ||
"frp", | ||
"functional-reactive-programming", | ||
"state management", | ||
"data flow", | ||
"react" | ||
], | ||
@@ -39,0 +41,0 @@ "files": [ |
133
README.md
# lom_atom | ||
Alternative implementation of eigenmethod [mol_atom](https://github.com/eigenmethod/mol) | ||
State management with error handling and reactive cache done right. | ||
Alternative standalone implementation of eigenmethod [mol_atom](https://github.com/eigenmethod/mol/tree/master/atom). | ||
* Tiny size (about 7kb minified) | ||
* Memory-efficient | ||
* Simpler, less core concept than mobx | ||
* Loading status / error handling features | ||
Install ``` npm install --save lom_atom ``` | ||
## Observable state | ||
```js | ||
import {mem} from 'lom_atom' | ||
class Todo { | ||
id = Math.random() | ||
@mem title = '' | ||
@mem finished = false | ||
} | ||
``` | ||
## Computed values | ||
One decorator for all cases. | ||
```js | ||
class TodoList { | ||
@mem todos = [] | ||
@mem get unfinishedTodos() { | ||
return this.todos.filter((todo) => !todo.finished) | ||
} | ||
} | ||
``` | ||
Like mobx, unfinishedTodos is updated automatically when a todo changed. | ||
## Side effects | ||
Like [mobx reaction](https://mobx.js.org/refguide/reaction.html) produces a side effect for making network requests, etc. But side effects in lom are simpler. | ||
```js | ||
class TodoList { | ||
@mem set todos(todos: Todo | Error) {} | ||
@mem get todos() { | ||
// Side effect, cached in mem | ||
fetch('/todos') | ||
.then((todos) => { | ||
this.todos = todos | ||
}) | ||
.catch(e => this.todos = e) | ||
throw new mem.Wait() | ||
} | ||
@mem get unfinishedTodos() { | ||
return this.todos.filter((todo) => !todo.finished) | ||
} | ||
} | ||
class Listener { | ||
_list = new TodoList() | ||
@mem listen() { | ||
try { | ||
console.log('total todos:', this._list.todos.length) | ||
console.log('unfinished todos:', this._list.unfinishedTodos.length) | ||
} catch (e) { | ||
if (e instanceof mem.Wait) { | ||
console.log('loading...') | ||
} else { | ||
console.error('error', e.message) | ||
} | ||
} | ||
} | ||
} | ||
const listener = new Listener() | ||
listener.listen() | ||
``` | ||
## Cache management | ||
Killer feature, grabbed from [mol_atom](https://github.com/eigenmethod/mol). We can reset cache on get or obviously set cache value, using magic force property. | ||
```js | ||
import {force, mem} from 'lom_atom' | ||
class TodoList { | ||
@force force: TodoList | ||
@mem set todos(todos: Todo | Error) { | ||
console.log('set handler') | ||
} | ||
@mem get todos() { | ||
console.log('get handler') | ||
return [someTodo] | ||
} | ||
} | ||
const list = new TodoList() | ||
list.todos = [someTodo] // console: set handler | ||
list.todos = [someTodo] // someTodo already set - no handler call | ||
list.force.todos = [someTodo] // force, console: set handler | ||
list.todos // console: get handler | ||
list.todos // return cached value | ||
list.force.todos // console: get handler | ||
``` | ||
## Key-value | ||
Basic dictionary support. See eigenmethod [mol_mem](https://github.com/eigenmethod/mol/tree/master/mem). | ||
```js | ||
class TodoList { | ||
@mem.key todo(id: string, next?: Todo, force?: boolean): Todo { | ||
if (next === undefined) { | ||
return {} | ||
} | ||
return next | ||
} | ||
} | ||
const list = new TodoList() | ||
list.todo('1', {id: 1, title: 'Todo 1'}) // set todo | ||
list.todo('1') // get todo | ||
``` |
@@ -10,4 +10,2 @@ import babel from 'rollup-plugin-babel' | ||
const plugins = [ uglify({}, minify) ] | ||
const commonConf = { | ||
@@ -14,0 +12,0 @@ entry: 'src/index.js', |
// @flow | ||
import {catchedId, ATOM_STATUS} from './interfaces' | ||
import { | ||
catchedId, | ||
ATOM_STATUS_DESTROYED, ATOM_STATUS_OBSOLETE, ATOM_STATUS_CHECKING, ATOM_STATUS_PULLING, ATOM_STATUS_ACTUAL | ||
} from './interfaces' | ||
@@ -29,3 +32,3 @@ import type { | ||
function actualizeMaster(master: IAtomInt) { | ||
if (this.status === ATOM_STATUS.CHECKING) { | ||
if (this.status === ATOM_STATUS_CHECKING) { | ||
master.actualize() | ||
@@ -36,9 +39,9 @@ } | ||
export default class Atom<V> implements IAtom<V>, IAtomInt { | ||
status: IAtomStatus | ||
status: IAtomStatus = ATOM_STATUS_OBSOLETE | ||
field: string | Function | ||
isComponent: boolean | ||
_masters: ?Set<IAtomInt> | ||
_slaves: ?Set<IAtomInt> | ||
_masters: ?Set<IAtomInt> = null | ||
_slaves: ?Set<IAtomInt> = null | ||
_context: IContext | ||
_cached: V | void | ||
_cached: V | void = undefined | ||
_normalize: (nv: V, old?: V) => V | ||
@@ -63,7 +66,2 @@ | ||
this._context = context | ||
this.status = ATOM_STATUS.OBSOLETE | ||
this._masters = null | ||
this._slaves = null | ||
this._cached = undefined | ||
} | ||
@@ -73,7 +71,7 @@ | ||
if (isDestroyed === undefined) { | ||
return this.status === ATOM_STATUS.DESTROYED | ||
return this.status === ATOM_STATUS_DESTROYED | ||
} | ||
if (isDestroyed) { | ||
if (this.status !== ATOM_STATUS.DESTROYED) { | ||
if (this.status !== ATOM_STATUS_DESTROYED) { | ||
// console.log('destroy', this.field) | ||
@@ -86,3 +84,3 @@ if (this._masters) { | ||
this._cached = undefined | ||
this.status = ATOM_STATUS.DESTROYED | ||
this.status = ATOM_STATUS_DESTROYED | ||
const host = this._host | ||
@@ -138,3 +136,3 @@ if (host !== undefined) { | ||
this._context.force = false | ||
this.status = ATOM_STATUS.ACTUAL | ||
this.status = ATOM_STATUS_ACTUAL | ||
this._context.newValue(this, this._cached, normalized) | ||
@@ -156,7 +154,7 @@ this._cached = normalized instanceof Error | ||
actualize(proposedValue?: V): void { | ||
if (this.status === ATOM_STATUS.ACTUAL) { | ||
if (this.status === ATOM_STATUS_ACTUAL) { | ||
return | ||
} | ||
if (this.status === ATOM_STATUS.CHECKING) { | ||
if (this.status === ATOM_STATUS_CHECKING) { | ||
if (this._masters) { | ||
@@ -166,8 +164,8 @@ this._masters.forEach(actualizeMaster, this) | ||
if (this.status === ATOM_STATUS.CHECKING) { | ||
this.status = ATOM_STATUS.ACTUAL | ||
if (this.status === ATOM_STATUS_CHECKING) { | ||
this.status = ATOM_STATUS_ACTUAL | ||
} | ||
} | ||
if (this.status !== ATOM_STATUS.ACTUAL) { | ||
if (this.status !== ATOM_STATUS_ACTUAL) { | ||
this._pullPush(proposedValue) | ||
@@ -183,3 +181,3 @@ } | ||
this.status = ATOM_STATUS.PULLING | ||
this.status = ATOM_STATUS_PULLING | ||
@@ -206,3 +204,3 @@ const context = this._context | ||
this.status = ATOM_STATUS.ACTUAL | ||
this.status = ATOM_STATUS_ACTUAL | ||
@@ -242,4 +240,4 @@ if (newValue !== undefined && this._cached !== newValue) { | ||
check() { | ||
if (this.status === ATOM_STATUS.ACTUAL) { | ||
this.status = ATOM_STATUS.CHECKING | ||
if (this.status === ATOM_STATUS_ACTUAL) { | ||
this.status = ATOM_STATUS_CHECKING | ||
this._checkSlaves() | ||
@@ -250,4 +248,4 @@ } | ||
obsolete() { | ||
if (this.status !== ATOM_STATUS.OBSOLETE) { | ||
this.status = ATOM_STATUS.OBSOLETE | ||
if (this.status !== ATOM_STATUS_OBSOLETE) { | ||
this.status = ATOM_STATUS_OBSOLETE | ||
this._checkSlaves() | ||
@@ -254,0 +252,0 @@ } |
@@ -23,24 +23,14 @@ // @flow | ||
export default class Context implements IContext { | ||
last: ?IAtomInt | ||
force: boolean | ||
last: ?IAtomInt = null | ||
force: boolean = false | ||
_logger: ?ILogger | ||
_updating: IAtomInt[] | ||
_reaping: Set<IAtomInt> | ||
_scheduled: boolean | ||
_atomMap: WeakMap<IAtomHost, Map<string | Function, IAtom<*>>> | ||
_run: () => void | ||
_logger: ?ILogger = null | ||
_updating: IAtomInt[] = [] | ||
_reaping: Set<IAtomInt> = new Set() | ||
_scheduled: boolean = false | ||
_atomMap: WeakMap<IAtomHost, Map<string | Function, IAtom<*>>> = new WeakMap() | ||
constructor() { | ||
this.last = null | ||
this.force = false | ||
this._logger = null | ||
this._updating = [] | ||
this._reaping = new Set() | ||
this._scheduled = false | ||
this._atomMap = new WeakMap() | ||
this._run = () => { | ||
if (this._scheduled) { | ||
this.run() | ||
} | ||
_run: () => void = () => { | ||
if (this._scheduled) { | ||
this.run() | ||
} | ||
@@ -157,3 +147,2 @@ } | ||
} | ||
// console.log('---------------------------- state changed\n') | ||
this._scheduled = false | ||
@@ -160,0 +149,0 @@ } |
@@ -9,11 +9,2 @@ // @flow | ||
export { | ||
AtomWait | ||
} from './utils' | ||
export { | ||
ATOM_STATUS, | ||
catchedId | ||
} from './interfaces' | ||
export type { | ||
@@ -20,0 +11,0 @@ ResultOf, |
@@ -29,18 +29,13 @@ // @flow | ||
export const ATOM_STATUS = { | ||
DESTROYED: 0, | ||
OBSOLETE: 1, | ||
CHECKING: 2, | ||
PULLING: 3, | ||
ACTUAL: 4 | ||
} | ||
export const ATOM_STATUS_DESTROYED = 0 | ||
export const ATOM_STATUS_OBSOLETE = 1 | ||
export const ATOM_STATUS_CHECKING = 2 | ||
export const ATOM_STATUS_PULLING = 3 | ||
export const ATOM_STATUS_ACTUAL = 4 | ||
export const catchedId = Symbol('lom_atom_catched') | ||
type $Object<V> = { +[ key : string] : V } | ||
type $ObjectValues<V, O : $Object<V>> = V | ||
type $Values<O: Object> = $ObjectValues<*, O> | ||
export type IAtomStatus = typeof ATOM_STATUS_DESTROYED | typeof ATOM_STATUS_OBSOLETE | ||
| typeof ATOM_STATUS_CHECKING | typeof ATOM_STATUS_PULLING | typeof ATOM_STATUS_ACTUAL | ||
export type IAtomStatus = $Values<typeof ATOM_STATUS> | ||
export interface IAtom<V> { | ||
@@ -47,0 +42,0 @@ status: IAtomStatus; |
@@ -5,2 +5,3 @@ // @flow | ||
import {defaultContext} from './Context' | ||
import {AtomWait} from './utils' | ||
@@ -142,3 +143,3 @@ type TypedPropertyDescriptor<T> = { | ||
export default function mem<P: Object, V, K>( | ||
export function detached<P: Object, V, K>( | ||
proto: P, | ||
@@ -148,8 +149,6 @@ name: string, | ||
): TypedPropertyDescriptor<any> | void { | ||
return descr.value === undefined | ||
? memProp(proto, name, descr) | ||
: memMethod(proto, name, descr) | ||
return memMethod(proto, name, descr, true) | ||
} | ||
export function detached<P: Object, V, K>( | ||
export default function mem<P: Object, V, K>( | ||
proto: P, | ||
@@ -159,3 +158,9 @@ name: string, | ||
): TypedPropertyDescriptor<any> | void { | ||
return memMethod(proto, name, descr, true) | ||
return descr.value === undefined | ||
? memProp(proto, name, descr) | ||
: memMethod(proto, name, descr) | ||
} | ||
mem.Wait = AtomWait | ||
mem.key = memkey | ||
mem.detached = detached |
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
147223
135
1319