typedux
Version:
Slightly adjusted Redux (awesome by default) for TS
54 lines • 2.1 kB
JavaScript
/**
* Responsible for observing
* and notifying store listeners
* with provided paths
*/
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports", "@3fv/logger-proxy"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StateObserver = void 0;
var logger_proxy_1 = require("@3fv/logger-proxy");
var log = logger_proxy_1.getLogger(__filename);
var StateObserver = /** @class */ (function () {
//constructor(s:string | Array<string|number>,private handler:TStateChangeHandler<S,T>) {
function StateObserver(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('.')) : []
}
StateObserver.prototype.onChange = function (state) {
var newValue = this.selector(state); // this.keyPath.length ? getNewValue(state,this.keyPath) : state
// Check for change/diff
var 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;
};
return StateObserver;
}());
exports.StateObserver = StateObserver;
exports.default = StateObserver;
});
//# sourceMappingURL=StateObserver.js.map