增加了runningtree的resetItem,容了updateItem有子结点的case

This commit is contained in:
Xu Chang 2023-02-14 20:00:34 +08:00
parent 2c478e2310
commit f2acc9584a
9 changed files with 84 additions and 22 deletions

View File

@ -77,6 +77,7 @@ declare class ListNode<ED extends EntityDict & BaseEntityDict, T extends keyof E
addItem(item: Omit<ED[T]['CreateSingle']['data'], 'id'>, beforeExecute?: () => Promise<void>, afterExecute?: () => Promise<void>): void;
removeItem(id: string, beforeExecute?: () => Promise<void>, afterExecute?: () => Promise<void>): void;
recoverItem(id: string): void;
resetItem(id: string): void;
/**
* itemId进行更新
* @param data
@ -201,7 +202,8 @@ export declare class RunningTree<ED extends EntityDict & BaseEntityDict, Cxt ext
removeItem(path: string, id: string, beforeExecute?: () => Promise<void>, afterExecute?: () => Promise<void>): void;
updateItem<T extends keyof ED>(path: string, data: ED[T]['Update']['data'], id: string, action?: ED[T]['Action'], beforeExecute?: () => Promise<void>, afterExecute?: () => Promise<void>): void;
recoverItem(path: string, id: string): void;
create<T extends keyof ED>(path: string, data: Omit<ED[T]['CreateSingle']['data'], 'id'>, beforeExecute?: () => Promise<void>, afterExecute?: () => Promise<void>): Promise<void>;
resetItem(path: string, id: string): void;
create<T extends keyof ED>(path: string, data: Omit<ED[T]['CreateSingle']['data'], 'id'>, beforeExecute?: () => Promise<void>, afterExecute?: () => Promise<void>): void;
update<T extends keyof ED>(path: string, data: ED[T]['Update']['data'], action?: ED[T]['Action'], beforeExecute?: () => Promise<void>, afterExecute?: () => Promise<void>): void;
remove(path: string, beforeExecute?: () => Promise<void>, afterExecute?: () => Promise<void>): void;
isLoading(path: string): boolean | undefined;

View File

@ -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);

View File

@ -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);
},

View File

@ -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<T extends keyof ED>(data: Omit<ED[T]['CreateSingle']['data'], 'id'>, beforeExecute?: () => Promise<void>, afterExecute?: () => Promise<void>) {
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, {

3
lib/types/Page.d.ts vendored
View File

@ -200,6 +200,7 @@ export declare type OakListComponentMethods<ED extends EntityDict & BaseEntityDi
removeItem: (id: string, beforeExecute?: () => Promise<void>, afterExecute?: () => Promise<void>, path?: string) => void;
updateItem: (data: ED[T]['Update']['data'], id: string, action?: ED[T]['Action'], beforeExecute?: () => Promise<void>, afterExecute?: () => Promise<void>, 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<ED extends EntityDict & BaseEntityDict,
};
export declare type MakeOakComponent<ED extends EntityDict & BaseEntityDict, Cxt extends AsyncContext<ED>, FrontCxt extends SyncContext<ED>, AD extends Record<string, Aspect<ED, Cxt>>, FD extends Record<string, Feature>> = <T extends keyof ED, FormedData extends DataOption, IsList extends boolean, TData extends DataOption, TProperty extends PropertyOption, TMethod extends MethodOption>(options: OakComponentOption<ED, T, Cxt, FrontCxt, AD, FD, FormedData, IsList, TData, TProperty, TMethod>) => React.ComponentType<any>;
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<ED extends EntityDict & BaseEntityDict, T extends keyof ED, IsList extends boolean, TData extends DataOption = {}, TMethod extends MethodOption = {}> = {
methods: TMethod & OakCommonComponentMethods<ED, T> & OakListComponentMethods<ED, T> & OakSingleComponentMethods<ED, T>;

View File

@ -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<void>, afterExecute?: () => Promise<void>) {
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<T extends keyof ED>(path: string, data: Omit<ED[T]['CreateSingle']['data'], 'id'>, beforeExecute?: () => Promise<void>, afterExecute?: () => Promise<void>) {
resetItem(path: string, id: string) {
const node = this.findNode(path);
assert(node instanceof ListNode);
node.resetItem(id);
}
create<T extends keyof ED>(path: string, data: Omit<ED[T]['CreateSingle']['data'], 'id'>, beforeExecute?: () => Promise<void>, afterExecute?: () => Promise<void>) {
const node = this.findNode(path);
assert(node instanceof SingleNode);
await node.create(data, beforeExecute, afterExecute);
node.create(data, beforeExecute, afterExecute);
}
update<T extends keyof ED>(path: string, data: ED[T]['Update']['data'], action?: ED[T]['Action'], beforeExecute?: () => Promise<void>, afterExecute?: () => Promise<void>) {

View File

@ -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(

View File

@ -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<T extends keyof ED>(data: Omit<ED[T]['CreateSingle']['data'], 'id'>, beforeExecute?: () => Promise<void>, afterExecute?: () => Promise<void>) {
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<WebComponentListMethodNames, Function>);

View File

@ -360,6 +360,7 @@ export type OakListComponentMethods<ED extends EntityDict & BaseEntityDict, T ex
removeItem: (id: string, beforeExecute?: () => Promise<void>, afterExecute?: () => Promise<void>, path?: string) => void;
updateItem: (data: ED[T]['Update']['data'], id: string, action?: ED[T]['Action'], beforeExecute?: () => Promise<void>, afterExecute?: () => Promise<void>, 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';