增加了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) {
this.id = id;
if (this.operation) {
throw new Error('结点上的operation没有clean之前是不能setId的');
}
this.refresh();
if (this.dirty) {
throw new Error('结点没有clean之前是不能setId的');
}
this.id = id;
};
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) {
this.id = id;
if (this.operation) {
throw new Error('结点上的operation没有clean之前是不能setId的');
}
this.refresh();
if (this.dirty) {
throw new Error('结点没有clean之前是不能setId的');
}
this.id = id;
}
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

@ -18,7 +18,7 @@ import {
import {
onPathSet, reRender, refresh,
loadMore, execute,
loadMore, execute,
destroyNode,
} from './page.common';
import { MessageProps } from './types/Message';
@ -50,17 +50,17 @@ const oakBehavior = Behavior<
DataOption,
WechatMiniprogram.Component.PropertyOption,
OakCommonComponentMethods<EDD, keyof EDD> &
OakListComponentMethods<EDD, keyof EDD> &
OakSingleComponentMethods<EDD, keyof EDD> & {
iAmThePage: () => boolean;
setState: (
data: Record<string, any>,
callback?: () => void
) => void;
onLoad: (query: Record<string, any>) => Promise<void>;
onPullDownRefresh: () => Promise<void>;
onReachBottom: () => Promise<void>;
},
OakListComponentMethods<EDD, keyof EDD> &
OakSingleComponentMethods<EDD, keyof EDD> & {
iAmThePage: () => boolean;
setState: (
data: Record<string, any>,
callback?: () => void
) => void;
onLoad: (query: Record<string, any>) => Promise<void>;
onPullDownRefresh: () => Promise<void>;
onReachBottom: () => Promise<void>;
},
{
state: Record<string, any>;
props: {
@ -81,7 +81,7 @@ const oakBehavior = Behavior<
FrontCxt,
ADD & CommonAspectDict<EDD, Cxt>
> &
FDD;
FDD;
subscribed: Array<() => void>;
oakOption: OakComponentOption<
EDD,
@ -221,7 +221,7 @@ const oakBehavior = Behavior<
);
}
}
if (Object.keys(dataResolved).length > 0) {
this.setState(dataResolved);
}
@ -364,7 +364,7 @@ const oakBehavior = Behavior<
},
setFilters(filters, path) {
const path2 = path? `${this.state.oakFullpath}.${path}` : this.state.oakFullpath;
const path2 = path ? `${this.state.oakFullpath}.${path}` : this.state.oakFullpath;
this.features.runningTree.setNamedFilters(
path2,
filters
@ -373,7 +373,7 @@ const oakBehavior = Behavior<
getFilters(path) {
if (this.state.oakFullpath) {
const path2 = path? `${this.state.oakFullpath}.${path}` : this.state.oakFullpath;
const path2 = path ? `${this.state.oakFullpath}.${path}` : this.state.oakFullpath;
const namedFilters = this.features.runningTree.getNamedFilters(
this.state.oakFullpath
);
@ -388,7 +388,7 @@ const oakBehavior = Behavior<
getFilterByName(name, path) {
if (this.state.oakFullpath) {
const path2 = path? `${this.state.oakFullpath}.${path}` : this.state.oakFullpath;
const path2 = path ? `${this.state.oakFullpath}.${path}` : this.state.oakFullpath;
const filter = this.features.runningTree.getNamedFilterByName(
path2,
name
@ -403,7 +403,7 @@ const oakBehavior = Behavior<
},
addNamedFilter(namedFilter, refresh, path) {
const path2 = path? `${this.state.oakFullpath}.${path}` : this.state.oakFullpath;
const path2 = path ? `${this.state.oakFullpath}.${path}` : this.state.oakFullpath;
this.features.runningTree.addNamedFilter(
path2,
namedFilter,
@ -412,7 +412,7 @@ const oakBehavior = Behavior<
},
removeNamedFilter(namedFilter, refresh, path) {
const path2 = path? `${this.state.oakFullpath}.${path}` : this.state.oakFullpath;
const path2 = path ? `${this.state.oakFullpath}.${path}` : this.state.oakFullpath;
this.features.runningTree.removeNamedFilter(
path2,
namedFilter,
@ -421,7 +421,7 @@ const oakBehavior = Behavior<
},
removeNamedFilterByName(name, refresh, path) {
const path2 = path? `${this.state.oakFullpath}.${path}` : this.state.oakFullpath;
const path2 = path ? `${this.state.oakFullpath}.${path}` : this.state.oakFullpath;
this.features.runningTree.removeNamedFilterByName(
path2,
name,
@ -430,7 +430,7 @@ const oakBehavior = Behavior<
},
setNamedSorters(namedSorters, path) {
const path2 = path? `${this.state.oakFullpath}.${path}` : this.state.oakFullpath;
const path2 = path ? `${this.state.oakFullpath}.${path}` : this.state.oakFullpath;
this.features.runningTree.setNamedSorters(
path2,
namedSorters
@ -439,7 +439,7 @@ const oakBehavior = Behavior<
getSorters(path) {
if (this.state.oakFullpath) {
const path2 = path? `${this.state.oakFullpath}.${path}` : this.state.oakFullpath;
const path2 = path ? `${this.state.oakFullpath}.${path}` : this.state.oakFullpath;
const namedSorters = this.features.runningTree.getNamedSorters(path2);
const sorters = namedSorters
.map(({ sorter }) => {
@ -455,7 +455,7 @@ const oakBehavior = Behavior<
getSorterByName(name, path) {
if (this.state.oakFullpath) {
const path2 = path? `${this.state.oakFullpath}.${path}` : this.state.oakFullpath;
const path2 = path ? `${this.state.oakFullpath}.${path}` : this.state.oakFullpath;
const sorter = this.features.runningTree.getNamedSorterByName(
path2,
name
@ -470,12 +470,12 @@ const oakBehavior = Behavior<
},
addNamedSorter(namedSorter, refresh, path) {
const path2 = path? `${this.state.oakFullpath}.${path}` : this.state.oakFullpath;
const path2 = path ? `${this.state.oakFullpath}.${path}` : this.state.oakFullpath;
this.features.runningTree.addNamedSorter(path2, namedSorter, refresh);
},
removeNamedSorter(namedSorter, refresh, path) {
const path2 = path? `${this.state.oakFullpath}.${path}` : this.state.oakFullpath;
const path2 = path ? `${this.state.oakFullpath}.${path}` : this.state.oakFullpath;
this.features.runningTree.removeNamedSorter(
path2,
namedSorter,
@ -484,19 +484,19 @@ const oakBehavior = Behavior<
},
removeNamedSorterByName(name, refresh, path) {
const path2 = path? `${this.state.oakFullpath}.${path}` : this.state.oakFullpath;
const path2 = path ? `${this.state.oakFullpath}.${path}` : this.state.oakFullpath;
this.features.runningTree.removeNamedSorterByName(path2, name, refresh);
},
getPagination(path) {
if (this.state.oakFullpath) {
const path2 = path? `${this.state.oakFullpath}.${path}` : this.state.oakFullpath;
const path2 = path ? `${this.state.oakFullpath}.${path}` : this.state.oakFullpath;
return this.features.runningTree.getPagination(path2);
}
},
setPageSize(pageSize, path) {
const path2 = path? `${this.state.oakFullpath}.${path}` : this.state.oakFullpath;
const path2 = path ? `${this.state.oakFullpath}.${path}` : this.state.oakFullpath;
this.features.runningTree.setPageSize(
path2,
pageSize
@ -507,7 +507,7 @@ const oakBehavior = Behavior<
assert(currentPage !== 0);
if (this.state.oakEntity && this.state.oakFullpath) {
const path2 = path? `${this.state.oakFullpath}.${path}` : this.state.oakFullpath;
const path2 = path ? `${this.state.oakFullpath}.${path}` : this.state.oakFullpath;
this.features.runningTree.setCurrentPage(
path2,
currentPage
@ -515,7 +515,7 @@ const oakBehavior = Behavior<
}
},
addItem(data, beforeExecute, afterExecute, path) {
const path2 = path? `${this.state.oakFullpath}.${path}` : this.state.oakFullpath;
const path2 = path ? `${this.state.oakFullpath}.${path}` : this.state.oakFullpath;
return this.features.runningTree.addItem(
path2,
data,
@ -524,7 +524,7 @@ const oakBehavior = Behavior<
);
},
updateItem(data, id, action, beforeExecute, afterExecute, path) {
const path2 = path? `${this.state.oakFullpath}.${path}` : this.state.oakFullpath;
const path2 = path ? `${this.state.oakFullpath}.${path}` : this.state.oakFullpath;
return this.features.runningTree.updateItem(
path2,
data,
@ -535,7 +535,7 @@ const oakBehavior = Behavior<
);
},
removeItem(id, beforeExecute, afterExecute, path) {
const path2 = path? `${this.state.oakFullpath}.${path}` : this.state.oakFullpath;
const path2 = path ? `${this.state.oakFullpath}.${path}` : this.state.oakFullpath;
return this.features.runningTree.removeItem(
path2,
id,
@ -544,7 +544,7 @@ const oakBehavior = Behavior<
);
},
recoverItem(id, path) {
const path2 = path? `${this.state.oakFullpath}.${path}` : this.state.oakFullpath;
const path2 = path ? `${this.state.oakFullpath}.${path}` : this.state.oakFullpath;
return this.features.runningTree.recoverItem(
path2,
id
@ -556,8 +556,16 @@ 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;
const path2 = path ? `${this.state.oakFullpath}.${path}` : this.state.oakFullpath;
return this.features.runningTree.update(
path2,
data,
@ -567,7 +575,7 @@ const oakBehavior = Behavior<
);
},
remove(beforeExecute, afterExecute, path) {
const path2 = path? `${this.state.oakFullpath}.${path}` : this.state.oakFullpath;
const path2 = path ? `${this.state.oakFullpath}.${path}` : this.state.oakFullpath;
return this.features.runningTree.remove(
path2,
beforeExecute,
@ -655,7 +663,7 @@ export function createComponent<
lifetimes,
observers,
} = option;
const { attached, show, hide, created, detached, ready, moved, error } = lifetimes || {};
const { attached, show, hide, created, detached, ready, moved, error } = lifetimes || {};
const { options, externalClasses } = wechatMp || {};
return Component<
@ -682,7 +690,7 @@ export function createComponent<
FrontCxt,
AD & CommonAspectDict<ED, Cxt>
> &
FD;
FD;
subscribed: Array<() => void>;
oakOption: OakComponentOption<
ED,
@ -745,7 +753,7 @@ export function createComponent<
if (option.entity) {
this.subscribed.push(
features.cache.subscribe(() => this.reRender())
);
);
}
attached && attached.call(this);
},
@ -756,7 +764,7 @@ export function createComponent<
this.state.oakFullpath &&
(this.iAmThePage() || this.props.oakAutoUnmount) &&
destroyNode.call(this as any);
detached && detached.call(this);
},
ready() {

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,