Comparing version 1.0.3 to 1.0.4
@@ -5,2 +5,7 @@ # Change Log | ||
<a name="1.0.4"></a> | ||
## [1.0.4](https://github.com/zerkalica/lom_atom/compare/v1.0.3...v1.0.4) (2017-08-02) | ||
<a name="1.0.3"></a> | ||
@@ -7,0 +12,0 @@ ## [1.0.3](https://github.com/zerkalica/lom_atom/compare/v1.0.2...v1.0.3) (2017-07-31) |
@@ -107,10 +107,10 @@ // eslint-disable-line | ||
var Atom = function () { | ||
function Atom(field, handler, host, isComponent, context, normalize) { | ||
function Atom(field, host, context, key, normalize, isComponent) { | ||
this.status = ATOM_STATUS_OBSOLETE; | ||
this.cached = undefined; | ||
this._masters = null; | ||
this._slaves = null; | ||
this._cached = undefined; | ||
this.field = field; | ||
this._handler = handler; | ||
this._host = host; | ||
this.key = key; | ||
this.host = host; | ||
this.isComponent = isComponent || false; | ||
@@ -137,9 +137,11 @@ this._normalize = normalize || defaultNormalize; | ||
this._cached = undefined; | ||
this.status = ATOM_STATUS_DESTROYED; | ||
var host = this._host; | ||
var host = this.host; | ||
if (host !== undefined) { | ||
this._context.destroyHost(host, this.field); | ||
this._context.destroyHost(this); | ||
} | ||
this.cached = undefined; | ||
this.status = ATOM_STATUS_DESTROYED; | ||
this.key = undefined; | ||
} | ||
@@ -179,9 +181,9 @@ | ||
return this._cached; | ||
return this.cached; | ||
}; | ||
Atom.prototype.set = function set(v, force) { | ||
var normalized = this._normalize(v, this._cached); | ||
var normalized = this._normalize(v, this.cached); | ||
if (this._cached === normalized) { | ||
if (this.cached === normalized) { | ||
return normalized; | ||
@@ -191,3 +193,3 @@ } | ||
if (normalized === undefined) { | ||
return this._cached; | ||
return this.cached; | ||
} // console.log('set', this.field, 'value', normalized) | ||
@@ -200,5 +202,5 @@ | ||
this._context.newValue(this, this._cached, normalized); | ||
this._context.newValue(this, this.cached, normalized); | ||
this._cached = normalized instanceof Error ? createMock(normalized) : normalized; | ||
this.cached = normalized instanceof Error ? createMock(normalized) : normalized; | ||
@@ -213,3 +215,3 @@ if (this._slaves) { | ||
return this._cached; | ||
return this.cached; | ||
}; | ||
@@ -249,3 +251,3 @@ | ||
try { | ||
newValue = this._normalize(this._host === undefined ? this._handler(proposedValue, force) : this._handler.call(this._host, proposedValue, force), this._cached); | ||
newValue = this._normalize(this.key === undefined ? this.host[this.field](proposedValue, force, this.cached) : this.host[this.field](this.key, proposedValue, force, this.cached), this.cached); | ||
} catch (error) { | ||
@@ -263,6 +265,6 @@ if (error[catchedId] === undefined) { | ||
if (newValue !== undefined && this._cached !== newValue) { | ||
this._context.newValue(this, this._cached, newValue); | ||
if (newValue !== undefined && this.cached !== newValue) { | ||
this._context.newValue(this, this.cached, newValue); | ||
this._cached = newValue; | ||
this.cached = newValue; | ||
@@ -339,8 +341,2 @@ if (this._slaves) { | ||
function createKeyedHandler(host, handler, key) { | ||
return function keyedHandler(next, force) { | ||
return handler.call(host, key, next, force); | ||
}; | ||
} | ||
var Context = function () { | ||
@@ -365,3 +361,3 @@ function Context() { | ||
Context.prototype.getKeyAtom = function getKeyAtom(host, keyHandler, key) { | ||
Context.prototype.getAtom = function getAtom(field, host, key, normalize, isComponent) { | ||
var map = this._atomMap.get(host); | ||
@@ -375,7 +371,8 @@ | ||
var atom = map.get(key); | ||
var k = key === undefined ? field : key; | ||
var atom = map.get(k); | ||
if (atom === undefined) { | ||
atom = new Atom(key, createKeyedHandler(host, keyHandler, key), host, undefined, this); | ||
map.set(key, atom); // host[key + '@'] = atom | ||
atom = new Atom(field, host, this, key, normalize, isComponent); | ||
map.set(k, atom); // host[field + '@'] = atom | ||
} | ||
@@ -386,25 +383,14 @@ | ||
Context.prototype.getAtom = function getAtom(host, handler, key, isComponent) { | ||
Context.prototype.destroyHost = function destroyHost(atom) { | ||
var host = atom.host; | ||
var map = this._atomMap.get(host); | ||
if (map === undefined) { | ||
map = new Map(); | ||
if (map !== undefined) { | ||
var key = atom.key === undefined ? atom.field : atom.key; | ||
this._atomMap.set(host, map); | ||
} | ||
if (host._destroyProp !== undefined) { | ||
host._destroyProp(key, atom.cached); | ||
} | ||
var atom = map.get(key); | ||
if (atom === undefined) { | ||
atom = new Atom(key, handler, host, isComponent, this); | ||
map.set(key, atom); // host[key + '@'] = atom | ||
} | ||
return atom; | ||
}; | ||
Context.prototype.destroyHost = function destroyHost(host, key) { | ||
var map = this._atomMap.get(host); | ||
if (map !== undefined) { | ||
map.delete(key); | ||
@@ -499,10 +485,10 @@ | ||
function memMethod(proto, name, descr, isComponent) { | ||
proto[name + "$"] = descr.value; | ||
var handler = descr.value; | ||
function memMethod(proto, field, descr, normalize, isComponent) { | ||
var handlerKey = field + "$"; | ||
if (handler === undefined) { | ||
throw new TypeError(name + " is not an function (next?: V)"); | ||
if (descr.value === undefined) { | ||
throw new TypeError(field + " is not an function (next?: V)"); | ||
} | ||
proto[handlerKey] = descr.value; | ||
return { | ||
@@ -512,3 +498,3 @@ enumerable: descr.enumerable, | ||
value: function value(next, force) { | ||
return defaultContext.getAtom(this, handler, name, isComponent).value(next, force); | ||
return defaultContext.getAtom(handlerKey, this, undefined, normalize, isComponent).value(next, force); | ||
} | ||
@@ -535,7 +521,7 @@ }; | ||
function memProp(proto, name, descr) { | ||
function memProp(proto, name, descr, normalize) { | ||
var handlerKey = name + "$"; | ||
if (proto[handlerKey] !== undefined) { | ||
return; | ||
return undefined; | ||
} | ||
@@ -548,6 +534,6 @@ | ||
get: function get$$1() { | ||
return defaultContext.getAtom(this, handler, name).get(); | ||
return defaultContext.getAtom(handlerKey, this, undefined, normalize).get(); | ||
}, | ||
set: function set$$1(val) { | ||
defaultContext.getAtom(this, handler, name).set(val); | ||
defaultContext.getAtom(handlerKey, this, undefined, normalize).set(val); | ||
} | ||
@@ -567,3 +553,3 @@ }; | ||
function memkey(proto, name, descr) { | ||
function memkeyProp(proto, name, descr, normalize) { | ||
var handler = descr.value; | ||
@@ -575,3 +561,4 @@ | ||
proto[name + "$"] = handler; | ||
var handlerKey = name + "$"; | ||
proto[handlerKey] = handler; | ||
return { | ||
@@ -581,3 +568,3 @@ enumerable: descr.enumerable, | ||
value: function value(rawKey, next, force) { | ||
return defaultContext.getKeyAtom(this, handler, typeof rawKey === 'function' ? rawKey : name + "(" + getKey(rawKey) + ")").value(next, force); | ||
return defaultContext.getAtom(handlerKey, this, typeof rawKey === 'function' ? rawKey : name + "(" + getKey(rawKey) + ")", normalize).value(next, force); | ||
} | ||
@@ -587,2 +574,13 @@ }; | ||
function memkey() { | ||
if (arguments.length === 3) { | ||
return memkeyProp(arguments[0], arguments[1], arguments[2]); | ||
} | ||
var normalize = arguments[0]; | ||
return function (proto, name, descr) { | ||
return memkeyProp(proto, name, descr, normalize); | ||
}; | ||
} | ||
function forceGet() { | ||
@@ -601,6 +599,13 @@ defaultContext.force = true; | ||
function detached(proto, name, descr) { | ||
return memMethod(proto, name, descr, true); | ||
return memMethod(proto, name, descr, undefined, true); | ||
} | ||
function mem(proto, name, descr) { | ||
return descr.value === undefined ? memProp(proto, name, descr) : memMethod(proto, name, descr); | ||
function mem() { | ||
if (arguments.length === 3) { | ||
return arguments[2].value === undefined ? memProp(arguments[0], arguments[1], arguments[2]) : memMethod(arguments[0], arguments[1], arguments[2]); | ||
} | ||
var normalize = arguments[0]; | ||
return function (proto, name, descr) { | ||
return descr.value === undefined ? memProp(proto, name, descr, normalize) : memMethod(proto, name, descr, normalize); | ||
}; | ||
} | ||
@@ -607,0 +612,0 @@ mem.Wait = AtomWait; |
@@ -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 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; | ||
"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 memMethod(t,e,o,i,s){var n=e+"$";if(void 0===o.value)throw new TypeError(e+" is not an function (next?: V)");return t[n]=o.value,{enumerable:o.enumerable,configurable:o.configurable,value:function(t,e){return defaultContext.getAtom(n,this,void 0,i,s).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 s=e+"$";if(void 0===t[s]){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(s,this,void 0,i).get()},set:function(t){defaultContext.getAtom(s,this,void 0,i).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 memkeyProp(t,e,o,i){var s=o.value;if(void 0===s)throw new TypeError(e+" is not an function (rawKey: K, next?: V)");var n=e+"$";return t[n]=s,{enumerable:o.enumerable,configurable:o.configurable,value:function(t,o,s){return defaultContext.getAtom(n,this,"function"==typeof t?t:e+"("+getKey(t)+")",i).value(o,s)}}}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 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,s,n){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=n||!1,this._normalize=s||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);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 i=this._context,s=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)}i.last=s,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.getAtom=function(t,e,o,i,s){var n=this._atomMap.get(e);void 0===n&&(n=new Map,this._atomMap.set(e,n));var r=void 0===o?t:o,a=n.get(r);return void 0===a&&(a=new Atom(t,e,this,o,i,s),n.set(r,a)),a},t.prototype.destroyHost=function(t){var e=t.host,o=this._atomMap.get(e);if(void 0!==o){var i=void 0===t.key?t.field:t.key;void 0!==e._destroyProp&&e._destroyProp(i,t.cached),o.delete(i),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||(this._scheduled=!0,animationFrame(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(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,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})}); | ||
!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,k)}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,o,s){var n=e+"$";if(void 0===i.value)throw new TypeError(e+" is not an function (next?: V)");return t[n]=i.value,{enumerable:i.enumerable,configurable:i.configurable,value:function(t,e){return A.getAtom(n,this,void 0,o,s).value(t,e)}}}function u(t,e){return function(i){return void 0===i?t.call(this):(e.call(this,i),i)}}function c(t){return function(e){return void 0===e&&void 0!==t?t.call(this):e}}function l(t,e,i,o){var s=e+"$";if(void 0===t[s]){t[s]=void 0===i.get&&void 0===i.set?c(i.initializer):u(i.get,i.set);return{enumerable:i.enumerable,configurable:i.configurable,get:function(){return A.getAtom(s,this,void 0,o).get()},set:function(t){A.getAtom(s,this,void 0,o).set(t)}}}}function f(t){return t?"object"===w(t)?Object.keys(t).sort().map(function(e){return e+":"+JSON.stringify(t[e])}).join("."):JSON.stringify(t):""}function d(t,e,i,o){var s=i.value;if(void 0===s)throw new TypeError(e+" is not an function (rawKey: K, next?: V)");var n=e+"$";return t[n]=s,{enumerable:i.enumerable,configurable:i.configurable,value:function(t,i,s){return A.getAtom(n,this,"function"==typeof t?t:e+"("+f(t)+")",o).value(i,s)}}}function p(){if(3===arguments.length)return d(arguments[0],arguments[1],arguments[2]);var t=arguments[0];return function(e,i,o){return d(e,i,o,t)}}function _(){return A.force=!0,this}function v(t,e,i){return h(t,e,i,void 0,!0)}function y(){if(3===arguments.length)return void 0===arguments[2].value?l(arguments[0],arguments[1],arguments[2]):h(arguments[0],arguments[1],arguments[2]);var t=arguments[0];return function(e,i,o){return void 0===o.value?l(e,i,o,t):h(e,i,o,t)}}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},k={get:function(t){throw t.valueOf()},ownKeys:function(t){throw t.valueOf()}},S=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),E=function(){function t(t,e,o,s,n,r){this.status=m,this.cached=void 0,this._masters=null,this._slaves=null,this.field=t,this.key=s,this.host=e,this.isComponent=r||!1,this._normalize=n||i,this._context=o}return t.prototype.destroyed=function(t){return void 0===t?0===this.status:!!t&&(0!==this.status&&(this._masters&&(this._masters.forEach(n,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 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.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[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)},A=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.getAtom=function(t,e,i,o,s){var n=this._atomMap.get(e);void 0===n&&(n=new Map,this._atomMap.set(e,n));var r=void 0===i?t:i,a=n.get(r);return void 0===a&&(a=new E(t,e,this,i,o,s),n.set(r,a)),a},t.prototype.destroyHost=function(t){var e=t.host,i=this._atomMap.get(e);if(void 0!==i){var o=void 0===t.key?t.field:t.key;void 0!==e._destroyProp&&e._destroyProp(o,t.cached),i.delete(o),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 S?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=S,y.key=p,y.detached=v,t.Atom=E,t.mem=y,t.memkey=p,t.detached=v,t.force=function(t,e,i){return{enumerable:i.enumerable,configurable:i.configurable,get:_}},t.defaultContext=A,t.animationFrame=z,Object.defineProperty(t,"__esModule",{value:!0})}); | ||
//# sourceMappingURL=lom_atom.umd.js.map |
{ | ||
"name": "lom_atom", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "Observable state management", | ||
@@ -5,0 +5,0 @@ "publishConfig": { |
@@ -10,2 +10,3 @@ // @flow | ||
IAtom, | ||
INormalize, | ||
IAtomInt, | ||
@@ -40,24 +41,25 @@ IAtomStatus, | ||
status: IAtomStatus = ATOM_STATUS_OBSOLETE | ||
field: string | Function | ||
field: string | ||
key: string | Function | void | ||
host: IAtomHost | ||
cached: V | void = undefined | ||
isComponent: boolean | ||
_masters: ?Set<IAtomInt> = null | ||
_slaves: ?Set<IAtomInt> = null | ||
_context: IContext | ||
_cached: V | void = undefined | ||
_normalize: (nv: V, old?: V) => V | ||
_normalize: INormalize<V> | ||
_handler: IAtomHandler<V> | ||
_host: IAtomHost | void | ||
constructor( | ||
field: string | Function, | ||
handler: IAtomHandler<V>, | ||
host?: IAtomHost, | ||
isComponent?: boolean, | ||
field: string, | ||
host: IAtomHost, | ||
context: IContext, | ||
normalize?: (nv: V, old?: V) => V | ||
key?: string | Function, | ||
normalize?: INormalize<V>, | ||
isComponent?: boolean | ||
) { | ||
this.field = field | ||
this._handler = handler | ||
this._host = host | ||
this.key = key | ||
this.host = host | ||
this.isComponent = isComponent || false | ||
@@ -81,8 +83,9 @@ this._normalize = normalize || defaultNormalize | ||
this._checkSlaves() | ||
this._cached = undefined | ||
this.status = ATOM_STATUS_DESTROYED | ||
const host = this._host | ||
const host = this.host | ||
if (host !== undefined) { | ||
this._context.destroyHost(host, this.field) | ||
this._context.destroyHost(this) | ||
} | ||
this.cached = undefined | ||
this.status = ATOM_STATUS_DESTROYED | ||
this.key = undefined | ||
} | ||
@@ -117,13 +120,13 @@ | ||
return (this._cached: any) | ||
return (this.cached: any) | ||
} | ||
set(v: V | Error, force?: boolean): V { | ||
const normalized: V = this._normalize((v: any), this._cached) | ||
const normalized: V = this._normalize((v: any), this.cached) | ||
if (this._cached === normalized) { | ||
if (this.cached === normalized) { | ||
return normalized | ||
} | ||
if (normalized === undefined) { | ||
return this._cached | ||
return this.cached | ||
} | ||
@@ -136,4 +139,4 @@ | ||
this.status = ATOM_STATUS_ACTUAL | ||
this._context.newValue(this, this._cached, normalized) | ||
this._cached = normalized instanceof Error | ||
this._context.newValue(this, this.cached, normalized) | ||
this.cached = normalized instanceof Error | ||
? createMock(normalized) | ||
@@ -149,3 +152,3 @@ : normalized | ||
return (this._cached: any) | ||
return (this.cached: any) | ||
} | ||
@@ -186,6 +189,6 @@ | ||
newValue = this._normalize( | ||
this._host === undefined | ||
? this._handler(proposedValue, force) | ||
: this._handler.call(this._host, proposedValue, force), | ||
this._cached | ||
this.key === undefined | ||
? (this.host: any)[this.field](proposedValue, force, this.cached) | ||
: (this.host: any)[this.field](this.key, proposedValue, force, this.cached), | ||
this.cached | ||
) | ||
@@ -204,5 +207,5 @@ } catch (error) { | ||
if (newValue !== undefined && this._cached !== newValue) { | ||
this._context.newValue(this, this._cached, newValue) | ||
this._cached = newValue | ||
if (newValue !== undefined && this.cached !== newValue) { | ||
this._context.newValue(this, this.cached, newValue) | ||
this.cached = newValue | ||
if (this._slaves) { | ||
@@ -209,0 +212,0 @@ this._slaves.forEach(obsoleteSlave) |
// @flow | ||
import type {IAtomHost, IAtomHandlers, IAtomHandler, IAtomKeyHandler, IAtomInt, IAtom, IContext, ILogger} from './interfaces' | ||
import type { | ||
INormalize, | ||
IAtomHost, | ||
IAtomHandler, | ||
IAtomInt, | ||
IAtom, | ||
IContext, | ||
ILogger | ||
} from './interfaces' | ||
import {AtomWait} from './utils' | ||
@@ -16,8 +24,2 @@ import Atom from './Atom' | ||
function createKeyedHandler<V>(host: IAtomHost, handler: IAtomKeyHandler<V, *>, key: string | Function): IAtomHandler<V> { | ||
return function keyedHandler(next?: V, force?: boolean): V { | ||
return handler.call(host, key, next, force) | ||
} | ||
} | ||
export default class Context implements IContext { | ||
@@ -31,3 +33,3 @@ last: ?IAtomInt = null | ||
_scheduled: boolean = false | ||
_atomMap: WeakMap<IAtomHost, Map<string | Function, IAtom<*>>> = new WeakMap() | ||
_atomMap: WeakMap<IAtomHost, Map<string | Function, IAtom<any>>> = new WeakMap() | ||
@@ -40,3 +42,9 @@ _run: () => void = () => { | ||
getKeyAtom(host: IAtomHost, keyHandler: IAtomKeyHandler<*, *>, key: string | Function): IAtom<*> { | ||
getAtom<V>( | ||
field: string, | ||
host: IAtomHost, | ||
key?: string | Function, | ||
normalize?: INormalize<V>, | ||
isComponent?: boolean | ||
): IAtom<V> { | ||
let map = this._atomMap.get(host) | ||
@@ -47,7 +55,8 @@ if (map === undefined) { | ||
} | ||
let atom = map.get(key) | ||
const k = key === undefined ? field : key | ||
let atom: IAtom<V> | void = map.get(k) | ||
if (atom === undefined) { | ||
atom = new Atom(key, createKeyedHandler(host, keyHandler, key), host, undefined, this) | ||
map.set(key, atom) | ||
// host[key + '@'] = atom | ||
atom = new Atom(field, host, this, key, normalize, isComponent) | ||
map.set(k, atom) | ||
// host[field + '@'] = atom | ||
} | ||
@@ -58,21 +67,10 @@ | ||
getAtom(host: IAtomHost, handler: IAtomHandler<*>, key: string | Function, isComponent?: boolean): IAtom<*> { | ||
let map = this._atomMap.get(host) | ||
if (map === undefined) { | ||
map = new Map() | ||
this._atomMap.set(host, map) | ||
} | ||
let atom = map.get(key) | ||
if (atom === undefined) { | ||
atom = new Atom(key, handler, host, isComponent, this) | ||
map.set(key, atom) | ||
// host[key + '@'] = atom | ||
} | ||
return atom | ||
} | ||
destroyHost(host: IAtomHost, key: string | Function) { | ||
destroyHost(atom: IAtomInt) { | ||
const host = atom.host | ||
const map = this._atomMap.get(host) | ||
if (map !== undefined) { | ||
const key: string | Function = atom.key === undefined ? atom.field : atom.key | ||
if (host._destroyProp !== undefined) { | ||
host._destroyProp(key, atom.cached) | ||
} | ||
map.delete(key) | ||
@@ -79,0 +77,0 @@ if (map.size === 0) { |
@@ -17,6 +17,12 @@ // @flow | ||
getKeyAtom(host: IAtomHost, handler: IAtomKeyHandler<*, *>, key: string | Function): IAtom<*>; | ||
getAtom(host: IAtomHost, handler: IAtomHandler<*>, key: string | Function, isComponent?: boolean): IAtom<*>; | ||
destroyHost(host: IAtomHost, field: string | Function): void; | ||
getAtom<V>( | ||
field: string, | ||
host: IAtomHost, | ||
key?: string | Function, | ||
normalize?: INormalize<V>, | ||
isComponent?: boolean | ||
): IAtom<V>; | ||
destroyHost(atom: IAtomInt): void; | ||
newValue<V>(t: IAtom<V>, from?: V | Error, to: V | Error): void; | ||
@@ -43,3 +49,3 @@ setLogger(logger: ILogger): void; | ||
status: IAtomStatus; | ||
field: string | Function; | ||
field: string; | ||
get(force?: boolean): V; | ||
@@ -53,2 +59,5 @@ set(v: V | Error, force?: boolean): V; | ||
isComponent: boolean; | ||
key: string | Function | void; | ||
host: IAtomHost; | ||
cached: any; | ||
@@ -62,9 +71,11 @@ actualize(): void; | ||
// | Error | ||
export type IAtomHandler<V> = (next?: V, force?: boolean) => V | ||
export type IAtomKeyHandler<V, K> = (key: K, next?: V | Error, force?: boolean) => V | ||
export type IAtomHandlers<V, K> = IAtomHandler<V> | IAtomKeyHandler<V, K> | ||
export type IAtomHandler<V, K> = (key: K, next?: V | Error, force?: boolean, oldValue?: V) => V | ||
| (next?: V | Error, force?: boolean, oldValue?: V) => V | ||
export type INormalize<V> = (next: V, prev?: V) => V | ||
export interface IAtomHost { | ||
[key: string]: IAtomHandlers<*, *>; | ||
[key: string]: IAtomHandler<*, *>; | ||
_destroyProp?: (key: string | Function, value: mixed | void) => void; | ||
_destroy?: () => void; | ||
} |
121
src/mem.js
// @flow | ||
import type {IAtom, IAtomHandler, IAtomKeyHandler, IAtomHost} from './interfaces' | ||
import type {IAtom, IAtomHandler, IAtomHost, INormalize} from './interfaces' | ||
import {defaultContext} from './Context' | ||
@@ -19,11 +19,12 @@ import {AtomWait} from './utils' | ||
proto: P, | ||
name: string, | ||
descr: TypedPropertyDescriptor<IAtomHandler<V>>, | ||
field: string, | ||
descr: TypedPropertyDescriptor<*>, | ||
normalize?: INormalize<V>, | ||
isComponent?: boolean | ||
): TypedPropertyDescriptor<IAtomHandler<V>> { | ||
proto[`${name}$`] = descr.value | ||
const handler = descr.value | ||
if (handler === undefined) { | ||
throw new TypeError(`${name} is not an function (next?: V)`) | ||
): TypedPropertyDescriptor<*> { | ||
const handlerKey = `${field}$` | ||
if (descr.value === undefined) { | ||
throw new TypeError(`${field} is not an function (next?: V)`) | ||
} | ||
proto[handlerKey] = descr.value | ||
@@ -34,3 +35,3 @@ return { | ||
value(next?: V | Error, force?: boolean) { | ||
return defaultContext.getAtom(this, handler, name, isComponent) | ||
return defaultContext.getAtom(handlerKey, this, undefined, normalize, isComponent) | ||
.value(next, force) | ||
@@ -44,3 +45,3 @@ } | ||
set?: (v: V | Error) => void | ||
): IAtomHandler<V> { | ||
): IAtomHandler<V, *> { | ||
return function getSetHandler(next?: V) { | ||
@@ -55,3 +56,3 @@ if (next === undefined) { | ||
function createValueHandler<V>(initializer?: () => V): IAtomHandler<V> { | ||
function createValueHandler<V>(initializer?: () => V): IAtomHandler<V, *> { | ||
return function valueHandler(next?: V | Error) { | ||
@@ -67,7 +68,8 @@ return next === undefined && initializer !== undefined | ||
name: string, | ||
descr: TypedPropertyDescriptor<V> | ||
): TypedPropertyDescriptor<*> | void { | ||
descr: TypedPropertyDescriptor<V>, | ||
normalize?: INormalize<V> | ||
): TypedPropertyDescriptor<V> { | ||
const handlerKey = `${name}$` | ||
if (proto[handlerKey] !== undefined) { | ||
return | ||
return (undefined: any) | ||
} | ||
@@ -83,6 +85,6 @@ | ||
get() { | ||
return defaultContext.getAtom(this, handler, name).get() | ||
return defaultContext.getAtom(handlerKey, this, undefined, normalize).get() | ||
}, | ||
set(val: V | Error) { | ||
defaultContext.getAtom(this, handler, name).set(val) | ||
defaultContext.getAtom(handlerKey, this, undefined, normalize).set(val) | ||
} | ||
@@ -105,7 +107,8 @@ } | ||
export function memkey<V, K, P: Object>( | ||
function memkeyProp<V, K, P: Object>( | ||
proto: P, | ||
name: string, | ||
descr: TypedPropertyDescriptor<IAtomKeyHandler<V, K>> | ||
): TypedPropertyDescriptor<IAtomKeyHandler<V, K>> { | ||
descr: TypedPropertyDescriptor<IAtomHandler<V, K>>, | ||
normalize?: INormalize<V> | ||
): TypedPropertyDescriptor<IAtomHandler<V, K>> { | ||
const handler = descr.value | ||
@@ -116,4 +119,6 @@ if (handler === undefined) { | ||
proto[`${name}$`] = handler | ||
const handlerKey = `${name}$` | ||
proto[handlerKey] = handler | ||
return { | ||
@@ -123,6 +128,7 @@ enumerable: descr.enumerable, | ||
value(rawKey: K, next?: V | Error, force?: boolean) { | ||
return defaultContext.getKeyAtom( | ||
return defaultContext.getAtom( | ||
handlerKey, | ||
this, | ||
handler, | ||
typeof rawKey === 'function' ? rawKey : `${name}(${getKey(rawKey)})` | ||
typeof rawKey === 'function' ? rawKey : `${name}(${getKey(rawKey)})`, | ||
normalize | ||
) | ||
@@ -134,2 +140,32 @@ .value(next, force) | ||
type IMemKeyProp<V, K, P: Object> = ( | ||
proto: P, | ||
name: string, | ||
descr: TypedPropertyDescriptor<IAtomHandler<V, K>>, | ||
normalize?: INormalize<V> | ||
) => TypedPropertyDescriptor<IAtomHandler<V, K>> | ||
declare function memkey<V, K, P: Object>(normalize: INormalize<V>): () => IMemKeyProp<V, K, P> | ||
declare function memkey<V, K, P: Object>( | ||
proto: P, | ||
name: string, | ||
descr: TypedPropertyDescriptor<IAtomHandler<V, K>>, | ||
normalize?: INormalize<V> | ||
): TypedPropertyDescriptor<IAtomHandler<V, K>> | ||
export function memkey() { | ||
if (arguments.length === 3) { | ||
return memkeyProp(arguments[0], arguments[1], arguments[2]) | ||
} | ||
const normalize: INormalize<*> = arguments[0] | ||
return function ( | ||
proto: Object, | ||
name: string, | ||
descr: TypedPropertyDescriptor<IAtomHandler<*, *>> | ||
): TypedPropertyDescriptor<IAtomHandler<*, *>> { | ||
return memkeyProp(proto, name, descr, normalize) | ||
} | ||
} | ||
function forceGet() { | ||
@@ -157,13 +193,38 @@ defaultContext.force = true | ||
): TypedPropertyDescriptor<any> | void { | ||
return memMethod(proto, name, descr, true) | ||
return memMethod(proto, name, descr, undefined, true) | ||
} | ||
export default function mem<P: Object, V, K>( | ||
type IMemProp<V, P: Object> = ( | ||
proto: P, | ||
name: string, | ||
descr: TypedPropertyDescriptor<*> | ||
): TypedPropertyDescriptor<any> | void { | ||
return descr.value === undefined | ||
? memProp(proto, name, descr) | ||
: memMethod(proto, name, descr) | ||
descr: TypedPropertyDescriptor<IAtomHandler<V>>, | ||
normalize?: INormalize<V> | ||
) => TypedPropertyDescriptor<IAtomHandler<V>> | ||
declare function mem<V, P: Object>(normalize: INormalize<V>): () => IMemProp<V, P> | ||
declare function mem<V, P: Object>( | ||
proto: P, | ||
name: string, | ||
descr: TypedPropertyDescriptor<V>, | ||
normalize?: INormalize<V> | ||
): TypedPropertyDescriptor<*> | ||
export default function mem() { | ||
if (arguments.length === 3) { | ||
return arguments[2].value === undefined | ||
? memProp(arguments[0], arguments[1], arguments[2]) | ||
: memMethod(arguments[0], arguments[1], arguments[2]) | ||
} | ||
const normalize: INormalize<*> = arguments[0] | ||
return function ( | ||
proto: Object, | ||
name: string, | ||
descr: TypedPropertyDescriptor<*> | ||
): TypedPropertyDescriptor<*> { | ||
return descr.value === undefined | ||
? memProp(proto, name, descr, normalize) | ||
: memMethod(proto, name, descr, normalize) | ||
} | ||
} | ||
@@ -170,0 +231,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
155585
1389