From 684f51a4fd6ea568c9f8c59fa1f7f8c692b8852e Mon Sep 17 00:00:00 2001 From: Xc Date: Thu, 2 Jun 2022 13:53:01 +0800 Subject: [PATCH] build --- lib/aspects/crud.d.ts | 2 +- lib/cacheStore/CacheStore.d.ts | 2 +- lib/debugStore/debugStore.d.ts | 4 +-- lib/debugStore/index.d.ts | 4 +-- lib/debugStore/index.js | 57 ++++++++++++++++++++++++++++++- lib/features/cache.d.ts | 2 +- lib/features/node.js | 10 +++--- lib/index.js | 6 +--- lib/initialize.d.ts | 4 +-- lib/initialize.js | 14 +++----- lib/platforms/wechatMp/index.d.ts | 4 +-- lib/platforms/wechatMp/index.js | 4 +-- src/debugStore/index.ts | 2 +- src/features/node.ts | 10 +++--- 14 files changed, 84 insertions(+), 41 deletions(-) diff --git a/lib/aspects/crud.d.ts b/lib/aspects/crud.d.ts index 3ddb82c2..439e7672 100644 --- a/lib/aspects/crud.d.ts +++ b/lib/aspects/crud.d.ts @@ -3,7 +3,7 @@ export declare function operate[]>; +}, context: Cxt): Promise | Promise>[]>; export declare function select>(options: { entity: T; selection: ED[T]['Selection']; diff --git a/lib/cacheStore/CacheStore.d.ts b/lib/cacheStore/CacheStore.d.ts index 54638cd8..e5e89507 100644 --- a/lib/cacheStore/CacheStore.d.ts +++ b/lib/cacheStore/CacheStore.d.ts @@ -9,7 +9,7 @@ export declare class CacheStore> [ID: string]: ED[T]['OpSchema']; }; }); - operate(entity: T, operation: ED[T]['Operation'], context: Cxt, params?: Object): Promise; + operate(entity: T, operation: ED[T]['Operation'], context: Cxt, params?: Object): Promise>; select(entity: T, selection: S, context: Cxt, params?: Object): Promise>; registerChecker(checker: Checker): void; } diff --git a/lib/debugStore/debugStore.d.ts b/lib/debugStore/debugStore.d.ts index 65b764ea..5507c57d 100644 --- a/lib/debugStore/debugStore.d.ts +++ b/lib/debugStore/debugStore.d.ts @@ -17,9 +17,9 @@ export declare class DebugStore> remove: number; commit: number; }); - protected cascadeUpdate(entity: T, operation: DeduceCreateOperation | DeduceUpdateOperation | DeduceRemoveOperation, context: Cxt, params?: OperateParams): Promise; + protected cascadeUpdate(entity: T, operation: DeduceCreateOperation | DeduceUpdateOperation | DeduceRemoveOperation, context: Cxt, params?: OperateParams): Promise>; protected cascadeSelect(entity: T, selection: ED[T]["Selection"], context: Cxt, params?: OperateParams): Promise; - operate(entity: T, operation: ED[T]['Operation'], context: Cxt, params?: DebugStoreOperationParams): Promise; + operate(entity: T, operation: ED[T]['Operation'], context: Cxt, params?: DebugStoreOperationParams): Promise>; select(entity: T, selection: S, context: Cxt, params?: DebugStoreOperationParams): Promise>; registerTrigger(trigger: Trigger): void; registerChecker(checker: Checker): void; diff --git a/lib/debugStore/index.d.ts b/lib/debugStore/index.d.ts index cb7dd89d..0a9c0f19 100644 --- a/lib/debugStore/index.d.ts +++ b/lib/debugStore/index.d.ts @@ -1,5 +1,5 @@ import { DebugStore } from './debugStore'; -import { Checker, Trigger, StorageSchema, FormCreateData, Context, EntityDict, RowStore, ActionDictOfEntityDict } from "oak-domain/lib/types"; -export declare function createDebugStore>(storageSchema: StorageSchema, createContext: (store: RowStore, scene: string) => Cxt, triggers?: Array>, checkers?: Array>, initialData?: { +import { Checker, Trigger, StorageSchema, FormCreateData, Context, EntityDict, RowStore, ActionDictOfEntityDict, Watcher } from "oak-domain/lib/types"; +export declare function createDebugStore>(storageSchema: StorageSchema, createContext: (store: RowStore, scene: string) => Cxt, triggers: Array>, checkers: Array>, watchers: Array>, initialData?: { [T in keyof ED]?: Array>; }, actionDict?: ActionDictOfEntityDict): DebugStore; diff --git a/lib/debugStore/index.js b/lib/debugStore/index.js index 0e7577ba..beb79714 100644 --- a/lib/debugStore/index.js +++ b/lib/debugStore/index.js @@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true }); exports.createDebugStore = void 0; const debugStore_1 = require("./debugStore"); const actionDef_1 = require("oak-domain/lib/store/actionDef"); +const watchers_1 = require("oak-domain/lib/store/watchers"); async function initDataInStore(store, createContext, initialData) { store.startInitializing(); if (false) { @@ -62,7 +63,59 @@ function materializeData(data, stat) { } } } -function createDebugStore(storageSchema, createContext, triggers, checkers, initialData, actionDict) { +/** + * 在debug环境上创建watcher + * @param store + * @param watchers + */ +function initializeWatchers(store, createContext, watchers) { + const schema = store.getSchema(); + const intrinsicWatchers = (0, watchers_1.makeIntrinsicWatchers)(schema); + const totalWatchers = watchers.concat(intrinsicWatchers); + let count = 0; + async function doWatchers() { + count++; + const start = Date.now(); + const context = createContext(store, 'doWatchers'); + for (const w of totalWatchers) { + await context.begin(); + try { + if (w.hasOwnProperty('actionData')) { + const { entity, action, filter, actionData } = w; + const filter2 = typeof filter === 'function' ? await filter() : filter; + const data = typeof actionData === 'function' ? await actionData() : actionData; // 这里有个奇怪的编译错误,不理解 by Xc + const result = await store.operate(entity, { + action, + data, + filter: filter2 + }, context); + console.log(`执行了watcher【${w.name}】,结果是:`, result); + } + else { + const { entity, projection, fn, filter } = w; + const filter2 = typeof filter === 'function' ? await filter() : filter; + const projection2 = typeof projection === 'function' ? await projection() : projection; + const { result: rows } = await store.select(entity, { + data: projection2, + filter: filter2, + }, context); + const result = fn(context, rows); + console.log(`执行了watcher【${w.name}】,结果是:`, result); + } + await context.commit(); + } + catch (err) { + await context.rollback(); + console.error(`执行了watcher【${w.name}】,发生错误:`, err); + } + } + const duration = Date.now() - start; + console.log(`第${count}次执行watchers,共执行${totalWatchers.length}个,耗时${duration}毫秒`); + setTimeout(() => doWatchers(), 120000); + } + doWatchers(); +} +function createDebugStore(storageSchema, createContext, triggers, checkers, watchers, initialData, actionDict) { const data = getMaterializedData(); const store = new debugStore_1.DebugStore(storageSchema, createContext, data && data.data, data && data.stat); triggers?.forEach(ele => store.registerTrigger(ele)); @@ -90,6 +143,8 @@ function createDebugStore(storageSchema, createContext, triggers, checkers, init const data = store.getCurrentData(); materializeData(data, stat); }, 10000); + // 启动watcher + initializeWatchers(store, createContext, watchers); return store; } exports.createDebugStore = createDebugStore; diff --git a/lib/features/cache.d.ts b/lib/features/cache.d.ts index a0c5361c..2857dabf 100644 --- a/lib/features/cache.d.ts +++ b/lib/features/cache.d.ts @@ -21,7 +21,7 @@ export declare class Cache, AD ex * @param params * @returns */ - operate(entity: T, operation: ED[T]['Operation'], scene: string, params?: OperateParams): Promise; + operate(entity: T, operation: ED[T]['Operation'], scene: string, params?: OperateParams): Promise>; get(entity: T, selection: ED[T]['Selection'], scene: string, params?: object): Promise[]>; judgeRelation(entity: keyof ED, attr: string): string | 0 | 2 | 1 | string[]; bindOnSync(callback: (opRecords: OpRecord[]) => Promise): void; diff --git a/lib/features/node.js b/lib/features/node.js index 0ac28b64..8843991c 100644 --- a/lib/features/node.js +++ b/lib/features/node.js @@ -404,14 +404,13 @@ class ListNode extends Node { if (e === this.entity) { const { id } = d; const filter = (0, filter_1.combineFilters)([{ id }, ...(this.filters).map(ele => ele.filter)]); - const { ids } = await this.cache.operate(e, { + const rows = await this.cache.get(e, { data: { id: 1, }, filter, - action: 'select', }, 'onRecordSynchoronized', { obscure: true }); - if (ids.length > 0) { + if (rows.length > 0) { // todo 这里更严格应该还要考虑sorter,但前端可能没有完整的供sort用的cache数据 this.ids.push(id); needReGetValue = true; @@ -439,14 +438,13 @@ class ListNode extends Node { } else { const filter = (0, filter_1.combineFilters)([{ id }, ...(this.filters.map(ele => ele.filter))]); - const { ids } = await this.cache.operate(e, { + const rows = await this.cache.get(e, { data: { id: 1, }, filter, - action: 'select', }, 'onRecordSynchoronized', { obscure: true }); - if (ids.length > 0) { + if (rows.length > 0) { // todo 这里更严格应该还要考虑sorter,但前端可能没有完整的供sort用的cache数据 this.ids.push(id); needReGetValue = true; diff --git a/lib/index.js b/lib/index.js index 9a13b29d..af70a8f3 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,11 +1,7 @@ "use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; - var desc = Object.getOwnPropertyDescriptor(m, k); - if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { - desc = { enumerable: true, get: function() { return m[k]; } }; - } - Object.defineProperty(o, k2, desc); + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; diff --git a/lib/initialize.d.ts b/lib/initialize.d.ts index 3a5849fc..bf054195 100644 --- a/lib/initialize.d.ts +++ b/lib/initialize.d.ts @@ -1,9 +1,9 @@ -import { Aspect, Checker, Trigger, StorageSchema, Context, RowStore } from "oak-domain/lib/types"; +import { Aspect, Checker, Trigger, StorageSchema, Context, RowStore, Watcher } from "oak-domain/lib/types"; import { EntityDict } from 'oak-domain/lib/types/Entity'; import { Feature, subscribe } from './types/Feature'; import { BasicFeatures } from './features'; import { ActionDictOfEntityDict } from "oak-domain/lib/types/Action"; -export declare function initialize, AD extends Record>, FD extends Record>>(storageSchema: StorageSchema, createFeatures: (basicFeatures: BasicFeatures) => FD, createContext: (store: RowStore, scene: string) => Cxt, triggers?: Array>, checkers?: Array>, aspectDict?: AD, initialData?: { +export declare function initialize, AD extends Record>, FD extends Record>>(storageSchema: StorageSchema, createFeatures: (basicFeatures: BasicFeatures) => FD, createContext: (store: RowStore, scene: string) => Cxt, triggers?: Array>, checkers?: Array>, watchers?: Array>, aspectDict?: AD, initialData?: { [T in keyof ED]?: Array; }, actionDict?: ActionDictOfEntityDict): { subscribe: typeof subscribe; diff --git a/lib/initialize.js b/lib/initialize.js index fd3f1005..a37fc72b 100644 --- a/lib/initialize.js +++ b/lib/initialize.js @@ -1,11 +1,7 @@ "use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; - var desc = Object.getOwnPropertyDescriptor(m, k); - if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { - desc = { enumerable: true, get: function() { return m[k]; } }; - } - Object.defineProperty(o, k2, desc); + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; @@ -25,14 +21,14 @@ const features_1 = require("./features"); const lodash_1 = require("lodash"); const aspects_1 = __importDefault(require("./aspects")); const actionDef_1 = require("oak-domain/lib/store/actionDef"); -function createAspectProxy(storageSchema, createContext, triggers, checkers, features, aspectDict, initialData, actionDict) { +function createAspectProxy(storageSchema, createContext, triggers, checkers, watchers, features, aspectDict, initialData, actionDict) { if (process.env.NODE_ENV === 'production') { // todo 发请求到后台获取数据 throw new Error('method not implemented'); } else { // todo initialData - const debugStore = (0, debugStore_1.createDebugStore)(storageSchema, createContext, triggers, checkers, initialData, actionDict); + const debugStore = (0, debugStore_1.createDebugStore)(storageSchema, createContext, triggers, checkers, watchers, initialData, actionDict); const connectAspectToDebugStore = (aspect) => { return async (params, scene) => { const runningContext = createContext(debugStore, scene); @@ -61,7 +57,7 @@ function createAspectProxy(storageSchema, createContext, triggers, checkers, fea return aspectProxy; } } -function initialize(storageSchema, createFeatures, createContext, triggers, checkers, aspectDict, initialData, actionDict) { +function initialize(storageSchema, createFeatures, createContext, triggers, checkers, watchers, aspectDict, initialData, actionDict) { const basicFeatures = (0, features_1.initialize)(storageSchema, createContext, checkers); if (actionDict) { const { checkers: adCheckers } = (0, actionDef_1.analyzeActionDefDict)(storageSchema, actionDict); @@ -76,7 +72,7 @@ function initialize(storageSchema, createFeatures, createContext, triggers, chec } const features = (0, lodash_1.assign)(basicFeatures, userDefinedfeatures); // todo default triggers - const aspectProxy = createAspectProxy(storageSchema, createContext, triggers || [], checkers || [], features, aspectDict, initialData, actionDict); + const aspectProxy = createAspectProxy(storageSchema, createContext, triggers || [], checkers || [], watchers || [], features, aspectDict, initialData, actionDict); (0, lodash_1.keys)(features).forEach(ele => { features[ele].setAspectProxy(aspectProxy); }); diff --git a/lib/platforms/wechatMp/index.d.ts b/lib/platforms/wechatMp/index.d.ts index 1038b25f..0db26f44 100644 --- a/lib/platforms/wechatMp/index.d.ts +++ b/lib/platforms/wechatMp/index.d.ts @@ -1,6 +1,6 @@ /// import './polyfill'; -import { Aspect, Checker, Context, EntityDict, RowStore, StorageSchema, Trigger, ActionDictOfEntityDict, DeduceSorterItem, DeduceOperation, SelectRowShape } from "oak-domain/lib/types"; +import { Aspect, Checker, Context, EntityDict, RowStore, StorageSchema, Trigger, ActionDictOfEntityDict, DeduceSorterItem, DeduceOperation, SelectRowShape, Watcher } from "oak-domain/lib/types"; import { Feature } from '../../types/Feature'; import { Pagination } from "../../types/Pagination"; import { BasicFeatures } from "../../features"; @@ -133,7 +133,7 @@ declare type OakPageData = { oakLegalActions: string[]; }; declare type OakComponentData = {} & OakPageData; -export declare function initialize, AD extends Record>, FD extends Record>>(storageSchema: StorageSchema, createFeatures: (basicFeatures: BasicFeatures) => FD, createContext: (store: RowStore, scene: string) => Cxt, exceptionRouters?: ExceptionRouters, triggers?: Array>, checkers?: Array>, aspectDict?: AD, initialData?: { +export declare function initialize, AD extends Record>, FD extends Record>>(storageSchema: StorageSchema, createFeatures: (basicFeatures: BasicFeatures) => FD, createContext: (store: RowStore, scene: string) => Cxt, exceptionRouters?: ExceptionRouters, triggers?: Array>, checkers?: Array>, watchers?: Array>, aspectDict?: AD, initialData?: { [T in keyof ED]?: Array; }, actionDict?: ActionDictOfEntityDict): { OakPage: (options: OakPageOption, componentOptions?: WechatMiniprogram.Component.Options, true>) => string; diff --git a/lib/platforms/wechatMp/index.js b/lib/platforms/wechatMp/index.js index 0548fc9c..5de8586a 100644 --- a/lib/platforms/wechatMp/index.js +++ b/lib/platforms/wechatMp/index.js @@ -609,8 +609,8 @@ function mergeMethods(methods) { } return merged; } -function initialize(storageSchema, createFeatures, createContext, exceptionRouters = [], triggers, checkers, aspectDict, initialData, actionDict) { - const { subscribe, features } = (0, initialize_1.initialize)(storageSchema, createFeatures, createContext, triggers, checkers, aspectDict, initialData, actionDict); +function initialize(storageSchema, createFeatures, createContext, exceptionRouters = [], triggers, checkers, watchers, aspectDict, initialData, actionDict) { + const { subscribe, features } = (0, initialize_1.initialize)(storageSchema, createFeatures, createContext, triggers, checkers, watchers, aspectDict, initialData, actionDict); const exceptionRouterDict = {}; for (const router of exceptionRouters) { (0, lodash_1.assign)(exceptionRouterDict, { diff --git a/src/debugStore/index.ts b/src/debugStore/index.ts index d082b904..87eabdb5 100644 --- a/src/debugStore/index.ts +++ b/src/debugStore/index.ts @@ -124,7 +124,7 @@ function initializeWatchers>( const duration = Date.now() - start; console.log(`第${count}次执行watchers,共执行${totalWatchers.length}个,耗时${duration}毫秒`); - setTimeout(() => doWatchers(), 2000); + setTimeout(() => doWatchers(), 120000); } doWatchers(); diff --git a/src/features/node.ts b/src/features/node.ts index bdbadf30..1a8b253e 100644 --- a/src/features/node.ts +++ b/src/features/node.ts @@ -465,14 +465,13 @@ class ListNode ele.filter)]); - const { ids } = await this.cache.operate(e, { + const rows = await this.cache.get(e, { data: { id: 1, } as any, filter, - action: 'select', }, 'onRecordSynchoronized', { obscure: true }); - if (ids!.length > 0) { + if (rows.length > 0) { // todo 这里更严格应该还要考虑sorter,但前端可能没有完整的供sort用的cache数据 this.ids.push(id); needReGetValue = true; @@ -500,14 +499,13 @@ class ListNode ele.filter))]); - const { ids } = await this.cache.operate(e, { + const rows = await this.cache.get(e, { data: { id: 1, } as any, filter, - action: 'select', }, 'onRecordSynchoronized', { obscure: true }); - if (ids!.length > 0) { + if (rows!.length > 0) { // todo 这里更严格应该还要考虑sorter,但前端可能没有完整的供sort用的cache数据 this.ids.push(id); needReGetValue = true;