reactant-share
Version:
A framework for building shared web applications with Reactant
67 lines (64 loc) • 3.52 kB
JavaScript
import { __extends, __decorate, __param, __metadata } from '../node_modules/tslib/tslib.es6.js';
import { injectable, optional, PluginModule } from 'reactant';
import { routerModuleName } from '../constants.js';
import { Storage as ReactantStorage } from './storage.js';
import { PortDetector } from './portDetector.js';
var PatchesChecker = /** @class */ (function (_super) {
__extends(PatchesChecker, _super);
function PatchesChecker(portDetector, storage) {
var _this = _super.call(this) || this;
_this.portDetector = portDetector;
_this.storage = storage;
_this.middleware = function (store) { return function (next) { return function (_action) {
var _patches = _action._patches, type = _action.type, method = _action.method;
var hasIsolatedState;
_patches === null || _patches === void 0 ? void 0 : _patches.forEach(function (_a, index) {
var path = _a.path; _a.op; _a.value;
var _hasIsolatedState = _this.portDetector.hasIsolatedState("".concat(path[0]));
// ignore first patch
if (!index) {
hasIsolatedState = _hasIsolatedState;
}
else if (hasIsolatedState !== _hasIsolatedState) {
var methodName = "".concat(type, ".").concat(method);
throw new Error("Update state error: Mixed update of shared state and isolated state is not supported, please check method '".concat(methodName, "'."));
}
});
return next(_action);
}; }; };
if (process.env.NODE_ENV !== 'production' && _this.storage) {
_this.afterCreateStore = function (store) {
_this.storage.storageSettingMap.forEach(function (_, module) {
if (_this.portDetector.isolatedModules.includes(module)) {
throw new Error("The module \"".concat(module.constructor.name, "\" has been disabled for state sharing, its module state cannot be enabled for storage."));
}
});
return store;
};
}
return _this;
}
PatchesChecker.prototype.checkPatches = function (oldStateTree, options) {
options._patches.forEach(function (_a) {
var op = _a.op, path = _a.path, value = _a.value;
if (op === 'replace' &&
(toString.call(value) === '[object Object]' || Array.isArray(value))) {
var oldState = path.reduce(function (state, _path) { return state === null || state === void 0 ? void 0 : state[_path]; }, oldStateTree);
if (oldState &&
typeof oldState === 'object' &&
path[0] !== routerModuleName) {
var state = path.join('.');
console.warn("The state '".concat(state, "' operation in the method '").concat(options.method, "' of the module '").concat(String(options.type), "' is a replacement update operation, be sure to check the state '").concat(state, "' update operation and use mutation updates to ensure the minimum set of update patches."));
}
}
});
};
PatchesChecker = __decorate([
injectable(),
__param(1, optional()),
__metadata("design:paramtypes", [PortDetector,
ReactantStorage])
], PatchesChecker);
return PatchesChecker;
}(PluginModule));
export { PatchesChecker };