调整了一些runningTree的细节

This commit is contained in:
Xu Chang 2023-03-22 20:01:46 +08:00
parent 460f4c140f
commit 28898a21b5
9 changed files with 96 additions and 88 deletions

View File

@ -121,7 +121,7 @@ declare class ListNode<ED extends EntityDict & BaseEntityDict, T extends keyof E
getIntrinsticFilters(): ED[T]["Selection"]["filter"];
}
declare class SingleNode<ED extends EntityDict & BaseEntityDict, T extends keyof ED, Cxt extends AsyncContext<ED>, FrontCxt extends SyncContext<ED>, AD extends CommonAspectDict<ED, Cxt>> extends Node<ED, T, Cxt, FrontCxt, AD> {
private id;
private id?;
private children;
private operation?;
constructor(entity: T, schema: StorageSchema<ED>, cache: Cache<ED, Cxt, FrontCxt, AD>, authDict: AuthDefDict<ED>, projection?: ED[T]['Selection']['data'] | (() => Promise<ED[T]['Selection']['data']>), parent?: SingleNode<ED, keyof ED, Cxt, FrontCxt, AD> | ListNode<ED, T, Cxt, FrontCxt, AD> | VirtualNode<ED, Cxt, FrontCxt, AD>, path?: string, id?: string, actions?: ActionDef<ED, T>[] | (() => ActionDef<ED, T>[]), cascadeActions?: () => {
@ -133,14 +133,13 @@ declare class SingleNode<ED extends EntityDict & BaseEntityDict, T extends keyof
getChild(path: string): SingleNode<ED, keyof ED, Cxt, FrontCxt, AD> | ListNode<ED, keyof ED, Cxt, FrontCxt, AD>;
setId(id: string): void;
unsetId(): void;
getId(): string;
getId(): string | undefined;
getChildren(): {
[K: string]: SingleNode<ED, keyof ED, Cxt, FrontCxt, AD> | ListNode<ED, keyof ED, Cxt, FrontCxt, AD>;
};
addChild(path: string, node: SingleNode<ED, keyof ED, Cxt, FrontCxt, AD> | ListNode<ED, keyof ED, Cxt, FrontCxt, AD>): void;
removeChild(path: string): void;
getFreshValue(context?: FrontCxt): 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;
@ -232,7 +231,7 @@ export declare class RunningTree<ED extends EntityDict & BaseEntityDict, Cxt ext
getPagination(path: string): Pagination;
setId(path: string, id: string): void;
unsetId(path: string): void;
getId(path: string): string;
getId(path: string): string | undefined;
setPageSize<T extends keyof ED>(path: string, pageSize: number): void;
setCurrentPage<T extends keyof ED>(path: string, currentPage: number): void;
getNamedFilters<T extends keyof ED>(path: string): NamedFilterItem<ED, keyof ED>[];
@ -253,7 +252,6 @@ 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<{
result: Awaited<ReturnType<AD["operate"]>>;
message: string | null | undefined;

View File

@ -1054,8 +1054,7 @@ var SingleNode = /** @class */ (function (_super) {
this.publish();
};
SingleNode.prototype.unsetId = function () {
this.create({});
this.id = this.operation.operation.data.id;
this.id = undefined;
this.publish();
};
// 最好用getFreshValue取值
@ -1085,12 +1084,6 @@ var SingleNode = /** @class */ (function (_super) {
return result[0];
}
};
SingleNode.prototype.isCreation = function () {
var _a;
var operations = this.composeOperations();
// 如果是create一定在第一个
return !!(operations && ((_a = operations[0]) === 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 () {
@ -1161,6 +1154,7 @@ var SingleNode = /** @class */ (function (_super) {
};
SingleNode.prototype.create = function (data, beforeExecute, afterExecute) {
var id = (0, uuid_1.generateNewId)();
(0, assert_1.assert)(!this.id && !this.dirty, 'create前要保证singleNode为空');
this.operation = {
operation: {
id: (0, uuid_1.generateNewId)(),
@ -1170,6 +1164,7 @@ var SingleNode = /** @class */ (function (_super) {
beforeExecute: beforeExecute,
afterExecute: afterExecute,
};
this.id = id;
this.setDirty();
};
SingleNode.prototype.update = function (data, action, beforeExecute, afterExecute) {
@ -1238,10 +1233,16 @@ var SingleNode = /** @class */ (function (_super) {
if (childOperations) {
if (child instanceof SingleNode) {
(0, assert_1.assert)(childOperations.length === 1);
(0, assert_1.assert)(!operation.data[ele2]); // 多对一的子结点不应该有多项
Object.assign(operation.data, (_a = {},
_a[ele2] = childOperations[0].operation,
_a));
if (!operation.data[ele2]) {
Object.assign(operation.data, (_a = {},
_a[ele2] = childOperations[0].operation,
_a));
}
else {
// 目前应该只允许一种情况就是父create子update
(0, assert_1.assert)(operation.data[ele].action === 'create' && childOperations[0].operation.action === 'update');
Object.assign(operation.data[ele].data, childOperations[0].operation.data);
}
}
else {
(0, assert_1.assert)(child instanceof ListNode);
@ -1305,11 +1306,12 @@ var SingleNode = /** @class */ (function (_super) {
return projection;
};
SingleNode.prototype.refresh = function () {
var _a;
return tslib_1.__awaiter(this, void 0, void 0, function () {
var projection, filter, err_2;
var _this = this;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0:
// SingleNode如果是ListNode的子结点则不必refresh优化ListNode有义务负责子层对象的数据
if (this.parent && this.parent instanceof ListNode && this.parent.getEntity() === this.getEntity()) {
@ -1317,7 +1319,7 @@ var SingleNode = /** @class */ (function (_super) {
return [2 /*return*/];
}
// 如果是新建也不用refresh
if (this.isCreation()) {
if (((_a = this.operation) === null || _a === void 0 ? void 0 : _a.operation.action) === 'create') {
return [2 /*return*/];
}
projection = this.getProjection(undefined, true);
@ -1325,9 +1327,9 @@ var SingleNode = /** @class */ (function (_super) {
if (!(projection && filter)) return [3 /*break*/, 4];
this.setLoading(true);
this.publish();
_a.label = 1;
_b.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
_b.trys.push([1, 3, , 4]);
return [4 /*yield*/, this.cache.refresh(this.entity, {
data: projection,
filter: filter,
@ -1341,10 +1343,10 @@ var SingleNode = /** @class */ (function (_super) {
_this.setLoading(false);
})];
case 2:
_a.sent();
_b.sent();
return [3 /*break*/, 4];
case 3:
err_2 = _a.sent();
err_2 = _b.sent();
this.setLoading(false);
throw err_2;
case 4: return [2 /*return*/];
@ -2026,11 +2028,6 @@ 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, result, err_3;

View File

@ -398,18 +398,18 @@ 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);
},
create: function (data, beforeExecute, afterExecute, path) {
var path2 = path
? "".concat(this.state.oakFullpath, ".").concat(path)
: this.state.oakFullpath;
return this.features.runningTree.create(path2, data, beforeExecute, afterExecute);
},
remove: function (beforeExecute, afterExecute, path) {
var path2 = path
? "".concat(this.state.oakFullpath, ".").concat(path)

View File

@ -163,18 +163,18 @@ 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)
: this.state.oakFullpath;
this.features.runningTree.update(path2, data, action, beforeExecute, afterExecute);
};
OakComponentBase.prototype.create = function (data, beforeExecute, afterExecute, path) {
var path2 = path
? "".concat(this.state.oakFullpath, ".").concat(path)
: this.state.oakFullpath;
this.features.runningTree.create(path2, data, beforeExecute, afterExecute);
};
OakComponentBase.prototype.remove = function (beforeExecute, afterExecute, path) {
var path2 = path
? "".concat(this.state.oakFullpath, ".").concat(path)
@ -536,12 +536,12 @@ function createComponent(option, features) {
update: function (data, action, beforeExecute, afterExecute, path) {
return _this.update(data, action, beforeExecute, afterExecute, path);
},
create: function (data, beforeExecute, afterExecute, path) {
return _this.create(data, beforeExecute, afterExecute, path);
},
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) {

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

@ -200,9 +200,9 @@ export declare type OakSingleComponentMethods<ED extends EntityDict & BaseEntity
setId: (id: string) => void;
unsetId: () => void;
getId: () => string | undefined;
create: (data: Omit<ED[T]['CreateSingle']['data'], 'id'>, beforeExecute?: () => Promise<void>, afterExecute?: () => Promise<void>, path?: string) => 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 declare type OakListComponentMethods<ED extends EntityDict & BaseEntityDict, T extends keyof ED> = {
loadMore: () => Promise<void>;
@ -260,7 +260,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>) => (props: ReactComponentProps<IsList, TProperty>) => React.ReactElement;
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' | 'resetItem' | 'recoverItem';
export declare type WebComponentSingleMethodNames = 'update' | 'remove' | 'isCreation';
export declare type WebComponentSingleMethodNames = 'update' | 'remove' | 'create';
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

@ -1066,7 +1066,7 @@ class SingleNode<ED extends EntityDict & BaseEntityDict,
Cxt extends AsyncContext<ED>,
FrontCxt extends SyncContext<ED>,
AD extends CommonAspectDict<ED, Cxt>> extends Node<ED, T, Cxt, FrontCxt, AD> {
private id: string;
private id?: string;
private children: {
[K: string]: SingleNode<ED, keyof ED, Cxt, FrontCxt, AD> | ListNode<ED, keyof ED, Cxt, FrontCxt, AD>;
};
@ -1153,8 +1153,7 @@ class SingleNode<ED extends EntityDict & BaseEntityDict,
}
unsetId() {
this.create({});
this.id = this.operation!.operation.data.id;
this.id = undefined;
this.publish();
}
@ -1190,12 +1189,6 @@ class SingleNode<ED extends EntityDict & BaseEntityDict,
}
}
isCreation() {
const operations = this.composeOperations();
// 如果是create一定在第一个
return !!(operations && operations[0]?.operation.action === 'create');
}
async doBeforeTrigger(): Promise<void> {
if (this.operation?.beforeExecute) {
@ -1221,6 +1214,7 @@ class SingleNode<ED extends EntityDict & BaseEntityDict,
create(data: Partial<Omit<ED[T]['CreateSingle']['data'], 'id'>>, beforeExecute?: () => Promise<void>, afterExecute?: () => Promise<void>) {
const id = generateNewId();
assert(!this.id && !this.dirty, 'create前要保证singleNode为空');
this.operation = {
operation: {
id: generateNewId(),
@ -1230,6 +1224,7 @@ class SingleNode<ED extends EntityDict & BaseEntityDict,
beforeExecute,
afterExecute,
};
this.id = id;
this.setDirty();
}
@ -1307,10 +1302,16 @@ class SingleNode<ED extends EntityDict & BaseEntityDict,
if (childOperations) {
if (child instanceof SingleNode) {
assert(childOperations.length === 1);
assert(!operation.data[ele2]); // 多对一的子结点不应该有多项
Object.assign(operation.data, {
[ele2]: childOperations[0].operation,
});
if (!operation.data[ele2]) {
Object.assign(operation.data, {
[ele2]: childOperations[0].operation,
});
}
else {
// 目前应该只允许一种情况就是父create子update
assert(operation.data[ele].action === 'create' && childOperations[0].operation.action === 'update');
Object.assign(operation.data[ele].data, childOperations[0].operation.data);
}
}
else {
assert(child instanceof ListNode);
@ -1383,7 +1384,7 @@ class SingleNode<ED extends EntityDict & BaseEntityDict,
}
// 如果是新建也不用refresh
if (this.isCreation()) {
if (this.operation?.operation.action === 'create') {
return;
}
@ -2199,12 +2200,6 @@ 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

@ -585,13 +585,6 @@ const oakBehavior = Behavior<
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}`
@ -604,6 +597,19 @@ const oakBehavior = Behavior<
afterExecute
);
},
create(data, beforeExecute, afterExecute, path) {
const path2 = path
? `${this.state.oakFullpath}.${path}`
: this.state.oakFullpath;
return this.features.runningTree.create(
path2,
data,
beforeExecute,
afterExecute
);
},
remove(beforeExecute, afterExecute, path) {
const path2 = path
? `${this.state.oakFullpath}.${path}`

View File

@ -78,7 +78,7 @@ abstract class OakComponentBase<
name: string,
detail?: DetailType,
options?: WechatMiniprogram.Component.TriggerEventOption
) {}
) { }
sub(type: string, callback: Function) {
this.features.eventBus.sub(type, callback);
@ -301,13 +301,6 @@ 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'],
@ -327,6 +320,23 @@ abstract class OakComponentBase<
);
}
create<T extends keyof ED>(
data: Omit<ED[T]['CreateSingle']['data'], 'id'>,
beforeExecute?: () => Promise<void>,
afterExecute?: () => Promise<void>,
path?: string
) {
const path2 = path
? `${this.state.oakFullpath}.${path}`
: this.state.oakFullpath;
this.features.runningTree.create(
path2,
data,
beforeExecute,
afterExecute
);
}
remove(
beforeExecute?: () => Promise<void>,
afterExecute?: () => Promise<void>,
@ -682,7 +692,7 @@ export function createComponent<
> & {
getRender: () => React.ComponentType<any>;
};
if (option.observers) {
console.error('observers即将废弃已经没有效果请使用listeners重写');
}
@ -815,12 +825,15 @@ export function createComponent<
update: (data: ED[T]['Update']['data'], action: ED[T]['Action'], beforeExecute?: () => Promise<void>, afterExecute?: () => Promise<void>, path?: string) => {
return this.update(data, action, beforeExecute, afterExecute, path);
},
create: (data: Omit<ED[T]['CreateSingle']['data'], 'id'>,
beforeExecute?: () => Promise<void>,
afterExecute?: () => Promise<void>,
path?: string) => {
return this.create(data, beforeExecute, afterExecute, path);
},
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

@ -381,10 +381,9 @@ export type OakSingleComponentMethods<ED extends EntityDict & BaseEntityDict, T
setId: (id: string) => void;
unsetId: () => void;
getId: () => string | undefined;
// create: (data: Omit<ED[T]['CreateSingle']['data'], 'id'>, beforeExecute?: () => Promise<void>, afterExecute?: () => Promise<void>) => void;
create: (data: Omit<ED[T]['CreateSingle']['data'], 'id'>, beforeExecute?: () => Promise<void>, afterExecute?: () => Promise<void>, path?: string) => 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> = {
@ -489,7 +488,7 @@ export type WebComponentListMethodNames = 'loadMore' | 'setFilters' | 'addNamedF
| 'addNamedSorter' | 'removeNamedSorter' | 'removeNamedSorterByName' | 'setPageSize' | 'setCurrentPage' | 'addItem' | 'removeItem' | 'updateItem' | 'resetItem' | 'recoverItem';
// 暴露给single组件的方法
export type WebComponentSingleMethodNames = 'update' | 'remove' | 'isCreation';
export type WebComponentSingleMethodNames = 'update' | 'remove' | 'create';
export type WebComponentProps<
ED extends EntityDict & BaseEntityDict,