typedux
Version:
Slightly adjusted Redux (awesome by default) for TS
43 lines • 1.48 kB
JavaScript
;
/**
* Responsible for observing
* and notifying store listeners
* with provided paths
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.StateObserver = void 0;
const logger_proxy_1 = require("@3fv/logger-proxy");
const log = logger_proxy_1.getLogger(__filename);
class StateObserver {
//constructor(s:string | Array<string|number>,private handler:TStateChangeHandler<S,T>) {
constructor(selector, handler) {
this.selector = selector;
this.handler = handler;
/**
* Last value received
*/
this.cachedValue = undefined;
/**
* Flags when the observer has been removed
*
* @type {boolean}
*/
this.removed = false;
//this.keyPath = path ? ((isArray(path)) ? path : path.split('.')) : []
}
onChange(state) {
const newValue = this.selector(state); // this.keyPath.length ? getNewValue(state,this.keyPath) : state
// Check for change/diff
const cachedValue = this.cachedValue;
if (newValue === cachedValue)
return false;
// Update the old ref
this.cachedValue = newValue;
//log.debug(`Path ${this.keyPath.join(',')} changed, to`,newValue,'from',cachedValue)
this.handler(newValue, cachedValue, this);
return true;
}
}
exports.StateObserver = StateObserver;
exports.default = StateObserver;
//# sourceMappingURL=StateObserver.js.map