UNPKG

@lcap/asl

Version:

NetEase Application Specific Language

283 lines 9.06 kB
"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; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Vertex = void 0; const decorators_1 = require("../decorators"); const __1 = require(".."); const lodash_1 = require("lodash"); const common_1 = require("../../service/common"); const uuid_1 = require("uuid"); /** * 顶点类 * 属性均为只读,仅在初始化和 load 的情况下可以修改 */ class Vertex extends __1.EventEmitter { /** * @param source 需要合并的部分参数 */ constructor(source) { super(); /** * 可修改标志,内部属性 * 标记在构造函数结束后是否可修改 */ this._mutable = true; /** * 概念类型 */ this.level = __1.LEVEL_ENUM.vertex; /** * 顶点 id */ this.id = undefined; /** * 唯一 uuid * 目前主要用于前端 Debug 区分两个实例 */ this._uuid = uuid_1.v4(); /** * 前端记录缓存的时间戳 */ this._cacheTimestamp = undefined; /** * 创建人 */ this.createdBy = undefined; /** * 创建时间 */ this.createdTime = undefined; /** * 修改人 */ this.updatedBy = undefined; /** * 修改时间 */ this.updatedTime = undefined; /** * IDE 版本 */ this.ideVersion = undefined; /** * 是否可编辑 */ this.editable = true; /** * 是否正在编辑 * 前端 UI 状态 */ this.editing = false; /** * 是否正在请求 * 前端 UI 状态 */ this.loading = false; /** * 节点是否为展开状态 * 前端 UI 状态 */ this.expanded = false; /** * 是否为叶子节点 * 前端 UI 状态 */ this.isLeaf = false; if (typeof window !== 'undefined') { const globalData = window.globalData; this.ideVersion = globalData && globalData.ideVersion; } else { this.ideVersion = '1.2'; } source && this.assign(source); if (this.hasOwnProperty('JSON_EXCLUDED_KEYS') && this.JSON_EXCLUDED_KEYS === undefined) delete this.JSON_EXCLUDED_KEYS; } assign(source) { this._mutable = true; const oldId = this.id; Object.assign(this, source); oldId && __1.vertexsMap.delete(oldId); this.id && __1.vertexsMap.set(this.id, this); this.emitVertexIdToNameChange(); this._mutable = false; } emitVertexIdToNameChange() { if (typeof window === 'undefined') return; const { id, name } = this; const service = window && window.$data && window.$data.app && window.$data.app.firstMicroService; if (service) service.emit('vertexIdToNameChange', id, name); } plainAssign(source) { this._mutable = true; const oldId = this.id; Object.keys(source).forEach((key) => { const value = source[key]; if (typeof value !== 'object' && typeof value !== 'function') this[key] = value; }); oldId && __1.vertexsMap.delete(oldId); this.id && __1.vertexsMap.set(this.id, this); this.emitVertexIdToNameChange(); this._mutable = false; } pick(source, keys = []) { const obj = {}; keys.forEach((key) => obj[key] = source[key]); // 这里统一用 assign,集中处理 id 的问题 this.assign(obj); } /** * 从对象中深度获取 * @param source * @param keys * @example 比如一般后端返回只是添加了个 id * ``` * this.deepPick(result, ['id']) */ deepPick(source, keys = []) { __1.utils.traverse((current) => { const subSource = current.jsonPath ? lodash_1.at(source, [current.jsonPath])[0] : source; const target = current.jsonPath ? lodash_1.at(this, [current.jsonPath])[0] : this; if (!target) return; const obj = {}; keys.forEach((key) => { if (subSource.hasOwnProperty(key)) { obj[key] = subSource[key]; } }); // 这里统一用 assign,集中处理 id 的问题 target.assign ? target.assign(obj) : Object.assign(target, obj); }, { node: source }, { mode: 'anyObject' }); } /** * 去除循环依赖,转为纯 JSON * @param parentKey 外面的 key,提供给 JSON.stringify 使用 * @param excludedKeys 需要额外排除的 keys */ toJSON(parentKey, excludedKeys = []) { const JSON_EXCLUDED_KEYS = new Set(this.JSON_EXCLUDED_KEYS || []); // Object.getPrototypeOf(this).constructor.JSON_EXCLUDED_KEYS || []; excludedKeys.forEach((key) => JSON_EXCLUDED_KEYS.add(key)); const temp = {}; Object.keys(this).forEach((key) => { if (!JSON_EXCLUDED_KEYS.has(key)) temp[key] = this[key]; }); return JSON.parse(JSON.stringify(temp)); } /** * 转为单层的 JSON * @param parentKey 外面的 key,提供给 JSON.stringify 使用 * @param excludedKeys 需要额外排除的 keys */ toPlainJSON(parentKey, excludedKeys = []) { const JSON_EXCLUDED_KEYS = new Set(this.JSON_EXCLUDED_KEYS || []); // Object.getPrototypeOf(this).constructor.JSON_EXCLUDED_KEYS || []; excludedKeys.forEach((key) => JSON_EXCLUDED_KEYS.add(key)); const temp = {}; Object.keys(this).forEach((key) => { const value = this[key]; if (!JSON_EXCLUDED_KEYS.has(key) && typeof value !== 'object' && typeof value !== 'function') temp[key] = value; }); return JSON.parse(JSON.stringify(temp)); } /** * 查找引用 */ async findUsage() { const result = await common_1.findUsageService.findUsage({ query: { id: this.id, }, }); return result; } /** * 销毁 * 从 Map 中删除点和子节点 */ destroy() { __1.utils.traverse((current) => { __1.vertexsMap.delete(current.node.id); }, { node: this }, { mode: 'anyObject', excludedKeySet: this.JSON_EXCLUDED_KEYS, }); } /** * 通过 Ref 去查找点 * @param ref */ static getVertexByRef(ref) { const arr = ref.split('/'); const id = arr[arr.length - 1]; return __1.vertexsMap.get(id); } } __decorate([ decorators_1.excludedInJSON() ], Vertex.prototype, "_mutable", void 0); __decorate([ decorators_1.immutable() ], Vertex.prototype, "level", void 0); __decorate([ decorators_1.immutable() ], Vertex.prototype, "JSON_EXCLUDED_KEYS", void 0); __decorate([ decorators_1.immutable() ], Vertex.prototype, "id", void 0); __decorate([ decorators_1.excludedInJSON() ], Vertex.prototype, "_uuid", void 0); __decorate([ decorators_1.excludedInJSON() ], Vertex.prototype, "_cacheTimestamp", void 0); __decorate([ decorators_1.immutable() ], Vertex.prototype, "name", void 0); __decorate([ decorators_1.immutable() ], Vertex.prototype, "createdBy", void 0); __decorate([ decorators_1.immutable() ], Vertex.prototype, "createdTime", void 0); __decorate([ decorators_1.immutable() ], Vertex.prototype, "updatedBy", void 0); __decorate([ decorators_1.immutable() ], Vertex.prototype, "updatedTime", void 0); __decorate([ decorators_1.immutable() ], Vertex.prototype, "ideVersion", void 0); __decorate([ decorators_1.excludedInJSON() ], Vertex.prototype, "editing", void 0); __decorate([ decorators_1.excludedInJSON() ], Vertex.prototype, "loading", void 0); __decorate([ decorators_1.excludedInJSON() ], Vertex.prototype, "expanded", void 0); __decorate([ decorators_1.excludedInJSON() ], Vertex.prototype, "isLeaf", void 0); __decorate([ decorators_1.action('查找引用') ], Vertex.prototype, "findUsage", null); exports.Vertex = Vertex; exports.default = Vertex; //# sourceMappingURL=Vertex.js.map