debugStore装载初始化数据
This commit is contained in:
parent
4a6028a867
commit
adb8f4de1f
|
|
@ -3,7 +3,6 @@
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"description": "oak框架中前端与平台无关的逻辑部分",
|
"description": "oak框架中前端与平台无关的逻辑部分",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@reduxjs/toolkit": "^1.7.2",
|
|
||||||
"lodash": "^4.17.21",
|
"lodash": "^4.17.21",
|
||||||
"uuid": "^8.3.2"
|
"uuid": "^8.3.2"
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,56 @@
|
||||||
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
|
import { assign } from 'lodash';
|
||||||
import { DebugStore } from 'oak-debug-store';
|
import { DebugStore, Context } from 'oak-debug-store';
|
||||||
import { EntityDef, Selection, SelectionResult } from "oak-domain/lib/types/Entity";
|
import { FormCreateData, Selection, EntityDict } from "oak-domain/lib/types/Entity";
|
||||||
|
import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-domain/EntityDict';
|
||||||
import { StorageSchema } from 'oak-domain/lib/types/Storage';
|
import { StorageSchema } from 'oak-domain/lib/types/Storage';
|
||||||
|
import { Trigger } from 'oak-domain/lib/types/Trigger';
|
||||||
import { TriggerExecutor } from 'oak-trigger-executor';
|
import { TriggerExecutor } from 'oak-trigger-executor';
|
||||||
|
import { data as generalData, triggers as generalTriggers } from 'oak-general-business';
|
||||||
|
import { FrontContext } from '../FrontContext';
|
||||||
|
|
||||||
|
async function initDataInStore<ED extends EntityDict & BaseEntityDict>(store: DebugStore<ED>, initialData?: {
|
||||||
|
[T in keyof ED]?: Array<FormCreateData<ED[T]['OpSchema']>>;
|
||||||
|
}) {
|
||||||
|
if (false) {
|
||||||
|
// todo 在不同环境下读取相应的store数据并初始化
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
const context = new Context(store);
|
||||||
|
await context.begin();
|
||||||
|
if (initialData) {
|
||||||
|
for (const entity in initialData) {
|
||||||
|
await store.operate(entity, {
|
||||||
|
action: 'create',
|
||||||
|
data: initialData[entity]!,
|
||||||
|
}, context);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (const entity in (generalData as typeof initialData)) {
|
||||||
|
await store.operate(entity, {
|
||||||
|
action: 'create',
|
||||||
|
data: (generalData as typeof initialData)![entity]!,
|
||||||
|
}, context);
|
||||||
|
}
|
||||||
|
await context.commit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
export function createDebugStore<ED extends {
|
export function createDebugStore<ED extends EntityDict & BaseEntityDict>(storageSchema: StorageSchema<ED>, triggers?: Array<Trigger<ED, keyof ED>>, initialData?: {
|
||||||
[E: string]: EntityDef;
|
[T in keyof ED]?: Array<FormCreateData<ED[T]['OpSchema']>>;
|
||||||
}>(storageSchema: StorageSchema<ED>, initialData?: {
|
|
||||||
[T in keyof ED]?: {
|
|
||||||
[ID: string]: ED[T]['OpSchema'];
|
|
||||||
};
|
|
||||||
}){
|
}){
|
||||||
const executor = new TriggerExecutor<ED>();
|
const executor = new TriggerExecutor<ED>();
|
||||||
const store = new DebugStore<ED>(executor, storageSchema, initialData);
|
const store = new DebugStore<ED>(executor, storageSchema);
|
||||||
|
|
||||||
|
generalTriggers.forEach(
|
||||||
|
ele => store.registerTrigger(ele)
|
||||||
|
);
|
||||||
|
triggers?.forEach(
|
||||||
|
ele => store.registerTrigger(ele)
|
||||||
|
);
|
||||||
|
|
||||||
|
// 如果有物化存储的数据使用此数据,否则使用initialData初始化debugStore
|
||||||
|
initDataInStore(store, initialData);
|
||||||
return store;
|
return store;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -170,7 +170,7 @@ class ListNode<ED extends EntityDict, AD extends Record<string, Aspect<ED>>, T e
|
||||||
const { step } = pagination;
|
const { step } = pagination;
|
||||||
const { ids } = await cache.refresh(entity, {
|
const { ids } = await cache.refresh(entity, {
|
||||||
data: projection as any,
|
data: projection as any,
|
||||||
filter: combineFilters(filters),
|
filter: filters.length > 0 ? combineFilters(filters) : undefined,
|
||||||
sorter,
|
sorter,
|
||||||
indexFrom: 0,
|
indexFrom: 0,
|
||||||
count: step,
|
count: step,
|
||||||
|
|
@ -250,12 +250,14 @@ class SingleNode<ED extends EntityDict, AD extends Record<string, Aspect<ED>>, T
|
||||||
|
|
||||||
async refresh(cache: Cache<ED, AD>) {
|
async refresh(cache: Cache<ED, AD>) {
|
||||||
assert(this.projection);
|
assert(this.projection);
|
||||||
await cache.refresh(this.entity, {
|
if (this.action !== 'create') {
|
||||||
data: this.projection,
|
await cache.refresh(this.entity, {
|
||||||
filter: {
|
data: this.projection,
|
||||||
id: this.id,
|
filter: {
|
||||||
},
|
id: this.id,
|
||||||
} as any);
|
},
|
||||||
|
} as any);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
composeAction(): DeduceOperation<ED[T]['Schema']> | undefined {
|
composeAction(): DeduceOperation<ED[T]['Schema']> | undefined {
|
||||||
|
|
@ -416,8 +418,8 @@ export class RunningNode<ED extends EntityDict, AD extends Record<string, Aspect
|
||||||
this.root = {};
|
this.root = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
async createNode<T extends keyof ED, L extends boolean>(path: string, parent?: string,
|
async createNode<T extends keyof ED>(path: string, parent?: string,
|
||||||
entity?: T, isList?: L, projection?: ED[T]['Selection']['data'],id?: string,
|
entity?: T, isList?: boolean, isPicker?: boolean, projection?: ED[T]['Selection']['data'],id?: string,
|
||||||
pagination?: Pagination, filters?: DeduceFilter<ED[T]['Schema']>[],
|
pagination?: Pagination, filters?: DeduceFilter<ED[T]['Schema']>[],
|
||||||
sorter?: ED[T]['Selection']['sorter']) {
|
sorter?: ED[T]['Selection']['sorter']) {
|
||||||
let node: ListNode<ED, AD, T> | SingleNode<ED, AD, T>;
|
let node: ListNode<ED, AD, T> | SingleNode<ED, AD, T>;
|
||||||
|
|
@ -428,7 +430,7 @@ export class RunningNode<ED extends EntityDict, AD extends Record<string, Aspect
|
||||||
const isList2 = subEntity ? subEntity.isList : isList!;
|
const isList2 = subEntity ? subEntity.isList : isList!;
|
||||||
|
|
||||||
const context = this.getContext();
|
const context = this.getContext();
|
||||||
if (isList2) {
|
if (isPicker || isList2) {
|
||||||
node = new ListNode<ED, AD, T>(entity2 as T, fullPath, this.schema!, projection, parentNode, pagination, filters, sorter);
|
node = new ListNode<ED, AD, T>(entity2 as T, fullPath, this.schema!, projection, parentNode, pagination, filters, sorter);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
@ -616,6 +618,7 @@ export class RunningNode<ED extends EntityDict, AD extends Record<string, Aspect
|
||||||
while (iter < paths.length) {
|
while (iter < paths.length) {
|
||||||
const childPath = paths[iter];
|
const childPath = paths[iter];
|
||||||
node = (await node.getChild(childPath))!;
|
node = (await node.getChild(childPath))!;
|
||||||
|
iter ++;
|
||||||
}
|
}
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
14
src/index.ts
14
src/index.ts
|
|
@ -5,16 +5,16 @@ import { aspectDict as basicAspectDict } from 'oak-general-business';
|
||||||
|
|
||||||
import { Aspect } from 'oak-domain/lib/types/Aspect';
|
import { Aspect } from 'oak-domain/lib/types/Aspect';
|
||||||
import { Feature, subscribe } from './types/Feature';
|
import { Feature, subscribe } from './types/Feature';
|
||||||
|
import { createDebugStore } from './dataStore/debugStore';
|
||||||
|
|
||||||
import { initialize as createBasicFeatures, BasicFeatures } from './features/index';
|
import { initialize as createBasicFeatures, BasicFeatures } from './features/index';
|
||||||
import { assign, intersection, keys, mapValues, pull } from 'lodash';
|
import { assign, intersection, keys, mapValues, pull } from 'lodash';
|
||||||
import { EntityDict } from 'oak-domain/lib/types/Entity';
|
import { EntityDict, FormCreateData } from 'oak-domain/lib/types/Entity';
|
||||||
import { FrontContext } from './FrontContext';
|
import { FrontContext } from './FrontContext';
|
||||||
import { RunningContext } from "oak-domain/lib/types/Context";
|
import { RunningContext } from "oak-domain/lib/types/Context";
|
||||||
import { DebugStore, Context } from 'oak-debug-store';
|
import { DebugStore, Context } from 'oak-debug-store';
|
||||||
import { Schema as Application } from "oak-domain/lib/base-domain/Application/Schema";
|
import { Schema as Application } from "oak-domain/lib/base-domain/Application/Schema";
|
||||||
import { Schema as Token } from 'oak-domain/lib/base-domain/Token/Schema';
|
import { Schema as Token } from 'oak-domain/lib/base-domain/Token/Schema';
|
||||||
import { TriggerExecutor } from "oak-trigger-executor";
|
|
||||||
import { AspectProxy } from './types/AspectProxy';
|
import { AspectProxy } from './types/AspectProxy';
|
||||||
import { CacheStore } from './dataStore/CacheStore';
|
import { CacheStore } from './dataStore/CacheStore';
|
||||||
|
|
||||||
|
|
@ -39,19 +39,15 @@ function createAspectProxy<ED extends BaseEntityDict & EntityDict,
|
||||||
features: BasicFeatures<ED, AD> & FD,
|
features: BasicFeatures<ED, AD> & FD,
|
||||||
aspectDict?: AD,
|
aspectDict?: AD,
|
||||||
initialData?: {
|
initialData?: {
|
||||||
[T in keyof ED]?: Array<ED[T]['OpSchema']>;
|
[T in keyof ED]?: Array<FormCreateData<ED[T]['OpSchema']>>;
|
||||||
}): AspectProxy<ED, AD & typeof basicAspectDict> {
|
}): AspectProxy<ED, AD & typeof basicAspectDict> {
|
||||||
if (process.env.NODE_ENV === 'production') {
|
if (process.env.NODE_ENV === 'production') {
|
||||||
// todo 发请求到后台获取数据
|
// todo 发请求到后台获取数据
|
||||||
throw new Error('method not implemented');
|
throw new Error('method not implemented');
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// todo initialData
|
// todo initialData
|
||||||
const executor = new TriggerExecutor<ED>();
|
const debugStore = createDebugStore(storageSchema, triggers, initialData);
|
||||||
const debugStore = new DebugStore<ED>(executor, storageSchema);
|
|
||||||
triggers.forEach(
|
|
||||||
(trigger) => debugStore.registerTrigger(trigger)
|
|
||||||
);
|
|
||||||
|
|
||||||
const connectAspectToDebugStore = (aspect: Aspect<ED>): (p: Parameters<typeof aspect>[0]) => ReturnType<typeof aspect> => {
|
const connectAspectToDebugStore = (aspect: Aspect<ED>): (p: Parameters<typeof aspect>[0]) => ReturnType<typeof aspect> => {
|
||||||
return async (params: Parameters<typeof aspect>[0]) => {
|
return async (params: Parameters<typeof aspect>[0]) => {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue