@lcap/asl
Version:
NetEase Application Specific Language
481 lines • 15.7 kB
JavaScript
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Logic = void 0;
const decorators_1 = require("../decorators");
const __1 = require("..");
const logic_1 = require("../../service/logic");
const View_1 = __importDefault(require("../page/View"));
const LogicItem_1 = require("./LogicItem");
const dataTypeUtils_1 = require("../data/dataTypeUtils");
const lodash_1 = require("lodash");
/**
* 逻辑类
*/
class Logic extends __1.Vertex {
/**
* @param source 需要合并的部分参数
*/
constructor(source) {
super();
/**
* 概念类型
*/
this.level = __1.LEVEL_ENUM.logic;
/**
* Id
*/
this.id = undefined;
/**
* 名称
*/
this.name = undefined;
/**
* 描述
*/
this.description = undefined;
/**
* serviceId 或者 viewId
*/
this.moduleId = undefined;
/**
* 值为 view 或者 microService 或者 processComponent
*/
this.moduleType = undefined;
/**
* serviceId 或者 viewId
*/
this.serviceId = undefined;
/**
* 值为 view 或者 microService
*/
this.serviceType = undefined;
/**
* 所属实体 Id
*/
this.entityId = undefined;
/**
* 输入参数列表
*/
this.params = [];
/**
* 输出参数列表
*/
this.returns = [];
/**
* 局部变量列表
*/
this.variables = [];
/**
* 逻辑体
*/
this.body = [];
/**
* 画布
*/
this.playgroundId = undefined;
/**
* 画布
*/
this.playground = [];
/**
* 接口
*/
this.interface = undefined;
/**
* 视图
*/
this.view = undefined;
/**
* 流程组件
*/
this.processComponent = undefined;
/**
* 流程组件
*/
this.process = undefined;
/**
* 正在请求的 Promise
* 前端 UI 状态
*/
this.contentPromise = undefined;
/**
* 树组件的子节点字段
*/
this.moreChildrenFields = ['params', 'returns', 'variables'];
source && this.assign(source);
this.on('change', lodash_1.throttle(this._onChange.bind(this), __1.config.throttleWait, { leading: true, trailing: true }));
}
/**
* 添加逻辑
*/
async create(none, actionOptions) {
__1.config.defaultApp?.emit('saving');
const body = this.toJSON();
// body.params.forEach((param: any) => convert2SchemaType(param.schema));
// body.returns.forEach((ret: any) => convert2SchemaType(ret.schema));
// body.variables.forEach((variable: any) => convert2SchemaType(variable.schema));
__1.utils.logger.debug('添加逻辑', body);
const result = await logic_1.logicService.create({
headers: {
appId: __1.config.defaultApp?.id,
operationAction: actionOptions?.actionName || 'Logic.create',
operationDesc: actionOptions?.actionDesc || `添加逻辑"${this.name}"`,
},
body,
});
this.deepPick(result, ['id']);
this.pick(result, ['playgroundId']);
this.assign({ body: result.body.map((logicNode) => LogicItem_1.LogicNode.from(logicNode, this, null)) });
this.params.forEach((param) => {
param.assign({ code: `ID_${param.id}` });
});
this.returns.forEach((returns) => {
returns.assign({ code: `ID_${returns.id}` });
});
this.variables.forEach((variable) => {
variable.assign({ code: `ID_${variable.id}` });
});
if (this.view) {
this.view.page.service.emit('pageTreeChange');
this.emit('change');
}
await __1.config.defaultApp?.history.load();
__1.config.defaultApp?.emit('saved');
return this;
}
/**
* 删除逻辑
*/
async delete(none, actionOptions) {
__1.config.defaultApp?.emit('saving');
if (this.id) {
const body = this.toPlainJSON();
if (this.view) {
body.moduleId = this.view.id;
body.moduleType = 'view';
}
else {
body.moduleId = this.interface.service.id;
body.moduleType = 'microService';
}
try {
await logic_1.logicService.delete({
headers: {
appId: __1.config.defaultApp?.id,
operationAction: actionOptions?.actionName || 'Logic.delete',
operationDesc: actionOptions?.actionDesc || `删除逻辑"${this.name}"`,
},
body,
});
}
catch (err) {
await __1.config.defaultApp?.history.load();
throw err;
}
if (this.view) {
const { logics } = this.view.$def;
const pos = logics.indexOf(this);
if (pos >= 0)
logics.splice(pos, 1);
}
[...this.body, ...this.playground].forEach((node) => {
__1.typeCheck.delete(node.id);
});
// const index =
this.destroy();
if (this.view) {
this.view.page.service.emit('pageTreeChange');
this.emit('change');
}
await __1.config.defaultApp?.history.load();
__1.config.defaultApp?.emit('saved');
}
else {
if (this.view) {
const { logics } = this.view.$def;
const pos = logics.indexOf(this);
if (pos >= 0)
logics.splice(pos, 1);
}
}
}
/**
* 修改逻辑
*/
async update(none, actionOptions, then) {
__1.config.defaultApp?.emit('saving');
const body = this.toPlainJSON();
__1.utils.logger.debug('修改逻辑', body);
const result = await logic_1.logicService.update({
headers: {
appId: __1.config.defaultApp?.id,
operationAction: actionOptions?.actionName || 'Logic.update',
operationDesc: actionOptions?.actionDesc || `修改逻辑"${this.name}"`,
},
body,
});
await then?.();
await __1.config.defaultApp?.history.load();
__1.config.defaultApp?.emit('saved');
return this;
}
/**
* 设置逻辑名称
*/
async setName(name) {
const oldName = this.name;
this.assign({ name });
await this.update(undefined, {
actionDesc: `设置逻辑"${oldName}"的名称为"${name}"`,
});
if (this.view) {
this.view.page.service.emit('pageTreeChange');
this.emit('change');
}
return this;
}
/**
* 设置逻辑描述
*/
async setDescription(description) {
this.assign({ description });
await this.update(undefined, {
actionDesc: `设置逻辑"${this.name}"的描述为"${description}"`,
});
if (this.view) {
this.view.page.service.emit('pageTreeChange');
this.emit('change');
}
return this;
}
_onChange() {
this.view && this.view.emit('change');
}
/**
* 按当前 id 加载逻辑数据
*/
async load() {
if (!this.id)
return;
// 如果有正在进行的 Promise,则直接返回它
if (this.contentPromise)
return this.contentPromise;
return this.contentPromise = (async () => {
try {
const result = await logic_1.logicService.loadDetail({
query: {
moduleId: this.moduleId,
moduleType: this.moduleType,
id: this.id,
},
});
const newLogic = Logic.from(result, this.interface || this.view || this.processComponent, this);
this.assign(newLogic);
}
catch (err) { }
return this;
})().finally(() => this.contentPromise = undefined);
}
isContentLoaded() {
return !!this.body.length;
}
/**
* 批量添加参数
*/
async addParamList(params) {
const body = params.map((param) => {
param = param.toJSON();
dataTypeUtils_1.convert2SchemaType(param.schema);
return param;
});
const results = await logic_1.paramService.addList({
body,
});
params.forEach((param, index) => param.pick(results[index], ['id']));
this.params.push(...params);
}
/**
* 添加逻辑节点
* @param item
*/
addItem(item, actionOptions) {
if (lodash_1.isPlainObject(item)) {
item = LogicItem_1.LogicItem.from(item, this, null);
}
// if (!this.children.includes(child)) {
// const index = child._posIndex === undefined ? this.children.length : child._posIndex;
// this.children.splice(index, 0, child);
// }
const parent = item.parentAttr ? __1.vertexsMap.get(item.parentId) : undefined;
return item.create({
parent,
parentId: item.parentId,
parentAttr: item.parentAttr,
_posIndex: item._posIndex,
cache: false,
}, actionOptions);
}
/**
* 删除逻辑节点
* @param item
*/
removeItem(item, actionOptions) {
if (lodash_1.isPlainObject(item)) {
item = __1.vertexsMap.get(item.id);
}
return item.delete(undefined, actionOptions);
}
/**
*
* @param type
* @returns
*/
findLogicItemByType(type) {
let result;
__1.utils.traverse((current) => {
if (current.node.type === type)
return result = current.node;
}, { node: this }, {
mode: 'anyObject',
excludedKeySet: this.JSON_EXCLUDED_KEYS,
});
return result;
}
/**
* 从后端 JSON 生成规范的 Logic 对象
*/
static from(source, parent, currentLogic) {
const logic = new Logic(source);
currentLogic = currentLogic || logic;
logic.assign({ params: logic.params.map((param) => __1.Param.from(param, currentLogic)) });
logic.assign({ returns: logic.returns.map((ret) => __1.Return.from(ret, currentLogic)) });
logic.assign({ variables: logic.variables.map((variable) => __1.Variable.from(variable, currentLogic)) });
logic.body.forEach((node, index) => {
if (node.level === __1.LEVEL_ENUM.logicNode)
node = logic.body[index] = LogicItem_1.LogicNode.from(node, currentLogic, null);
else if (node.level === __1.LEVEL_ENUM.expressionNode)
node = logic.body[index] = LogicItem_1.ExpressionNode.from(node, currentLogic, null);
});
logic.playground.forEach((node, index) => {
if (node.level === __1.LEVEL_ENUM.logicNode)
node = logic.playground[index] = LogicItem_1.LogicNode.from(node, currentLogic, null);
else if (node.level === __1.LEVEL_ENUM.expressionNode)
node = logic.playground[index] = LogicItem_1.ExpressionNode.from(node, currentLogic, null);
});
if (parent instanceof __1.Interface)
logic.assign({
interface: parent,
moduleType: 'microService',
moduleId: parent.id,
});
else if (parent instanceof View_1.default)
logic.assign({
view: parent,
moduleType: 'view',
moduleId: parent.id,
});
else if (parent instanceof __1.ProcessComponent)
logic.assign({
processComponent: parent,
moduleType: 'process',
moduleId: parent.id,
});
else if (parent instanceof __1.ProcessInterface) {
logic.assign({
process: parent,
});
}
return logic;
}
}
__decorate([
decorators_1.immutable()
], Logic.prototype, "level", void 0);
__decorate([
decorators_1.immutable()
], Logic.prototype, "id", void 0);
__decorate([
decorators_1.immutable()
], Logic.prototype, "name", void 0);
__decorate([
decorators_1.immutable()
], Logic.prototype, "description", void 0);
__decorate([
decorators_1.immutable()
], Logic.prototype, "moduleId", void 0);
__decorate([
decorators_1.immutable()
], Logic.prototype, "moduleType", void 0);
__decorate([
decorators_1.immutable()
], Logic.prototype, "serviceId", void 0);
__decorate([
decorators_1.immutable()
], Logic.prototype, "serviceType", void 0);
__decorate([
decorators_1.immutable()
], Logic.prototype, "entityId", void 0);
__decorate([
decorators_1.immutable()
], Logic.prototype, "params", void 0);
__decorate([
decorators_1.immutable()
], Logic.prototype, "returns", void 0);
__decorate([
decorators_1.immutable()
], Logic.prototype, "variables", void 0);
__decorate([
decorators_1.immutable()
], Logic.prototype, "body", void 0);
__decorate([
decorators_1.immutable()
], Logic.prototype, "playgroundId", void 0);
__decorate([
decorators_1.immutable()
], Logic.prototype, "playground", void 0);
__decorate([
decorators_1.circular(),
decorators_1.immutable()
], Logic.prototype, "interface", void 0);
__decorate([
decorators_1.circular(),
decorators_1.immutable()
], Logic.prototype, "view", void 0);
__decorate([
decorators_1.circular(),
decorators_1.immutable()
], Logic.prototype, "processComponent", void 0);
__decorate([
decorators_1.circular(),
decorators_1.immutable()
], Logic.prototype, "process", void 0);
__decorate([
decorators_1.excludedInJSON()
], Logic.prototype, "contentPromise", void 0);
__decorate([
decorators_1.immutable()
], Logic.prototype, "moreChildrenFields", void 0);
__decorate([
decorators_1.action('添加逻辑')
], Logic.prototype, "create", null);
__decorate([
decorators_1.action('删除逻辑')
], Logic.prototype, "delete", null);
__decorate([
decorators_1.action('设置逻辑名称')
], Logic.prototype, "setName", null);
__decorate([
decorators_1.action('设置逻辑描述')
], Logic.prototype, "setDescription", null);
exports.Logic = Logic;
exports.default = Logic;
//# sourceMappingURL=Logic.js.map