@formily/core
Version:
English | [简体中文](./README.zh-cn.md)
206 lines (205 loc) • 9.19 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
var shared_1 = require("@formily/shared");
var immer_1 = require("immer");
var hasProxy = !!shared_1.globalThisPolyfill.Proxy;
immer_1.enablePatches();
var produce = new immer_1.Immer({
autoFreeze: false
}).produce;
exports.createStateModel = function (Factory) {
return (function (_super) {
__extends(Model, _super);
function Model(defaultProps) {
var _this = _super.call(this) || this;
_this.batch = function (callback) {
_this.batching = true;
if (shared_1.isFn(callback)) {
callback();
}
if (_this.dirtyNum > 0) {
_this.notify(_this.getState());
}
_this.dirtys = {};
_this.dirtyNum = 0;
_this.batching = false;
};
_this.getState = function (callback) {
if (shared_1.isFn(callback)) {
return callback(_this.getState());
}
else {
if (shared_1.isFn(_this.controller.publishState)) {
return _this.controller.publishState(_this.state);
}
if (!hasProxy || _this.props.useDirty) {
return shared_1.clone(_this.state);
}
else {
return produce(_this.state, function () { });
}
}
};
_this.getSourceState = function (callback) {
if (shared_1.isFn(callback)) {
return callback(_this.state);
}
else {
return _this.state;
}
};
_this.setSourceState = function (callback) {
if (shared_1.isFn(callback)) {
callback(_this.state);
}
};
_this.setCache = function (key, value) {
_this.cache[key] = shared_1.shallowClone(value);
};
_this.getCache = function (key) {
return _this.cache[key];
};
_this.removeCache = function (key) {
delete _this.cache[key];
};
_this.setState = function (callback, silent) {
if (silent === void 0) { silent = false; }
if (shared_1.isFn(callback)) {
_this.stackCount++;
if (!hasProxy || _this.props.useDirty) {
var draft_1 = _this.getState();
if (!_this.batching) {
_this.dirtys = {};
_this.dirtyNum = 0;
}
callback(draft_1);
if (shared_1.isFn(_this.props.computeState)) {
_this.props.computeState(draft_1, _this.state);
}
if (shared_1.isFn(_this.controller.computeState)) {
_this.controller.computeState(draft_1, _this.state);
}
var draftKeys = Object.keys(draft_1 || {});
var stateKeys = Object.keys(_this.state || {});
shared_1.each(draftKeys.length > stateKeys.length ? draft_1 : _this.state, function (value, key) {
if (!shared_1.isEqual(_this.state[key], draft_1[key])) {
_this.state[key] = draft_1[key];
_this.dirtys[key] = true;
_this.dirtyNum++;
}
});
if (shared_1.isFn(_this.controller.dirtyCheck)) {
var result = _this.controller.dirtyCheck(_this.dirtys);
if (shared_1.isValid(result)) {
Object.assign(_this.dirtys, result);
}
}
if (_this.dirtyNum > 0 && !silent) {
if (_this.batching) {
_this.stackCount--;
return;
}
_this.notify(_this.getState());
_this.dirtys = {};
_this.dirtyNum = 0;
}
}
else {
if (!_this.batching) {
_this.dirtys = {};
_this.dirtyNum = 0;
}
_this.state = produce(_this.state, function (draft) {
callback(draft);
if (shared_1.isFn(_this.props.computeState)) {
_this.props.computeState(draft, _this.state);
}
if (shared_1.isFn(_this.controller.computeState)) {
_this.controller.computeState(draft, _this.state);
}
}, function (patches) {
patches.forEach(function (_a) {
var path = _a.path, op = _a.op, value = _a.value;
if (op === 'replace') {
if (path.length > 1 || !shared_1.isEqual(_this.state[path[0]], value)) {
_this.dirtys[path[0]] = true;
_this.dirtyNum++;
}
}
else {
_this.dirtys[path[0]] = true;
_this.dirtyNum++;
}
});
});
if (shared_1.isFn(_this.controller.dirtyCheck)) {
var result = _this.controller.dirtyCheck(_this.dirtys);
if (shared_1.isValid(result)) {
Object.assign(_this.dirtys, result);
}
}
if (_this.dirtyNum > 0 && !silent) {
if (_this.batching) {
_this.stackCount--;
return;
}
_this.notify(_this.getState());
_this.dirtys = {};
_this.dirtyNum = 0;
}
}
_this.stackCount--;
if (!_this.stackCount) {
_this.prevState = _this.state;
}
}
};
_this.isDirty = function (key) {
return key ? _this.dirtys[key] === true : _this.dirtyNum > 0;
};
_this.getDirtyInfo = function () { return _this.dirtys; };
_this.hasChanged = function (path) {
return path
? !shared_1.isEqual(shared_1.FormPath.getIn(_this.prevState, path), shared_1.FormPath.getIn(_this.state, path))
: !shared_1.isEqual(_this.prevState, _this.state);
};
_this.state = __assign({}, Factory.defaultState);
_this.prevState = _this.state;
_this.props = shared_1.defaults(Factory.defaultProps, defaultProps);
_this.dirtys = {};
_this.cache = {};
_this.dirtyNum = 0;
_this.stackCount = 0;
_this.batching = false;
_this.controller = new Factory(_this.state, _this.props);
_this.displayName = Factory.displayName;
_this.state.displayName = _this.displayName;
return _this;
}
return Model;
}(shared_1.Subscribable));
};