Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

lom_atom

Package Overview
Dependencies
Maintainers
1
Versions
60
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

lom_atom - npm Package Compare versions

Comparing version 2.0.1 to 2.0.2

src/conform.js

5

CHANGELOG.md

@@ -5,2 +5,7 @@ # Change Log

<a name="2.0.2"></a>
## [2.0.2](https://github.com/zerkalica/lom_atom/compare/v2.0.1...v2.0.2) (2017-10-21)
<a name="2.0.1"></a>

@@ -7,0 +12,0 @@ ## [2.0.1](https://github.com/zerkalica/lom_atom/compare/v2.0.0...v2.0.1) (2017-10-11)

216

dist/lom_atom.es.js

@@ -72,17 +72,2 @@ // eslint-disable-line

}
function defaultNormalize(next, prev) {
if (next === prev) return next;
if (next instanceof Array && prev instanceof Array && next.length === prev.length) {
for (var i = 0; i < next.length; i++) {
if (next[i] !== prev[i]) {
return next;
}
}
return prev;
}
return next;
}
var AtomWait = function (_Error) {

@@ -105,2 +90,46 @@ inheritsLoose(AtomWait, _Error);

var handlers = new Map([[Array, function arrayHandler(target, source, stack) {
var equal = target.length === source.length;
for (var i = 0; i < target.length; ++i) {
var conformed = target[i] = conform(target[i], source[i], stack);
if (equal && conformed !== source[i]) equal = false;
}
return equal ? source : target;
}], [Object, function objectHandler(target, source, stack) {
var count = 0;
var equal = true;
for (var key in target) {
var conformed = target[key] = conform(target[key], source[key], stack);
if (equal && conformed !== source[key]) equal = false;
++count;
}
for (var _key in source) {
if (--count < 0) break;
}
return equal && count === 0 ? source : target;
}], [Date, function dateHandler(target, source) {
return target.getTime() === source.getTime() ? source : target;
}], [RegExp, function dateHandler(target, source) {
return target.toString() === source.toString() ? source : target;
}]]);
function conform(target, source) {
var stack = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
if (target === source) return source;
if (!target || _typeof(target) !== 'object') return target;
if (!source || _typeof(source) !== 'object') return target;
if (target.constructor !== source.constructor) return target;
var conformHandler = handlers.get(target.constructor);
if (!conformHandler) return target;
if (stack.indexOf(target) !== -1) return target;
stack.push(target);
var res = conformHandler(target, source, stack);
stack.pop();
return res;
}
function checkSlave(slave) {

@@ -115,2 +144,3 @@ slave.check();

function disleadThis(master) {
this;
master.dislead(this);

@@ -120,2 +150,4 @@ }

function actualizeMaster(master) {
this;
if (this.status === ATOM_STATUS_CHECKING) {

@@ -127,3 +159,3 @@ master.actualize();

var Atom = function () {
function Atom(field, owner, context, hostAtoms, normalize, key, keyHash, isComponent) {
function Atom(field, owner, context, hostAtoms, key, keyHash, isComponent) {
this._masters = null;

@@ -136,5 +168,6 @@ this._slaves = null;

this.isComponent = isComponent || false;
this._normalize = normalize || defaultNormalize;
this._context = context;
this.value = context.create(this);
this._next = undefined;
this._ignore = undefined;
this._hostAtoms = hostAtoms;

@@ -165,14 +198,13 @@ this.status = this.value === undefined ? ATOM_STATUS_OBSOLETE : ATOM_STATUS_ACTUAL;

var hostAtoms = this._hostAtoms;
this._hostAtoms.delete(this._keyHash || this.owner);
if (hostAtoms instanceof WeakMap) {
hostAtoms.delete(this.owner);
} else if (this._keyHash) {
hostAtoms.delete(this._keyHash);
}
this._context.destroyHost(this);
this.value = undefined;
this._next = undefined;
this._ignore = undefined;
this.status = ATOM_STATUS_DESTROYED;
this._hostAtoms = undefined;
this.key = undefined;
this._keyHash = undefined;
};

@@ -197,3 +229,3 @@

if (force) {
this._pullPush(undefined, true);
this._push(this._pull(true));
} else {

@@ -206,33 +238,19 @@ this.actualize();

Atom.prototype.set = function set$$1(v, force) {
var oldValue = this.value;
Atom.prototype.set = function set$$1(next, force) {
if (force) return this._push(next);
var normalized = conform(next, this._ignore);
var normalized = this._normalize(v, oldValue);
if (oldValue === normalized) {
return normalized;
if (normalized === this._ignore) {
return this.value;
}
if (normalized === undefined) {
return oldValue;
}
if (force || normalized instanceof Error) {
this.status = ATOM_STATUS_ACTUAL;
this.value = normalized instanceof Error ? createMock(normalized) : normalized;
this._context.newValue(this, oldValue, normalized);
if (this._slaves) {
this._slaves.forEach(obsoleteSlave);
}
} else {
this.obsolete();
this.actualize(normalized);
}
normalized = conform(next, this.value);
if (normalized === this.value) return this.value;
this._ignore = this._next = normalized;
this.obsolete();
this.actualize();
return this.value;
};
Atom.prototype.actualize = function actualize(proposedValue) {
Atom.prototype.actualize = function actualize() {
if (this.status === ATOM_STATUS_PULLING) {

@@ -242,5 +260,3 @@ throw new Error("Cyclic atom dependency of " + String(this));

if (this.status === ATOM_STATUS_ACTUAL) {
return;
}
if (this.status === ATOM_STATUS_ACTUAL) return;

@@ -258,7 +274,32 @@ if (this.status === ATOM_STATUS_CHECKING) {

if (this.status !== ATOM_STATUS_ACTUAL) {
this._pullPush(proposedValue);
this._push(this._pull());
}
};
Atom.prototype._pullPush = function _pullPush(proposedValue, force) {
Atom.prototype._push = function _push(nextRaw) {
this.status = ATOM_STATUS_ACTUAL;
if (!(nextRaw instanceof AtomWait)) {
this._ignore = this._next;
this._next = undefined;
}
var prev = this.value;
if (nextRaw === undefined) return prev;
var next = nextRaw instanceof Error ? createMock(nextRaw) : prev instanceof Error ? nextRaw : conform(nextRaw, prev);
if (prev !== next) {
this.value = next;
this._context.newValue(this, prev, next, true);
if (this._slaves) {
this._slaves.forEach(obsoleteSlave);
}
}
return next;
};
Atom.prototype._pull = function _pull(force) {
if (this._masters) {

@@ -273,6 +314,5 @@ this._masters.forEach(disleadThis, this);

context.last = this;
var value = this.value;
try {
newValue = this._normalize(this.key === undefined ? this.owner[this.field + '$'](proposedValue, force, value) : this.owner[this.field + '$'](this.key, proposedValue, force, value), value);
newValue = this.key === undefined ? this.owner[this.field + '$'](this._next, force, this.value) : this.owner[this.field + '$'](this.key, this._next, force, this.value);
} catch (error) {

@@ -284,17 +324,7 @@ if (error[catchedId] === undefined) {

newValue = createMock(error);
newValue = error instanceof Error ? error : new Error(error.stack || error);
}
context.last = slave;
this.status = ATOM_STATUS_ACTUAL;
if (newValue !== undefined && value !== newValue) {
this.value = newValue;
this._context.newValue(this, value, newValue, true);
if (this._slaves) {
this._slaves.forEach(obsoleteSlave);
}
}
return newValue;
};

@@ -378,2 +408,4 @@

BaseLogger.prototype.sync = function sync() {};
BaseLogger.prototype.status = function status(_status, atom, namespace) {};

@@ -395,2 +427,6 @@

ConsoleLogger.prototype.sync = function sync() {
console.log('sync');
};
ConsoleLogger.prototype.status = function status(_status2, atom, namespace) {

@@ -443,5 +479,3 @@ console.log(namespace, _status2, atom.displayName);

Context.prototype.destroyHost = function destroyHost(atom) {
var from = atom.value;
Context.prototype._destroyValue = function _destroyValue(atom, from) {
if (from && !(from instanceof Error) && _typeof(from) === 'object' && typeof from.destructor === 'function' && this._owners.get(from) === atom) {

@@ -452,3 +486,7 @@ from.destructor();

}
};
Context.prototype.destroyHost = function destroyHost(atom) {
this._destroyValue(atom, atom.value);
if (this._logger !== undefined) {

@@ -464,8 +502,4 @@ this._logger.onDestruct(atom, this._namespace);

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._destroyValue(atom, from);
this._owners.delete(from);
}
if (to && !(to instanceof Error) && _typeof(to) === 'object' && typeof to.destructor === 'function') {

@@ -481,3 +515,3 @@ this._owners.set(to, atom);

} else {
this._logger.newValue(atom, from, to, isActualize, this._namespace);
this._logger.newValue(atom, from instanceof Error ? undefined : from, to, isActualize, this._namespace);
}

@@ -525,2 +559,6 @@ }

if (this._logger !== undefined) {
this._logger.sync();
}
do {

@@ -579,3 +617,3 @@ var end = updating.length;

function memMethod(proto, rname, descr, normalize, isComponent) {
function memMethod(proto, rname, descr, isComponent) {
var name = getId(proto, rname);

@@ -608,3 +646,3 @@

if (atom === undefined) {
atom = new Atom(name, this, defaultContext, hostAtoms, normalize, undefined, undefined, isComponent);
atom = new Atom(name, this, defaultContext, hostAtoms, undefined, undefined, isComponent);
hostAtoms.set(this, atom);

@@ -645,3 +683,3 @@ }

function memProp(proto, rname, descr, normalize) {
function memProp(proto, rname, descr) {
var name = getId(proto, rname);

@@ -672,3 +710,3 @@ var handlerKey = name + "$";

if (atom === undefined) {
atom = new Atom(name, this, defaultContext, hostAtoms, normalize);
atom = new Atom(name, this, defaultContext, hostAtoms);
hostAtoms.set(this, atom);

@@ -688,3 +726,3 @@ }

if (atom === undefined) {
atom = new Atom(name, this, defaultContext, hostAtoms, normalize);
atom = new Atom(name, this, defaultContext, hostAtoms);
hostAtoms.set(this, atom);

@@ -725,3 +763,3 @@ }

function memKeyMethod(proto, rname, descr, normalize) {
function memKeyMethod(proto, rname, descr) {
var name = getId(proto, rname);

@@ -763,3 +801,3 @@ var handler = descr.value;

if (atom === undefined) {
atom = new Atom(name, this, defaultContext, atomMap, normalize, rawKey, key);
atom = new Atom(name, this, defaultContext, atomMap, rawKey, key);
atomMap.set(key, atom);

@@ -778,5 +816,4 @@ }

var normalize = arguments[0];
return function (proto, name, descr) {
return memKeyMethod(proto, name, descr, normalize);
return memKeyMethod(proto, name, descr);
};

@@ -824,3 +861,3 @@ }

function detached(proto, name, descr) {
return memMethod(proto, name, descr, undefined, true);
return memMethod(proto, name, descr, true);
}

@@ -963,5 +1000,4 @@

var normalize = arguments[0];
return function (proto, name, descr) {
return descr.value === undefined ? memProp(proto, name, descr, normalize) : memMethod(proto, name, descr, normalize);
return descr.value === undefined ? memProp(proto, name, descr) : memMethod(proto, name, descr);
};

@@ -968,0 +1004,0 @@ }

@@ -76,17 +76,2 @@ 'use strict';

}
function defaultNormalize(next, prev) {
if (next === prev) return next;
if (next instanceof Array && prev instanceof Array && next.length === prev.length) {
for (var i = 0; i < next.length; i++) {
if (next[i] !== prev[i]) {
return next;
}
}
return prev;
}
return next;
}
var AtomWait = function (_Error) {

@@ -109,2 +94,46 @@ inheritsLoose(AtomWait, _Error);

var handlers = new Map([[Array, function arrayHandler(target, source, stack) {
var equal = target.length === source.length;
for (var i = 0; i < target.length; ++i) {
var conformed = target[i] = conform(target[i], source[i], stack);
if (equal && conformed !== source[i]) equal = false;
}
return equal ? source : target;
}], [Object, function objectHandler(target, source, stack) {
var count = 0;
var equal = true;
for (var key in target) {
var conformed = target[key] = conform(target[key], source[key], stack);
if (equal && conformed !== source[key]) equal = false;
++count;
}
for (var _key in source) {
if (--count < 0) break;
}
return equal && count === 0 ? source : target;
}], [Date, function dateHandler(target, source) {
return target.getTime() === source.getTime() ? source : target;
}], [RegExp, function dateHandler(target, source) {
return target.toString() === source.toString() ? source : target;
}]]);
function conform(target, source) {
var stack = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
if (target === source) return source;
if (!target || _typeof(target) !== 'object') return target;
if (!source || _typeof(source) !== 'object') return target;
if (target.constructor !== source.constructor) return target;
var conformHandler = handlers.get(target.constructor);
if (!conformHandler) return target;
if (stack.indexOf(target) !== -1) return target;
stack.push(target);
var res = conformHandler(target, source, stack);
stack.pop();
return res;
}
function checkSlave(slave) {

@@ -119,2 +148,3 @@ slave.check();

function disleadThis(master) {
this;
master.dislead(this);

@@ -124,2 +154,4 @@ }

function actualizeMaster(master) {
this;
if (this.status === ATOM_STATUS_CHECKING) {

@@ -131,3 +163,3 @@ master.actualize();

var Atom = function () {
function Atom(field, owner, context, hostAtoms, normalize, key, keyHash, isComponent) {
function Atom(field, owner, context, hostAtoms, key, keyHash, isComponent) {
this._masters = null;

@@ -140,5 +172,6 @@ this._slaves = null;

this.isComponent = isComponent || false;
this._normalize = normalize || defaultNormalize;
this._context = context;
this.value = context.create(this);
this._next = undefined;
this._ignore = undefined;
this._hostAtoms = hostAtoms;

@@ -169,14 +202,13 @@ this.status = this.value === undefined ? ATOM_STATUS_OBSOLETE : ATOM_STATUS_ACTUAL;

var hostAtoms = this._hostAtoms;
this._hostAtoms.delete(this._keyHash || this.owner);
if (hostAtoms instanceof WeakMap) {
hostAtoms.delete(this.owner);
} else if (this._keyHash) {
hostAtoms.delete(this._keyHash);
}
this._context.destroyHost(this);
this.value = undefined;
this._next = undefined;
this._ignore = undefined;
this.status = ATOM_STATUS_DESTROYED;
this._hostAtoms = undefined;
this.key = undefined;
this._keyHash = undefined;
};

@@ -201,3 +233,3 @@

if (force) {
this._pullPush(undefined, true);
this._push(this._pull(true));
} else {

@@ -210,33 +242,19 @@ this.actualize();

Atom.prototype.set = function set$$1(v, force) {
var oldValue = this.value;
Atom.prototype.set = function set$$1(next, force) {
if (force) return this._push(next);
var normalized = conform(next, this._ignore);
var normalized = this._normalize(v, oldValue);
if (oldValue === normalized) {
return normalized;
if (normalized === this._ignore) {
return this.value;
}
if (normalized === undefined) {
return oldValue;
}
if (force || normalized instanceof Error) {
this.status = ATOM_STATUS_ACTUAL;
this.value = normalized instanceof Error ? createMock(normalized) : normalized;
this._context.newValue(this, oldValue, normalized);
if (this._slaves) {
this._slaves.forEach(obsoleteSlave);
}
} else {
this.obsolete();
this.actualize(normalized);
}
normalized = conform(next, this.value);
if (normalized === this.value) return this.value;
this._ignore = this._next = normalized;
this.obsolete();
this.actualize();
return this.value;
};
Atom.prototype.actualize = function actualize(proposedValue) {
Atom.prototype.actualize = function actualize() {
if (this.status === ATOM_STATUS_PULLING) {

@@ -246,5 +264,3 @@ throw new Error("Cyclic atom dependency of " + String(this));

if (this.status === ATOM_STATUS_ACTUAL) {
return;
}
if (this.status === ATOM_STATUS_ACTUAL) return;

@@ -262,7 +278,32 @@ if (this.status === ATOM_STATUS_CHECKING) {

if (this.status !== ATOM_STATUS_ACTUAL) {
this._pullPush(proposedValue);
this._push(this._pull());
}
};
Atom.prototype._pullPush = function _pullPush(proposedValue, force) {
Atom.prototype._push = function _push(nextRaw) {
this.status = ATOM_STATUS_ACTUAL;
if (!(nextRaw instanceof AtomWait)) {
this._ignore = this._next;
this._next = undefined;
}
var prev = this.value;
if (nextRaw === undefined) return prev;
var next = nextRaw instanceof Error ? createMock(nextRaw) : prev instanceof Error ? nextRaw : conform(nextRaw, prev);
if (prev !== next) {
this.value = next;
this._context.newValue(this, prev, next, true);
if (this._slaves) {
this._slaves.forEach(obsoleteSlave);
}
}
return next;
};
Atom.prototype._pull = function _pull(force) {
if (this._masters) {

@@ -277,6 +318,5 @@ this._masters.forEach(disleadThis, this);

context.last = this;
var value = this.value;
try {
newValue = this._normalize(this.key === undefined ? this.owner[this.field + '$'](proposedValue, force, value) : this.owner[this.field + '$'](this.key, proposedValue, force, value), value);
newValue = this.key === undefined ? this.owner[this.field + '$'](this._next, force, this.value) : this.owner[this.field + '$'](this.key, this._next, force, this.value);
} catch (error) {

@@ -288,17 +328,7 @@ if (error[catchedId] === undefined) {

newValue = createMock(error);
newValue = error instanceof Error ? error : new Error(error.stack || error);
}
context.last = slave;
this.status = ATOM_STATUS_ACTUAL;
if (newValue !== undefined && value !== newValue) {
this.value = newValue;
this._context.newValue(this, value, newValue, true);
if (this._slaves) {
this._slaves.forEach(obsoleteSlave);
}
}
return newValue;
};

@@ -382,2 +412,4 @@

BaseLogger.prototype.sync = function sync() {};
BaseLogger.prototype.status = function status(_status, atom, namespace) {};

@@ -399,2 +431,6 @@

ConsoleLogger.prototype.sync = function sync() {
console.log('sync');
};
ConsoleLogger.prototype.status = function status(_status2, atom, namespace) {

@@ -447,5 +483,3 @@ console.log(namespace, _status2, atom.displayName);

Context.prototype.destroyHost = function destroyHost(atom) {
var from = atom.value;
Context.prototype._destroyValue = function _destroyValue(atom, from) {
if (from && !(from instanceof Error) && _typeof(from) === 'object' && typeof from.destructor === 'function' && this._owners.get(from) === atom) {

@@ -456,3 +490,7 @@ from.destructor();

}
};
Context.prototype.destroyHost = function destroyHost(atom) {
this._destroyValue(atom, atom.value);
if (this._logger !== undefined) {

@@ -468,8 +506,4 @@ this._logger.onDestruct(atom, this._namespace);

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._destroyValue(atom, from);
this._owners.delete(from);
}
if (to && !(to instanceof Error) && _typeof(to) === 'object' && typeof to.destructor === 'function') {

@@ -485,3 +519,3 @@ this._owners.set(to, atom);

} else {
this._logger.newValue(atom, from, to, isActualize, this._namespace);
this._logger.newValue(atom, from instanceof Error ? undefined : from, to, isActualize, this._namespace);
}

@@ -529,2 +563,6 @@ }

if (this._logger !== undefined) {
this._logger.sync();
}
do {

@@ -583,3 +621,3 @@ var end = updating.length;

function memMethod(proto, rname, descr, normalize, isComponent) {
function memMethod(proto, rname, descr, isComponent) {
var name = getId(proto, rname);

@@ -612,3 +650,3 @@

if (atom === undefined) {
atom = new Atom(name, this, defaultContext, hostAtoms, normalize, undefined, undefined, isComponent);
atom = new Atom(name, this, defaultContext, hostAtoms, undefined, undefined, isComponent);
hostAtoms.set(this, atom);

@@ -649,3 +687,3 @@ }

function memProp(proto, rname, descr, normalize) {
function memProp(proto, rname, descr) {
var name = getId(proto, rname);

@@ -676,3 +714,3 @@ var handlerKey = name + "$";

if (atom === undefined) {
atom = new Atom(name, this, defaultContext, hostAtoms, normalize);
atom = new Atom(name, this, defaultContext, hostAtoms);
hostAtoms.set(this, atom);

@@ -692,3 +730,3 @@ }

if (atom === undefined) {
atom = new Atom(name, this, defaultContext, hostAtoms, normalize);
atom = new Atom(name, this, defaultContext, hostAtoms);
hostAtoms.set(this, atom);

@@ -729,3 +767,3 @@ }

function memKeyMethod(proto, rname, descr, normalize) {
function memKeyMethod(proto, rname, descr) {
var name = getId(proto, rname);

@@ -767,3 +805,3 @@ var handler = descr.value;

if (atom === undefined) {
atom = new Atom(name, this, defaultContext, atomMap, normalize, rawKey, key);
atom = new Atom(name, this, defaultContext, atomMap, rawKey, key);
atomMap.set(key, atom);

@@ -782,5 +820,4 @@ }

var normalize = arguments[0];
return function (proto, name, descr) {
return memKeyMethod(proto, name, descr, normalize);
return memKeyMethod(proto, name, descr);
};

@@ -828,3 +865,3 @@ }

function detached(proto, name, descr) {
return memMethod(proto, name, descr, undefined, true);
return memMethod(proto, name, descr, true);
}

@@ -967,5 +1004,4 @@

var normalize = arguments[0];
return function (proto, name, descr) {
return descr.value === undefined ? memProp(proto, name, descr, normalize) : memMethod(proto, name, descr, normalize);
return descr.value === undefined ? memProp(proto, name, descr) : memMethod(proto, name, descr);
};

@@ -972,0 +1008,0 @@ }

@@ -78,17 +78,2 @@ (function (global, factory) {

}
function defaultNormalize(next, prev) {
if (next === prev) return next;
if (next instanceof Array && prev instanceof Array && next.length === prev.length) {
for (var i = 0; i < next.length; i++) {
if (next[i] !== prev[i]) {
return next;
}
}
return prev;
}
return next;
}
var AtomWait = function (_Error) {

@@ -111,2 +96,46 @@ inheritsLoose(AtomWait, _Error);

var handlers = new Map([[Array, function arrayHandler(target, source, stack) {
var equal = target.length === source.length;
for (var i = 0; i < target.length; ++i) {
var conformed = target[i] = conform(target[i], source[i], stack);
if (equal && conformed !== source[i]) equal = false;
}
return equal ? source : target;
}], [Object, function objectHandler(target, source, stack) {
var count = 0;
var equal = true;
for (var key in target) {
var conformed = target[key] = conform(target[key], source[key], stack);
if (equal && conformed !== source[key]) equal = false;
++count;
}
for (var _key in source) {
if (--count < 0) break;
}
return equal && count === 0 ? source : target;
}], [Date, function dateHandler(target, source) {
return target.getTime() === source.getTime() ? source : target;
}], [RegExp, function dateHandler(target, source) {
return target.toString() === source.toString() ? source : target;
}]]);
function conform(target, source) {
var stack = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
if (target === source) return source;
if (!target || _typeof(target) !== 'object') return target;
if (!source || _typeof(source) !== 'object') return target;
if (target.constructor !== source.constructor) return target;
var conformHandler = handlers.get(target.constructor);
if (!conformHandler) return target;
if (stack.indexOf(target) !== -1) return target;
stack.push(target);
var res = conformHandler(target, source, stack);
stack.pop();
return res;
}
function checkSlave(slave) {

@@ -121,2 +150,3 @@ slave.check();

function disleadThis(master) {
this;
master.dislead(this);

@@ -126,2 +156,4 @@ }

function actualizeMaster(master) {
this;
if (this.status === ATOM_STATUS_CHECKING) {

@@ -133,3 +165,3 @@ master.actualize();

var Atom = function () {
function Atom(field, owner, context, hostAtoms, normalize, key, keyHash, isComponent) {
function Atom(field, owner, context, hostAtoms, key, keyHash, isComponent) {
this._masters = null;

@@ -142,5 +174,6 @@ this._slaves = null;

this.isComponent = isComponent || false;
this._normalize = normalize || defaultNormalize;
this._context = context;
this.value = context.create(this);
this._next = undefined;
this._ignore = undefined;
this._hostAtoms = hostAtoms;

@@ -171,14 +204,13 @@ this.status = this.value === undefined ? ATOM_STATUS_OBSOLETE : ATOM_STATUS_ACTUAL;

var hostAtoms = this._hostAtoms;
this._hostAtoms.delete(this._keyHash || this.owner);
if (hostAtoms instanceof WeakMap) {
hostAtoms.delete(this.owner);
} else if (this._keyHash) {
hostAtoms.delete(this._keyHash);
}
this._context.destroyHost(this);
this.value = undefined;
this._next = undefined;
this._ignore = undefined;
this.status = ATOM_STATUS_DESTROYED;
this._hostAtoms = undefined;
this.key = undefined;
this._keyHash = undefined;
};

@@ -203,3 +235,3 @@

if (force) {
this._pullPush(undefined, true);
this._push(this._pull(true));
} else {

@@ -212,33 +244,19 @@ this.actualize();

Atom.prototype.set = function set$$1(v, force) {
var oldValue = this.value;
Atom.prototype.set = function set$$1(next, force) {
if (force) return this._push(next);
var normalized = conform(next, this._ignore);
var normalized = this._normalize(v, oldValue);
if (oldValue === normalized) {
return normalized;
if (normalized === this._ignore) {
return this.value;
}
if (normalized === undefined) {
return oldValue;
}
if (force || normalized instanceof Error) {
this.status = ATOM_STATUS_ACTUAL;
this.value = normalized instanceof Error ? createMock(normalized) : normalized;
this._context.newValue(this, oldValue, normalized);
if (this._slaves) {
this._slaves.forEach(obsoleteSlave);
}
} else {
this.obsolete();
this.actualize(normalized);
}
normalized = conform(next, this.value);
if (normalized === this.value) return this.value;
this._ignore = this._next = normalized;
this.obsolete();
this.actualize();
return this.value;
};
Atom.prototype.actualize = function actualize(proposedValue) {
Atom.prototype.actualize = function actualize() {
if (this.status === ATOM_STATUS_PULLING) {

@@ -248,5 +266,3 @@ throw new Error("Cyclic atom dependency of " + String(this));

if (this.status === ATOM_STATUS_ACTUAL) {
return;
}
if (this.status === ATOM_STATUS_ACTUAL) return;

@@ -264,7 +280,32 @@ if (this.status === ATOM_STATUS_CHECKING) {

if (this.status !== ATOM_STATUS_ACTUAL) {
this._pullPush(proposedValue);
this._push(this._pull());
}
};
Atom.prototype._pullPush = function _pullPush(proposedValue, force) {
Atom.prototype._push = function _push(nextRaw) {
this.status = ATOM_STATUS_ACTUAL;
if (!(nextRaw instanceof AtomWait)) {
this._ignore = this._next;
this._next = undefined;
}
var prev = this.value;
if (nextRaw === undefined) return prev;
var next = nextRaw instanceof Error ? createMock(nextRaw) : prev instanceof Error ? nextRaw : conform(nextRaw, prev);
if (prev !== next) {
this.value = next;
this._context.newValue(this, prev, next, true);
if (this._slaves) {
this._slaves.forEach(obsoleteSlave);
}
}
return next;
};
Atom.prototype._pull = function _pull(force) {
if (this._masters) {

@@ -279,6 +320,5 @@ this._masters.forEach(disleadThis, this);

context.last = this;
var value = this.value;
try {
newValue = this._normalize(this.key === undefined ? this.owner[this.field + '$'](proposedValue, force, value) : this.owner[this.field + '$'](this.key, proposedValue, force, value), value);
newValue = this.key === undefined ? this.owner[this.field + '$'](this._next, force, this.value) : this.owner[this.field + '$'](this.key, this._next, force, this.value);
} catch (error) {

@@ -290,17 +330,7 @@ if (error[catchedId] === undefined) {

newValue = createMock(error);
newValue = error instanceof Error ? error : new Error(error.stack || error);
}
context.last = slave;
this.status = ATOM_STATUS_ACTUAL;
if (newValue !== undefined && value !== newValue) {
this.value = newValue;
this._context.newValue(this, value, newValue, true);
if (this._slaves) {
this._slaves.forEach(obsoleteSlave);
}
}
return newValue;
};

@@ -384,2 +414,4 @@

BaseLogger.prototype.sync = function sync() {};
BaseLogger.prototype.status = function status(_status, atom, namespace) {};

@@ -401,2 +433,6 @@

ConsoleLogger.prototype.sync = function sync() {
console.log('sync');
};
ConsoleLogger.prototype.status = function status(_status2, atom, namespace) {

@@ -449,5 +485,3 @@ console.log(namespace, _status2, atom.displayName);

Context.prototype.destroyHost = function destroyHost(atom) {
var from = atom.value;
Context.prototype._destroyValue = function _destroyValue(atom, from) {
if (from && !(from instanceof Error) && _typeof(from) === 'object' && typeof from.destructor === 'function' && this._owners.get(from) === atom) {

@@ -458,3 +492,7 @@ from.destructor();

}
};
Context.prototype.destroyHost = function destroyHost(atom) {
this._destroyValue(atom, atom.value);
if (this._logger !== undefined) {

@@ -470,8 +508,4 @@ this._logger.onDestruct(atom, this._namespace);

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._destroyValue(atom, from);
this._owners.delete(from);
}
if (to && !(to instanceof Error) && _typeof(to) === 'object' && typeof to.destructor === 'function') {

@@ -487,3 +521,3 @@ this._owners.set(to, atom);

} else {
this._logger.newValue(atom, from, to, isActualize, this._namespace);
this._logger.newValue(atom, from instanceof Error ? undefined : from, to, isActualize, this._namespace);
}

@@ -531,2 +565,6 @@ }

if (this._logger !== undefined) {
this._logger.sync();
}
do {

@@ -585,3 +623,3 @@ var end = updating.length;

function memMethod(proto, rname, descr, normalize, isComponent) {
function memMethod(proto, rname, descr, isComponent) {
var name = getId(proto, rname);

@@ -614,3 +652,3 @@

if (atom === undefined) {
atom = new Atom(name, this, defaultContext, hostAtoms, normalize, undefined, undefined, isComponent);
atom = new Atom(name, this, defaultContext, hostAtoms, undefined, undefined, isComponent);
hostAtoms.set(this, atom);

@@ -651,3 +689,3 @@ }

function memProp(proto, rname, descr, normalize) {
function memProp(proto, rname, descr) {
var name = getId(proto, rname);

@@ -678,3 +716,3 @@ var handlerKey = name + "$";

if (atom === undefined) {
atom = new Atom(name, this, defaultContext, hostAtoms, normalize);
atom = new Atom(name, this, defaultContext, hostAtoms);
hostAtoms.set(this, atom);

@@ -694,3 +732,3 @@ }

if (atom === undefined) {
atom = new Atom(name, this, defaultContext, hostAtoms, normalize);
atom = new Atom(name, this, defaultContext, hostAtoms);
hostAtoms.set(this, atom);

@@ -731,3 +769,3 @@ }

function memKeyMethod(proto, rname, descr, normalize) {
function memKeyMethod(proto, rname, descr) {
var name = getId(proto, rname);

@@ -769,3 +807,3 @@ var handler = descr.value;

if (atom === undefined) {
atom = new Atom(name, this, defaultContext, atomMap, normalize, rawKey, key);
atom = new Atom(name, this, defaultContext, atomMap, rawKey, key);
atomMap.set(key, atom);

@@ -784,5 +822,4 @@ }

var normalize = arguments[0];
return function (proto, name, descr) {
return memKeyMethod(proto, name, descr, normalize);
return memKeyMethod(proto, name, descr);
};

@@ -830,3 +867,3 @@ }

function detached(proto, name, descr) {
return memMethod(proto, name, descr, undefined, true);
return memMethod(proto, name, descr, true);
}

@@ -969,5 +1006,4 @@

var normalize = arguments[0];
return function (proto, name, descr) {
return descr.value === undefined ? memProp(proto, name, descr, normalize) : memMethod(proto, name, descr, normalize);
return descr.value === undefined ? memProp(proto, name, descr) : memMethod(proto, name, descr);
};

@@ -974,0 +1010,0 @@ }

{
"name": "lom_atom",
"version": "2.0.1",
"version": "2.0.2",
"description": "Alternative implementation of eigenmethod mol_atom state management library",

@@ -68,2 +68,3 @@ "publishConfig": {

"rollup-plugin-uglify": "^2.0.1",
"source-map-support": "^0.5.0",
"standard-version": "^4.2.0",

@@ -70,0 +71,0 @@ "uglify-es": "^3.1.3"

@@ -11,3 +11,2 @@ // @flow

IAtom,
INormalize,
IAtomInt,

@@ -20,3 +19,4 @@ IAtomStatus,

import {defaultNormalize, createMock, AtomWait} from './utils'
import {createMock, AtomWait} from './utils'
import conform from './conform'

@@ -32,2 +32,3 @@ function checkSlave(slave: IAtomInt) {

function disleadThis(master: IAtomInt) {
(this: Atom<*>);
master.dislead(this)

@@ -37,2 +38,3 @@ }

function actualizeMaster(master: IAtomInt) {
(this: Atom<*>);
if (this.status === ATOM_STATUS_CHECKING) {

@@ -42,3 +44,2 @@ master.actualize()

}
export default class Atom<V> implements IAtom<V>, IAtomInt {

@@ -48,3 +49,7 @@ status: IAtomStatus

owner: IAtomOwner
value: V | void
value: V | Error | void
_next: V | Error | void
_ignore: V | Error | void
key: mixed | void

@@ -56,3 +61,2 @@ isComponent: boolean

_context: IContext
_normalize: INormalize<V>
_hostAtoms: WeakMap<Object, IAtom<*>> | Map<string, IAtom<*>>

@@ -66,3 +70,2 @@ _keyHash: string | void

hostAtoms: WeakMap<Object, IAtom<*>> | Map<string, IAtom<*>>,
normalize?: INormalize<V>,
key?: mixed,

@@ -77,5 +80,6 @@ keyHash?: string,

this.isComponent = isComponent || false
this._normalize = normalize || defaultNormalize
this._context = context
this.value = context.create(this)
this._next = undefined
this._ignore = undefined
this._hostAtoms = hostAtoms

@@ -111,11 +115,11 @@ this.status = this.value === undefined ? ATOM_STATUS_OBSOLETE : ATOM_STATUS_ACTUAL

this._checkSlaves()
const hostAtoms = this._hostAtoms
if (hostAtoms instanceof WeakMap) {
hostAtoms.delete(this.owner)
} else if (this._keyHash) {
hostAtoms.delete(this._keyHash)
}
this._hostAtoms.delete(((this._keyHash || this.owner): any))
this._context.destroyHost(this)
this.value = undefined
this._next = undefined
this._ignore = undefined
this.status = ATOM_STATUS_DESTROYED
this._hostAtoms = (undefined: any)
this.key = undefined
this._keyHash = undefined
}

@@ -136,3 +140,3 @@

if (force) {
this._pullPush(undefined, true)
this._push(this._pull(true))
} else {

@@ -145,26 +149,16 @@ this.actualize()

set(v: V | Error, force?: boolean): V {
let oldValue = this.value
const normalized: V = this._normalize((v: any), oldValue)
if (oldValue === normalized) {
return normalized
set(next: V | Error, force?: boolean): V {
if (force) return this._push(next)
let normalized: V | Error = conform(next, this._ignore)
if (normalized === this._ignore) {
return (this.value: any)
}
if (normalized === undefined) {
return (oldValue: any)
}
if (force || normalized instanceof Error) {
this.status = ATOM_STATUS_ACTUAL
this.value = normalized instanceof Error
? createMock(normalized)
: normalized
normalized = conform(next, this.value)
if (normalized === this.value) return (this.value: any)
this._context.newValue(this, oldValue, normalized)
if (this._slaves) {
this._slaves.forEach(obsoleteSlave)
}
} else {
this.obsolete()
this.actualize(normalized)
}
this._ignore = this._next = normalized
this.obsolete()
this.actualize()

@@ -174,9 +168,7 @@ return (this.value: any)

actualize(proposedValue?: V): void {
actualize(): void {
if (this.status === ATOM_STATUS_PULLING) {
throw new Error(`Cyclic atom dependency of ${String(this)}`)
}
if (this.status === ATOM_STATUS_ACTUAL) {
return
}
if (this.status === ATOM_STATUS_ACTUAL) return

@@ -194,11 +186,34 @@ if (this.status === ATOM_STATUS_CHECKING) {

if (this.status !== ATOM_STATUS_ACTUAL) {
this._pullPush(proposedValue)
this._push(this._pull())
}
}
_pullPush(proposedValue?: V, force?: boolean): void {
_push(nextRaw: V | Error): V {
this.status = ATOM_STATUS_ACTUAL
if (!(nextRaw instanceof AtomWait)) {
this._ignore = this._next
this._next = undefined
}
const prev = this.value
if (nextRaw === undefined) return (prev: any)
const next: V | Error = nextRaw instanceof Error
? createMock(nextRaw)
: (prev instanceof Error ? nextRaw : conform(nextRaw, prev))
if (prev !== next) {
this.value = next
this._context.newValue(this, prev, next, true)
if (this._slaves) {
this._slaves.forEach(obsoleteSlave)
}
}
return (next: any)
}
_pull(force?: boolean): V | Error {
if (this._masters) {
this._masters.forEach(disleadThis, this)
}
let newValue: V
let newValue: V | Error

@@ -210,10 +225,6 @@ this.status = ATOM_STATUS_PULLING

context.last = this
const value = this.value
try {
newValue = this._normalize(
this.key === undefined
? (this.owner: any)[this.field + '$'](proposedValue, force, value)
: (this.owner: any)[this.field + '$'](this.key, proposedValue, force, value),
value
)
newValue = this.key === undefined
? (this.owner: any)[this.field + '$'](this._next, force, this.value)
: (this.owner: any)[this.field + '$'](this.key, this._next, force, this.value)
} catch (error) {

@@ -224,16 +235,7 @@ if (error[catchedId] === undefined) {

}
newValue = createMock(error)
newValue = error instanceof Error ? error : new Error(error.stack || error)
}
context.last = slave
this.status = ATOM_STATUS_ACTUAL
if (newValue !== undefined && value !== newValue) {
this.value = newValue
this._context.newValue(this, value, newValue, true)
if (this._slaves) {
this._slaves.forEach(obsoleteSlave)
}
}
return newValue
}

@@ -240,0 +242,0 @@

// @flow
import type {
INormalize,
IAtomHandler,

@@ -30,2 +29,3 @@ IAtomInt,

onDestruct(atom: IAtom<*>, namespace: string): void {}
sync() {}
status(status: ILoggerStatus, atom: IAtom<*>, namespace: string): void {}

@@ -37,2 +37,6 @@ error<V>(atom: IAtom<V>, err: Error, namespace: string): void {}

export class ConsoleLogger extends BaseLogger {
sync() {
console.log('sync')
}
status(status: ILoggerStatus, atom: IAtom<*>, namespace: string): void {

@@ -68,4 +72,3 @@ console.log(namespace, status, atom.displayName)

destroyHost(atom: IAtomInt) {
const from = atom.value
_destroyValue<V>(atom: IAtom<V>, from?: V | Error) {
if (

@@ -81,2 +84,6 @@ from

}
}
destroyHost(atom: IAtomInt) {
this._destroyValue(atom, atom.value)
if (this._logger !== undefined) {

@@ -92,13 +99,4 @@ this._logger.onDestruct(atom, this._namespace)

newValue<V>(atom: IAtom<V>, from?: V | Error, to: V | Error, isActualize?: boolean) {
this._destroyValue(atom, (from: any))
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

@@ -117,3 +115,3 @@ && !(to instanceof Error)

} else {
this._logger.newValue(atom, from, to, isActualize, this._namespace)
this._logger.newValue(atom, from instanceof Error ? undefined : from, to, isActualize, this._namespace)
}

@@ -164,2 +162,5 @@ }

let start = this._start
if (this._logger !== undefined) {
this._logger.sync()
}
do {

@@ -166,0 +167,0 @@ const end = updating.length

@@ -10,2 +10,4 @@ // @flow

export interface ILogger {
sync(): void;
/**

@@ -70,3 +72,3 @@ * Invokes before atom creating

status: IAtomStatus;
value: V | void;
value: V | Error | void;
+field: string;

@@ -94,4 +96,2 @@ +displayName: string;

export type INormalize<V> = (next: V, prev?: V) => V
export interface IAtomOwner {

@@ -98,0 +98,0 @@ displayName?: string;

// @flow
import type {IAtom, IAtomHandler, INormalize, IContext} from './interfaces'
import type {IAtom, IAtomHandler, IContext} from './interfaces'
import {defaultContext} from './Context'

@@ -26,3 +26,2 @@ import {AtomWait} from './utils'

descr: TypedPropertyDescriptor<*>,
normalize?: INormalize<V>,
isComponent?: boolean

@@ -54,3 +53,3 @@ ): TypedPropertyDescriptor<*> {

if (atom === undefined) {
atom = new Atom(name, this, defaultContext, hostAtoms, normalize, undefined, undefined, isComponent)
atom = new Atom(name, this, defaultContext, hostAtoms, undefined, undefined, isComponent)
hostAtoms.set(this, atom)

@@ -97,4 +96,3 @@ }

rname: string,
descr: TypedPropertyDescriptor<V>,
normalize?: INormalize<V>
descr: TypedPropertyDescriptor<V>
): TypedPropertyDescriptor<V> {

@@ -130,3 +128,3 @@ const name = getId(proto, rname)

if (atom === undefined) {
atom = new Atom(name, this, defaultContext, hostAtoms, normalize)
atom = new Atom(name, this, defaultContext, hostAtoms)
hostAtoms.set(this, atom)

@@ -143,3 +141,3 @@ }

if (atom === undefined) {
atom = new Atom(name, this, defaultContext, hostAtoms, normalize)
atom = new Atom(name, this, defaultContext, hostAtoms)
hostAtoms.set(this, atom)

@@ -182,4 +180,3 @@ }

rname: string,
descr: TypedPropertyDescriptor<IAtomHandler<V, K>>,
normalize?: INormalize<V>
descr: TypedPropertyDescriptor<IAtomHandler<V, K>>
): TypedPropertyDescriptor<IAtomHandler<V, K>> {

@@ -216,3 +213,3 @@ const name = getId(proto, rname)

if (atom === undefined) {
atom = new Atom(name, this, defaultContext, atomMap, normalize, rawKey, key)
atom = new Atom(name, this, defaultContext, atomMap, rawKey, key)
atomMap.set(key, atom)

@@ -229,12 +226,10 @@ }

name: string,
descr: TypedPropertyDescriptor<IAtomHandler<V, K>>,
normalize?: INormalize<V>
descr: TypedPropertyDescriptor<IAtomHandler<V, K>>
) => TypedPropertyDescriptor<IAtomHandler<V, K>>
declare function memkey<V, K, P: Object>(normalize: INormalize<V>): () => IMemKeyMethod<V, K, P>
declare function memkey<V, K, P: Object>(): () => IMemKeyMethod<V, K, P>
declare function memkey<V, K, P: Object>(
proto: P,
name: string,
descr: TypedPropertyDescriptor<IAtomHandler<V, K>>,
normalize?: INormalize<V>
descr: TypedPropertyDescriptor<IAtomHandler<V, K>>
): TypedPropertyDescriptor<IAtomHandler<V, K>>

@@ -247,3 +242,2 @@

const normalize: INormalize<*> = arguments[0]
return function (

@@ -254,3 +248,3 @@ proto: Object,

): TypedPropertyDescriptor<IAtomHandler<*, *>> {
return memKeyMethod(proto, name, descr, normalize)
return memKeyMethod(proto, name, descr)
}

@@ -307,3 +301,3 @@ }

): TypedPropertyDescriptor<any> | void {
return memMethod(proto, name, descr, undefined, true)
return memMethod(proto, name, descr, true)
}

@@ -314,4 +308,3 @@

name: string,
descr: TypedPropertyDescriptor<IAtomHandler<V>>,
normalize?: INormalize<V>
descr: TypedPropertyDescriptor<IAtomHandler<V>>
) => TypedPropertyDescriptor<IAtomHandler<V>>

@@ -419,8 +412,7 @@

declare function mem<V, P: Object>(normalize: INormalize<V>): () => IMemProp<V, P>
declare function mem<V, P: Object>(): () => IMemProp<V, P>
declare function mem<V, P: Object>(
proto: P,
name: string,
descr: TypedPropertyDescriptor<V>,
normalize?: INormalize<V>
descr: TypedPropertyDescriptor<V>
): TypedPropertyDescriptor<*>

@@ -435,4 +427,2 @@

const normalize: INormalize<*> = arguments[0]
return function (

@@ -444,4 +434,4 @@ proto: Object,

return descr.value === undefined
? memProp(proto, name, descr, normalize)
: memMethod(proto, name, descr, normalize)
? memProp(proto, name, descr)
: memMethod(proto, name, descr)
}

@@ -448,0 +438,0 @@ }

@@ -18,22 +18,2 @@ // @flow

export function defaultNormalize<V>(next: V, prev?: V): V {
if(next === prev) return next
if(
(next instanceof Array)
&& (prev instanceof Array)
&& (next.length === prev.length)
) {
for(let i = 0; i < next.length; i++) {
if(next[i] !== prev[i]) {
return next
}
}
return prev
}
return next
}
export class AtomWait extends Error {

@@ -40,0 +20,0 @@ constructor(message?: string = 'Wait...') {

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc