UNPKG

typedux

Version:

Slightly adjusted Redux (awesome by default) for TS

39 lines 1.31 kB
/** * Responsible for observing * and notifying store listeners * with provided paths */ import { getLogger } from '@3fv/logger-proxy'; const log = getLogger(__filename); export 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; } } export default StateObserver; //# sourceMappingURL=StateObserver.js.map