增加了isCreation方法,差别一个singleNode是不是创建动作

This commit is contained in:
Xu Chang 2023-02-05 12:39:55 +08:00
parent 8deede6ed8
commit bd1d4ce0e9
9 changed files with 103 additions and 53 deletions

View File

@ -125,6 +125,7 @@ declare class SingleNode<ED extends EntityDict & BaseEntityDict, T extends keyof
addChild(path: string, node: SingleNode<ED, keyof ED, Cxt, FrontCxt, AD> | ListNode<ED, keyof ED, Cxt, FrontCxt, AD>): void;
removeChild(path: string): void;
getFreshValue(noCascade?: boolean): Partial<ED[T]['Schema']> | undefined;
isCreation(): boolean;
doBeforeTrigger(): Promise<void>;
doAfterTrigger(): Promise<void>;
create(data: Partial<Omit<ED[T]['CreateSingle']['data'], 'id'>>, beforeExecute?: () => Promise<void>, afterExecute?: () => Promise<void>): void;
@ -230,6 +231,7 @@ export declare class RunningTree<ED extends EntityDict & BaseEntityDict, Cxt ext
entity: keyof ED;
operation: ED[keyof ED]["Operation"];
}[] | undefined;
isCreation(path: string): boolean;
execute<T extends keyof ED>(path: string, action?: ED[T]['Action']): Promise<{
entity: keyof ED;
operation: ED[keyof ED]["Operation"];

View File

@ -1258,13 +1258,10 @@ var SingleNode = /** @class */ (function (_super) {
* 似乎只有最顶层设置了id的结点的这种情况才需要刷新
* 如果是子结点这里的id可以从父结点获得这个操作是因为create action所引起可以无视userRelation/byMobile会跑出来
*/
if (this.id && this.id !== id) {
if (this.dirty) {
throw new Error('结点没有clean之前是不能setId的');
}
this.id = id;
if (this.operation) {
throw new Error('结点上的operation没有clean之前是不能setId的');
}
this.refresh();
}
};
SingleNode.prototype.unsetId = function () {
this.id = undefined;
@ -1306,6 +1303,10 @@ var SingleNode = /** @class */ (function (_super) {
return result[0];
}
};
SingleNode.prototype.isCreation = function () {
var _a;
return ((_a = this.operation) === null || _a === void 0 ? void 0 : _a.operation.action) === 'create';
};
SingleNode.prototype.doBeforeTrigger = function () {
var _a;
return tslib_1.__awaiter(this, void 0, void 0, function () {
@ -2218,6 +2219,11 @@ var RunningTree = /** @class */ (function (_super) {
var operations = node.composeOperations();
return operations;
};
RunningTree.prototype.isCreation = function (path) {
var node = this.findNode(path);
(0, assert_1.assert)(node instanceof SingleNode);
return node.isCreation();
};
RunningTree.prototype.execute = function (path, action) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var node, operations, entities, err_3;

View File

@ -388,6 +388,12 @@ var oakBehavior = Behavior({
unsetId: function () {
return this.features.runningTree.unsetId(this.state.oakFullpath);
},
isCreation: function (path) {
var path2 = path
? "".concat(this.state.oakFullpath, ".").concat(path)
: this.state.oakFullpath;
return this.features.runningTree.isCreation(path2);
},
update: function (data, action, beforeExecute, afterExecute, path) {
var path2 = path ? "".concat(this.state.oakFullpath, ".").concat(path) : this.state.oakFullpath;
return this.features.runningTree.update(path2, data, action, beforeExecute, afterExecute);

View File

@ -154,6 +154,12 @@ var OakComponentBase = /** @class */ (function (_super) {
/* 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);
} */
OakComponentBase.prototype.isCreation = function (path) {
var path2 = path
? "".concat(this.state.oakFullpath, ".").concat(path)
: this.state.oakFullpath;
return this.features.runningTree.isCreation(path2);
};
OakComponentBase.prototype.update = function (data, action, beforeExecute, afterExecute, path) {
var path2 = path
? "".concat(this.state.oakFullpath, ".").concat(path)
@ -510,6 +516,9 @@ function createComponent(option, features) {
remove: function (beforeExecute, afterExecute, path) {
return _this.remove(beforeExecute, afterExecute, path);
},
isCreation: function (path) {
return _this.isCreation(path);
}
});
if (methods) {
var _loop_1 = function (m) {

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

@ -177,6 +177,7 @@ export declare type OakSingleComponentMethods<ED extends EntityDict & BaseEntity
getId: () => string | undefined;
update: (data: ED[T]['Update']['data'], action?: ED[T]['Action'], beforeExecute?: () => Promise<void>, afterExecute?: () => Promise<void>, path?: string) => void;
remove: (beforeExecute?: () => Promise<void>, afterExecute?: () => Promise<void>, path?: string) => void;
isCreation: (path?: string) => boolean;
};
export declare type OakListComponentMethods<ED extends EntityDict & BaseEntityDict, T extends keyof ED> = {
loadMore: () => Promise<void>;
@ -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 WebComponentSingleMethodNames = 'update' | 'remove';
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>;
data: TData & OakComponentData<ED, T> & (IsList extends true ? OakListComoponetData<ED, T> : {});

View File

@ -1267,13 +1267,10 @@ class SingleNode<ED extends EntityDict & BaseEntityDict,
* id的结点的这种情况才需要刷新
* id可以从父结点获得create action所引起userRelation/byMobile会跑出来
*/
if (this.id && this.id !== id) {
if (this.dirty) {
throw new Error('结点没有clean之前是不能setId的');
}
this.id = id;
if (this.operation) {
throw new Error('结点上的operation没有clean之前是不能setId的');
}
this.refresh();
}
}
unsetId() {
@ -1328,6 +1325,10 @@ class SingleNode<ED extends EntityDict & BaseEntityDict,
}
}
isCreation() {
return this.operation?.operation.action === 'create';
}
async doBeforeTrigger(): Promise<void> {
if (this.operation?.beforeExecute) {
@ -2250,6 +2251,12 @@ export class RunningTree<
return operations;
}
isCreation(path: string) {
const node = this.findNode(path)!;
assert(node instanceof SingleNode);
return node.isCreation();
}
async execute<T extends keyof ED>(path: string, action?: ED[T]['Action']) {
const node = this.findNode(path)!;
if (action) {

View File

@ -556,6 +556,14 @@ const oakBehavior = Behavior<
unsetId() {
return this.features.runningTree.unsetId(this.state.oakFullpath);
},
isCreation(path?: string) {
const path2 = path
? `${this.state.oakFullpath}.${path}`
: 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

@ -282,6 +282,13 @@ abstract class OakComponentBase<
this.features.runningTree.create(this.state.oakFullpath, data, beforeExecute, afterExecute);
} */
isCreation(path?: string) {
const path2 = path
? `${this.state.oakFullpath}.${path}`
: this.state.oakFullpath;
return this.features.runningTree.isCreation(path2);
}
update<T extends keyof ED>(
data: ED[T]['Update']['data'],
action?: ED[T]['Action'],
@ -782,6 +789,9 @@ export function createComponent<
remove: (beforeExecute?: () => Promise<void>, afterExecute?: () => Promise<void>, path?: string) => {
return this.remove(beforeExecute, afterExecute, path);
},
isCreation: (path?: string)=> {
return this.isCreation(path);
}
} as Record<WebComponentSingleMethodNames, Function>);
if (methods) {

View File

@ -333,6 +333,7 @@ export type OakSingleComponentMethods<ED extends EntityDict & BaseEntityDict, T
// create: (data: Omit<ED[T]['CreateSingle']['data'], 'id'>, beforeExecute?: () => Promise<void>, afterExecute?: () => Promise<void>) => void;
update: (data: ED[T]['Update']['data'], action?: ED[T]['Action'], beforeExecute?: () => Promise<void>, afterExecute?: () => Promise<void>, path?: string) => void;
remove: (beforeExecute?: () => Promise<void>, afterExecute?: () => Promise<void>, path?: string) => void;
isCreation: (path?: string) => boolean;
}
export type OakListComponentMethods<ED extends EntityDict & BaseEntityDict, T extends keyof ED> = {
@ -436,7 +437,7 @@ export type WebComponentListMethodNames = 'loadMore' | 'setFilters' | 'addNamedF
| 'addNamedSorter' | 'removeNamedSorter' | 'removeNamedSorterByName' | 'setPageSize' | 'setCurrentPage' | 'addItem' | 'removeItem' | 'updateItem';
// 暴露给single组件的方法
export type WebComponentSingleMethodNames = 'update' | 'remove';
export type WebComponentSingleMethodNames = 'update' | 'remove' | 'isCreation';
export type WebComponentProps<
ED extends EntityDict & BaseEntityDict,