@state-sync/redux-path-reducer
Version:
state-sync client only json path reducer
46 lines (45 loc) • 1.51 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var Patch_1 = require("./Patch");
var PatchArea = /** @class */ (function () {
function PatchArea(rootPath, store) {
this.rootPath = rootPath;
this.store = store;
}
PatchArea.prototype.select = function (path) {
return new Patch_1.OpSelect({ op: 'select', path: this.path(path) }).apply(this.store.getState());
};
PatchArea.prototype.replace = function (path, value) {
if (!this.store)
throw 'PatchArea is not initialized';
this.store.dispatch({
type: '@STATE_SYNC/PATCH_REDUCE', payload: [{
op: 'replace', path: path, value: value
}]
});
};
PatchArea.prototype.remove = function (path) {
if (!this.store)
throw 'PatchArea is not initialized';
try {
this.store.dispatch({
type: '@STATE_SYNC/PATCH_REDUCE', payload: [{
op: 'remove', path: this.path(path)
}]
});
}
catch (e) {
console.error(e);
}
};
PatchArea.prototype.child = function (path) {
return new PatchArea(this.rootPath + '/' + path, this.store);
};
PatchArea.prototype.path = function (path) {
if (path.startsWith('/'))
path = path.substring(1);
return this.rootPath + path;
};
return PatchArea;
}());
exports.PatchArea = PatchArea;