UNPKG

@formily/core

Version:

English | [简体中文](./README.zh-cn.md)

132 lines (131 loc) 5.04 kB
"use strict"; 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 __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var shared_1 = require("@formily/shared"); var FormLifeCycle = (function () { function FormLifeCycle() { var _this = this; var params = []; for (var _i = 0; _i < arguments.length; _i++) { params[_i] = arguments[_i]; } this.notify = function (type, payload, ctx) { if (shared_1.isStr(type)) { _this.listener.call(ctx, { type: type, payload: payload }, ctx); } }; this.listener = this.buildListener(params); } FormLifeCycle.prototype.buildListener = function (params) { return function (payload, ctx) { var _this = this; for (var index = 0; index < params.length; index++) { var item = params[index]; if (shared_1.isFn(item)) { item.call(this, payload, ctx); } else if (shared_1.isStr(item) && shared_1.isFn(params[index + 1])) { if (item === payload.type) { params[index + 1].call(this, payload.payload, ctx); } index++; } else if (shared_1.isObj(item)) { shared_1.each(item, function (handler, type) { if (shared_1.isFn(handler) && shared_1.isStr(type)) { if (type === payload.type) { handler.call(_this, payload.payload, ctx); return false; } } }); } } }; }; return FormLifeCycle; }()); exports.FormLifeCycle = FormLifeCycle; var FormHeart = (function (_super) { __extends(FormHeart, _super); function FormHeart(_a) { var _b = _a === void 0 ? {} : _a, lifecycles = _b.lifecycles, context = _b.context, beforeNotify = _b.beforeNotify, afterNotify = _b.afterNotify; var _this = _super.call(this) || this; _this.batch = function (callback) { if (shared_1.isFn(callback)) { _this.batching = true; _this.buffer = []; callback(); _this.batching = false; _this.buffer.forEach(function (_a) { var type = _a.type, payload = _a.payload, context = _a.context; _this.publish(type, payload, context); }); _this.buffer = []; } }; _this.publish = function (type, payload, context) { if (_this.batching) { _this.buffer.push({ type: type, payload: payload, context: context }); return; } if (shared_1.isStr(type)) { if (shared_1.isFn(_this.beforeNotify)) { _this.beforeNotify(type, payload, context); } _this.lifecycles.forEach(function (lifecycle) { lifecycle.notify(type, payload, context || _this.context); }); _this.notify({ type: type, payload: payload }); if (shared_1.isFn(_this.afterNotify)) { _this.afterNotify(type, payload, context); } } }; _this.lifecycles = _this.buildLifeCycles(lifecycles || []); _this.context = context; _this.buffer = []; _this.beforeNotify = beforeNotify; _this.afterNotify = afterNotify; return _this; } FormHeart.prototype.buildLifeCycles = function (lifecycles) { var _this = this; return lifecycles.reduce(function (buf, item) { if (item instanceof FormLifeCycle) { return buf.concat(item); } else { if (typeof item === 'object') { _this.context = item; return buf; } else if (shared_1.isArr(item)) { return _this.buildLifeCycles(item); } return buf; } }, []); }; return FormHeart; }(shared_1.Subscribable)); exports.FormHeart = FormHeart;