UNPKG

@nocobase/flow-engine

Version:

A standalone flow engine for NocoBase, managing workflows, models, and actions.

231 lines (229 loc) 7.04 kB
/** * This file is part of the NocoBase (R) project. * Copyright (c) 2020-2024 NocoBase Co., Ltd. * Authors: NocoBase Team. * * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License. * For more information, please refer to: https://www.nocobase.com/agreement. */ var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var multiRecordResource_exports = {}; __export(multiRecordResource_exports, { MultiRecordResource: () => MultiRecordResource }); module.exports = __toCommonJS(multiRecordResource_exports); var import_reactive = require("@formily/reactive"); var import_lodash = __toESM(require("lodash")); var import_baseRecordResource = require("./baseRecordResource"); const _MultiRecordResource = class _MultiRecordResource extends import_baseRecordResource.BaseRecordResource { _data = import_reactive.observable.ref([]); _meta = import_reactive.observable.ref({}); refreshTimer = null; createActionOptions = {}; updateActionOptions = {}; _refreshActionName = "list"; // 请求配置 - 与 APIClient 接口保持一致 request = { // url: null as string | null, method: "get", params: { filter: {}, filterByTk: null, appends: [], fields: [], sort: null, except: null, whitelist: null, blacklist: null, page: 1, pageSize: 20 }, headers: {} }; get supportsFilter() { return true; } setCreateActionOptions(options) { this.createActionOptions = options; return this; } setUpdateActionOptions(options) { this.updateActionOptions = options; return this; } setSelectedRows(selectedRows) { this.setMeta({ selectedRows }); return this; } getSelectedRows() { return this.getMeta("selectedRows") || []; } getCount() { return this.getMeta("count"); } setPage(page) { this.addRequestParameter("page", page); return this.setMeta({ page }); } getPage() { return this.getMeta("page"); } setPageSize(pageSize) { this.addRequestParameter("pageSize", pageSize); return this.setMeta({ pageSize }); } getPageSize() { return this.getMeta("pageSize"); } getTotalPage() { return this.getMeta("totalPage"); } getCell(rowIndex, columnKey) { var _a; const data = this.getData(); return (_a = data == null ? void 0 : data[rowIndex]) == null ? void 0 : _a[columnKey]; } async next() { this.request.params.page += 1; await this.refresh(); } async previous() { if (this.request.params.page > 1) { this.request.params.page -= 1; await this.refresh(); } } async goto(page) { if (page > 0) { this.request.params.page = page; await this.refresh(); } } async create(data, options) { const config = this.mergeRequestConfig({ data }, this.createActionOptions, options); await this.runAction("create", config); await this.refresh(); } async get(filterByTk) { const options = { params: { filterByTk } }; const { data } = await this.runAction("get", { ...options }); return data; } async update(filterByTk, data, options) { const config = this.mergeRequestConfig( { params: { filterByTk }, data }, this.updateActionOptions, options ); await this.runAction("update", config); await this.refresh(); } async destroySelectedRows() { const selectedRows = this.getSelectedRows(); if (selectedRows.length === 0) { throw new Error("No rows selected for deletion."); } await this.destroy(selectedRows); } async destroy(filterByTk, options) { const config = this.mergeRequestConfig( { params: { filterByTk: import_lodash.default.castArray(filterByTk).map((item) => { return typeof item === "object" ? item["id"] : item; }) } }, options ); await this.runAction("destroy", config); await this.refresh(); } setItem(index, newDataItem) { const oldData = this.getData(); const newData = oldData.slice(); newData[index] = { ...newDataItem }; this.setData(newData); } /** * 在同一个事件循环内多次调用 refresh 方法时,只有最后一次调用会生效。避免触发多次相同的接口请求。 * @returns */ async refresh() { if (this.refreshTimer) { clearTimeout(this.refreshTimer); } return new Promise((resolve, reject) => { this.refreshTimer = setTimeout(async () => { try { this.clearError(); this.loading = true; const { data, meta } = await this.runAction(this._refreshActionName, { method: "get", ...this.getRefreshRequestOptions() }); this.setData(data).setMeta(meta); if (meta == null ? void 0 : meta.page) { this.setPage(meta.page); } if (meta == null ? void 0 : meta.pageSize) { this.setPageSize(meta.pageSize); } this.emit("refresh"); this.loading = false; resolve(); } catch (error) { this.setError(error); reject(error instanceof Error ? error : new Error(String(error))); } finally { this.refreshTimer = null; this.loading = false; } }); }); } }; __name(_MultiRecordResource, "MultiRecordResource"); let MultiRecordResource = _MultiRecordResource; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { MultiRecordResource });