dreamstate
Version:
Store management library based on react context and observers
37 lines (35 loc) • 1.26 kB
JavaScript
/**
* A utility class for managing nested computed values.
*
* This class is used by the `ContextManager` to track and compare computed values during context updates.
* It helps optimize state management by recalculating values only when dependencies change.
*/
var ComputedValue = /** @class */function () {
function ComputedValue(selector, memo) {
this.__selector__ = selector;
this.__memo__ = memo;
}
ComputedValue.prototype.process = function (context) {
// Process memoized computed in a special way, updated default computed every time.
if (this.__memo__) {
var diff_1 = this.__memo__(context);
// Warn if someone provided wrong selector.
{
if (!Array.isArray(diff_1)) {
console.warn("Expecting diff function from createComputed to return diff-array of dependencies.");
}
}
// If diff initialized and we can check memo values.
if (!this.__diff__ || this.__diff__.some(function (it, idx) {
return it !== diff_1[idx];
})) {
this.__diff__ = diff_1;
Object.assign(this, this.__selector__(context));
}
} else {
Object.assign(this, this.__selector__(context));
}
};
return ComputedValue;
}();
export { ComputedValue };