diff --git a/lib/features/runningTree.d.ts b/lib/features/runningTree.d.ts index 26725a4f..62daad3b 100644 --- a/lib/features/runningTree.d.ts +++ b/lib/features/runningTree.d.ts @@ -125,6 +125,7 @@ declare class SingleNode | ListNode): void; removeChild(path: string): void; getFreshValue(noCascade?: boolean): Partial | undefined; + isCreation(): boolean; doBeforeTrigger(): Promise; doAfterTrigger(): Promise; create(data: Partial>, beforeExecute?: () => Promise, afterExecute?: () => Promise): void; @@ -230,6 +231,7 @@ export declare class RunningTree(path: string, action?: ED[T]['Action']): Promise<{ entity: keyof ED; operation: ED[keyof ED]["Operation"]; diff --git a/lib/features/runningTree.js b/lib/features/runningTree.js index 52484fe5..1b71f481 100644 --- a/lib/features/runningTree.js +++ b/lib/features/runningTree.js @@ -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; diff --git a/lib/page.mp.js b/lib/page.mp.js index 18340d71..49896d93 100644 --- a/lib/page.mp.js +++ b/lib/page.mp.js @@ -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); diff --git a/lib/page.web.js b/lib/page.web.js index 83ae945e..4bfd45e7 100644 --- a/lib/page.web.js +++ b/lib/page.web.js @@ -154,6 +154,12 @@ var OakComponentBase = /** @class */ (function (_super) { /* create(data: Omit, beforeExecute?: () => Promise, afterExecute?: () => Promise) { 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) { diff --git a/lib/types/Page.d.ts b/lib/types/Page.d.ts index c3a65890..a72feb06 100644 --- a/lib/types/Page.d.ts +++ b/lib/types/Page.d.ts @@ -177,6 +177,7 @@ export declare type OakSingleComponentMethods string | undefined; update: (data: ED[T]['Update']['data'], action?: ED[T]['Action'], beforeExecute?: () => Promise, afterExecute?: () => Promise, path?: string) => void; remove: (beforeExecute?: () => Promise, afterExecute?: () => Promise, path?: string) => void; + isCreation: (path?: string) => boolean; }; export declare type OakListComponentMethods = { loadMore: () => Promise; @@ -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 WebComponentSingleMethodNames = 'update' | 'remove'; +export declare type WebComponentSingleMethodNames = 'update' | 'remove' | 'isCreation'; export declare type WebComponentProps = { methods: TMethod & OakCommonComponentMethods & OakListComponentMethods & OakSingleComponentMethods; data: TData & OakComponentData & (IsList extends true ? OakListComoponetData : {}); diff --git a/src/features/runningTree.ts b/src/features/runningTree.ts index 0965a7e8..ffa148e7 100644 --- a/src/features/runningTree.ts +++ b/src/features/runningTree.ts @@ -1267,13 +1267,10 @@ class SingleNode { 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(path: string, action?: ED[T]['Action']) { const node = this.findNode(path)!; if (action) { diff --git a/src/page.mp.ts b/src/page.mp.ts index 436d60d6..bf076d3a 100644 --- a/src/page.mp.ts +++ b/src/page.mp.ts @@ -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 & - OakListComponentMethods & - OakSingleComponentMethods & { - iAmThePage: () => boolean; - setState: ( - data: Record, - callback?: () => void - ) => void; - onLoad: (query: Record) => Promise; - onPullDownRefresh: () => Promise; - onReachBottom: () => Promise; - }, + OakListComponentMethods & + OakSingleComponentMethods & { + iAmThePage: () => boolean; + setState: ( + data: Record, + callback?: () => void + ) => void; + onLoad: (query: Record) => Promise; + onPullDownRefresh: () => Promise; + onReachBottom: () => Promise; + }, { state: Record; props: { @@ -81,7 +81,7 @@ const oakBehavior = Behavior< FrontCxt, ADD & CommonAspectDict > & - 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 > & - 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() { diff --git a/src/page.web.tsx b/src/page.web.tsx index 60582050..65a801b6 100644 --- a/src/page.web.tsx +++ b/src/page.web.tsx @@ -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( data: ED[T]['Update']['data'], action?: ED[T]['Action'], @@ -782,6 +789,9 @@ export function createComponent< remove: (beforeExecute?: () => Promise, afterExecute?: () => Promise, path?: string) => { return this.remove(beforeExecute, afterExecute, path); }, + isCreation: (path?: string)=> { + return this.isCreation(path); + } } as Record); if (methods) { diff --git a/src/types/Page.ts b/src/types/Page.ts index d2e47dec..b7eea1b7 100644 --- a/src/types/Page.ts +++ b/src/types/Page.ts @@ -333,6 +333,7 @@ export type OakSingleComponentMethods, beforeExecute?: () => Promise, afterExecute?: () => Promise) => void; update: (data: ED[T]['Update']['data'], action?: ED[T]['Action'], beforeExecute?: () => Promise, afterExecute?: () => Promise, path?: string) => void; remove: (beforeExecute?: () => Promise, afterExecute?: () => Promise, path?: string) => void; + isCreation: (path?: string) => boolean; } export type OakListComponentMethods = { @@ -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,