Comparing version 2.0.0 to 2.0.1
@@ -5,2 +5,7 @@ # Change Log | ||
<a name="2.0.1"></a> | ||
## [2.0.1](https://github.com/zerkalica/lom_atom/compare/v2.0.0...v2.0.1) (2017-10-11) | ||
<a name="2.0.0"></a> | ||
@@ -7,0 +12,0 @@ # [2.0.0](https://github.com/zerkalica/lom_atom/compare/v1.1.4...v2.0.0) (2017-10-10) |
@@ -123,12 +123,14 @@ // eslint-disable-line | ||
var Atom = function () { | ||
function Atom(field, host, context, normalize, key, isComponent) { | ||
function Atom(field, owner, context, hostAtoms, normalize, key, keyHash, isComponent) { | ||
this._masters = null; | ||
this._slaves = null; | ||
this._keyHash = keyHash; | ||
this.key = key; | ||
this.field = field; | ||
this.host = host; | ||
this.owner = owner; | ||
this.isComponent = isComponent || false; | ||
this._normalize = normalize || defaultNormalize; | ||
this._context = context; | ||
this.value = context.create(host, field, key); | ||
this.value = context.create(this); | ||
this._hostAtoms = hostAtoms; | ||
this.status = this.value === undefined ? ATOM_STATUS_OBSOLETE : ATOM_STATUS_ACTUAL; | ||
@@ -138,5 +140,5 @@ } | ||
Atom.prototype.toString = function toString() { | ||
var hc = this.host.constructor; | ||
var hc = this.owner.constructor; | ||
var k = this.key; | ||
return (this.host.displayName || (hc ? String(hc.displayName || hc.name) : '')) + '.' + this.field + (k ? '(' + (typeof k === 'function' ? k.displayName || k.name : String(k)) + ')' : ''); | ||
return this.field + (k ? '(' + (typeof k === 'function' ? k.displayName || k.name : String(k)) + ')' : ''); | ||
}; | ||
@@ -148,31 +150,25 @@ | ||
Atom.prototype.destroyed = function destroyed(isDestroyed) { | ||
if (isDestroyed === undefined) { | ||
return this.status === ATOM_STATUS_DESTROYED; | ||
} | ||
Atom.prototype.destructor = function destructor() { | ||
if (this.status === ATOM_STATUS_DESTROYED) return; | ||
if (isDestroyed) { | ||
if (this.status !== ATOM_STATUS_DESTROYED) { | ||
if (this._masters) { | ||
this._masters.forEach(disleadThis, this); | ||
if (this._masters) { | ||
this._masters.forEach(disleadThis, this); | ||
this._masters = null; | ||
} | ||
this._masters = null; | ||
} | ||
this._checkSlaves(); | ||
this._checkSlaves(); | ||
if (this.host.destroy !== undefined) { | ||
this.host.destroy(this.value, this.field, this.key); | ||
} | ||
var hostAtoms = this._hostAtoms; | ||
this._context.destroyHost(this); | ||
if (hostAtoms instanceof WeakMap) { | ||
hostAtoms.delete(this.owner); | ||
} else if (this._keyHash) { | ||
hostAtoms.delete(this._keyHash); | ||
} | ||
this.value = undefined; | ||
this.status = ATOM_STATUS_DESTROYED; | ||
} | ||
this._context.destroyHost(this); | ||
return true; | ||
} | ||
return false; | ||
this.value = undefined; | ||
this.status = ATOM_STATUS_DESTROYED; | ||
}; | ||
@@ -272,3 +268,3 @@ | ||
try { | ||
newValue = this._normalize(this.key === undefined ? this.host[this.field + '$'](proposedValue, force, value) : this.host[this.field + '$'](this.key, proposedValue, force, value), value); | ||
newValue = this._normalize(this.key === undefined ? this.owner[this.field + '$'](proposedValue, force, value) : this.owner[this.field + '$'](this.key, proposedValue, force, value), value); | ||
} catch (error) { | ||
@@ -362,3 +358,3 @@ if (error[catchedId] === undefined) { | ||
if (!atom.slaves) { | ||
atom.destroyed(true); | ||
atom.destructor(); | ||
} | ||
@@ -370,5 +366,5 @@ } | ||
BaseLogger.prototype.create = function create(host, field, key, namespace) {}; | ||
BaseLogger.prototype.create = function create(owner, field, key, namespace) {}; | ||
BaseLogger.prototype.destroy = function destroy(atom, namespace) {}; | ||
BaseLogger.prototype.onDestruct = function onDestruct(atom, namespace) {}; | ||
@@ -416,2 +412,3 @@ BaseLogger.prototype.status = function status(_status, atom, namespace) {}; | ||
this._namespace = '$'; | ||
this._owners = new WeakMap(); | ||
@@ -430,5 +427,7 @@ this.__run = function () { | ||
Context.prototype.create = function create(host, field, key) { | ||
Context.prototype.create = function create(atom) { | ||
this._owners.set(atom, atom.owner); | ||
if (this._logger !== undefined) { | ||
return this._logger.create(host, field, key, this._namespace); | ||
return this._logger.create(atom.owner, atom.field, atom.key, this._namespace); | ||
} | ||
@@ -438,4 +437,12 @@ }; | ||
Context.prototype.destroyHost = function destroyHost(atom) { | ||
var from = atom.value; | ||
if (from && !(from instanceof Error) && _typeof(from) === 'object' && typeof from.destructor === 'function' && this._owners.get(from) === atom) { | ||
from.destructor(); | ||
this._owners.delete(from); | ||
} | ||
if (this._logger !== undefined) { | ||
this._logger.destroy(atom, this._namespace); | ||
this._logger.onDestruct(atom, this._namespace); | ||
} | ||
@@ -449,2 +456,12 @@ }; | ||
Context.prototype.newValue = function newValue(atom, from, to, isActualize) { | ||
if (from && !(from instanceof Error) && _typeof(from) === 'object' && typeof from.destructor === 'function' && this._owners.get(from) === atom) { | ||
from.destructor(); | ||
this._owners.delete(from); | ||
} | ||
if (to && !(to instanceof Error) && _typeof(to) === 'object' && typeof to.destructor === 'function') { | ||
this._owners.set(to, atom); | ||
} | ||
if (this._logger !== undefined) { | ||
@@ -507,3 +524,3 @@ if (to instanceof AtomWait) { | ||
if (!reaping.has(atom) && !atom.destroyed()) { | ||
if (!reaping.has(atom) && atom.status !== ATOM_STATUS_DESTROYED) { | ||
atom.actualize(); | ||
@@ -549,3 +566,9 @@ } | ||
function memMethod(proto, name, descr, normalize, isComponent) { | ||
function getId(t, hk) { | ||
return (t.constructor.displayName || t.constructor.name) + "." + hk; | ||
} | ||
function memMethod(proto, rname, descr, normalize, isComponent) { | ||
var name = getId(proto, rname); | ||
if (descr.value === undefined) { | ||
@@ -564,7 +587,7 @@ throw new TypeError(name + " is not an function (next?: V)"); | ||
var forcedFn = function forcedFn(next, force) { | ||
return this[name](next, force === undefined ? true : force); | ||
return this[rname](next, force === undefined ? true : force); | ||
}; | ||
setFunctionName(forcedFn, name + "*"); | ||
proto[name + "*"] = forcedFn; | ||
proto[rname + "*"] = forcedFn; | ||
return { | ||
@@ -577,3 +600,3 @@ enumerable: descr.enumerable, | ||
if (atom === undefined) { | ||
atom = new Atom(name, this, defaultContext, normalize, undefined, isComponent); | ||
atom = new Atom(name, this, defaultContext, hostAtoms, normalize, undefined, undefined, isComponent); | ||
hostAtoms.set(this, atom); | ||
@@ -614,3 +637,4 @@ } | ||
function memProp(proto, name, descr, normalize) { | ||
function memProp(proto, rname, descr, normalize) { | ||
var name = getId(proto, rname); | ||
var handlerKey = name + "$"; | ||
@@ -640,3 +664,3 @@ | ||
if (atom === undefined) { | ||
atom = new Atom(name, this, defaultContext, normalize); | ||
atom = new Atom(name, this, defaultContext, hostAtoms, normalize); | ||
hostAtoms.set(this, atom); | ||
@@ -656,3 +680,3 @@ } | ||
if (atom === undefined) { | ||
atom = new Atom(name, this, defaultContext, normalize); | ||
atom = new Atom(name, this, defaultContext, hostAtoms, normalize); | ||
hostAtoms.set(this, atom); | ||
@@ -693,3 +717,4 @@ } | ||
function memKeyMethod(proto, name, descr, normalize) { | ||
function memKeyMethod(proto, rname, descr, normalize) { | ||
var name = getId(proto, rname); | ||
var handler = descr.value; | ||
@@ -710,7 +735,7 @@ | ||
var forcedFn = function forcedFn(rawKey, next, force) { | ||
return this[name](rawKey, next, force === undefined ? true : force); | ||
return this[rname](rawKey, next, force === undefined ? true : force); | ||
}; | ||
setFunctionName(forcedFn, name + "*"); | ||
proto[name + "*"] = forcedFn; | ||
proto[rname + "*"] = forcedFn; | ||
return { | ||
@@ -731,3 +756,3 @@ enumerable: descr.enumerable, | ||
if (atom === undefined) { | ||
atom = new Atom(name, this, defaultContext, normalize, rawKey); | ||
atom = new Atom(name, this, defaultContext, atomMap, normalize, rawKey, key); | ||
atomMap.set(key, atom); | ||
@@ -795,3 +820,3 @@ } | ||
function createActionMethod(t, hk, context) { | ||
var name = (t.displayName || t.constructor.displayName || t.constructor.name) + "." + hk; | ||
var name = getId(t, hk); | ||
@@ -798,0 +823,0 @@ function action() { |
@@ -127,12 +127,14 @@ 'use strict'; | ||
var Atom = function () { | ||
function Atom(field, host, context, normalize, key, isComponent) { | ||
function Atom(field, owner, context, hostAtoms, normalize, key, keyHash, isComponent) { | ||
this._masters = null; | ||
this._slaves = null; | ||
this._keyHash = keyHash; | ||
this.key = key; | ||
this.field = field; | ||
this.host = host; | ||
this.owner = owner; | ||
this.isComponent = isComponent || false; | ||
this._normalize = normalize || defaultNormalize; | ||
this._context = context; | ||
this.value = context.create(host, field, key); | ||
this.value = context.create(this); | ||
this._hostAtoms = hostAtoms; | ||
this.status = this.value === undefined ? ATOM_STATUS_OBSOLETE : ATOM_STATUS_ACTUAL; | ||
@@ -142,5 +144,5 @@ } | ||
Atom.prototype.toString = function toString() { | ||
var hc = this.host.constructor; | ||
var hc = this.owner.constructor; | ||
var k = this.key; | ||
return (this.host.displayName || (hc ? String(hc.displayName || hc.name) : '')) + '.' + this.field + (k ? '(' + (typeof k === 'function' ? k.displayName || k.name : String(k)) + ')' : ''); | ||
return this.field + (k ? '(' + (typeof k === 'function' ? k.displayName || k.name : String(k)) + ')' : ''); | ||
}; | ||
@@ -152,31 +154,25 @@ | ||
Atom.prototype.destroyed = function destroyed(isDestroyed) { | ||
if (isDestroyed === undefined) { | ||
return this.status === ATOM_STATUS_DESTROYED; | ||
} | ||
Atom.prototype.destructor = function destructor() { | ||
if (this.status === ATOM_STATUS_DESTROYED) return; | ||
if (isDestroyed) { | ||
if (this.status !== ATOM_STATUS_DESTROYED) { | ||
if (this._masters) { | ||
this._masters.forEach(disleadThis, this); | ||
if (this._masters) { | ||
this._masters.forEach(disleadThis, this); | ||
this._masters = null; | ||
} | ||
this._masters = null; | ||
} | ||
this._checkSlaves(); | ||
this._checkSlaves(); | ||
if (this.host.destroy !== undefined) { | ||
this.host.destroy(this.value, this.field, this.key); | ||
} | ||
var hostAtoms = this._hostAtoms; | ||
this._context.destroyHost(this); | ||
if (hostAtoms instanceof WeakMap) { | ||
hostAtoms.delete(this.owner); | ||
} else if (this._keyHash) { | ||
hostAtoms.delete(this._keyHash); | ||
} | ||
this.value = undefined; | ||
this.status = ATOM_STATUS_DESTROYED; | ||
} | ||
this._context.destroyHost(this); | ||
return true; | ||
} | ||
return false; | ||
this.value = undefined; | ||
this.status = ATOM_STATUS_DESTROYED; | ||
}; | ||
@@ -276,3 +272,3 @@ | ||
try { | ||
newValue = this._normalize(this.key === undefined ? this.host[this.field + '$'](proposedValue, force, value) : this.host[this.field + '$'](this.key, proposedValue, force, value), value); | ||
newValue = this._normalize(this.key === undefined ? this.owner[this.field + '$'](proposedValue, force, value) : this.owner[this.field + '$'](this.key, proposedValue, force, value), value); | ||
} catch (error) { | ||
@@ -366,3 +362,3 @@ if (error[catchedId] === undefined) { | ||
if (!atom.slaves) { | ||
atom.destroyed(true); | ||
atom.destructor(); | ||
} | ||
@@ -374,5 +370,5 @@ } | ||
BaseLogger.prototype.create = function create(host, field, key, namespace) {}; | ||
BaseLogger.prototype.create = function create(owner, field, key, namespace) {}; | ||
BaseLogger.prototype.destroy = function destroy(atom, namespace) {}; | ||
BaseLogger.prototype.onDestruct = function onDestruct(atom, namespace) {}; | ||
@@ -420,2 +416,3 @@ BaseLogger.prototype.status = function status(_status, atom, namespace) {}; | ||
this._namespace = '$'; | ||
this._owners = new WeakMap(); | ||
@@ -434,5 +431,7 @@ this.__run = function () { | ||
Context.prototype.create = function create(host, field, key) { | ||
Context.prototype.create = function create(atom) { | ||
this._owners.set(atom, atom.owner); | ||
if (this._logger !== undefined) { | ||
return this._logger.create(host, field, key, this._namespace); | ||
return this._logger.create(atom.owner, atom.field, atom.key, this._namespace); | ||
} | ||
@@ -442,4 +441,12 @@ }; | ||
Context.prototype.destroyHost = function destroyHost(atom) { | ||
var from = atom.value; | ||
if (from && !(from instanceof Error) && _typeof(from) === 'object' && typeof from.destructor === 'function' && this._owners.get(from) === atom) { | ||
from.destructor(); | ||
this._owners.delete(from); | ||
} | ||
if (this._logger !== undefined) { | ||
this._logger.destroy(atom, this._namespace); | ||
this._logger.onDestruct(atom, this._namespace); | ||
} | ||
@@ -453,2 +460,12 @@ }; | ||
Context.prototype.newValue = function newValue(atom, from, to, isActualize) { | ||
if (from && !(from instanceof Error) && _typeof(from) === 'object' && typeof from.destructor === 'function' && this._owners.get(from) === atom) { | ||
from.destructor(); | ||
this._owners.delete(from); | ||
} | ||
if (to && !(to instanceof Error) && _typeof(to) === 'object' && typeof to.destructor === 'function') { | ||
this._owners.set(to, atom); | ||
} | ||
if (this._logger !== undefined) { | ||
@@ -511,3 +528,3 @@ if (to instanceof AtomWait) { | ||
if (!reaping.has(atom) && !atom.destroyed()) { | ||
if (!reaping.has(atom) && atom.status !== ATOM_STATUS_DESTROYED) { | ||
atom.actualize(); | ||
@@ -553,3 +570,9 @@ } | ||
function memMethod(proto, name, descr, normalize, isComponent) { | ||
function getId(t, hk) { | ||
return (t.constructor.displayName || t.constructor.name) + "." + hk; | ||
} | ||
function memMethod(proto, rname, descr, normalize, isComponent) { | ||
var name = getId(proto, rname); | ||
if (descr.value === undefined) { | ||
@@ -568,7 +591,7 @@ throw new TypeError(name + " is not an function (next?: V)"); | ||
var forcedFn = function forcedFn(next, force) { | ||
return this[name](next, force === undefined ? true : force); | ||
return this[rname](next, force === undefined ? true : force); | ||
}; | ||
setFunctionName(forcedFn, name + "*"); | ||
proto[name + "*"] = forcedFn; | ||
proto[rname + "*"] = forcedFn; | ||
return { | ||
@@ -581,3 +604,3 @@ enumerable: descr.enumerable, | ||
if (atom === undefined) { | ||
atom = new Atom(name, this, defaultContext, normalize, undefined, isComponent); | ||
atom = new Atom(name, this, defaultContext, hostAtoms, normalize, undefined, undefined, isComponent); | ||
hostAtoms.set(this, atom); | ||
@@ -618,3 +641,4 @@ } | ||
function memProp(proto, name, descr, normalize) { | ||
function memProp(proto, rname, descr, normalize) { | ||
var name = getId(proto, rname); | ||
var handlerKey = name + "$"; | ||
@@ -644,3 +668,3 @@ | ||
if (atom === undefined) { | ||
atom = new Atom(name, this, defaultContext, normalize); | ||
atom = new Atom(name, this, defaultContext, hostAtoms, normalize); | ||
hostAtoms.set(this, atom); | ||
@@ -660,3 +684,3 @@ } | ||
if (atom === undefined) { | ||
atom = new Atom(name, this, defaultContext, normalize); | ||
atom = new Atom(name, this, defaultContext, hostAtoms, normalize); | ||
hostAtoms.set(this, atom); | ||
@@ -697,3 +721,4 @@ } | ||
function memKeyMethod(proto, name, descr, normalize) { | ||
function memKeyMethod(proto, rname, descr, normalize) { | ||
var name = getId(proto, rname); | ||
var handler = descr.value; | ||
@@ -714,7 +739,7 @@ | ||
var forcedFn = function forcedFn(rawKey, next, force) { | ||
return this[name](rawKey, next, force === undefined ? true : force); | ||
return this[rname](rawKey, next, force === undefined ? true : force); | ||
}; | ||
setFunctionName(forcedFn, name + "*"); | ||
proto[name + "*"] = forcedFn; | ||
proto[rname + "*"] = forcedFn; | ||
return { | ||
@@ -735,3 +760,3 @@ enumerable: descr.enumerable, | ||
if (atom === undefined) { | ||
atom = new Atom(name, this, defaultContext, normalize, rawKey); | ||
atom = new Atom(name, this, defaultContext, atomMap, normalize, rawKey, key); | ||
atomMap.set(key, atom); | ||
@@ -799,3 +824,3 @@ } | ||
function createActionMethod(t, hk, context) { | ||
var name = (t.displayName || t.constructor.displayName || t.constructor.name) + "." + hk; | ||
var name = getId(t, hk); | ||
@@ -802,0 +827,0 @@ function action() { |
@@ -129,12 +129,14 @@ (function (global, factory) { | ||
var Atom = function () { | ||
function Atom(field, host, context, normalize, key, isComponent) { | ||
function Atom(field, owner, context, hostAtoms, normalize, key, keyHash, isComponent) { | ||
this._masters = null; | ||
this._slaves = null; | ||
this._keyHash = keyHash; | ||
this.key = key; | ||
this.field = field; | ||
this.host = host; | ||
this.owner = owner; | ||
this.isComponent = isComponent || false; | ||
this._normalize = normalize || defaultNormalize; | ||
this._context = context; | ||
this.value = context.create(host, field, key); | ||
this.value = context.create(this); | ||
this._hostAtoms = hostAtoms; | ||
this.status = this.value === undefined ? ATOM_STATUS_OBSOLETE : ATOM_STATUS_ACTUAL; | ||
@@ -144,5 +146,5 @@ } | ||
Atom.prototype.toString = function toString() { | ||
var hc = this.host.constructor; | ||
var hc = this.owner.constructor; | ||
var k = this.key; | ||
return (this.host.displayName || (hc ? String(hc.displayName || hc.name) : '')) + '.' + this.field + (k ? '(' + (typeof k === 'function' ? k.displayName || k.name : String(k)) + ')' : ''); | ||
return this.field + (k ? '(' + (typeof k === 'function' ? k.displayName || k.name : String(k)) + ')' : ''); | ||
}; | ||
@@ -154,31 +156,25 @@ | ||
Atom.prototype.destroyed = function destroyed(isDestroyed) { | ||
if (isDestroyed === undefined) { | ||
return this.status === ATOM_STATUS_DESTROYED; | ||
} | ||
Atom.prototype.destructor = function destructor() { | ||
if (this.status === ATOM_STATUS_DESTROYED) return; | ||
if (isDestroyed) { | ||
if (this.status !== ATOM_STATUS_DESTROYED) { | ||
if (this._masters) { | ||
this._masters.forEach(disleadThis, this); | ||
if (this._masters) { | ||
this._masters.forEach(disleadThis, this); | ||
this._masters = null; | ||
} | ||
this._masters = null; | ||
} | ||
this._checkSlaves(); | ||
this._checkSlaves(); | ||
if (this.host.destroy !== undefined) { | ||
this.host.destroy(this.value, this.field, this.key); | ||
} | ||
var hostAtoms = this._hostAtoms; | ||
this._context.destroyHost(this); | ||
if (hostAtoms instanceof WeakMap) { | ||
hostAtoms.delete(this.owner); | ||
} else if (this._keyHash) { | ||
hostAtoms.delete(this._keyHash); | ||
} | ||
this.value = undefined; | ||
this.status = ATOM_STATUS_DESTROYED; | ||
} | ||
this._context.destroyHost(this); | ||
return true; | ||
} | ||
return false; | ||
this.value = undefined; | ||
this.status = ATOM_STATUS_DESTROYED; | ||
}; | ||
@@ -278,3 +274,3 @@ | ||
try { | ||
newValue = this._normalize(this.key === undefined ? this.host[this.field + '$'](proposedValue, force, value) : this.host[this.field + '$'](this.key, proposedValue, force, value), value); | ||
newValue = this._normalize(this.key === undefined ? this.owner[this.field + '$'](proposedValue, force, value) : this.owner[this.field + '$'](this.key, proposedValue, force, value), value); | ||
} catch (error) { | ||
@@ -368,3 +364,3 @@ if (error[catchedId] === undefined) { | ||
if (!atom.slaves) { | ||
atom.destroyed(true); | ||
atom.destructor(); | ||
} | ||
@@ -376,5 +372,5 @@ } | ||
BaseLogger.prototype.create = function create(host, field, key, namespace) {}; | ||
BaseLogger.prototype.create = function create(owner, field, key, namespace) {}; | ||
BaseLogger.prototype.destroy = function destroy(atom, namespace) {}; | ||
BaseLogger.prototype.onDestruct = function onDestruct(atom, namespace) {}; | ||
@@ -422,2 +418,3 @@ BaseLogger.prototype.status = function status(_status, atom, namespace) {}; | ||
this._namespace = '$'; | ||
this._owners = new WeakMap(); | ||
@@ -436,5 +433,7 @@ this.__run = function () { | ||
Context.prototype.create = function create(host, field, key) { | ||
Context.prototype.create = function create(atom) { | ||
this._owners.set(atom, atom.owner); | ||
if (this._logger !== undefined) { | ||
return this._logger.create(host, field, key, this._namespace); | ||
return this._logger.create(atom.owner, atom.field, atom.key, this._namespace); | ||
} | ||
@@ -444,4 +443,12 @@ }; | ||
Context.prototype.destroyHost = function destroyHost(atom) { | ||
var from = atom.value; | ||
if (from && !(from instanceof Error) && _typeof(from) === 'object' && typeof from.destructor === 'function' && this._owners.get(from) === atom) { | ||
from.destructor(); | ||
this._owners.delete(from); | ||
} | ||
if (this._logger !== undefined) { | ||
this._logger.destroy(atom, this._namespace); | ||
this._logger.onDestruct(atom, this._namespace); | ||
} | ||
@@ -455,2 +462,12 @@ }; | ||
Context.prototype.newValue = function newValue(atom, from, to, isActualize) { | ||
if (from && !(from instanceof Error) && _typeof(from) === 'object' && typeof from.destructor === 'function' && this._owners.get(from) === atom) { | ||
from.destructor(); | ||
this._owners.delete(from); | ||
} | ||
if (to && !(to instanceof Error) && _typeof(to) === 'object' && typeof to.destructor === 'function') { | ||
this._owners.set(to, atom); | ||
} | ||
if (this._logger !== undefined) { | ||
@@ -513,3 +530,3 @@ if (to instanceof AtomWait) { | ||
if (!reaping.has(atom) && !atom.destroyed()) { | ||
if (!reaping.has(atom) && atom.status !== ATOM_STATUS_DESTROYED) { | ||
atom.actualize(); | ||
@@ -555,3 +572,9 @@ } | ||
function memMethod(proto, name, descr, normalize, isComponent) { | ||
function getId(t, hk) { | ||
return (t.constructor.displayName || t.constructor.name) + "." + hk; | ||
} | ||
function memMethod(proto, rname, descr, normalize, isComponent) { | ||
var name = getId(proto, rname); | ||
if (descr.value === undefined) { | ||
@@ -570,7 +593,7 @@ throw new TypeError(name + " is not an function (next?: V)"); | ||
var forcedFn = function forcedFn(next, force) { | ||
return this[name](next, force === undefined ? true : force); | ||
return this[rname](next, force === undefined ? true : force); | ||
}; | ||
setFunctionName(forcedFn, name + "*"); | ||
proto[name + "*"] = forcedFn; | ||
proto[rname + "*"] = forcedFn; | ||
return { | ||
@@ -583,3 +606,3 @@ enumerable: descr.enumerable, | ||
if (atom === undefined) { | ||
atom = new Atom(name, this, defaultContext, normalize, undefined, isComponent); | ||
atom = new Atom(name, this, defaultContext, hostAtoms, normalize, undefined, undefined, isComponent); | ||
hostAtoms.set(this, atom); | ||
@@ -620,3 +643,4 @@ } | ||
function memProp(proto, name, descr, normalize) { | ||
function memProp(proto, rname, descr, normalize) { | ||
var name = getId(proto, rname); | ||
var handlerKey = name + "$"; | ||
@@ -646,3 +670,3 @@ | ||
if (atom === undefined) { | ||
atom = new Atom(name, this, defaultContext, normalize); | ||
atom = new Atom(name, this, defaultContext, hostAtoms, normalize); | ||
hostAtoms.set(this, atom); | ||
@@ -662,3 +686,3 @@ } | ||
if (atom === undefined) { | ||
atom = new Atom(name, this, defaultContext, normalize); | ||
atom = new Atom(name, this, defaultContext, hostAtoms, normalize); | ||
hostAtoms.set(this, atom); | ||
@@ -699,3 +723,4 @@ } | ||
function memKeyMethod(proto, name, descr, normalize) { | ||
function memKeyMethod(proto, rname, descr, normalize) { | ||
var name = getId(proto, rname); | ||
var handler = descr.value; | ||
@@ -716,7 +741,7 @@ | ||
var forcedFn = function forcedFn(rawKey, next, force) { | ||
return this[name](rawKey, next, force === undefined ? true : force); | ||
return this[rname](rawKey, next, force === undefined ? true : force); | ||
}; | ||
setFunctionName(forcedFn, name + "*"); | ||
proto[name + "*"] = forcedFn; | ||
proto[rname + "*"] = forcedFn; | ||
return { | ||
@@ -737,3 +762,3 @@ enumerable: descr.enumerable, | ||
if (atom === undefined) { | ||
atom = new Atom(name, this, defaultContext, normalize, rawKey); | ||
atom = new Atom(name, this, defaultContext, atomMap, normalize, rawKey, key); | ||
atomMap.set(key, atom); | ||
@@ -801,3 +826,3 @@ } | ||
function createActionMethod(t, hk, context) { | ||
var name = (t.displayName || t.constructor.displayName || t.constructor.name) + "." + hk; | ||
var name = getId(t, hk); | ||
@@ -804,0 +829,0 @@ function action() { |
{ | ||
"name": "lom_atom", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"description": "Alternative implementation of eigenmethod mol_atom state management library", | ||
@@ -5,0 +5,0 @@ "publishConfig": { |
@@ -9,2 +9,14 @@ import babel from 'rollup-plugin-babel' | ||
const pkg = JSON.parse(fs.readFileSync('./package.json')) | ||
const uglifyOpts = { | ||
warnings: true, | ||
compress: { | ||
dead_code: true, | ||
unused: true, | ||
toplevel: true, | ||
warnings: true | ||
}, | ||
mangle: { | ||
toplevel: true | ||
} | ||
} | ||
@@ -16,14 +28,3 @@ const commonConf = { | ||
babel(babelrc()) | ||
].concat(process.env.UGLIFY === '1' ? [uglify({ | ||
warnings: true, | ||
compress: { | ||
dead_code: true, | ||
unused: true, | ||
toplevel: true, | ||
warnings: true | ||
}, | ||
mangle: { | ||
toplevel: true | ||
} | ||
}, minify)] : []), | ||
].concat(process.env.UGLIFY === '1' ? [uglify(uglifyOpts, minify)] : []), | ||
output: [ | ||
@@ -30,0 +31,0 @@ {file: pkg.module, format: 'es'}, |
@@ -5,3 +5,4 @@ // @flow | ||
catchedId, | ||
ATOM_STATUS_DESTROYED, ATOM_STATUS_OBSOLETE, ATOM_STATUS_CHECKING, ATOM_STATUS_PULLING, ATOM_STATUS_ACTUAL | ||
ATOM_STATUS_DESTROYED, | ||
ATOM_STATUS_OBSOLETE, ATOM_STATUS_CHECKING, ATOM_STATUS_PULLING, ATOM_STATUS_ACTUAL | ||
} from './interfaces' | ||
@@ -16,3 +17,3 @@ | ||
IAtomHandler, | ||
IAtomHost | ||
IAtomOwner | ||
} from './interfaces' | ||
@@ -43,3 +44,3 @@ | ||
field: string | ||
host: IAtomHost | ||
owner: IAtomOwner | ||
value: V | void | ||
@@ -53,18 +54,24 @@ key: mixed | void | ||
_normalize: INormalize<V> | ||
_hostAtoms: WeakMap<Object, IAtom<*>> | Map<string, IAtom<*>> | ||
_keyHash: string | void | ||
constructor( | ||
field: string, | ||
host: IAtomHost, | ||
owner: IAtomOwner, | ||
context: IContext, | ||
hostAtoms: WeakMap<Object, IAtom<*>> | Map<string, IAtom<*>>, | ||
normalize?: INormalize<V>, | ||
key?: mixed, | ||
isComponent?: boolean | ||
keyHash?: string, | ||
isComponent?: boolean, | ||
) { | ||
this._keyHash = keyHash | ||
this.key = key | ||
this.field = field | ||
this.host = host | ||
this.owner = owner | ||
this.isComponent = isComponent || false | ||
this._normalize = normalize || defaultNormalize | ||
this._context = context | ||
this.value = context.create(host, field, key) | ||
this.value = context.create(this) | ||
this._hostAtoms = hostAtoms | ||
this.status = this.value === undefined ? ATOM_STATUS_OBSOLETE : ATOM_STATUS_ACTUAL | ||
@@ -78,8 +85,6 @@ } | ||
toString() { | ||
const hc = this.host.constructor | ||
const hc = this.owner.constructor | ||
const k = this.key | ||
return (this.host.displayName || (hc ? String(hc.displayName || hc.name) : '')) | ||
+ '.' | ||
+ this.field | ||
return this.field | ||
+ (k | ||
@@ -95,26 +100,18 @@ ? ('(' + (typeof k === 'function' ? (k.displayName || k.name) : String(k)) + ')') | ||
destroyed(isDestroyed?: boolean): boolean { | ||
if (isDestroyed === undefined) { | ||
return this.status === ATOM_STATUS_DESTROYED | ||
destructor(): void { | ||
if (this.status === ATOM_STATUS_DESTROYED) return | ||
if (this._masters) { | ||
this._masters.forEach(disleadThis, this) | ||
this._masters = null | ||
} | ||
if (isDestroyed) { | ||
if (this.status !== ATOM_STATUS_DESTROYED) { | ||
if (this._masters) { | ||
this._masters.forEach(disleadThis, this) | ||
this._masters = null | ||
} | ||
this._checkSlaves() | ||
if (this.host.destroy !== undefined) { | ||
this.host.destroy(this.value, this.field, this.key) | ||
} | ||
this._context.destroyHost(this) | ||
this.value = undefined | ||
this.status = ATOM_STATUS_DESTROYED | ||
} | ||
return true | ||
this._checkSlaves() | ||
const hostAtoms = this._hostAtoms | ||
if (hostAtoms instanceof WeakMap) { | ||
hostAtoms.delete(this.owner) | ||
} else if (this._keyHash) { | ||
hostAtoms.delete(this._keyHash) | ||
} | ||
return false | ||
this._context.destroyHost(this) | ||
this.value = undefined | ||
this.status = ATOM_STATUS_DESTROYED | ||
} | ||
@@ -209,4 +206,4 @@ | ||
this.key === undefined | ||
? (this.host: any)[this.field + '$'](proposedValue, force, value) | ||
: (this.host: any)[this.field + '$'](this.key, proposedValue, force, value), | ||
? (this.owner: any)[this.field + '$'](proposedValue, force, value) | ||
: (this.owner: any)[this.field + '$'](this.key, proposedValue, force, value), | ||
value | ||
@@ -213,0 +210,0 @@ ) |
@@ -12,2 +12,3 @@ // @flow | ||
} from './interfaces' | ||
import {ATOM_STATUS_DESTROYED, ATOM_STATUS_ACTUAL} from './interfaces' | ||
import {AtomWait} from './utils' | ||
@@ -23,3 +24,3 @@ import Atom from './Atom' | ||
if (!atom.slaves) { | ||
atom.destroyed(true) | ||
atom.destructor() | ||
} | ||
@@ -29,4 +30,4 @@ } | ||
export class BaseLogger implements ILogger { | ||
create<V>(host: Object, field: string, key?: mixed, namespace: string): V | void {} | ||
destroy(atom: IAtom<*>, namespace: string): void {} | ||
create<V>(owner: Object, field: string, key?: mixed, namespace: string): V | void {} | ||
onDestruct(atom: IAtom<*>, namespace: string): void {} | ||
status(status: ILoggerStatus, atom: IAtom<*>, namespace: string): void {} | ||
@@ -59,6 +60,8 @@ error<V>(atom: IAtom<V>, err: Error, namespace: string): void {} | ||
_namespace: string = '$' | ||
_owners: WeakMap<Object, Object> = new WeakMap() | ||
create<V>(host: Object, field: string, key?: mixed): V | void { | ||
create<V>(atom: IAtomInt): V | void { | ||
this._owners.set(atom, atom.owner) | ||
if (this._logger !== undefined) { | ||
return this._logger.create(host, field, key, this._namespace) | ||
return this._logger.create(atom.owner, atom.field, atom.key, this._namespace) | ||
} | ||
@@ -68,4 +71,15 @@ } | ||
destroyHost(atom: IAtomInt) { | ||
const from = atom.value | ||
if ( | ||
from | ||
&& !(from instanceof Error) | ||
&& typeof from === 'object' | ||
&& typeof from.destructor === 'function' | ||
&& this._owners.get(from) === atom | ||
) { | ||
from.destructor() | ||
this._owners.delete(from) | ||
} | ||
if (this._logger !== undefined) { | ||
this._logger.destroy(atom, this._namespace) | ||
this._logger.onDestruct(atom, this._namespace) | ||
} | ||
@@ -79,2 +93,20 @@ } | ||
newValue<V>(atom: IAtom<V>, from?: V | Error, to: V | Error, isActualize?: boolean) { | ||
if ( | ||
from | ||
&& !(from instanceof Error) | ||
&& typeof from === 'object' | ||
&& typeof from.destructor === 'function' | ||
&& this._owners.get(from) === atom | ||
) { | ||
from.destructor() | ||
this._owners.delete(from) | ||
} | ||
if ( | ||
to | ||
&& !(to instanceof Error) | ||
&& typeof to === 'object' | ||
&& typeof to.destructor === 'function' | ||
) { | ||
this._owners.set(to, atom) | ||
} | ||
if (this._logger !== undefined) { | ||
@@ -138,3 +170,3 @@ if (to instanceof AtomWait) { | ||
const atom: IAtomInt = updating[i] | ||
if (!reaping.has(atom) && !atom.destroyed()) { | ||
if (!reaping.has(atom) && atom.status !== ATOM_STATUS_DESTROYED) { | ||
atom.actualize() | ||
@@ -141,0 +173,0 @@ } |
@@ -13,12 +13,12 @@ // @flow | ||
* | ||
* @param host Object Object with atom | ||
* @param owner Object Object with atom | ||
* @param field string property name | ||
* @param key mixed | void for dictionary atoms - dictionary key | ||
*/ | ||
create<V>(host: Object, field: string, key?: mixed, namespace: string): V | void; | ||
create<V>(owner: Object, field: string, key?: mixed, namespace: string): V | void; | ||
/** | ||
* After atom destroy | ||
* After atom destructor | ||
*/ | ||
destroy(atom: IAtom<*>, namespace: string): void; | ||
onDestruct(atom: IAtom<*>, namespace: string): void; | ||
@@ -47,3 +47,3 @@ /** | ||
last: ?IAtomInt; | ||
create<V>(host: Object, field: string, key?: mixed): V | void; | ||
create<V>(atom: IAtomInt): V | void; | ||
destroyHost(atom: IAtomInt): void; | ||
@@ -58,3 +58,2 @@ newValue<V>(t: IAtom<V>, from?: V | Error, to: V | Error, isActualize?: boolean): void; | ||
} | ||
export const ATOM_STATUS_DESTROYED = 0 | ||
@@ -68,3 +67,3 @@ export const ATOM_STATUS_OBSOLETE = 1 | ||
export type IAtomStatus = typeof ATOM_STATUS_DESTROYED | typeof ATOM_STATUS_OBSOLETE | ||
export type IAtomStatus = typeof ATOM_STATUS_OBSOLETE | ||
| typeof ATOM_STATUS_CHECKING | typeof ATOM_STATUS_PULLING | typeof ATOM_STATUS_ACTUAL | ||
@@ -79,3 +78,3 @@ | ||
set(v: V | Error, force?: boolean): V; | ||
destroyed(isDestroyed?: boolean): boolean; | ||
destructor(): void; | ||
} | ||
@@ -86,3 +85,3 @@ | ||
key: mixed | void; | ||
host: IAtomHost; | ||
owner: IAtomOwner; | ||
@@ -101,6 +100,6 @@ actualize(): void; | ||
export interface IAtomHost { | ||
export interface IAtomOwner { | ||
displayName?: string; | ||
[key: string]: IAtomHandler<*, *>; | ||
destroy?: (value: mixed, field: string, key?: mixed) => void; | ||
destructor?: (value: mixed, field: string, key?: mixed) => void; | ||
} |
// @flow | ||
import type {IAtom, IAtomHandler, IAtomHost, INormalize, IContext} from './interfaces' | ||
import type {IAtom, IAtomHandler, INormalize, IContext} from './interfaces' | ||
import {defaultContext} from './Context' | ||
@@ -18,5 +18,9 @@ import {AtomWait} from './utils' | ||
function getId(t: Object, hk: string): string { | ||
return `${t.constructor.displayName || t.constructor.name}.${hk}` | ||
} | ||
function memMethod<V, P: Object>( | ||
proto: P, | ||
name: string, | ||
rname: string, | ||
descr: TypedPropertyDescriptor<*>, | ||
@@ -26,2 +30,3 @@ normalize?: INormalize<V>, | ||
): TypedPropertyDescriptor<*> { | ||
const name = getId(proto, rname) | ||
if (descr.value === undefined) { | ||
@@ -39,6 +44,6 @@ throw new TypeError(`${name} is not an function (next?: V)`) | ||
const forcedFn = function (next?: V | Error, force?: boolean) { | ||
return this[name](next, force === undefined ? true : force) | ||
return this[rname](next, force === undefined ? true : force) | ||
} | ||
setFunctionName(forcedFn, `${name}*`) | ||
proto[`${name}*`] = forcedFn | ||
proto[`${rname}*`] = forcedFn | ||
@@ -51,3 +56,3 @@ return { | ||
if (atom === undefined) { | ||
atom = new Atom(name, this, defaultContext, normalize, undefined, isComponent) | ||
atom = new Atom(name, this, defaultContext, hostAtoms, normalize, undefined, undefined, isComponent) | ||
hostAtoms.set(this, atom) | ||
@@ -93,6 +98,7 @@ } | ||
proto: P, | ||
name: string, | ||
rname: string, | ||
descr: TypedPropertyDescriptor<V>, | ||
normalize?: INormalize<V> | ||
): TypedPropertyDescriptor<V> { | ||
const name = getId(proto, rname) | ||
const handlerKey = `${name}$` | ||
@@ -126,3 +132,3 @@ if (proto[handlerKey] !== undefined) { | ||
if (atom === undefined) { | ||
atom = new Atom(name, this, defaultContext, normalize) | ||
atom = new Atom(name, this, defaultContext, hostAtoms, normalize) | ||
hostAtoms.set(this, atom) | ||
@@ -139,3 +145,3 @@ } | ||
if (atom === undefined) { | ||
atom = new Atom(name, this, defaultContext, normalize) | ||
atom = new Atom(name, this, defaultContext, hostAtoms, normalize) | ||
hostAtoms.set(this, atom) | ||
@@ -177,6 +183,7 @@ } | ||
proto: P, | ||
name: string, | ||
rname: string, | ||
descr: TypedPropertyDescriptor<IAtomHandler<V, K>>, | ||
normalize?: INormalize<V> | ||
): TypedPropertyDescriptor<IAtomHandler<V, K>> { | ||
const name = getId(proto, rname) | ||
const handler = descr.value | ||
@@ -194,6 +201,6 @@ if (handler === undefined) { | ||
const forcedFn = function (rawKey: K, next?: V | Error, force?: boolean) { | ||
return this[name](rawKey, next, force === undefined ? true : force) | ||
return this[rname](rawKey, next, force === undefined ? true : force) | ||
} | ||
setFunctionName(forcedFn, `${name}*`) | ||
proto[`${name}*`] = forcedFn | ||
proto[`${rname}*`] = forcedFn | ||
@@ -212,3 +219,3 @@ return { | ||
if (atom === undefined) { | ||
atom = new Atom(name, this, defaultContext, normalize, rawKey) | ||
atom = new Atom(name, this, defaultContext, atomMap, normalize, rawKey, key) | ||
atomMap.set(key, atom) | ||
@@ -311,3 +318,3 @@ } | ||
function createActionMethod(t: Object, hk: string, context: IContext): (...args: any[]) => any { | ||
const name = `${t.displayName || t.constructor.displayName || t.constructor.name}.${hk}` | ||
const name = getId(t, hk) | ||
function action() { | ||
@@ -314,0 +321,0 @@ let result: mixed | void |
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
277665
3396