将crud的aspect移到了base里
This commit is contained in:
parent
37694a4d08
commit
7f3fee2222
|
|
@ -0,0 +1,24 @@
|
|||
import { OperateParams, EntityDict, Context } from 'oak-domain/lib/types';
|
||||
|
||||
export async function operate<ED extends EntityDict, T extends keyof ED>(
|
||||
options: { entity: T, operation: ED[T]['Operation'] | ED[T]['Operation'][], params?: OperateParams }, context: Context<ED>) {
|
||||
const { entity, operation, params } = options;
|
||||
if (operation instanceof Array) {
|
||||
return operation.map(
|
||||
(oper) => context.rowStore.operate(entity, oper, context, params)
|
||||
);
|
||||
}
|
||||
return context.rowStore.operate(entity, operation, context, params);
|
||||
}
|
||||
|
||||
export async function select<ED extends EntityDict, T extends keyof ED>(
|
||||
options: { entity: T, selection: ED[T]['Selection'], params?: object }, context: Context<ED>) {
|
||||
const { entity, selection, params } = options;
|
||||
return context.rowStore.select(entity, selection, context, params);
|
||||
}
|
||||
|
||||
/*
|
||||
export type AspectDict<ED extends EntityDict> = {
|
||||
operation: <T extends keyof ED>(options: { entity: T, operation: ED[T]['Operation'], params?: OperateParams }, context: Context<ED>) => Promise<OperationResult>;
|
||||
select: <T extends keyof ED, S extends ED[T]['Selection']>( options: { entity: T, selection: S, params?: object }, context: Context<ED>) => Promise<SelectionResult2<ED[T]['Schema'], S>>;
|
||||
}; */
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
import { operate, select } from './crud';
|
||||
const aspectDict = {
|
||||
operate,
|
||||
select,
|
||||
};
|
||||
|
||||
export default aspectDict;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
import { StorageSchema, DeduceSelection, EntityDict, OperateParams, OpRecord, Aspect, Checker, RowStore, Context } from 'oak-domain/lib/types';
|
||||
import { StorageSchema, DeduceSelection, EntityDict, OperateParams, OpRecord, Aspect, Checker, RowStore, Context, OperationResult } from 'oak-domain/lib/types';
|
||||
import { Action, Feature } from '../types/Feature';
|
||||
import { assign } from 'lodash';
|
||||
import { CacheStore } from '../cacheStore/CacheStore';
|
||||
|
|
@ -26,7 +26,7 @@ export class Cache<ED extends EntityDict, Cxt extends Context<ED>, AD extends Re
|
|||
entity: entity as any,
|
||||
operation: assign({}, selection, { action: 'select' }) as DeduceSelection<ED[T]['Schema']>,
|
||||
params,
|
||||
});
|
||||
}) as OperationResult;
|
||||
}
|
||||
|
||||
@Action
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ export class Node<ED extends EntityDict, T extends keyof ED> {
|
|||
protected projection?: ED[T]['Selection']['data']; // 只在Page层有
|
||||
protected parent?: Node<ED, keyof ED>;
|
||||
protected action?: ED[T]['Action'];
|
||||
protected dirty?: boolean;
|
||||
protected dirty: boolean;
|
||||
protected updateData?: DeduceUpdateOperation<ED[T]['OpSchema']>['data'];
|
||||
|
||||
constructor(entity: T, fullPath: string, schema: StorageSchema<ED>, projection?: ED[T]['Selection']['data'],
|
||||
|
|
@ -29,6 +29,7 @@ export class Node<ED extends EntityDict, T extends keyof ED> {
|
|||
this.projection = projection;
|
||||
this.parent = parent;
|
||||
this.action = action;
|
||||
this.dirty = false;
|
||||
}
|
||||
|
||||
getSubEntity(path: string): {
|
||||
|
|
@ -226,6 +227,16 @@ class ListNode<ED extends EntityDict, T extends keyof ED> extends Node<ED, T>{
|
|||
this.value = value;
|
||||
this.updateChildrenValue();
|
||||
}
|
||||
|
||||
resetUpdateData() {
|
||||
this.updateData = undefined;
|
||||
// this.action = undefined;
|
||||
this.dirty = false;
|
||||
|
||||
this.children.forEach(
|
||||
(ele) => ele.resetUpdateData()
|
||||
);
|
||||
}
|
||||
|
||||
async nextPage() {
|
||||
|
||||
|
|
@ -407,6 +418,16 @@ class SingleNode<ED extends EntityDict, T extends keyof ED> extends Node<ED, T>{
|
|||
this.value = value;
|
||||
this.updateChildrenValues();
|
||||
}
|
||||
|
||||
resetUpdateData() {
|
||||
this.updateData = undefined;
|
||||
// this.action = undefined;
|
||||
this.dirty = false;
|
||||
|
||||
for (const attr in this.children) {
|
||||
this.children[attr]?.resetUpdateData();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -749,8 +770,13 @@ export class RunningNode<ED extends EntityDict, Cxt extends Context<ED>, AD exte
|
|||
return;
|
||||
}
|
||||
|
||||
console.log('exeucte to aspectProxy');
|
||||
// this.getAspectProxy().operate
|
||||
await this.getAspectProxy().operate({
|
||||
entity: node.getEntity() as string,
|
||||
operation,
|
||||
});
|
||||
|
||||
// 清空缓存
|
||||
node.resetUpdateData();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import { createDebugStore } from './debugStore';
|
|||
import { initialize as createBasicFeatures, BasicFeatures } from './features';
|
||||
import { assign, intersection, keys, mapValues } from 'lodash';
|
||||
import { AspectProxy } from './types/AspectProxy';
|
||||
import baseAspectDict from './aspects';
|
||||
|
||||
function createAspectProxy<ED extends EntityDict, Cxt extends Context<ED>,
|
||||
AD extends Record<string, Aspect<ED, Cxt>>,
|
||||
|
|
@ -19,7 +20,7 @@ function createAspectProxy<ED extends EntityDict, Cxt extends Context<ED>,
|
|||
aspectDict?: AD,
|
||||
initialData?: {
|
||||
[T in keyof ED]?: Array<FormCreateData<ED[T]['OpSchema']>>;
|
||||
}): AspectProxy<ED, Cxt, AD> {
|
||||
}): AspectProxy<ED, Cxt, AD & typeof baseAspectDict> {
|
||||
if (process.env.NODE_ENV === 'production') {
|
||||
// todo 发请求到后台获取数据
|
||||
throw new Error('method not implemented');
|
||||
|
|
@ -50,7 +51,7 @@ function createAspectProxy<ED extends EntityDict, Cxt extends Context<ED>,
|
|||
}
|
||||
};
|
||||
|
||||
const aspectProxy = mapValues(aspectDict, ele => connectAspectToDebugStore(ele));
|
||||
const aspectProxy = mapValues(assign({}, baseAspectDict, aspectDict), ele => connectAspectToDebugStore(ele));
|
||||
|
||||
return aspectProxy as any;
|
||||
}
|
||||
|
|
@ -83,6 +84,7 @@ export function initialize<ED extends EntityDict, Cxt extends Context<ED>, AD ex
|
|||
const aspectProxy = createAspectProxy<ED, Cxt, AD, FD>(storageSchema, createContext, triggers || [], checkers || [],
|
||||
features, aspectDict, initialData);
|
||||
|
||||
|
||||
keys(features).forEach(
|
||||
ele => {
|
||||
features[ele].setAspectProxy(aspectProxy);
|
||||
|
|
|
|||
|
|
@ -159,6 +159,7 @@ function createPageOptions<ED extends EntityDict,
|
|||
oakFullpath: string;
|
||||
oakExecuting: boolean;
|
||||
oakFocused: object;
|
||||
oakDirty: boolean;
|
||||
},
|
||||
OakPageProperties,
|
||||
OakPageMethods<ED, T>,
|
||||
|
|
@ -179,9 +180,13 @@ function createPageOptions<ED extends EntityDict,
|
|||
async reRender() {
|
||||
const $rows = await features.runningNode.get(this.data.oakFullpath);
|
||||
const data = formData($rows as any, features);
|
||||
if (data) {
|
||||
this.setData(data);
|
||||
for (const k in data) {
|
||||
if (data[k] === undefined) {
|
||||
data[k] = null;
|
||||
}
|
||||
}
|
||||
const dirty = await features.runningNode.isDirty(this.data.oakFullpath);
|
||||
this.setData(assign({}, data, { oakDirty: dirty }));
|
||||
},
|
||||
|
||||
async refresh() {
|
||||
|
|
@ -364,6 +369,7 @@ function createComponentOptions<ED extends EntityDict,
|
|||
entity: keyof EntityDict;
|
||||
oakExecuting: boolean;
|
||||
oakFocused: object;
|
||||
oakDirty: boolean;
|
||||
},
|
||||
OakComponentProperties,
|
||||
OakComponentMethods<ED, T>,
|
||||
|
|
@ -376,10 +382,18 @@ function createComponentOptions<ED extends EntityDict,
|
|||
oakParent: String,
|
||||
},
|
||||
observers: {
|
||||
"oakValue": function (value: Partial<ED[T]['Schema']> | Partial<ED[T]['Schema']>[]) {
|
||||
"oakValue": async function (value: Partial<ED[T]['Schema']> | Partial<ED[T]['Schema']>[]) {
|
||||
const $rows = value instanceof Array ? value : [value];
|
||||
const data = formData($rows, features);
|
||||
this.setData(data);
|
||||
for (const k in data) {
|
||||
if (data[k] === undefined) {
|
||||
data[k] = null;
|
||||
}
|
||||
}
|
||||
const dirty = await features.runningNode.isDirty(this.data.oakFullpath);
|
||||
this.setData(assign({}, data, {
|
||||
oakDirty: dirty,
|
||||
}));
|
||||
},
|
||||
"oakParent": async function (oakParent) {
|
||||
if (oakParent) {
|
||||
|
|
@ -455,6 +469,11 @@ function createComponentOptions<ED extends EntityDict,
|
|||
const { oakPath, oakParent, oakValue } = this.data;
|
||||
const $rows = oakValue instanceof Array ? oakValue : [oakValue];
|
||||
const data = formData($rows as Partial<ED[T]['Schema']>[], features);
|
||||
for (const k in data) {
|
||||
if (data[k] === undefined) {
|
||||
data[k] = null;
|
||||
}
|
||||
}
|
||||
if (oakParent) {
|
||||
// 小程序component ready的时候,父组件还未构造完成
|
||||
const oakFullpath = `${oakParent}.${oakPath}`;
|
||||
|
|
|
|||
|
|
@ -1,16 +1,17 @@
|
|||
import { pull } from 'lodash';
|
||||
import { EntityDict, Aspect, Context } from 'oak-domain/lib/types';
|
||||
import { AspectProxy } from './AspectProxy';
|
||||
import baseAspectDict from '../aspects';
|
||||
|
||||
|
||||
export abstract class Feature<ED extends EntityDict, Cxt extends Context<ED>, AD extends Record<string, Aspect<ED, Cxt>>> {
|
||||
private aspectProxy?: AspectProxy<ED, Cxt, AD>;
|
||||
private aspectProxy?: AspectProxy<ED, Cxt, AD & typeof baseAspectDict>;
|
||||
|
||||
protected getAspectProxy() {
|
||||
return this.aspectProxy!;
|
||||
}
|
||||
|
||||
setAspectProxy(aspectProxy: AspectProxy<ED, Cxt, AD>) {
|
||||
setAspectProxy(aspectProxy: AspectProxy<ED, Cxt, AD & typeof baseAspectDict>) {
|
||||
this.aspectProxy = aspectProxy;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue