@rc-component/form
Version:
React Form Component
56 lines (55 loc) • 1.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.macroTask = exports.default = void 0;
var _valueUtil = require("../utils/valueUtil");
/**
* Call action with delay in macro task.
*/
const macroTask = fn => {
const channel = new MessageChannel();
channel.port1.onmessage = fn;
channel.port2.postMessage(null);
};
exports.macroTask = macroTask;
class WatcherCenter {
namePathList = [];
taskId = 0;
watcherList = new Set();
form;
constructor(form) {
this.form = form;
}
register(callback) {
this.watcherList.add(callback);
return () => {
this.watcherList.delete(callback);
};
}
notify(namePath) {
// Insert with deduplication
namePath.forEach(path => {
if (this.namePathList.every(exist => !(0, _valueUtil.matchNamePath)(exist, path))) {
this.namePathList.push(path);
}
});
this.doBatch();
}
doBatch() {
this.taskId += 1;
const currentId = this.taskId;
macroTask(() => {
if (currentId === this.taskId && this.watcherList.size) {
const formInst = this.form.getForm();
const values = formInst.getFieldsValue();
const allValues = formInst.getFieldsValue(true);
this.watcherList.forEach(callback => {
callback(values, allValues, this.namePathList);
});
this.namePathList = [];
}
});
}
}
exports.default = WatcherCenter;