From f2acc9584a1ad54db7657ca48c0970c2e4512b28 Mon Sep 17 00:00:00 2001 From: Xc Date: Tue, 14 Feb 2023 20:00:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86runningtree=E7=9A=84?= =?UTF-8?q?resetItem=EF=BC=8C=E5=AE=B9=E4=BA=86updateItem=E6=9C=89?= =?UTF-8?q?=E5=AD=90=E7=BB=93=E7=82=B9=E7=9A=84case?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/features/runningTree.d.ts | 4 +++- lib/features/runningTree.js | 34 +++++++++++++++++++--------------- lib/page.mp.js | 6 ++++++ lib/page.web.js | 12 ++++++++++++ lib/types/Page.d.ts | 3 ++- src/features/runningTree.ts | 23 ++++++++++++++++++++--- src/page.mp.ts | 8 +++++++- src/page.web.tsx | 13 +++++++++++++ src/types/Page.ts | 3 ++- 9 files changed, 84 insertions(+), 22 deletions(-) diff --git a/lib/features/runningTree.d.ts b/lib/features/runningTree.d.ts index cb7838eb..89740d2b 100644 --- a/lib/features/runningTree.d.ts +++ b/lib/features/runningTree.d.ts @@ -77,6 +77,7 @@ declare class ListNode, beforeExecute?: () => Promise, afterExecute?: () => Promise): void; removeItem(id: string, beforeExecute?: () => Promise, afterExecute?: () => Promise): void; recoverItem(id: string): void; + resetItem(id: string): void; /** * 目前只支持根据itemId进行更新 * @param data @@ -201,7 +202,8 @@ export declare class RunningTree Promise, afterExecute?: () => Promise): void; updateItem(path: string, data: ED[T]['Update']['data'], id: string, action?: ED[T]['Action'], beforeExecute?: () => Promise, afterExecute?: () => Promise): void; recoverItem(path: string, id: string): void; - create(path: string, data: Omit, beforeExecute?: () => Promise, afterExecute?: () => Promise): Promise; + resetItem(path: string, id: string): void; + create(path: string, data: Omit, beforeExecute?: () => Promise, afterExecute?: () => Promise): void; update(path: string, data: ED[T]['Update']['data'], action?: ED[T]['Action'], beforeExecute?: () => Promise, afterExecute?: () => Promise): void; remove(path: string, beforeExecute?: () => Promise, afterExecute?: () => Promise): void; isLoading(path: string): boolean | undefined; diff --git a/lib/features/runningTree.js b/lib/features/runningTree.js index 007a6455..1b8fe49e 100644 --- a/lib/features/runningTree.js +++ b/lib/features/runningTree.js @@ -853,6 +853,12 @@ var ListNode = /** @class */ (function (_super) { (0, lodash_1.unset)(this.updates, id); this.setDirty(); }; + ListNode.prototype.resetItem = function (id) { + var operation = this.updates[id].operation; + (0, assert_1.assert)(operation.action === 'update'); + (0, lodash_1.unset)(this.updates, id); + this.setDirty(); + }; /** * 目前只支持根据itemId进行更新 * @param data @@ -861,7 +867,11 @@ var ListNode = /** @class */ (function (_super) { * @param afterExecute */ ListNode.prototype.updateItem = function (data, id, action, beforeExecute, afterExecute) { - (0, assert_1.assert)(Object.keys(this.children).length === 0, "\u66F4\u65B0\u5B50\u7ED3\u70B9\u5E94\u8BE5\u843D\u5728\u76F8\u5E94\u7684component\u4E0A"); + // assert(Object.keys(this.children).length === 0, `更新子结点应该落在相应的component上`); + if (this.children && this.children[id]) { + // 实际中有这样的case出现,当使用actionButton时。先这样处理。by Xc 20230214 + return this.children[id].update(data, action, beforeExecute, afterExecute); + } if (this.updates[id]) { var operation = this.updates[id].operation; var dataOrigin = operation.data; @@ -2037,21 +2047,15 @@ var RunningTree = /** @class */ (function (_super) { (0, assert_1.assert)(node instanceof ListNode); node.recoverItem(id); }; + RunningTree.prototype.resetItem = function (path, id) { + var node = this.findNode(path); + (0, assert_1.assert)(node instanceof ListNode); + node.resetItem(id); + }; RunningTree.prototype.create = function (path, data, beforeExecute, afterExecute) { - return tslib_1.__awaiter(this, void 0, void 0, function () { - var node; - return tslib_1.__generator(this, function (_a) { - switch (_a.label) { - case 0: - node = this.findNode(path); - (0, assert_1.assert)(node instanceof SingleNode); - return [4 /*yield*/, node.create(data, beforeExecute, afterExecute)]; - case 1: - _a.sent(); - return [2 /*return*/]; - } - }); - }); + var node = this.findNode(path); + (0, assert_1.assert)(node instanceof SingleNode); + node.create(data, beforeExecute, afterExecute); }; RunningTree.prototype.update = function (path, data, action, beforeExecute, afterExecute) { var node = this.findNode(path); diff --git a/lib/page.mp.js b/lib/page.mp.js index 49896d93..6ed33d35 100644 --- a/lib/page.mp.js +++ b/lib/page.mp.js @@ -382,6 +382,12 @@ var oakBehavior = Behavior({ var path2 = path ? "".concat(this.state.oakFullpath, ".").concat(path) : this.state.oakFullpath; return this.features.runningTree.recoverItem(path2, id); }, + resetItem: function (id, path) { + var path2 = path + ? "".concat(this.state.oakFullpath, ".").concat(path) + : this.state.oakFullpath; + this.features.runningTree.resetItem(path2, id); + }, setId: function (id) { return this.features.runningTree.setId(this.state.oakFullpath, id); }, diff --git a/lib/page.web.js b/lib/page.web.js index 4bfd45e7..352bef6e 100644 --- a/lib/page.web.js +++ b/lib/page.web.js @@ -151,6 +151,12 @@ var OakComponentBase = /** @class */ (function (_super) { : this.state.oakFullpath; this.features.runningTree.recoverItem(path2, id); }; + OakComponentBase.prototype.resetItem = function (id, path) { + var path2 = path + ? "".concat(this.state.oakFullpath, ".").concat(path) + : this.state.oakFullpath; + this.features.runningTree.resetItem(path2, id); + }; /* create(data: Omit, beforeExecute?: () => Promise, afterExecute?: () => Promise) { this.features.runningTree.create(this.state.oakFullpath, data, beforeExecute, afterExecute); } */ @@ -504,6 +510,12 @@ function createComponent(option, features) { }, loadMore: function () { return _this.loadMore(); + }, + recoverItem: function (id, path) { + return _this.recoverItem(id, path); + }, + resetItem: function (id, path) { + return _this.resetItem(id, path); } }); Object.assign(methodProps, { diff --git a/lib/types/Page.d.ts b/lib/types/Page.d.ts index 10a48869..0c3d15e4 100644 --- a/lib/types/Page.d.ts +++ b/lib/types/Page.d.ts @@ -200,6 +200,7 @@ export declare type OakListComponentMethods Promise, afterExecute?: () => Promise, path?: string) => void; updateItem: (data: ED[T]['Update']['data'], id: string, action?: ED[T]['Action'], beforeExecute?: () => Promise, afterExecute?: () => Promise, path?: string) => void; recoverItem: (id: string, path?: string) => void; + resetItem: (id: string, path?: string) => void; }; declare type ComponentOnPropsChangeOption = { path?: string; @@ -233,7 +234,7 @@ export declare type OakListComoponetData, FrontCxt extends SyncContext, AD extends Record>, FD extends Record> = (options: OakComponentOption) => React.ComponentType; export declare type WebComponentCommonMethodNames = 'setNotification' | 'setMessage' | 'navigateTo' | 'navigateBack' | 'redirectTo' | 'clean' | 't' | 'execute' | 'refresh' | 'setDisablePulldownRefresh' | 'aggregate' | 'checkOperation'; -export declare type WebComponentListMethodNames = 'loadMore' | 'setFilters' | 'addNamedFilter' | 'removeNamedFilter' | 'removeNamedFilterByName' | 'setNamedSorters' | 'addNamedSorter' | 'removeNamedSorter' | 'removeNamedSorterByName' | 'setPageSize' | 'setCurrentPage' | 'addItem' | 'removeItem' | 'updateItem'; +export declare type WebComponentListMethodNames = 'loadMore' | 'setFilters' | 'addNamedFilter' | 'removeNamedFilter' | 'removeNamedFilterByName' | 'setNamedSorters' | 'addNamedSorter' | 'removeNamedSorter' | 'removeNamedSorterByName' | 'setPageSize' | 'setCurrentPage' | 'addItem' | 'removeItem' | 'updateItem' | 'resetItem' | 'recoverItem'; export declare type WebComponentSingleMethodNames = 'update' | 'remove' | 'isCreation'; export declare type WebComponentProps = { methods: TMethod & OakCommonComponentMethods & OakListComponentMethods & OakSingleComponentMethods; diff --git a/src/features/runningTree.ts b/src/features/runningTree.ts index f2c71c22..8b5b1034 100644 --- a/src/features/runningTree.ts +++ b/src/features/runningTree.ts @@ -922,6 +922,13 @@ class ListNode< this.setDirty(); } + resetItem(id: string) { + const { operation } = this.updates[id]; + assert(operation.action === 'update'); + unset(this.updates, id); + this.setDirty(); + } + /** * 目前只支持根据itemId进行更新 * @param data @@ -930,7 +937,11 @@ class ListNode< * @param afterExecute */ updateItem(data: ED[T]['Update']['data'], id: string, action?: ED[T]['Action'], beforeExecute?: () => Promise, afterExecute?: () => Promise) { - assert(Object.keys(this.children).length === 0, `更新子结点应该落在相应的component上`); + // assert(Object.keys(this.children).length === 0, `更新子结点应该落在相应的component上`); + if (this.children && this.children[id]) { + // 实际中有这样的case出现,当使用actionButton时。先这样处理。by Xc 20230214 + return this.children[id].update(data, action, beforeExecute, afterExecute); + } if (this.updates[id]) { const { operation } = this.updates[id]; const { data: dataOrigin } = operation; @@ -2053,10 +2064,16 @@ export class RunningTree< node.recoverItem(id); } - async create(path: string, data: Omit, beforeExecute?: () => Promise, afterExecute?: () => Promise) { + resetItem(path: string, id: string) { + const node = this.findNode(path); + assert(node instanceof ListNode); + node.resetItem(id); + } + + create(path: string, data: Omit, beforeExecute?: () => Promise, afterExecute?: () => Promise) { const node = this.findNode(path); assert(node instanceof SingleNode); - await node.create(data, beforeExecute, afterExecute); + node.create(data, beforeExecute, afterExecute); } update(path: string, data: ED[T]['Update']['data'], action?: ED[T]['Action'], beforeExecute?: () => Promise, afterExecute?: () => Promise) { diff --git a/src/page.mp.ts b/src/page.mp.ts index bf076d3a..4046f182 100644 --- a/src/page.mp.ts +++ b/src/page.mp.ts @@ -550,6 +550,12 @@ const oakBehavior = Behavior< id ); }, + resetItem(id: string, path?: string) { + const path2 = path + ? `${this.state.oakFullpath}.${path}` + : this.state.oakFullpath; + this.features.runningTree.resetItem(path2, id); + }, setId(id) { return this.features.runningTree.setId(this.state.oakFullpath, id); }, @@ -563,7 +569,7 @@ const oakBehavior = Behavior< : this.state.oakFullpath; return this.features.runningTree.isCreation(path2); }, - + update(data, action, beforeExecute, afterExecute, path) { const path2 = path ? `${this.state.oakFullpath}.${path}` : this.state.oakFullpath; return this.features.runningTree.update( diff --git a/src/page.web.tsx b/src/page.web.tsx index 65a801b6..9a7896a3 100644 --- a/src/page.web.tsx +++ b/src/page.web.tsx @@ -277,6 +277,13 @@ abstract class OakComponentBase< : this.state.oakFullpath; this.features.runningTree.recoverItem(path2, id); } + + resetItem(id: string, path?: string) { + const path2 = path + ? `${this.state.oakFullpath}.${path}` + : this.state.oakFullpath; + this.features.runningTree.resetItem(path2, id); + } /* create(data: Omit, beforeExecute?: () => Promise, afterExecute?: () => Promise) { this.features.runningTree.create(this.state.oakFullpath, data, beforeExecute, afterExecute); @@ -776,6 +783,12 @@ export function createComponent< }, loadMore: () => { return this.loadMore(); + }, + recoverItem: (id: string, path?: string) => { + return this.recoverItem(id, path); + }, + resetItem: (id: string, path?: string) => { + return this.resetItem(id, path); } } as Record); diff --git a/src/types/Page.ts b/src/types/Page.ts index 694c4014..f68746a5 100644 --- a/src/types/Page.ts +++ b/src/types/Page.ts @@ -360,6 +360,7 @@ export type OakListComponentMethods Promise, afterExecute?: () => Promise, path?: string) => void; updateItem: (data: ED[T]['Update']['data'], id: string, action?: ED[T]['Action'], beforeExecute?: () => Promise, afterExecute?: () => Promise, path?: string) => void; recoverItem: (id: string, path?: string) => void; + resetItem: (id: string, path?: string) => void; }; type ComponentOnPropsChangeOption = { @@ -436,7 +437,7 @@ export type WebComponentCommonMethodNames = 'setNotification' | 'setMessage' | ' // 暴露给list组件的方法 export type WebComponentListMethodNames = 'loadMore' | 'setFilters' | 'addNamedFilter' | 'removeNamedFilter' | 'removeNamedFilterByName' | 'setNamedSorters' - | 'addNamedSorter' | 'removeNamedSorter' | 'removeNamedSorterByName' | 'setPageSize' | 'setCurrentPage' | 'addItem' | 'removeItem' | 'updateItem'; + | 'addNamedSorter' | 'removeNamedSorter' | 'removeNamedSorterByName' | 'setPageSize' | 'setCurrentPage' | 'addItem' | 'removeItem' | 'updateItem' | 'resetItem' | 'recoverItem'; // 暴露给single组件的方法 export type WebComponentSingleMethodNames = 'update' | 'remove' | 'isCreation';