Comparing version 3.0.17 to 3.0.18
@@ -5,2 +5,7 @@ # Change Log | ||
<a name="3.0.18"></a> | ||
## [3.0.18](https://github.com/zerkalica/lom_atom/compare/v3.0.17...v3.0.18) (2017-12-18) | ||
<a name="3.0.17"></a> | ||
@@ -7,0 +12,0 @@ ## [3.0.17](https://github.com/zerkalica/lom_atom/compare/v3.0.16...v3.0.17) (2017-12-09) |
@@ -85,2 +85,141 @@ function _defineProperties(target, props) { | ||
function reap(atom, key, reaping) { | ||
reaping.delete(atom); | ||
if (!atom.slaves) { | ||
atom.destructor(); | ||
} | ||
} | ||
var Context = | ||
/*#__PURE__*/ | ||
function () { | ||
function Context() { | ||
var _this = this; | ||
this.current = null; | ||
this._logger = undefined; | ||
this._updating = []; | ||
this._reaping = new Set(); | ||
this._scheduled = false; | ||
this._owners = new WeakMap(); | ||
this._run = function () { | ||
if (_this._scheduled) { | ||
_this._scheduled = false; | ||
_this.sync(); | ||
} | ||
}; | ||
this._start = 0; | ||
} | ||
var _proto = Context.prototype; | ||
_proto._destroyValue = function _destroyValue(atom, from) { | ||
if (this._owners.get(from) === atom) { | ||
try { | ||
from.destructor(); | ||
} catch (e) { | ||
console.error(e); | ||
if (this._logger) this._logger.error(atom, e); | ||
} | ||
this._owners.delete(from); | ||
} | ||
}; | ||
_proto.destroyHost = function destroyHost(atom) { | ||
this._destroyValue(atom, atom.current); | ||
if (this._logger !== undefined) { | ||
this._logger.onDestruct(atom); | ||
} | ||
}; | ||
_proto.setLogger = function setLogger(logger) { | ||
this._logger = logger; | ||
}; | ||
_proto.newValue = function newValue(atom, from, to) { | ||
this._destroyValue(atom, from); | ||
if (to && typeof to === 'object' && !(to instanceof Error) && typeof to.destructor === 'function') { | ||
this._owners.set(to, atom); | ||
} | ||
var logger = this._logger; | ||
if (logger !== undefined) { | ||
try { | ||
logger.newValue(atom, from, to); | ||
} catch (error) { | ||
console.error(error); | ||
logger.error(atom, error); | ||
} | ||
} | ||
}; | ||
_proto.proposeToPull = function proposeToPull(atom) { | ||
this._updating.push(atom); | ||
this._schedule(); | ||
}; | ||
_proto.proposeToReap = function proposeToReap(atom) { | ||
this._reaping.add(atom); | ||
this._schedule(); | ||
}; | ||
_proto.unreap = function unreap(atom) { | ||
this._reaping.delete(atom); | ||
}; | ||
_proto._schedule = function _schedule() { | ||
if (!this._scheduled) { | ||
scheduleNative(this._run); | ||
this._scheduled = true; | ||
} | ||
}; | ||
_proto.sync = function sync() { | ||
this._schedule(); | ||
var reaping = this._reaping; | ||
var updating = this._updating; | ||
var start = this._start; | ||
do { | ||
var end = updating.length; | ||
for (var i = start; i < end; i++) { | ||
this._start = i; // save progress, atom.actualize or destroyed can throw exception | ||
var atom = updating[i]; | ||
if (!reaping.has(atom) && atom.status !== ATOM_STATUS_DESTROYED) { | ||
atom.actualize(); | ||
} | ||
} | ||
start = end; | ||
} while (updating.length > start); | ||
updating.length = 0; | ||
this._start = 0; | ||
while (reaping.size > 0) { | ||
reaping.forEach(reap); | ||
} | ||
this._scheduled = false; | ||
}; | ||
return Context; | ||
}(); | ||
var defaultContext = new Context(); | ||
var handlers = new Map([[Array, function arrayHandler(target, source, stack) { | ||
@@ -154,2 +293,3 @@ var equal = target.length === source.length; | ||
var proxyId = Symbol('lom_err_proxy'); | ||
var Atom = (_temp = _class = | ||
@@ -161,2 +301,3 @@ /*#__PURE__*/ | ||
this._slaves = null; | ||
this._retry = undefined; | ||
this._keyHash = keyHash; | ||
@@ -257,3 +398,7 @@ this.key = key; | ||
if (current instanceof Error) { | ||
if (forceCache !== ATOM_FORCE_NONE) return proxify(current); | ||
if (forceCache !== ATOM_FORCE_NONE) { | ||
if (current[proxyId] === undefined) current[proxyId] = proxify(current); | ||
return current[proxyId]; | ||
} | ||
throw current; | ||
@@ -418,32 +563,68 @@ } | ||
function fastCallMethod(host, methodName, args) { | ||
try { | ||
switch (args.length) { | ||
case 0: | ||
return host[methodName](); | ||
function createAction(host, methodName) { | ||
function actionFn() { | ||
var args = arguments; | ||
case 1: | ||
return host[methodName](args[0]); | ||
try { | ||
switch (args.length) { | ||
case 0: | ||
return host[methodName](); | ||
case 2: | ||
return host[methodName](args[0], args[1]); | ||
case 1: | ||
return host[methodName](args[0]); | ||
case 3: | ||
return host[methodName](args[0], args[1], args[2]); | ||
case 2: | ||
return host[methodName](args[0], args[1]); | ||
case 4: | ||
return host[methodName](args[0], args[1], args[2], args[3]); | ||
case 3: | ||
return host[methodName](args[0], args[1], args[2]); | ||
case 5: | ||
return host[methodName](args[0], args[1], args[2], args[3], args[4]); | ||
case 4: | ||
return host[methodName](args[0], args[1], args[2], args[3]); | ||
default: | ||
return host[methodName].apply(host, args); | ||
case 5: | ||
return host[methodName](args[0], args[1], args[2], args[3], args[4]); | ||
default: | ||
return host[methodName].apply(host, args); | ||
} | ||
} catch (e) { | ||
if (!(e instanceof AtomWait)) throw e; | ||
} | ||
} catch (e) { | ||
if (!(e instanceof AtomWait)) throw e; | ||
} | ||
setFunctionName(actionFn, getId(host, methodName)); | ||
return actionFn; | ||
} | ||
function action(host, name, descr, defer) { | ||
function createSyncedAction(action) { | ||
function syncedAction() { | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
action(args); | ||
defaultContext.sync(); | ||
} | ||
syncedAction.displayName = action.displayName + "_synced"; | ||
return syncedAction; | ||
} | ||
function createDeferedAction(action) { | ||
function deferedAction() { | ||
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
scheduleNative(function () { | ||
return action(args); | ||
}); | ||
} | ||
deferedAction.displayName = action.displayName + "_defered"; | ||
return deferedAction; | ||
} | ||
function action(host, name, descr, defer, sync) { | ||
var hk = name + "$"; | ||
@@ -457,26 +638,9 @@ | ||
var definingProperty = false; | ||
var actionId = getId(host, name); | ||
return { | ||
enumerable: descr.enumerable, | ||
configurable: descr.configurable, | ||
configurable: true, | ||
get: function get() { | ||
var _this = this; | ||
if (definingProperty) return this[hk].bind(this); | ||
var actionFn = defer ? function () { | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
return scheduleNative(function () { | ||
return fastCallMethod(_this, hk, args); | ||
}); | ||
} : function () { | ||
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
return fastCallMethod(_this, hk, args); | ||
}; | ||
setFunctionName(actionFn, actionId); | ||
var action = createAction(this, hk); | ||
var actionFn = defer ? createDeferedAction(action) : sync ? createSyncedAction(action) : action; | ||
definingProperty = true; | ||
@@ -500,167 +664,8 @@ Object.defineProperty(this, name, { | ||
function reap(atom, key, reaping) { | ||
reaping.delete(atom); | ||
if (!atom.slaves) { | ||
atom.destructor(); | ||
} | ||
function actionSync(host, name, descr) { | ||
return action(host, name, descr, false, true); | ||
} | ||
var Context = | ||
/*#__PURE__*/ | ||
function () { | ||
function Context() { | ||
var _this = this; | ||
action.sync = actionSync; | ||
this.current = null; | ||
this._logger = undefined; | ||
this._updating = []; | ||
this._reaping = new Set(); | ||
this._scheduled = false; | ||
this._namespace = '$'; | ||
this._owners = new WeakMap(); | ||
this.__run = function () { | ||
if (_this._scheduled) { | ||
_this._scheduled = false; | ||
_this._run(); | ||
} | ||
}; | ||
this._start = 0; | ||
this._pendCount = 0; | ||
} | ||
var _proto = Context.prototype; | ||
_proto._destroyValue = function _destroyValue(atom, from) { | ||
if (this._owners.get(from) === atom) { | ||
try { | ||
from.destructor(); | ||
} catch (e) { | ||
console.error(e); | ||
if (this._logger) this._logger.error(atom, e); | ||
} | ||
this._owners.delete(from); | ||
} | ||
}; | ||
_proto.destroyHost = function destroyHost(atom) { | ||
this._destroyValue(atom, atom.current); | ||
if (this._logger !== undefined) { | ||
this._logger.onDestruct(atom); | ||
} | ||
}; | ||
_proto.setLogger = function setLogger(logger) { | ||
this._logger = logger; | ||
}; | ||
_proto.newValue = function newValue(atom, from, to) { | ||
this._destroyValue(atom, from); | ||
if (to && typeof to === 'object' && !(to instanceof Error) && typeof to.destructor === 'function') { | ||
this._owners.set(to, atom); | ||
} | ||
var logger = this._logger; | ||
if (logger !== undefined) { | ||
try { | ||
// if (!this._scheduled && this._logger !== undefined) { | ||
// this._logger.beginGroup(this._namespace) | ||
// } | ||
logger.newValue(atom, from, to); | ||
} catch (error) { | ||
console.error(error); | ||
logger.error(atom, error); | ||
} | ||
} | ||
}; | ||
_proto.proposeToPull = function proposeToPull(atom) { | ||
this._updating.push(atom); | ||
this._schedule(); | ||
}; | ||
_proto.proposeToReap = function proposeToReap(atom) { | ||
this._reaping.add(atom); | ||
this._schedule(); | ||
}; | ||
_proto.unreap = function unreap(atom) { | ||
this._reaping.delete(atom); | ||
}; | ||
_proto._schedule = function _schedule() { | ||
if (!this._scheduled) { | ||
scheduleNative(this.__run); | ||
this._scheduled = true; | ||
} | ||
}; | ||
_proto._run = function _run() { | ||
this._schedule(); | ||
var reaping = this._reaping; | ||
var updating = this._updating; | ||
var start = this._start; | ||
do { | ||
var end = updating.length; | ||
for (var i = start; i < end; i++) { | ||
this._start = i; // save progress, atom.actualize or destroyed can throw exception | ||
var atom = updating[i]; | ||
if (!reaping.has(atom) && atom.status !== ATOM_STATUS_DESTROYED) { | ||
atom.actualize(); | ||
} | ||
} | ||
start = end; | ||
} while (updating.length > start); | ||
updating.length = 0; | ||
this._start = 0; | ||
while (reaping.size > 0) { | ||
reaping.forEach(reap); | ||
} // if (this._logger !== undefined) { | ||
// this._logger.endGroup() | ||
// } | ||
this._scheduled = false; | ||
this._pendCount = 0; | ||
}; | ||
_proto.beginTransaction = function beginTransaction(namespace) { | ||
var result = this._namespace; | ||
this._namespace = namespace; | ||
this._pendCount++; | ||
return result; | ||
}; | ||
_proto.endTransaction = function endTransaction(prev) { | ||
this._namespace = prev; | ||
if (this._pendCount === 1) { | ||
this._run(); | ||
} else { | ||
this._pendCount--; | ||
} | ||
}; | ||
return Context; | ||
}(); | ||
var defaultContext = new Context(); | ||
function detached(proto, name, descr) { | ||
@@ -667,0 +672,0 @@ proto[name + "$"] = descr.value; |
@@ -89,2 +89,141 @@ 'use strict'; | ||
function reap(atom, key, reaping) { | ||
reaping.delete(atom); | ||
if (!atom.slaves) { | ||
atom.destructor(); | ||
} | ||
} | ||
var Context = | ||
/*#__PURE__*/ | ||
function () { | ||
function Context() { | ||
var _this = this; | ||
this.current = null; | ||
this._logger = undefined; | ||
this._updating = []; | ||
this._reaping = new Set(); | ||
this._scheduled = false; | ||
this._owners = new WeakMap(); | ||
this._run = function () { | ||
if (_this._scheduled) { | ||
_this._scheduled = false; | ||
_this.sync(); | ||
} | ||
}; | ||
this._start = 0; | ||
} | ||
var _proto = Context.prototype; | ||
_proto._destroyValue = function _destroyValue(atom, from) { | ||
if (this._owners.get(from) === atom) { | ||
try { | ||
from.destructor(); | ||
} catch (e) { | ||
console.error(e); | ||
if (this._logger) this._logger.error(atom, e); | ||
} | ||
this._owners.delete(from); | ||
} | ||
}; | ||
_proto.destroyHost = function destroyHost(atom) { | ||
this._destroyValue(atom, atom.current); | ||
if (this._logger !== undefined) { | ||
this._logger.onDestruct(atom); | ||
} | ||
}; | ||
_proto.setLogger = function setLogger(logger) { | ||
this._logger = logger; | ||
}; | ||
_proto.newValue = function newValue(atom, from, to) { | ||
this._destroyValue(atom, from); | ||
if (to && typeof to === 'object' && !(to instanceof Error) && typeof to.destructor === 'function') { | ||
this._owners.set(to, atom); | ||
} | ||
var logger = this._logger; | ||
if (logger !== undefined) { | ||
try { | ||
logger.newValue(atom, from, to); | ||
} catch (error) { | ||
console.error(error); | ||
logger.error(atom, error); | ||
} | ||
} | ||
}; | ||
_proto.proposeToPull = function proposeToPull(atom) { | ||
this._updating.push(atom); | ||
this._schedule(); | ||
}; | ||
_proto.proposeToReap = function proposeToReap(atom) { | ||
this._reaping.add(atom); | ||
this._schedule(); | ||
}; | ||
_proto.unreap = function unreap(atom) { | ||
this._reaping.delete(atom); | ||
}; | ||
_proto._schedule = function _schedule() { | ||
if (!this._scheduled) { | ||
scheduleNative(this._run); | ||
this._scheduled = true; | ||
} | ||
}; | ||
_proto.sync = function sync() { | ||
this._schedule(); | ||
var reaping = this._reaping; | ||
var updating = this._updating; | ||
var start = this._start; | ||
do { | ||
var end = updating.length; | ||
for (var i = start; i < end; i++) { | ||
this._start = i; // save progress, atom.actualize or destroyed can throw exception | ||
var atom = updating[i]; | ||
if (!reaping.has(atom) && atom.status !== ATOM_STATUS_DESTROYED) { | ||
atom.actualize(); | ||
} | ||
} | ||
start = end; | ||
} while (updating.length > start); | ||
updating.length = 0; | ||
this._start = 0; | ||
while (reaping.size > 0) { | ||
reaping.forEach(reap); | ||
} | ||
this._scheduled = false; | ||
}; | ||
return Context; | ||
}(); | ||
var defaultContext = new Context(); | ||
var handlers = new Map([[Array, function arrayHandler(target, source, stack) { | ||
@@ -158,2 +297,3 @@ var equal = target.length === source.length; | ||
var proxyId = Symbol('lom_err_proxy'); | ||
var Atom = (_temp = _class = | ||
@@ -165,2 +305,3 @@ /*#__PURE__*/ | ||
this._slaves = null; | ||
this._retry = undefined; | ||
this._keyHash = keyHash; | ||
@@ -261,3 +402,7 @@ this.key = key; | ||
if (current instanceof Error) { | ||
if (forceCache !== ATOM_FORCE_NONE) return proxify(current); | ||
if (forceCache !== ATOM_FORCE_NONE) { | ||
if (current[proxyId] === undefined) current[proxyId] = proxify(current); | ||
return current[proxyId]; | ||
} | ||
throw current; | ||
@@ -422,32 +567,68 @@ } | ||
function fastCallMethod(host, methodName, args) { | ||
try { | ||
switch (args.length) { | ||
case 0: | ||
return host[methodName](); | ||
function createAction(host, methodName) { | ||
function actionFn() { | ||
var args = arguments; | ||
case 1: | ||
return host[methodName](args[0]); | ||
try { | ||
switch (args.length) { | ||
case 0: | ||
return host[methodName](); | ||
case 2: | ||
return host[methodName](args[0], args[1]); | ||
case 1: | ||
return host[methodName](args[0]); | ||
case 3: | ||
return host[methodName](args[0], args[1], args[2]); | ||
case 2: | ||
return host[methodName](args[0], args[1]); | ||
case 4: | ||
return host[methodName](args[0], args[1], args[2], args[3]); | ||
case 3: | ||
return host[methodName](args[0], args[1], args[2]); | ||
case 5: | ||
return host[methodName](args[0], args[1], args[2], args[3], args[4]); | ||
case 4: | ||
return host[methodName](args[0], args[1], args[2], args[3]); | ||
default: | ||
return host[methodName].apply(host, args); | ||
case 5: | ||
return host[methodName](args[0], args[1], args[2], args[3], args[4]); | ||
default: | ||
return host[methodName].apply(host, args); | ||
} | ||
} catch (e) { | ||
if (!(e instanceof AtomWait)) throw e; | ||
} | ||
} catch (e) { | ||
if (!(e instanceof AtomWait)) throw e; | ||
} | ||
setFunctionName(actionFn, getId(host, methodName)); | ||
return actionFn; | ||
} | ||
function action(host, name, descr, defer) { | ||
function createSyncedAction(action) { | ||
function syncedAction() { | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
action(args); | ||
defaultContext.sync(); | ||
} | ||
syncedAction.displayName = action.displayName + "_synced"; | ||
return syncedAction; | ||
} | ||
function createDeferedAction(action) { | ||
function deferedAction() { | ||
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
scheduleNative(function () { | ||
return action(args); | ||
}); | ||
} | ||
deferedAction.displayName = action.displayName + "_defered"; | ||
return deferedAction; | ||
} | ||
function action(host, name, descr, defer, sync) { | ||
var hk = name + "$"; | ||
@@ -461,26 +642,9 @@ | ||
var definingProperty = false; | ||
var actionId = getId(host, name); | ||
return { | ||
enumerable: descr.enumerable, | ||
configurable: descr.configurable, | ||
configurable: true, | ||
get: function get() { | ||
var _this = this; | ||
if (definingProperty) return this[hk].bind(this); | ||
var actionFn = defer ? function () { | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
return scheduleNative(function () { | ||
return fastCallMethod(_this, hk, args); | ||
}); | ||
} : function () { | ||
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
return fastCallMethod(_this, hk, args); | ||
}; | ||
setFunctionName(actionFn, actionId); | ||
var action = createAction(this, hk); | ||
var actionFn = defer ? createDeferedAction(action) : sync ? createSyncedAction(action) : action; | ||
definingProperty = true; | ||
@@ -504,167 +668,8 @@ Object.defineProperty(this, name, { | ||
function reap(atom, key, reaping) { | ||
reaping.delete(atom); | ||
if (!atom.slaves) { | ||
atom.destructor(); | ||
} | ||
function actionSync(host, name, descr) { | ||
return action(host, name, descr, false, true); | ||
} | ||
var Context = | ||
/*#__PURE__*/ | ||
function () { | ||
function Context() { | ||
var _this = this; | ||
action.sync = actionSync; | ||
this.current = null; | ||
this._logger = undefined; | ||
this._updating = []; | ||
this._reaping = new Set(); | ||
this._scheduled = false; | ||
this._namespace = '$'; | ||
this._owners = new WeakMap(); | ||
this.__run = function () { | ||
if (_this._scheduled) { | ||
_this._scheduled = false; | ||
_this._run(); | ||
} | ||
}; | ||
this._start = 0; | ||
this._pendCount = 0; | ||
} | ||
var _proto = Context.prototype; | ||
_proto._destroyValue = function _destroyValue(atom, from) { | ||
if (this._owners.get(from) === atom) { | ||
try { | ||
from.destructor(); | ||
} catch (e) { | ||
console.error(e); | ||
if (this._logger) this._logger.error(atom, e); | ||
} | ||
this._owners.delete(from); | ||
} | ||
}; | ||
_proto.destroyHost = function destroyHost(atom) { | ||
this._destroyValue(atom, atom.current); | ||
if (this._logger !== undefined) { | ||
this._logger.onDestruct(atom); | ||
} | ||
}; | ||
_proto.setLogger = function setLogger(logger) { | ||
this._logger = logger; | ||
}; | ||
_proto.newValue = function newValue(atom, from, to) { | ||
this._destroyValue(atom, from); | ||
if (to && typeof to === 'object' && !(to instanceof Error) && typeof to.destructor === 'function') { | ||
this._owners.set(to, atom); | ||
} | ||
var logger = this._logger; | ||
if (logger !== undefined) { | ||
try { | ||
// if (!this._scheduled && this._logger !== undefined) { | ||
// this._logger.beginGroup(this._namespace) | ||
// } | ||
logger.newValue(atom, from, to); | ||
} catch (error) { | ||
console.error(error); | ||
logger.error(atom, error); | ||
} | ||
} | ||
}; | ||
_proto.proposeToPull = function proposeToPull(atom) { | ||
this._updating.push(atom); | ||
this._schedule(); | ||
}; | ||
_proto.proposeToReap = function proposeToReap(atom) { | ||
this._reaping.add(atom); | ||
this._schedule(); | ||
}; | ||
_proto.unreap = function unreap(atom) { | ||
this._reaping.delete(atom); | ||
}; | ||
_proto._schedule = function _schedule() { | ||
if (!this._scheduled) { | ||
scheduleNative(this.__run); | ||
this._scheduled = true; | ||
} | ||
}; | ||
_proto._run = function _run() { | ||
this._schedule(); | ||
var reaping = this._reaping; | ||
var updating = this._updating; | ||
var start = this._start; | ||
do { | ||
var end = updating.length; | ||
for (var i = start; i < end; i++) { | ||
this._start = i; // save progress, atom.actualize or destroyed can throw exception | ||
var atom = updating[i]; | ||
if (!reaping.has(atom) && atom.status !== ATOM_STATUS_DESTROYED) { | ||
atom.actualize(); | ||
} | ||
} | ||
start = end; | ||
} while (updating.length > start); | ||
updating.length = 0; | ||
this._start = 0; | ||
while (reaping.size > 0) { | ||
reaping.forEach(reap); | ||
} // if (this._logger !== undefined) { | ||
// this._logger.endGroup() | ||
// } | ||
this._scheduled = false; | ||
this._pendCount = 0; | ||
}; | ||
_proto.beginTransaction = function beginTransaction(namespace) { | ||
var result = this._namespace; | ||
this._namespace = namespace; | ||
this._pendCount++; | ||
return result; | ||
}; | ||
_proto.endTransaction = function endTransaction(prev) { | ||
this._namespace = prev; | ||
if (this._pendCount === 1) { | ||
this._run(); | ||
} else { | ||
this._pendCount--; | ||
} | ||
}; | ||
return Context; | ||
}(); | ||
var defaultContext = new Context(); | ||
function detached(proto, name, descr) { | ||
@@ -671,0 +676,0 @@ proto[name + "$"] = descr.value; |
@@ -91,2 +91,141 @@ (function (global, factory) { | ||
function reap(atom, key, reaping) { | ||
reaping.delete(atom); | ||
if (!atom.slaves) { | ||
atom.destructor(); | ||
} | ||
} | ||
var Context = | ||
/*#__PURE__*/ | ||
function () { | ||
function Context() { | ||
var _this = this; | ||
this.current = null; | ||
this._logger = undefined; | ||
this._updating = []; | ||
this._reaping = new Set(); | ||
this._scheduled = false; | ||
this._owners = new WeakMap(); | ||
this._run = function () { | ||
if (_this._scheduled) { | ||
_this._scheduled = false; | ||
_this.sync(); | ||
} | ||
}; | ||
this._start = 0; | ||
} | ||
var _proto = Context.prototype; | ||
_proto._destroyValue = function _destroyValue(atom, from) { | ||
if (this._owners.get(from) === atom) { | ||
try { | ||
from.destructor(); | ||
} catch (e) { | ||
console.error(e); | ||
if (this._logger) this._logger.error(atom, e); | ||
} | ||
this._owners.delete(from); | ||
} | ||
}; | ||
_proto.destroyHost = function destroyHost(atom) { | ||
this._destroyValue(atom, atom.current); | ||
if (this._logger !== undefined) { | ||
this._logger.onDestruct(atom); | ||
} | ||
}; | ||
_proto.setLogger = function setLogger(logger) { | ||
this._logger = logger; | ||
}; | ||
_proto.newValue = function newValue(atom, from, to) { | ||
this._destroyValue(atom, from); | ||
if (to && typeof to === 'object' && !(to instanceof Error) && typeof to.destructor === 'function') { | ||
this._owners.set(to, atom); | ||
} | ||
var logger = this._logger; | ||
if (logger !== undefined) { | ||
try { | ||
logger.newValue(atom, from, to); | ||
} catch (error) { | ||
console.error(error); | ||
logger.error(atom, error); | ||
} | ||
} | ||
}; | ||
_proto.proposeToPull = function proposeToPull(atom) { | ||
this._updating.push(atom); | ||
this._schedule(); | ||
}; | ||
_proto.proposeToReap = function proposeToReap(atom) { | ||
this._reaping.add(atom); | ||
this._schedule(); | ||
}; | ||
_proto.unreap = function unreap(atom) { | ||
this._reaping.delete(atom); | ||
}; | ||
_proto._schedule = function _schedule() { | ||
if (!this._scheduled) { | ||
scheduleNative(this._run); | ||
this._scheduled = true; | ||
} | ||
}; | ||
_proto.sync = function sync() { | ||
this._schedule(); | ||
var reaping = this._reaping; | ||
var updating = this._updating; | ||
var start = this._start; | ||
do { | ||
var end = updating.length; | ||
for (var i = start; i < end; i++) { | ||
this._start = i; // save progress, atom.actualize or destroyed can throw exception | ||
var atom = updating[i]; | ||
if (!reaping.has(atom) && atom.status !== ATOM_STATUS_DESTROYED) { | ||
atom.actualize(); | ||
} | ||
} | ||
start = end; | ||
} while (updating.length > start); | ||
updating.length = 0; | ||
this._start = 0; | ||
while (reaping.size > 0) { | ||
reaping.forEach(reap); | ||
} | ||
this._scheduled = false; | ||
}; | ||
return Context; | ||
}(); | ||
var defaultContext = new Context(); | ||
var handlers = new Map([[Array, function arrayHandler(target, source, stack) { | ||
@@ -160,2 +299,3 @@ var equal = target.length === source.length; | ||
var proxyId = Symbol('lom_err_proxy'); | ||
var Atom = (_temp = _class = | ||
@@ -167,2 +307,3 @@ /*#__PURE__*/ | ||
this._slaves = null; | ||
this._retry = undefined; | ||
this._keyHash = keyHash; | ||
@@ -263,3 +404,7 @@ this.key = key; | ||
if (current instanceof Error) { | ||
if (forceCache !== ATOM_FORCE_NONE) return proxify(current); | ||
if (forceCache !== ATOM_FORCE_NONE) { | ||
if (current[proxyId] === undefined) current[proxyId] = proxify(current); | ||
return current[proxyId]; | ||
} | ||
throw current; | ||
@@ -424,32 +569,68 @@ } | ||
function fastCallMethod(host, methodName, args) { | ||
try { | ||
switch (args.length) { | ||
case 0: | ||
return host[methodName](); | ||
function createAction(host, methodName) { | ||
function actionFn() { | ||
var args = arguments; | ||
case 1: | ||
return host[methodName](args[0]); | ||
try { | ||
switch (args.length) { | ||
case 0: | ||
return host[methodName](); | ||
case 2: | ||
return host[methodName](args[0], args[1]); | ||
case 1: | ||
return host[methodName](args[0]); | ||
case 3: | ||
return host[methodName](args[0], args[1], args[2]); | ||
case 2: | ||
return host[methodName](args[0], args[1]); | ||
case 4: | ||
return host[methodName](args[0], args[1], args[2], args[3]); | ||
case 3: | ||
return host[methodName](args[0], args[1], args[2]); | ||
case 5: | ||
return host[methodName](args[0], args[1], args[2], args[3], args[4]); | ||
case 4: | ||
return host[methodName](args[0], args[1], args[2], args[3]); | ||
default: | ||
return host[methodName].apply(host, args); | ||
case 5: | ||
return host[methodName](args[0], args[1], args[2], args[3], args[4]); | ||
default: | ||
return host[methodName].apply(host, args); | ||
} | ||
} catch (e) { | ||
if (!(e instanceof AtomWait)) throw e; | ||
} | ||
} catch (e) { | ||
if (!(e instanceof AtomWait)) throw e; | ||
} | ||
setFunctionName(actionFn, getId(host, methodName)); | ||
return actionFn; | ||
} | ||
function action(host, name, descr, defer) { | ||
function createSyncedAction(action) { | ||
function syncedAction() { | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
action(args); | ||
defaultContext.sync(); | ||
} | ||
syncedAction.displayName = action.displayName + "_synced"; | ||
return syncedAction; | ||
} | ||
function createDeferedAction(action) { | ||
function deferedAction() { | ||
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
scheduleNative(function () { | ||
return action(args); | ||
}); | ||
} | ||
deferedAction.displayName = action.displayName + "_defered"; | ||
return deferedAction; | ||
} | ||
function action(host, name, descr, defer, sync) { | ||
var hk = name + "$"; | ||
@@ -463,26 +644,9 @@ | ||
var definingProperty = false; | ||
var actionId = getId(host, name); | ||
return { | ||
enumerable: descr.enumerable, | ||
configurable: descr.configurable, | ||
configurable: true, | ||
get: function get() { | ||
var _this = this; | ||
if (definingProperty) return this[hk].bind(this); | ||
var actionFn = defer ? function () { | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
return scheduleNative(function () { | ||
return fastCallMethod(_this, hk, args); | ||
}); | ||
} : function () { | ||
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
return fastCallMethod(_this, hk, args); | ||
}; | ||
setFunctionName(actionFn, actionId); | ||
var action = createAction(this, hk); | ||
var actionFn = defer ? createDeferedAction(action) : sync ? createSyncedAction(action) : action; | ||
definingProperty = true; | ||
@@ -506,167 +670,8 @@ Object.defineProperty(this, name, { | ||
function reap(atom, key, reaping) { | ||
reaping.delete(atom); | ||
if (!atom.slaves) { | ||
atom.destructor(); | ||
} | ||
function actionSync(host, name, descr) { | ||
return action(host, name, descr, false, true); | ||
} | ||
var Context = | ||
/*#__PURE__*/ | ||
function () { | ||
function Context() { | ||
var _this = this; | ||
action.sync = actionSync; | ||
this.current = null; | ||
this._logger = undefined; | ||
this._updating = []; | ||
this._reaping = new Set(); | ||
this._scheduled = false; | ||
this._namespace = '$'; | ||
this._owners = new WeakMap(); | ||
this.__run = function () { | ||
if (_this._scheduled) { | ||
_this._scheduled = false; | ||
_this._run(); | ||
} | ||
}; | ||
this._start = 0; | ||
this._pendCount = 0; | ||
} | ||
var _proto = Context.prototype; | ||
_proto._destroyValue = function _destroyValue(atom, from) { | ||
if (this._owners.get(from) === atom) { | ||
try { | ||
from.destructor(); | ||
} catch (e) { | ||
console.error(e); | ||
if (this._logger) this._logger.error(atom, e); | ||
} | ||
this._owners.delete(from); | ||
} | ||
}; | ||
_proto.destroyHost = function destroyHost(atom) { | ||
this._destroyValue(atom, atom.current); | ||
if (this._logger !== undefined) { | ||
this._logger.onDestruct(atom); | ||
} | ||
}; | ||
_proto.setLogger = function setLogger(logger) { | ||
this._logger = logger; | ||
}; | ||
_proto.newValue = function newValue(atom, from, to) { | ||
this._destroyValue(atom, from); | ||
if (to && typeof to === 'object' && !(to instanceof Error) && typeof to.destructor === 'function') { | ||
this._owners.set(to, atom); | ||
} | ||
var logger = this._logger; | ||
if (logger !== undefined) { | ||
try { | ||
// if (!this._scheduled && this._logger !== undefined) { | ||
// this._logger.beginGroup(this._namespace) | ||
// } | ||
logger.newValue(atom, from, to); | ||
} catch (error) { | ||
console.error(error); | ||
logger.error(atom, error); | ||
} | ||
} | ||
}; | ||
_proto.proposeToPull = function proposeToPull(atom) { | ||
this._updating.push(atom); | ||
this._schedule(); | ||
}; | ||
_proto.proposeToReap = function proposeToReap(atom) { | ||
this._reaping.add(atom); | ||
this._schedule(); | ||
}; | ||
_proto.unreap = function unreap(atom) { | ||
this._reaping.delete(atom); | ||
}; | ||
_proto._schedule = function _schedule() { | ||
if (!this._scheduled) { | ||
scheduleNative(this.__run); | ||
this._scheduled = true; | ||
} | ||
}; | ||
_proto._run = function _run() { | ||
this._schedule(); | ||
var reaping = this._reaping; | ||
var updating = this._updating; | ||
var start = this._start; | ||
do { | ||
var end = updating.length; | ||
for (var i = start; i < end; i++) { | ||
this._start = i; // save progress, atom.actualize or destroyed can throw exception | ||
var atom = updating[i]; | ||
if (!reaping.has(atom) && atom.status !== ATOM_STATUS_DESTROYED) { | ||
atom.actualize(); | ||
} | ||
} | ||
start = end; | ||
} while (updating.length > start); | ||
updating.length = 0; | ||
this._start = 0; | ||
while (reaping.size > 0) { | ||
reaping.forEach(reap); | ||
} // if (this._logger !== undefined) { | ||
// this._logger.endGroup() | ||
// } | ||
this._scheduled = false; | ||
this._pendCount = 0; | ||
}; | ||
_proto.beginTransaction = function beginTransaction(namespace) { | ||
var result = this._namespace; | ||
this._namespace = namespace; | ||
this._pendCount++; | ||
return result; | ||
}; | ||
_proto.endTransaction = function endTransaction(prev) { | ||
this._namespace = prev; | ||
if (this._pendCount === 1) { | ||
this._run(); | ||
} else { | ||
this._pendCount--; | ||
} | ||
}; | ||
return Context; | ||
}(); | ||
var defaultContext = new Context(); | ||
function detached(proto, name, descr) { | ||
@@ -673,0 +678,0 @@ proto[name + "$"] = descr.value; |
{ | ||
"name": "lom_atom", | ||
"version": "3.0.17", | ||
"version": "3.0.18", | ||
"description": "Alternative implementation of eigenmethod mol_atom state management library", | ||
@@ -5,0 +5,0 @@ "publishConfig": { |
@@ -15,7 +15,6 @@ // @flow | ||
IAtomStatus, | ||
IContext, | ||
IAtomHandler, | ||
IAtomOwner | ||
} from './interfaces' | ||
import Context from './Context' | ||
import {AtomWait, setFunctionName, origId, catchedId, proxify} from './utils' | ||
@@ -41,2 +40,3 @@ import conform from './conform' | ||
} | ||
const proxyId = Symbol('lom_err_proxy') | ||
@@ -57,3 +57,3 @@ export default class Atom<V> implements IAtom<V>, IAtomInt { | ||
_slaves: ?Set<IAtomInt> = null | ||
_context: IContext | ||
_context: Context | ||
_hostAtoms: WeakMap<Object, IAtom<*>> | Map<string, IAtom<*>> | ||
@@ -67,3 +67,3 @@ _keyHash: string | void | ||
owner: IAtomOwner, | ||
context: IContext, | ||
context: Context, | ||
hostAtoms: WeakMap<Object, IAtom<*>> | Map<string, IAtom<*>>, | ||
@@ -169,3 +169,6 @@ manualReset?: boolean, | ||
if (current instanceof Error) { | ||
if (forceCache !== ATOM_FORCE_NONE) return proxify((current: any)) | ||
if (forceCache !== ATOM_FORCE_NONE) { | ||
if ((current: Object)[proxyId] === undefined) (current: Object)[proxyId] = proxify((current: any)) | ||
return (current: Object)[proxyId] | ||
} | ||
throw current | ||
@@ -209,3 +212,3 @@ } | ||
this.status = ATOM_STATUS_ACTUAL | ||
const prev = this.current | ||
const prev: V | Error = this.current | ||
let next: V | Error | ||
@@ -223,3 +226,3 @@ if (nextRaw instanceof Error) { | ||
this.current = next | ||
this._context.newValue(this, prev, next) | ||
this._context.newValue((this: IAtomInt), prev, next) | ||
if (this._slaves) this._slaves.forEach(obsoleteSlave) | ||
@@ -299,3 +302,3 @@ } | ||
_retry: (() => void) | void | ||
_retry: (() => void) | void = undefined | ||
@@ -302,0 +305,0 @@ getRetry(): () => void { |
@@ -6,3 +6,2 @@ // @flow | ||
IAtom, | ||
IContext, | ||
ILogger, | ||
@@ -20,3 +19,3 @@ } from './interfaces' | ||
export default class Context implements IContext { | ||
export default class Context { | ||
current: ?IAtomInt = null | ||
@@ -28,3 +27,2 @@ | ||
_scheduled = false | ||
_namespace: string = '$' | ||
_owners: WeakMap<?Object, Object> = new WeakMap() | ||
@@ -68,5 +66,2 @@ | ||
try { | ||
// if (!this._scheduled && this._logger !== undefined) { | ||
// this._logger.beginGroup(this._namespace) | ||
// } | ||
logger.newValue(atom, from, to) | ||
@@ -96,3 +91,3 @@ } catch (error) { | ||
if (!this._scheduled) { | ||
scheduleNative(this.__run) | ||
scheduleNative(this._run) | ||
this._scheduled = true | ||
@@ -102,6 +97,6 @@ } | ||
__run = () => { | ||
_run = () => { | ||
if (this._scheduled) { | ||
this._scheduled = false | ||
this._run() | ||
this.sync() | ||
} | ||
@@ -112,3 +107,3 @@ } | ||
_run() { | ||
sync() { | ||
this._schedule() | ||
@@ -137,28 +132,6 @@ const reaping = this._reaping | ||
} | ||
// if (this._logger !== undefined) { | ||
// this._logger.endGroup() | ||
// } | ||
this._scheduled = false | ||
this._pendCount = 0 | ||
} | ||
_pendCount = 0 | ||
beginTransaction(namespace: string): string { | ||
const result = this._namespace | ||
this._namespace = namespace | ||
this._pendCount++ | ||
return result | ||
} | ||
endTransaction(prev: string) { | ||
this._namespace = prev | ||
if (this._pendCount === 1) { | ||
this._run() | ||
} else { | ||
this._pendCount-- | ||
} | ||
} | ||
} | ||
export const defaultContext = new Context() |
// @flow | ||
import type {TypedPropertyDescriptor, IContext} from '../interfaces' | ||
import type {TypedPropertyDescriptor} from '../interfaces' | ||
import {scheduleNative, getId, setFunctionName, AtomWait} from '../utils' | ||
import {defaultContext} from '../Context' | ||
type Handler = (...args: any[]) => void | ||
function fastCallMethod<Host: Object>(host: Host, methodName: string, args: any[]): void { | ||
try { | ||
switch (args.length) { | ||
case 0: return host[methodName]() | ||
case 1: return host[methodName](args[0]) | ||
case 2: return host[methodName](args[0], args[1]) | ||
case 3: return host[methodName](args[0], args[1], args[2]) | ||
case 4: return host[methodName](args[0], args[1], args[2], args[3]) | ||
case 5: return host[methodName](args[0], args[1], args[2], args[3], args[4]) | ||
default: return host[methodName].apply(host, args) | ||
function createAction<Host: Object>(host: Host, methodName: string): (...args: any[]) => void { | ||
function actionFn(): void { | ||
const args = arguments | ||
try { | ||
switch (args.length) { | ||
case 0: return host[methodName]() | ||
case 1: return host[methodName](args[0]) | ||
case 2: return host[methodName](args[0], args[1]) | ||
case 3: return host[methodName](args[0], args[1], args[2]) | ||
case 4: return host[methodName](args[0], args[1], args[2], args[3]) | ||
case 5: return host[methodName](args[0], args[1], args[2], args[3], args[4]) | ||
default: return host[methodName].apply(host, args) | ||
} | ||
} catch(e) { | ||
if (!(e instanceof AtomWait)) throw e | ||
} | ||
} catch(e) { | ||
if (!(e instanceof AtomWait)) throw e | ||
} | ||
setFunctionName(actionFn, getId(host, methodName)) | ||
return actionFn | ||
} | ||
function createSyncedAction(action: (args: any[]) => void) { | ||
function syncedAction(...args: any[]) { | ||
action(args) | ||
defaultContext.sync() | ||
} | ||
syncedAction.displayName = `${action.displayName}_synced` | ||
return syncedAction | ||
} | ||
function createDeferedAction(action: (args: any[]) => void) { | ||
function deferedAction(...args: any[]) { | ||
scheduleNative(() => action(args)) | ||
} | ||
deferedAction.displayName = `${action.displayName}_defered` | ||
return deferedAction | ||
} | ||
function action<Host: Object, H: Handler>( | ||
@@ -28,3 +54,4 @@ host: Host, | ||
descr: TypedPropertyDescriptor<H>, | ||
defer?: boolean | ||
defer?: boolean, | ||
sync?: boolean | ||
): TypedPropertyDescriptor<H> { | ||
@@ -38,14 +65,12 @@ const hk = `${name}$` | ||
const actionId = getId(host, name) | ||
return { | ||
enumerable: descr.enumerable, | ||
configurable: descr.configurable, | ||
configurable: true, | ||
get(): H { | ||
if (definingProperty) return this[hk].bind(this) | ||
const action = createAction(this, hk) | ||
const actionFn = defer | ||
? (...args: any[]) => scheduleNative(() => fastCallMethod(this, hk, args)) | ||
: (...args: any[]) => fastCallMethod(this, hk, args) | ||
setFunctionName(actionFn, actionId) | ||
? createDeferedAction(action) | ||
: (sync ? createSyncedAction(action) : action) | ||
@@ -75,2 +100,12 @@ definingProperty = true | ||
function actionSync<Host: Object, H: Handler>( | ||
host: Host, | ||
name: string, | ||
descr: TypedPropertyDescriptor<H> | ||
): TypedPropertyDescriptor<H> { | ||
return action(host, name, descr, false, true) | ||
} | ||
action.sync = actionSync | ||
interface IAction { | ||
@@ -87,4 +122,9 @@ <Host: Object, H: Handler>( | ||
) => TypedPropertyDescriptor<H>; | ||
sync: <Host: Object, H: Handler>( | ||
host: Host, | ||
name: string, | ||
descr: TypedPropertyDescriptor<H> | ||
) => TypedPropertyDescriptor<H>; | ||
} | ||
export default (action: IAction) |
@@ -25,14 +25,2 @@ // @flow | ||
export interface IContext { | ||
current: ?IAtomInt; | ||
destroyHost(atom: IAtomInt): void; | ||
newValue<V>(t: IAtom<V>, from?: V | Error, to: V | Error): void; | ||
setLogger(logger: ILogger): void; | ||
proposeToPull(atom: IAtomInt): void; | ||
proposeToReap(atom: IAtomInt): void; | ||
unreap(atom: IAtomInt): void; | ||
beginTransaction(namespace: string): string; | ||
endTransaction(oldNamespace: string): void; | ||
} | ||
export const ATOM_STATUS_DESTROYED: 0 = 0 | ||
@@ -39,0 +27,0 @@ export const ATOM_STATUS_OBSOLETE: 1 = 1 |
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
331769
3386