Comparing version 1.0.6 to 1.0.7
@@ -5,2 +5,7 @@ # Change Log | ||
<a name="1.0.7"></a> | ||
## [1.0.7](https://github.com/zerkalica/lom_atom/compare/v1.0.6...v1.0.7) (2017-08-14) | ||
<a name="1.0.6"></a> | ||
@@ -7,0 +12,0 @@ ## [1.0.6](https://github.com/zerkalica/lom_atom/compare/v1.0.5...v1.0.6) (2017-08-13) |
@@ -206,4 +206,2 @@ // eslint-disable-line | ||
context.newValue(this, oldValue, normalized); | ||
context.beginTransaction(); | ||
context.endTransaction(); | ||
} else { | ||
@@ -247,3 +245,2 @@ this.obsolete(); | ||
context.last = this; | ||
context.beginTransaction(); | ||
@@ -273,6 +270,2 @@ try { | ||
this._context.newValue(this, oldValue, newValue); | ||
context.endTransaction(); | ||
} else { | ||
context.endTransaction(true); | ||
} | ||
@@ -336,2 +329,8 @@ }; | ||
var scheduleNative = typeof requestAnimationFrame == 'function' ? function (handler) { | ||
return requestAnimationFrame(handler); | ||
} : function (handler) { | ||
return setTimeout(handler, 16); | ||
}; | ||
function reap(atom, key, reaping) { | ||
@@ -368,2 +367,4 @@ reaping.delete(atom); | ||
function Context() { | ||
var _this = this; | ||
this.last = null; | ||
@@ -375,2 +376,13 @@ this.force = false; | ||
this._atomMap = new WeakMap(); | ||
this._scheduled = false; | ||
this.__run = function () { | ||
if (_this._scheduled) { | ||
_this._scheduled = false; | ||
_this._run(); | ||
} | ||
}; | ||
this._start = 0; | ||
this._pendCount = 0; | ||
@@ -445,2 +457,4 @@ } | ||
this._updating.push(atom); | ||
this._schedule(); | ||
}; | ||
@@ -451,2 +465,4 @@ | ||
this._reaping.add(atom); | ||
this._schedule(); | ||
}; | ||
@@ -459,41 +475,54 @@ | ||
Context.prototype.beginTransaction = function beginTransaction() { | ||
this._pendCount++; | ||
Context.prototype._schedule = function _schedule() { | ||
if (!this._scheduled) { | ||
scheduleNative(this.__run); | ||
this._scheduled = true; | ||
} | ||
}; | ||
Context.prototype.run = function run() { | ||
this.beginTransaction(); | ||
this.endTransaction(); | ||
}; | ||
Context.prototype._run = function _run() { | ||
this._schedule(); | ||
Context.prototype.endTransaction = function endTransaction(noUpdate) { | ||
if (this._pendCount === 1 && noUpdate !== true) { | ||
var reaping = this._reaping; | ||
var updating = this._updating; | ||
var start = 0; | ||
var reaping = this._reaping; | ||
var updating = this._updating; | ||
var start = this._start; | ||
do { | ||
var end = updating.length; | ||
do { | ||
var end = updating.length; | ||
for (var i = start; i < end; i++) { | ||
var atom = updating[i]; | ||
for (var i = start; i < end; i++) { | ||
this._start = i; | ||
var atom = updating[i]; | ||
if (!reaping.has(atom) && !atom.destroyed()) { | ||
atom.actualize(); | ||
} | ||
if (!reaping.has(atom) && !atom.destroyed()) { | ||
atom.actualize(); | ||
} | ||
} | ||
start = end; | ||
} while (updating.length > start); | ||
start = end; | ||
} while (updating.length > start); | ||
updating.length = 0; | ||
updating.length = 0; | ||
this._start = 0; | ||
while (reaping.size > 0) { | ||
reaping.forEach(reap); | ||
} | ||
while (reaping.size > 0) { | ||
reaping.forEach(reap); | ||
} | ||
this._pendCount--; | ||
this._scheduled = false; | ||
this._pendCount = 0; | ||
}; | ||
Context.prototype.beginTransaction = function beginTransaction() { | ||
this._pendCount++; | ||
}; | ||
Context.prototype.endTransaction = function endTransaction() { | ||
if (this._pendCount === 1) { | ||
this._run(); | ||
} else { | ||
this._pendCount--; | ||
} | ||
}; | ||
return Context; | ||
@@ -603,6 +632,6 @@ }(); | ||
function createAction(t, hk) { | ||
function createActionMethod(t, hk, context) { | ||
function action() { | ||
var result = void 0; | ||
defaultContext.beginTransaction(); | ||
context.beginTransaction(); | ||
@@ -638,3 +667,3 @@ switch (arguments.length) { | ||
defaultContext.endTransaction(); | ||
context.endTransaction(); | ||
return result; | ||
@@ -647,3 +676,45 @@ } | ||
function action(proto, field, descr) { | ||
function createActionFn(fn, name, context) { | ||
function action() { | ||
var result = void 0; | ||
context.beginTransaction(); | ||
switch (arguments.length) { | ||
case 0: | ||
result = fn(); | ||
break; | ||
case 1: | ||
result = fn(arguments[0]); | ||
break; | ||
case 2: | ||
result = fn(arguments[0], arguments[1]); | ||
break; | ||
case 3: | ||
result = fn(arguments[0], arguments[1], arguments[2]); | ||
break; | ||
case 4: | ||
result = fn(arguments[0], arguments[1], arguments[2], arguments[3]); | ||
break; | ||
case 5: | ||
result = fn(arguments[0], arguments[1], arguments[2], arguments[3], arguments[4]); | ||
break; | ||
default: | ||
result = fn.apply(null, arguments); | ||
} | ||
context.endTransaction(); | ||
return result; | ||
} | ||
action.displayName = name || fn.displayName || fn.name; | ||
return action; | ||
} | ||
function actionMethod(proto, field, descr, context) { | ||
var hk = field + "$"; | ||
@@ -666,3 +737,3 @@ | ||
definingProperty = true; | ||
var actionFn = createAction(this, hk); | ||
var actionFn = createActionMethod(this, hk, context); | ||
Object.defineProperty(this, field, { | ||
@@ -679,2 +750,10 @@ configurable: true, | ||
} | ||
function action() { | ||
if (arguments.length === 3) { | ||
return actionMethod(arguments[0], arguments[1], arguments[2], defaultContext); | ||
} | ||
return createActionFn(arguments[0], arguments[1], defaultContext); | ||
} | ||
function mem() { | ||
@@ -681,0 +760,0 @@ if (arguments.length === 3) { |
@@ -1,2 +0,2 @@ | ||
"use strict";function createMock(t){return new Proxy(t,throwOnAccess)}function defaultNormalize(t,e){if(t===e)return t;if(t instanceof Array&&e instanceof Array&&t.length===e.length){for(var o=0;o<t.length;o++)if(t[o]!==e[o])return t;return e}return t}function checkSlave(t){t.check()}function obsoleteSlave(t){t.obsolete()}function disleadThis(t){t.dislead(this)}function actualizeMaster(t){this.status===ATOM_STATUS_CHECKING&&t.actualize()}function reap(t,e,o){o.delete(t),t.slaves||t.destroyed(!0)}function getKey(t){return"string"==typeof t||"number"==typeof t?t:t?"function"==typeof t?(t.__id=t.__id||++lastId,t.__id):"object"===_typeof(t)?Object.keys(t).sort().map(function(e){return e+":"+JSON.stringify(t[e])}).join("."):JSON.stringify(t):0}function memMethod(t,e,o,i,n){var s=e+"$";if(void 0===o.value)throw new TypeError(e+" is not an function (next?: V)");return t[s]=o.value,{enumerable:o.enumerable,configurable:o.configurable,value:function(t,e){return defaultContext.getAtom(s,this,void 0,i,n).value(t,e)}}}function createGetSetHandler(t,e){return function(o){return void 0===o?t.call(this):(e.call(this,o),o)}}function createValueHandler(t){return function(e){return void 0===e&&void 0!==t?t.call(this):e}}function memProp(t,e,o,i){var n=e+"$";if(void 0===t[n]){t[n]=void 0===o.get&&void 0===o.set?createValueHandler(o.initializer):createGetSetHandler(o.get,o.set);return{enumerable:o.enumerable,configurable:o.configurable,get:function(){return defaultContext.getAtom(n,this,void 0,i).get()},set:function(t){defaultContext.getAtom(n,this,void 0,i).set(t)}}}}function memkeyProp(t,e,o,i){var n=o.value;if(void 0===n)throw new TypeError(e+" is not an function (rawKey: K, next?: V)");var s=e+"$";return t[s]=n,{enumerable:o.enumerable,configurable:o.configurable,value:function(t,e,o){return defaultContext.getAtom(s,this,t,i).value(e,o)}}}function memkey(){if(3===arguments.length)return memkeyProp(arguments[0],arguments[1],arguments[2]);var t=arguments[0];return function(e,o,i){return memkeyProp(e,o,i,t)}}function forceGet(){return defaultContext.force=!0,this}function force(t,e,o){return{enumerable:o.enumerable,configurable:o.configurable,get:forceGet}}function detached(t,e,o){return memMethod(t,e,o,void 0,!0)}function createAction(t,e){function o(){var o=void 0;switch(defaultContext.beginTransaction(),arguments.length){case 0:o=t[e]();break;case 1:o=t[e](arguments[0]);break;case 2:o=t[e](arguments[0],arguments[1]);break;case 3:o=t[e](arguments[0],arguments[1],arguments[2]);break;case 4:o=t[e](arguments[0],arguments[1],arguments[2],arguments[3]);break;case 5:o=t[e](arguments[0],arguments[1],arguments[2],arguments[3],arguments[4]);break;default:o=t[e].apply(t,arguments)}return defaultContext.endTransaction(),o}return o.displayName=e,o}function action(t,e,o){var i=e+"$";if(void 0===o.value)throw new TypeError(e+" is not an function (next?: V)");t[i]=o.value;var n=!1;return{enumerable:o.enumerable,configurable:o.configurable,get:function(){if(n)return this[i];n=!0;var t=createAction(this,i);return Object.defineProperty(this,e,{configurable:!0,get:function(){return t}}),n=!1,t}}}function mem(){if(3===arguments.length)return void 0===arguments[2].value?memProp(arguments[0],arguments[1],arguments[2]):memMethod(arguments[0],arguments[1],arguments[2]);var t=arguments[0];return function(e,o,i){return void 0===i.value?memProp(e,o,i,t):memMethod(e,o,i,t)}}Object.defineProperty(exports,"__esModule",{value:!0});var ATOM_STATUS_DESTROYED=0,ATOM_STATUS_OBSOLETE=1,ATOM_STATUS_CHECKING=2,ATOM_STATUS_PULLING=3,ATOM_STATUS_ACTUAL=4,catchedId=Symbol("lom_atom_catched"),_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},inheritsLoose=function(t,e){t.prototype=Object.create(e.prototype),t.prototype.constructor=t,t.__proto__=e},throwOnAccess={get:function(t){throw t.valueOf()},ownKeys:function(t){throw t.valueOf()}},AtomWait=function(t){function e(){var e,o=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"Wait...";return e=t.call(this,o)||this,e.__proto__=new.target.prototype,e[catchedId]=!0,e}return inheritsLoose(e,t),e}(Error),Atom=function(){function t(t,e,o,i,n,s){this.status=ATOM_STATUS_OBSOLETE,this.cached=void 0,this._masters=null,this._slaves=null,this.field=t,this.key=i,this.host=e,this.isComponent=s||!1,this._normalize=n||defaultNormalize,this._context=o}return t.prototype.destroyed=function(t){return void 0===t?this.status===ATOM_STATUS_DESTROYED:!!t&&(this.status!==ATOM_STATUS_DESTROYED&&(this._masters&&(this._masters.forEach(disleadThis,this),this._masters=null),this._checkSlaves(),void 0!==this.host&&this._context.destroyHost(this),this.cached=void 0,this.status=ATOM_STATUS_DESTROYED,this.key=void 0),!0)},t.prototype.get=function(t){t||this._context.force?(this._context.force=!1,this._pullPush(void 0,!0)):this.actualize();var e=this._context.last;if(e&&(!e.isComponent||!this.isComponent)){var o=this._slaves;o||(this._context.unreap(this),o=this._slaves=new Set),o.add(e),e.addMaster(this)}return this.cached},t.prototype.set=function(t,e){var o=this._normalize(t,this.cached);if(this.cached===o)return o;if(void 0===o)return this.cached;var i=this._context;if(!e&&!i.force||o instanceof Error){i.force=!1,this.status=ATOM_STATUS_ACTUAL;var n=this.cached;this.cached=o instanceof Error?createMock(o):o,this._slaves&&this._slaves.forEach(obsoleteSlave),i.newValue(this,n,o),i.beginTransaction(),i.endTransaction()}else this.obsolete(),this.actualize(o);return this.cached},t.prototype.actualize=function(t){this.status!==ATOM_STATUS_ACTUAL&&(this.status===ATOM_STATUS_CHECKING&&(this._masters&&this._masters.forEach(actualizeMaster,this),this.status===ATOM_STATUS_CHECKING&&(this.status=ATOM_STATUS_ACTUAL)),this.status!==ATOM_STATUS_ACTUAL&&this._pullPush(t))},t.prototype._pullPush=function(t,e){this._masters&&this._masters.forEach(disleadThis,this);var o=void 0;this.status=ATOM_STATUS_PULLING;var i=this._context,n=i.last;i.last=this,i.beginTransaction();try{o=this._normalize(void 0===this.key?this.host[this.field](t,e,this.cached):this.host[this.field](this.key,t,e,this.cached),this.cached)}catch(t){void 0===t[catchedId]&&(t[catchedId]=!0,console.error(t.stack||t)),o=createMock(t)}if(i.last=n,this.status=ATOM_STATUS_ACTUAL,void 0!==o&&this.cached!==o){var s=this.cached;this.cached=o,this._slaves&&this._slaves.forEach(obsoleteSlave),this._context.newValue(this,s,o),i.endTransaction()}else i.endTransaction(!0)},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}(),lastId=0,Context=function(){function t(){this.last=null,this.force=!1,this._logger=null,this._updating=[],this._reaping=new Set,this._atomMap=new WeakMap,this._pendCount=0}return t.prototype.getAtom=function(t,e,o,i,n){var s=void 0===o?t:getKey(o),r=this._atomMap.get(e);void 0===r&&(r=new Map,this._atomMap.set(e,r));var a=r.get(s);return void 0===a&&(a=new Atom(t,e,this,o,i,n),r.set(s,a)),a},t.prototype.destroyHost=function(t){var e=t.host,o=this._atomMap.get(e);void 0!==o&&(void 0!==e._destroyProp&&e._destroyProp(void 0===t.key?t.field:t.key,t.cached),o.delete(void 0===t.key?t.field:getKey(t.key)),0===o.size&&(void 0!==e._destroy&&e._destroy(),this._atomMap.delete(e)))},t.prototype.setLogger=function(t){this._logger=t},t.prototype.newValue=function(t,e,o){this._logger&&(o instanceof AtomWait?this._logger.pulling(t):o instanceof Error?this._logger.error(t,o):this._logger.newValue(t,e,o))},t.prototype.proposeToPull=function(t){this._updating.push(t)},t.prototype.proposeToReap=function(t){this._reaping.add(t)},t.prototype.unreap=function(t){this._reaping.delete(t)},t.prototype.beginTransaction=function(){this._pendCount++},t.prototype.run=function(){this.beginTransaction(),this.endTransaction()},t.prototype.endTransaction=function(t){if(1===this._pendCount&&!0!==t){var e=this._reaping,o=this._updating,i=0;do{for(var n=o.length,s=i;s<n;s++){var r=o[s];e.has(r)||r.destroyed()||r.actualize()}i=n}while(o.length>i);for(o.length=0;e.size>0;)e.forEach(reap)}this._pendCount--},t}(),defaultContext=new Context;mem.Wait=AtomWait,mem.key=memkey,mem.detached=detached,exports.Atom=Atom,exports.mem=mem,exports.memkey=memkey,exports.detached=detached,exports.force=force,exports.action=action,exports.defaultContext=defaultContext; | ||
"use strict";function createMock(t){return new Proxy(t,throwOnAccess)}function defaultNormalize(t,e){if(t===e)return t;if(t instanceof Array&&e instanceof Array&&t.length===e.length){for(var o=0;o<t.length;o++)if(t[o]!==e[o])return t;return e}return t}function checkSlave(t){t.check()}function obsoleteSlave(t){t.obsolete()}function disleadThis(t){t.dislead(this)}function actualizeMaster(t){this.status===ATOM_STATUS_CHECKING&&t.actualize()}function reap(t,e,o){o.delete(t),t.slaves||t.destroyed(!0)}function getKey(t){return"string"==typeof t||"number"==typeof t?t:t?"function"==typeof t?(t.__id=t.__id||++lastId,t.__id):"object"===_typeof(t)?Object.keys(t).sort().map(function(e){return e+":"+JSON.stringify(t[e])}).join("."):JSON.stringify(t):0}function memMethod(t,e,o,i,n){var s=e+"$";if(void 0===o.value)throw new TypeError(e+" is not an function (next?: V)");return t[s]=o.value,{enumerable:o.enumerable,configurable:o.configurable,value:function(t,e){return defaultContext.getAtom(s,this,void 0,i,n).value(t,e)}}}function createGetSetHandler(t,e){return function(o){return void 0===o?t.call(this):(e.call(this,o),o)}}function createValueHandler(t){return function(e){return void 0===e&&void 0!==t?t.call(this):e}}function memProp(t,e,o,i){var n=e+"$";if(void 0===t[n]){t[n]=void 0===o.get&&void 0===o.set?createValueHandler(o.initializer):createGetSetHandler(o.get,o.set);return{enumerable:o.enumerable,configurable:o.configurable,get:function(){return defaultContext.getAtom(n,this,void 0,i).get()},set:function(t){defaultContext.getAtom(n,this,void 0,i).set(t)}}}}function memkeyProp(t,e,o,i){var n=o.value;if(void 0===n)throw new TypeError(e+" is not an function (rawKey: K, next?: V)");var s=e+"$";return t[s]=n,{enumerable:o.enumerable,configurable:o.configurable,value:function(t,e,o){return defaultContext.getAtom(s,this,t,i).value(e,o)}}}function memkey(){if(3===arguments.length)return memkeyProp(arguments[0],arguments[1],arguments[2]);var t=arguments[0];return function(e,o,i){return memkeyProp(e,o,i,t)}}function forceGet(){return defaultContext.force=!0,this}function force(t,e,o){return{enumerable:o.enumerable,configurable:o.configurable,get:forceGet}}function detached(t,e,o){return memMethod(t,e,o,void 0,!0)}function createActionMethod(t,e,o){function i(){var i=void 0;switch(o.beginTransaction(),arguments.length){case 0:i=t[e]();break;case 1:i=t[e](arguments[0]);break;case 2:i=t[e](arguments[0],arguments[1]);break;case 3:i=t[e](arguments[0],arguments[1],arguments[2]);break;case 4:i=t[e](arguments[0],arguments[1],arguments[2],arguments[3]);break;case 5:i=t[e](arguments[0],arguments[1],arguments[2],arguments[3],arguments[4]);break;default:i=t[e].apply(t,arguments)}return o.endTransaction(),i}return i.displayName=e,i}function createActionFn(t,e,o){function i(){var e=void 0;switch(o.beginTransaction(),arguments.length){case 0:e=t();break;case 1:e=t(arguments[0]);break;case 2:e=t(arguments[0],arguments[1]);break;case 3:e=t(arguments[0],arguments[1],arguments[2]);break;case 4:e=t(arguments[0],arguments[1],arguments[2],arguments[3]);break;case 5:e=t(arguments[0],arguments[1],arguments[2],arguments[3],arguments[4]);break;default:e=t.apply(null,arguments)}return o.endTransaction(),e}return i.displayName=e||t.displayName||t.name,i}function actionMethod(t,e,o,i){var n=e+"$";if(void 0===o.value)throw new TypeError(e+" is not an function (next?: V)");t[n]=o.value;var s=!1;return{enumerable:o.enumerable,configurable:o.configurable,get:function(){if(s)return this[n];s=!0;var t=createActionMethod(this,n,i);return Object.defineProperty(this,e,{configurable:!0,get:function(){return t}}),s=!1,t}}}function action(){return 3===arguments.length?actionMethod(arguments[0],arguments[1],arguments[2],defaultContext):createActionFn(arguments[0],arguments[1],defaultContext)}function mem(){if(3===arguments.length)return void 0===arguments[2].value?memProp(arguments[0],arguments[1],arguments[2]):memMethod(arguments[0],arguments[1],arguments[2]);var t=arguments[0];return function(e,o,i){return void 0===i.value?memProp(e,o,i,t):memMethod(e,o,i,t)}}Object.defineProperty(exports,"__esModule",{value:!0});var ATOM_STATUS_DESTROYED=0,ATOM_STATUS_OBSOLETE=1,ATOM_STATUS_CHECKING=2,ATOM_STATUS_PULLING=3,ATOM_STATUS_ACTUAL=4,catchedId=Symbol("lom_atom_catched"),_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},inheritsLoose=function(t,e){t.prototype=Object.create(e.prototype),t.prototype.constructor=t,t.__proto__=e},throwOnAccess={get:function(t){throw t.valueOf()},ownKeys:function(t){throw t.valueOf()}},AtomWait=function(t){function e(){var e,o=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"Wait...";return e=t.call(this,o)||this,e.__proto__=new.target.prototype,e[catchedId]=!0,e}return inheritsLoose(e,t),e}(Error),Atom=function(){function t(t,e,o,i,n,s){this.status=ATOM_STATUS_OBSOLETE,this.cached=void 0,this._masters=null,this._slaves=null,this.field=t,this.key=i,this.host=e,this.isComponent=s||!1,this._normalize=n||defaultNormalize,this._context=o}return t.prototype.destroyed=function(t){return void 0===t?this.status===ATOM_STATUS_DESTROYED:!!t&&(this.status!==ATOM_STATUS_DESTROYED&&(this._masters&&(this._masters.forEach(disleadThis,this),this._masters=null),this._checkSlaves(),void 0!==this.host&&this._context.destroyHost(this),this.cached=void 0,this.status=ATOM_STATUS_DESTROYED,this.key=void 0),!0)},t.prototype.get=function(t){t||this._context.force?(this._context.force=!1,this._pullPush(void 0,!0)):this.actualize();var e=this._context.last;if(e&&(!e.isComponent||!this.isComponent)){var o=this._slaves;o||(this._context.unreap(this),o=this._slaves=new Set),o.add(e),e.addMaster(this)}return this.cached},t.prototype.set=function(t,e){var o=this._normalize(t,this.cached);if(this.cached===o)return o;if(void 0===o)return this.cached;var i=this._context;if(!e&&!i.force||o instanceof Error){i.force=!1,this.status=ATOM_STATUS_ACTUAL;var n=this.cached;this.cached=o instanceof Error?createMock(o):o,this._slaves&&this._slaves.forEach(obsoleteSlave),i.newValue(this,n,o)}else this.obsolete(),this.actualize(o);return this.cached},t.prototype.actualize=function(t){this.status!==ATOM_STATUS_ACTUAL&&(this.status===ATOM_STATUS_CHECKING&&(this._masters&&this._masters.forEach(actualizeMaster,this),this.status===ATOM_STATUS_CHECKING&&(this.status=ATOM_STATUS_ACTUAL)),this.status!==ATOM_STATUS_ACTUAL&&this._pullPush(t))},t.prototype._pullPush=function(t,e){this._masters&&this._masters.forEach(disleadThis,this);var o=void 0;this.status=ATOM_STATUS_PULLING;var i=this._context,n=i.last;i.last=this;try{o=this._normalize(void 0===this.key?this.host[this.field](t,e,this.cached):this.host[this.field](this.key,t,e,this.cached),this.cached)}catch(t){void 0===t[catchedId]&&(t[catchedId]=!0,console.error(t.stack||t)),o=createMock(t)}if(i.last=n,this.status=ATOM_STATUS_ACTUAL,void 0!==o&&this.cached!==o){var s=this.cached;this.cached=o,this._slaves&&this._slaves.forEach(obsoleteSlave),this._context.newValue(this,s,o)}},t.prototype.dislead=function(t){var e=this._slaves;e&&(1===e.size?(this._slaves=null,this._context.proposeToReap(this)):e.delete(t))},t.prototype._checkSlaves=function(){this._slaves?this._slaves.forEach(checkSlave):this._context.proposeToPull(this)},t.prototype.check=function(){this.status===ATOM_STATUS_ACTUAL&&(this.status=ATOM_STATUS_CHECKING,this._checkSlaves())},t.prototype.obsolete=function(){this.status!==ATOM_STATUS_OBSOLETE&&(this.status=ATOM_STATUS_OBSOLETE,this._checkSlaves())},t.prototype.addMaster=function(t){this._masters||(this._masters=new Set),this._masters.add(t)},t.prototype.value=function(t,e){return void 0===t?this.get(e):this.set(t,e)},t}(),scheduleNative="function"==typeof requestAnimationFrame?function(t){return requestAnimationFrame(t)}:function(t){return setTimeout(t,16)},lastId=0,Context=function(){function t(){var t=this;this.last=null,this.force=!1,this._logger=null,this._updating=[],this._reaping=new Set,this._atomMap=new WeakMap,this._scheduled=!1,this.__run=function(){t._scheduled&&(t._scheduled=!1,t._run())},this._start=0,this._pendCount=0}return t.prototype.getAtom=function(t,e,o,i,n){var s=void 0===o?t:getKey(o),r=this._atomMap.get(e);void 0===r&&(r=new Map,this._atomMap.set(e,r));var a=r.get(s);return void 0===a&&(a=new Atom(t,e,this,o,i,n),r.set(s,a)),a},t.prototype.destroyHost=function(t){var e=t.host,o=this._atomMap.get(e);void 0!==o&&(void 0!==e._destroyProp&&e._destroyProp(void 0===t.key?t.field:t.key,t.cached),o.delete(void 0===t.key?t.field:getKey(t.key)),0===o.size&&(void 0!==e._destroy&&e._destroy(),this._atomMap.delete(e)))},t.prototype.setLogger=function(t){this._logger=t},t.prototype.newValue=function(t,e,o){this._logger&&(o instanceof AtomWait?this._logger.pulling(t):o instanceof Error?this._logger.error(t,o):this._logger.newValue(t,e,o))},t.prototype.proposeToPull=function(t){this._updating.push(t),this._schedule()},t.prototype.proposeToReap=function(t){this._reaping.add(t),this._schedule()},t.prototype.unreap=function(t){this._reaping.delete(t)},t.prototype._schedule=function(){this._scheduled||(scheduleNative(this.__run),this._scheduled=!0)},t.prototype._run=function(){this._schedule();var t=this._reaping,e=this._updating,o=this._start;do{for(var i=e.length,n=o;n<i;n++){this._start=n;var s=e[n];t.has(s)||s.destroyed()||s.actualize()}o=i}while(e.length>o);for(e.length=0,this._start=0;t.size>0;)t.forEach(reap);this._scheduled=!1,this._pendCount=0},t.prototype.beginTransaction=function(){this._pendCount++},t.prototype.endTransaction=function(){1===this._pendCount?this._run():this._pendCount--},t}(),defaultContext=new Context;mem.Wait=AtomWait,mem.key=memkey,mem.detached=detached,exports.Atom=Atom,exports.mem=mem,exports.memkey=memkey,exports.detached=detached,exports.force=force,exports.action=action,exports.defaultContext=defaultContext; | ||
//# sourceMappingURL=lom_atom.js.map |
@@ -1,2 +0,2 @@ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(t.lom_atom={})}(this,function(t){"use strict";function e(t){return new Proxy(t,T)}function i(t,e){if(t===e)return t;if(t instanceof Array&&e instanceof Array&&t.length===e.length){for(var i=0;i<t.length;i++)if(t[i]!==e[i])return t;return e}return t}function n(t){t.check()}function o(t){t.obsolete()}function s(t){t.dislead(this)}function r(t){this.status===b&&t.actualize()}function a(t,e,i){i.delete(t),t.slaves||t.destroyed(!0)}function u(t){return"string"==typeof t||"number"==typeof t?t:t?"function"==typeof t?(t.__id=t.__id||++z,t.__id):"object"===w(t)?Object.keys(t).sort().map(function(e){return e+":"+JSON.stringify(t[e])}).join("."):JSON.stringify(t):0}function c(t,e,i,n,o){var s=e+"$";if(void 0===i.value)throw new TypeError(e+" is not an function (next?: V)");return t[s]=i.value,{enumerable:i.enumerable,configurable:i.configurable,value:function(t,e){return M.getAtom(s,this,void 0,n,o).value(t,e)}}}function h(t,e){return function(i){return void 0===i?t.call(this):(e.call(this,i),i)}}function l(t){return function(e){return void 0===e&&void 0!==t?t.call(this):e}}function f(t,e,i,n){var o=e+"$";if(void 0===t[o]){t[o]=void 0===i.get&&void 0===i.set?l(i.initializer):h(i.get,i.set);return{enumerable:i.enumerable,configurable:i.configurable,get:function(){return M.getAtom(o,this,void 0,n).get()},set:function(t){M.getAtom(o,this,void 0,n).set(t)}}}}function d(t,e,i,n){var o=i.value;if(void 0===o)throw new TypeError(e+" is not an function (rawKey: K, next?: V)");var s=e+"$";return t[s]=o,{enumerable:i.enumerable,configurable:i.configurable,value:function(t,e,i){return M.getAtom(s,this,t,n).value(e,i)}}}function p(){if(3===arguments.length)return d(arguments[0],arguments[1],arguments[2]);var t=arguments[0];return function(e,i,n){return d(e,i,n,t)}}function v(){return M.force=!0,this}function _(t,e,i){return c(t,e,i,void 0,!0)}function y(t,e){function i(){var i=void 0;switch(M.beginTransaction(),arguments.length){case 0:i=t[e]();break;case 1:i=t[e](arguments[0]);break;case 2:i=t[e](arguments[0],arguments[1]);break;case 3:i=t[e](arguments[0],arguments[1],arguments[2]);break;case 4:i=t[e](arguments[0],arguments[1],arguments[2],arguments[3]);break;case 5:i=t[e](arguments[0],arguments[1],arguments[2],arguments[3],arguments[4]);break;default:i=t[e].apply(t,arguments)}return M.endTransaction(),i}return i.displayName=e,i}function g(){if(3===arguments.length)return void 0===arguments[2].value?f(arguments[0],arguments[1],arguments[2]):c(arguments[0],arguments[1],arguments[2]);var t=arguments[0];return function(e,i,n){return void 0===n.value?f(e,i,n,t):c(e,i,n,t)}}var m=1,b=2,k=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},T={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[k]=!0,e}return x(e,t),e}(Error),E=function(){function t(t,e,n,o,s,r){this.status=m,this.cached=void 0,this._masters=null,this._slaves=null,this.field=t,this.key=o,this.host=e,this.isComponent=r||!1,this._normalize=s||i,this._context=n}return t.prototype.destroyed=function(t){return void 0===t?0===this.status:!!t&&(0!==this.status&&(this._masters&&(this._masters.forEach(s,this),this._masters=null),this._checkSlaves(),void 0!==this.host&&this._context.destroyHost(this),this.cached=void 0,this.status=0,this.key=void 0),!0)},t.prototype.get=function(t){t||this._context.force?(this._context.force=!1,this._pullPush(void 0,!0)):this.actualize();var e=this._context.last;if(e&&(!e.isComponent||!this.isComponent)){var i=this._slaves;i||(this._context.unreap(this),i=this._slaves=new Set),i.add(e),e.addMaster(this)}return this.cached},t.prototype.set=function(t,i){var n=this._normalize(t,this.cached);if(this.cached===n)return n;if(void 0===n)return this.cached;var s=this._context;if(!i&&!s.force||n instanceof Error){s.force=!1,this.status=4;var r=this.cached;this.cached=n instanceof Error?e(n):n,this._slaves&&this._slaves.forEach(o),s.newValue(this,r,n),s.beginTransaction(),s.endTransaction()}else this.obsolete(),this.actualize(n);return this.cached},t.prototype.actualize=function(t){4!==this.status&&(this.status===b&&(this._masters&&this._masters.forEach(r,this),this.status===b&&(this.status=4)),4!==this.status&&this._pullPush(t))},t.prototype._pullPush=function(t,i){this._masters&&this._masters.forEach(s,this);var n=void 0;this.status=3;var r=this._context,a=r.last;r.last=this,r.beginTransaction();try{n=this._normalize(void 0===this.key?this.host[this.field](t,i,this.cached):this.host[this.field](this.key,t,i,this.cached),this.cached)}catch(t){void 0===t[k]&&(t[k]=!0,console.error(t.stack||t)),n=e(t)}if(r.last=a,this.status=4,void 0!==n&&this.cached!==n){var u=this.cached;this.cached=n,this._slaves&&this._slaves.forEach(o),this._context.newValue(this,u,n),r.endTransaction()}else r.endTransaction(!0)},t.prototype.dislead=function(t){var e=this._slaves;e&&(1===e.size?(this._slaves=null,this._context.proposeToReap(this)):e.delete(t))},t.prototype._checkSlaves=function(){this._slaves?this._slaves.forEach(n):this._context.proposeToPull(this)},t.prototype.check=function(){4===this.status&&(this.status=b,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=0,M=new(function(){function t(){this.last=null,this.force=!1,this._logger=null,this._updating=[],this._reaping=new Set,this._atomMap=new WeakMap,this._pendCount=0}return t.prototype.getAtom=function(t,e,i,n,o){var s=void 0===i?t:u(i),r=this._atomMap.get(e);void 0===r&&(r=new Map,this._atomMap.set(e,r));var a=r.get(s);return void 0===a&&(a=new E(t,e,this,i,n,o),r.set(s,a)),a},t.prototype.destroyHost=function(t){var e=t.host,i=this._atomMap.get(e);void 0!==i&&(void 0!==e._destroyProp&&e._destroyProp(void 0===t.key?t.field:t.key,t.cached),i.delete(void 0===t.key?t.field:u(t.key)),0===i.size&&(void 0!==e._destroy&&e._destroy(),this._atomMap.delete(e)))},t.prototype.setLogger=function(t){this._logger=t},t.prototype.newValue=function(t,e,i){this._logger&&(i instanceof 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)},t.prototype.proposeToReap=function(t){this._reaping.add(t)},t.prototype.unreap=function(t){this._reaping.delete(t)},t.prototype.beginTransaction=function(){this._pendCount++},t.prototype.run=function(){this.beginTransaction(),this.endTransaction()},t.prototype.endTransaction=function(t){if(1===this._pendCount&&!0!==t){var e=this._reaping,i=this._updating,n=0;do{for(var o=i.length,s=n;s<o;s++){var r=i[s];e.has(r)||r.destroyed()||r.actualize()}n=o}while(i.length>n);for(i.length=0;e.size>0;)e.forEach(a)}this._pendCount--},t}());g.Wait=S,g.key=p,g.detached=_,t.Atom=E,t.mem=g,t.memkey=p,t.detached=_,t.force=function(t,e,i){return{enumerable:i.enumerable,configurable:i.configurable,get:v}},t.action=function(t,e,i){var n=e+"$";if(void 0===i.value)throw new TypeError(e+" is not an function (next?: V)");t[n]=i.value;var o=!1;return{enumerable:i.enumerable,configurable:i.configurable,get:function(){if(o)return this[n];o=!0;var t=y(this,n);return Object.defineProperty(this,e,{configurable:!0,get:function(){return t}}),o=!1,t}}},t.defaultContext=M,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,T)}function i(t,e){if(t===e)return t;if(t instanceof Array&&e instanceof Array&&t.length===e.length){for(var i=0;i<t.length;i++)if(t[i]!==e[i])return t;return e}return t}function n(t){t.check()}function o(t){t.obsolete()}function s(t){t.dislead(this)}function r(t){this.status===w&&t.actualize()}function a(t,e,i){i.delete(t),t.slaves||t.destroyed(!0)}function u(t){return"string"==typeof t||"number"==typeof t?t:t?"function"==typeof t?(t.__id=t.__id||++P,t.__id):"object"===S(t)?Object.keys(t).sort().map(function(e){return e+":"+JSON.stringify(t[e])}).join("."):JSON.stringify(t):0}function h(t,e,i,n,o){var s=e+"$";if(void 0===i.value)throw new TypeError(e+" is not an function (next?: V)");return t[s]=i.value,{enumerable:i.enumerable,configurable:i.configurable,value:function(t,e){return C.getAtom(s,this,void 0,n,o).value(t,e)}}}function c(t,e){return function(i){return void 0===i?t.call(this):(e.call(this,i),i)}}function l(t){return function(e){return void 0===e&&void 0!==t?t.call(this):e}}function f(t,e,i,n){var o=e+"$";if(void 0===t[o]){t[o]=void 0===i.get&&void 0===i.set?l(i.initializer):c(i.get,i.set);return{enumerable:i.enumerable,configurable:i.configurable,get:function(){return C.getAtom(o,this,void 0,n).get()},set:function(t){C.getAtom(o,this,void 0,n).set(t)}}}}function d(t,e,i,n){var o=i.value;if(void 0===o)throw new TypeError(e+" is not an function (rawKey: K, next?: V)");var s=e+"$";return t[s]=o,{enumerable:i.enumerable,configurable:i.configurable,value:function(t,e,i){return C.getAtom(s,this,t,n).value(e,i)}}}function p(){if(3===arguments.length)return d(arguments[0],arguments[1],arguments[2]);var t=arguments[0];return function(e,i,n){return d(e,i,n,t)}}function _(){return C.force=!0,this}function v(t,e,i){return h(t,e,i,void 0,!0)}function y(t,e,i){function n(){var n=void 0;switch(i.beginTransaction(),arguments.length){case 0:n=t[e]();break;case 1:n=t[e](arguments[0]);break;case 2:n=t[e](arguments[0],arguments[1]);break;case 3:n=t[e](arguments[0],arguments[1],arguments[2]);break;case 4:n=t[e](arguments[0],arguments[1],arguments[2],arguments[3]);break;case 5:n=t[e](arguments[0],arguments[1],arguments[2],arguments[3],arguments[4]);break;default:n=t[e].apply(t,arguments)}return i.endTransaction(),n}return n.displayName=e,n}function g(t,e,i){function n(){var e=void 0;switch(i.beginTransaction(),arguments.length){case 0:e=t();break;case 1:e=t(arguments[0]);break;case 2:e=t(arguments[0],arguments[1]);break;case 3:e=t(arguments[0],arguments[1],arguments[2]);break;case 4:e=t(arguments[0],arguments[1],arguments[2],arguments[3]);break;case 5:e=t(arguments[0],arguments[1],arguments[2],arguments[3],arguments[4]);break;default:e=t.apply(null,arguments)}return i.endTransaction(),e}return n.displayName=e||t.displayName||t.name,n}function m(t,e,i,n){var o=e+"$";if(void 0===i.value)throw new TypeError(e+" is not an function (next?: V)");t[o]=i.value;var s=!1;return{enumerable:i.enumerable,configurable:i.configurable,get:function(){if(s)return this[o];s=!0;var t=y(this,o,n);return Object.defineProperty(this,e,{configurable:!0,get:function(){return t}}),s=!1,t}}}function b(){if(3===arguments.length)return void 0===arguments[2].value?f(arguments[0],arguments[1],arguments[2]):h(arguments[0],arguments[1],arguments[2]);var t=arguments[0];return function(e,i,n){return void 0===n.value?f(e,i,n,t):h(e,i,n,t)}}var k=1,w=2,x=Symbol("lom_atom_catched"),S="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},E=function(t,e){t.prototype=Object.create(e.prototype),t.prototype.constructor=t,t.__proto__=e},T={get:function(t){throw t.valueOf()},ownKeys:function(t){throw t.valueOf()}},z=function(t){function e(){var e,i=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"Wait...";return e=t.call(this,i)||this,e.__proto__=new.target.prototype,e[x]=!0,e}return E(e,t),e}(Error),A=function(){function t(t,e,n,o,s,r){this.status=k,this.cached=void 0,this._masters=null,this._slaves=null,this.field=t,this.key=o,this.host=e,this.isComponent=r||!1,this._normalize=s||i,this._context=n}return t.prototype.destroyed=function(t){return void 0===t?0===this.status:!!t&&(0!==this.status&&(this._masters&&(this._masters.forEach(s,this),this._masters=null),this._checkSlaves(),void 0!==this.host&&this._context.destroyHost(this),this.cached=void 0,this.status=0,this.key=void 0),!0)},t.prototype.get=function(t){t||this._context.force?(this._context.force=!1,this._pullPush(void 0,!0)):this.actualize();var e=this._context.last;if(e&&(!e.isComponent||!this.isComponent)){var i=this._slaves;i||(this._context.unreap(this),i=this._slaves=new Set),i.add(e),e.addMaster(this)}return this.cached},t.prototype.set=function(t,i){var n=this._normalize(t,this.cached);if(this.cached===n)return n;if(void 0===n)return this.cached;var s=this._context;if(!i&&!s.force||n instanceof Error){s.force=!1,this.status=4;var r=this.cached;this.cached=n instanceof Error?e(n):n,this._slaves&&this._slaves.forEach(o),s.newValue(this,r,n)}else this.obsolete(),this.actualize(n);return this.cached},t.prototype.actualize=function(t){4!==this.status&&(this.status===w&&(this._masters&&this._masters.forEach(r,this),this.status===w&&(this.status=4)),4!==this.status&&this._pullPush(t))},t.prototype._pullPush=function(t,i){this._masters&&this._masters.forEach(s,this);var n=void 0;this.status=3;var r=this._context,a=r.last;r.last=this;try{n=this._normalize(void 0===this.key?this.host[this.field](t,i,this.cached):this.host[this.field](this.key,t,i,this.cached),this.cached)}catch(t){void 0===t[x]&&(t[x]=!0,console.error(t.stack||t)),n=e(t)}if(r.last=a,this.status=4,void 0!==n&&this.cached!==n){var u=this.cached;this.cached=n,this._slaves&&this._slaves.forEach(o),this._context.newValue(this,u,n)}},t.prototype.dislead=function(t){var e=this._slaves;e&&(1===e.size?(this._slaves=null,this._context.proposeToReap(this)):e.delete(t))},t.prototype._checkSlaves=function(){this._slaves?this._slaves.forEach(n):this._context.proposeToPull(this)},t.prototype.check=function(){4===this.status&&(this.status=w,this._checkSlaves())},t.prototype.obsolete=function(){this.status!==k&&(this.status=k,this._checkSlaves())},t.prototype.addMaster=function(t){this._masters||(this._masters=new Set),this._masters.add(t)},t.prototype.value=function(t,e){return void 0===t?this.get(e):this.set(t,e)},t}(),M="function"==typeof requestAnimationFrame?function(t){return requestAnimationFrame(t)}:function(t){return setTimeout(t,16)},P=0,C=new(function(){function t(){var t=this;this.last=null,this.force=!1,this._logger=null,this._updating=[],this._reaping=new Set,this._atomMap=new WeakMap,this._scheduled=!1,this.__run=function(){t._scheduled&&(t._scheduled=!1,t._run())},this._start=0,this._pendCount=0}return t.prototype.getAtom=function(t,e,i,n,o){var s=void 0===i?t:u(i),r=this._atomMap.get(e);void 0===r&&(r=new Map,this._atomMap.set(e,r));var a=r.get(s);return void 0===a&&(a=new A(t,e,this,i,n,o),r.set(s,a)),a},t.prototype.destroyHost=function(t){var e=t.host,i=this._atomMap.get(e);void 0!==i&&(void 0!==e._destroyProp&&e._destroyProp(void 0===t.key?t.field:t.key,t.cached),i.delete(void 0===t.key?t.field:u(t.key)),0===i.size&&(void 0!==e._destroy&&e._destroy(),this._atomMap.delete(e)))},t.prototype.setLogger=function(t){this._logger=t},t.prototype.newValue=function(t,e,i){this._logger&&(i instanceof z?this._logger.pulling(t):i instanceof Error?this._logger.error(t,i):this._logger.newValue(t,e,i))},t.prototype.proposeToPull=function(t){this._updating.push(t),this._schedule()},t.prototype.proposeToReap=function(t){this._reaping.add(t),this._schedule()},t.prototype.unreap=function(t){this._reaping.delete(t)},t.prototype._schedule=function(){this._scheduled||(M(this.__run),this._scheduled=!0)},t.prototype._run=function(){this._schedule();var t=this._reaping,e=this._updating,i=this._start;do{for(var n=e.length,o=i;o<n;o++){this._start=o;var s=e[o];t.has(s)||s.destroyed()||s.actualize()}i=n}while(e.length>i);for(e.length=0,this._start=0;t.size>0;)t.forEach(a);this._scheduled=!1,this._pendCount=0},t.prototype.beginTransaction=function(){this._pendCount++},t.prototype.endTransaction=function(){1===this._pendCount?this._run():this._pendCount--},t}());b.Wait=z,b.key=p,b.detached=v,t.Atom=A,t.mem=b,t.memkey=p,t.detached=v,t.force=function(t,e,i){return{enumerable:i.enumerable,configurable:i.configurable,get:_}},t.action=function(){return 3===arguments.length?m(arguments[0],arguments[1],arguments[2],C):g(arguments[0],arguments[1],C)},t.defaultContext=C,Object.defineProperty(t,"__esModule",{value:!0})}); | ||
//# sourceMappingURL=lom_atom.umd.js.map |
{ | ||
"name": "lom_atom", | ||
"version": "1.0.6", | ||
"version": "1.0.7", | ||
"description": "Observable state management", | ||
@@ -5,0 +5,0 @@ "publishConfig": { |
@@ -51,3 +51,2 @@ // @flow | ||
constructor( | ||
@@ -143,4 +142,2 @@ field: string, | ||
context.newValue(this, oldValue, normalized) | ||
context.beginTransaction() | ||
context.endTransaction() | ||
} else { | ||
@@ -185,3 +182,2 @@ this.obsolete() | ||
context.last = this | ||
context.beginTransaction() | ||
try { | ||
@@ -213,5 +209,2 @@ newValue = this._normalize( | ||
this._context.newValue(this, oldValue, newValue) | ||
context.endTransaction() | ||
} else { | ||
context.endTransaction(true) | ||
} | ||
@@ -218,0 +211,0 @@ } |
@@ -15,2 +15,6 @@ // @flow | ||
const scheduleNative: (handler: () => void) => number = typeof requestAnimationFrame == 'function' | ||
? (handler: () => void) => requestAnimationFrame(handler) | ||
: (handler: () => void) => setTimeout(handler, 16) | ||
function reap(atom: IAtomInt, key: IAtomInt, reaping: Set<IAtomInt>) { | ||
@@ -52,2 +56,3 @@ reaping.delete(atom) | ||
_atomMap: WeakMap<IAtomHost, Map<string | number, IAtom<any>>> = new WeakMap() | ||
_scheduled = false | ||
@@ -123,2 +128,3 @@ getAtom<V>( | ||
this._updating.push(atom) | ||
this._schedule() | ||
} | ||
@@ -129,2 +135,3 @@ | ||
this._reaping.add(atom) | ||
this._schedule() | ||
} | ||
@@ -137,40 +144,61 @@ | ||
_pendCount = 0 | ||
beginTransaction() { | ||
this._pendCount++ | ||
_schedule() { | ||
if (!this._scheduled) { | ||
scheduleNative(this.__run) | ||
this._scheduled = true | ||
} | ||
} | ||
run() { | ||
this.beginTransaction() | ||
this.endTransaction() | ||
__run = () => { | ||
if (this._scheduled) { | ||
this._scheduled = false | ||
this._run() | ||
} | ||
} | ||
endTransaction(noUpdate?: boolean) { | ||
if (this._pendCount === 1 && noUpdate !== true) { | ||
const reaping = this._reaping | ||
const updating = this._updating | ||
let start = 0 | ||
do { | ||
const end = updating.length | ||
_start = 0 | ||
for (let i = start; i < end; i++) { | ||
const atom: IAtomInt = updating[i] | ||
if (!reaping.has(atom) && !atom.destroyed()) { | ||
atom.actualize() | ||
} | ||
_run() { | ||
this._schedule() | ||
const reaping = this._reaping | ||
const updating = this._updating | ||
let start = this._start | ||
do { | ||
const end = updating.length | ||
for (let i = start; i < end; i++) { | ||
this._start = i | ||
const atom: IAtomInt = updating[i] | ||
if (!reaping.has(atom) && !atom.destroyed()) { | ||
atom.actualize() | ||
} | ||
} | ||
start = end | ||
} while (updating.length > start) | ||
updating.length = 0 | ||
start = end | ||
} while (updating.length > start) | ||
updating.length = 0 | ||
this._start = 0 | ||
while (reaping.size > 0) { | ||
reaping.forEach(reap) | ||
} | ||
while (reaping.size > 0) { | ||
reaping.forEach(reap) | ||
} | ||
this._pendCount-- | ||
this._scheduled = false | ||
this._pendCount = 0 | ||
} | ||
_pendCount = 0 | ||
beginTransaction() { | ||
this._pendCount++ | ||
} | ||
endTransaction() { | ||
if (this._pendCount === 1) { | ||
this._run() | ||
} else { | ||
this._pendCount-- | ||
} | ||
} | ||
} | ||
export const defaultContext = new Context() |
@@ -33,4 +33,3 @@ // @flow | ||
beginTransaction(): void; | ||
run(): void; | ||
endTransaction(noUpdate?: boolean): void; | ||
endTransaction(): void; | ||
} | ||
@@ -37,0 +36,0 @@ |
// @flow | ||
import type {IAtom, IAtomHandler, IAtomHost, INormalize} from './interfaces' | ||
import type {IAtom, IAtomHandler, IAtomHost, INormalize, IContext} from './interfaces' | ||
import {defaultContext} from './Context' | ||
@@ -180,6 +180,6 @@ import {AtomWait} from './utils' | ||
function createAction(t: Object, hk: string): (...args: any[]) => any { | ||
function createActionMethod(t: Object, hk: string, context: IContext): (...args: any[]) => any { | ||
function action() { | ||
let result: mixed | void | ||
defaultContext.beginTransaction() | ||
context.beginTransaction() | ||
switch (arguments.length) { | ||
@@ -194,3 +194,3 @@ case 0: result = t[hk](); break | ||
} | ||
defaultContext.endTransaction() | ||
context.endTransaction() | ||
@@ -204,7 +204,29 @@ return result | ||
function createActionFn<F: Function>(fn: F, name?: string, context: IContext): F { | ||
function action(): any { | ||
let result: mixed | void | ||
context.beginTransaction() | ||
switch (arguments.length) { | ||
case 0: result = fn(); break | ||
case 1: result = fn(arguments[0]); break | ||
case 2: result = fn(arguments[0], arguments[1]); break | ||
case 3: result = fn(arguments[0], arguments[1], arguments[2]); break | ||
case 4: result = fn(arguments[0], arguments[1], arguments[2], arguments[3]); break | ||
case 5: result = fn(arguments[0], arguments[1], arguments[2], arguments[3], arguments[4]); break | ||
default: result = fn.apply(null, arguments) | ||
} | ||
context.endTransaction() | ||
export function action<V, P: Object>( | ||
return result | ||
} | ||
action.displayName = name || fn.displayName || fn.name | ||
return (action: any) | ||
} | ||
function actionMethod<V, P: Object>( | ||
proto: P, | ||
field: string, | ||
descr: TypedPropertyDescriptor<*> | ||
descr: TypedPropertyDescriptor<*>, | ||
context: IContext | ||
): TypedPropertyDescriptor<*> { | ||
@@ -226,3 +248,3 @@ const hk = `${field}$` | ||
definingProperty = true | ||
const actionFn = createAction(this, hk) | ||
const actionFn = createActionMethod(this, hk, context) | ||
Object.defineProperty(this, field, { | ||
@@ -241,2 +263,17 @@ configurable: true, | ||
declare function action<F: Function>(fn: F, name?: string): F | ||
declare function action( | ||
proto: Object, | ||
name: string, | ||
descr: TypedPropertyDescriptor<*> | ||
): TypedPropertyDescriptor<*> | ||
export function action() { | ||
if (arguments.length === 3) { | ||
return actionMethod(arguments[0], arguments[1], arguments[2], defaultContext) | ||
} | ||
return createActionFn(arguments[0], arguments[1], defaultContext) | ||
} | ||
declare function mem<V, P: Object>(normalize: INormalize<V>): () => IMemProp<V, P> | ||
@@ -243,0 +280,0 @@ declare function mem<V, P: Object>( |
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
186573
1651