From 1bc53f24f7d0a239ca1f207680924d66d8b82d16 Mon Sep 17 00:00:00 2001 From: Xc Date: Mon, 21 Mar 2022 20:59:50 +0800 Subject: [PATCH] build --- lib/dataStore/context.d.ts | 7 ++- lib/dataStore/debugStore.d.ts | 11 +++-- lib/dataStore/debugStore.js | 6 ++- lib/features/sentry.d.ts | 23 ++++++++-- lib/features/sentry.js | 48 +++++++++++++++----- lib/features/token.d.ts | 17 ++++--- lib/features/token.js | 32 ++++++-------- lib/index.d.ts | 40 +++++------------ lib/index.js | 83 ++++++++++++++++++++--------------- src/FrontContext.ts | 12 ++--- src/features/data.ts | 3 +- src/features/sentry.ts | 60 ------------------------- src/features/token.ts | 3 +- src/index.ts | 8 ++-- src/types/Feature.ts | 2 +- 15 files changed, 169 insertions(+), 186 deletions(-) delete mode 100644 src/features/sentry.ts diff --git a/lib/dataStore/context.d.ts b/lib/dataStore/context.d.ts index be13ad0d..043a6afc 100644 --- a/lib/dataStore/context.d.ts +++ b/lib/dataStore/context.d.ts @@ -1,7 +1,6 @@ import { EntityDef } from "oak-domain/lib/types/Entity"; -import { TriggerEntityShape } from "oak-domain/lib/types/Trigger"; import { Context as BaseContext } from 'oak-debug-store'; -export declare class Context; -}, SH extends TriggerEntityShape = TriggerEntityShape> extends BaseContext { +export declare class Context extends BaseContext { } diff --git a/lib/dataStore/debugStore.d.ts b/lib/dataStore/debugStore.d.ts index 73cef366..b33bde51 100644 --- a/lib/dataStore/debugStore.d.ts +++ b/lib/dataStore/debugStore.d.ts @@ -1,12 +1,11 @@ import { DebugStore } from 'oak-debug-store'; import { EntityDef } from "oak-domain/lib/types/Entity"; import { StorageSchema } from 'oak-domain/lib/types/Storage'; -import { TriggerEntityShape } from "oak-domain/lib/types/Trigger"; -export declare function createDebugStore; -}, SH extends TriggerEntityShape = TriggerEntityShape>(storageSchema: StorageSchema, initialData?: { - [T in E]?: { +export declare function createDebugStore(storageSchema: StorageSchema, initialData?: { + [T in keyof ED]?: { [ID: string]: ED[T]['OpSchema']; }; -}): DebugStore; +}): DebugStore; export * from './context'; diff --git a/lib/dataStore/debugStore.js b/lib/dataStore/debugStore.js index 519ff93b..4b49e535 100644 --- a/lib/dataStore/debugStore.js +++ b/lib/dataStore/debugStore.js @@ -1,7 +1,11 @@ "use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; - Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[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); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; diff --git a/lib/features/sentry.d.ts b/lib/features/sentry.d.ts index 4a28fb5f..8781b592 100644 --- a/lib/features/sentry.d.ts +++ b/lib/features/sentry.d.ts @@ -1,4 +1,19 @@ -declare const dataSlice: import("@reduxjs/toolkit").Slice void; -}, "sentry">; -export default dataSlice; +import { Slice, ThunkAction } from '@reduxjs/toolkit'; +import { EntityDef, OperationResult, SelectionResult } from 'oak-domain/lib/types/Entity'; +import { StorageSchema } from 'oak-domain/lib/types/Storage'; +import { Trigger } from 'oak-domain/lib/types/Trigger'; +import { Context } from '../dataStore/debugStore'; +export declare function initialize(storageSchema: StorageSchema, triggers?: Array>): { + slice: Slice void; + }, "sentry">; + actions: { + operate(entity: T, operation: ED[T]["Operation"], context: Context, params?: Object | undefined): ThunkAction, any, any, { + payload: undefined; + type: string; + }>; + }; + selectData(entity: T_1, selection: ED[T_1]["Selection"], context: Context, params?: Object | undefined): Promise>; +}; diff --git a/lib/features/sentry.js b/lib/features/sentry.js index 3c74437a..65ce4cc7 100644 --- a/lib/features/sentry.js +++ b/lib/features/sentry.js @@ -1,17 +1,43 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); +exports.initialize = void 0; const toolkit_1 = require("@reduxjs/toolkit"); +const debugStore_1 = require("../dataStore/debugStore"); // Define the initial state using that type const initialState = 0; -const dataSlice = (0, toolkit_1.createSlice)({ - name: 'sentry', - // `createSlice` will infer the state type from the `initialState` argument - initialState, - reducers: { - // Use the PayloadAction type to declare the contents of `action.payload` - refreshSentry: (state) => { - state = state + 1; - }, +function initialize(storageSchema, triggers) { + const dataStore = (0, debugStore_1.createDebugStore)(storageSchema); + if (triggers) { + for (const trigger of triggers) { + dataStore.registerTrigger(trigger); + } } -}); -exports.default = dataSlice; + const slice = (0, toolkit_1.createSlice)({ + name: 'sentry', + // `createSlice` will infer the state type from the `initialState` argument + initialState, + reducers: { + // Use the PayloadAction type to declare the contents of `action.payload` + refreshSentry: (state) => { + state = state + 1; + }, + } + }); + const actions = { + operate(entity, operation, context, params) { + return async (dispatch) => { + const result = await dataStore.operate(entity, operation, context, params); + dispatch(slice.actions.refreshSentry()); + return result; + }; + }, + }; + return { + slice, + actions, + selectData(entity, selection, context, params) { + return dataStore.select(entity, selection, context, params); + }, + }; +} +exports.initialize = initialize; diff --git a/lib/features/token.d.ts b/lib/features/token.d.ts index b9587271..da69882a 100644 --- a/lib/features/token.d.ts +++ b/lib/features/token.d.ts @@ -1,6 +1,11 @@ -import { PayloadAction } from '@reduxjs/toolkit'; -export declare const tokenSlice: import("@reduxjs/toolkit").Slice) => void; - unsetToken: (state: string) => void; -}, "token">; -export default tokenSlice; +import { EntityDict } from 'oak-domain/lib/types/Entity'; +import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-domain/EntityDict'; +import { Aspect } from 'oak-domain/lib/types/Aspect'; +import { Feature } from '../types/Feature'; +import { FrontContext } from '../FrontContext'; +export declare class Token>> extends Feature { + tokenValue?: string; + get(context: FrontContext): Promise; + action(context: FrontContext, type: string, payload?: any): Promise; + getTokenValue(): string | undefined; +} diff --git a/lib/features/token.js b/lib/features/token.js index 7719c1b1..978ac1ee 100644 --- a/lib/features/token.js +++ b/lib/features/token.js @@ -1,21 +1,17 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -exports.tokenSlice = void 0; -const toolkit_1 = require("@reduxjs/toolkit"); -// Define the initial state using that type -const initialState = ''; -exports.tokenSlice = (0, toolkit_1.createSlice)({ - name: 'token', - // `createSlice` will infer the state type from the `initialState` argument - initialState, - reducers: { - // Use the PayloadAction type to declare the contents of `action.payload` - setToken: (state, action) => { - state = action.payload; - }, - unsetToken: (state) => { - state = ''; - } +exports.Token = void 0; +const Feature_1 = require("../types/Feature"); +class Token extends Feature_1.Feature { + tokenValue; + async get(context) { + throw new Error('Method not implemented.'); } -}); -exports.default = exports.tokenSlice; + async action(context, type, payload) { + throw new Error('Method not implemented.'); + } + getTokenValue() { + return this.tokenValue; + } +} +exports.Token = Token; diff --git a/lib/index.d.ts b/lib/index.d.ts index 48c0f1ba..7597e67b 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -1,29 +1,13 @@ -import { EntityDef } from "oak-domain/lib/types/Entity"; import { StorageSchema } from 'oak-domain/lib/types/Storage'; -import { Trigger, TriggerEntityShape } from "oak-domain/lib/types/Trigger"; -import { Context as DebugContext } from './dataStore/debugStore'; -declare type RootState = ReturnType<(ReturnType)['store']['getState']>; -export declare const getToken: (state: RootState) => string; -export declare const getSentry: (state: RootState) => number; -export declare function initialize; -}, SH extends TriggerEntityShape = TriggerEntityShape>(storageSchema: StorageSchema, triggers?: Array>, initialState?: Object, isDebug?: boolean): { - store: import("@reduxjs/toolkit").EnhancedStore<{ - t: string; - s: number; - }, import("redux").AnyAction, [import("redux-thunk").ThunkMiddleware<{ - t: string; - s: number; - }, import("redux").AnyAction, null> | import("redux-thunk").ThunkMiddleware<{ - t: string; - s: number; - }, import("redux").AnyAction, undefined>]>; - actions: { - operate: (entity: T, operation: ED[T]["Operation"], context: DebugContext, params?: Object | undefined) => Promise; - select: (entity: T_1, selection: ED[T_1]["Selection"], context: DebugContext, params?: Object | undefined) => Promise>; - count: (entity: T_3, selection: ED[T_3]["Selection"], context: DebugContext, params?: Object | undefined) => any; - setToken: import("@reduxjs/toolkit").ActionCreatorWithPayload; - unsetToken: import("@reduxjs/toolkit").ActionCreatorWithoutPayload; - }; -}; -export {}; +import { Trigger } from "oak-domain/lib/types/Trigger"; +import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-domain/EntityDict'; +import { Aspect } from 'oak-domain/lib/types/Aspect'; +import { Feature } from './types/Feature'; +import { EntityDict } from 'oak-domain/lib/types/Entity'; +export declare function initialize>, FD extends Record>>() => Feature>>(storageSchema: StorageSchema, applicationId: string, featureClazzDict?: FD, triggers?: Array>, aspectDict?: AD, initialData?: { + [T in keyof ED]?: Array; +}): Promise<{ + subscribe: (features: F[], callback: () => void) => () => void; + getFeature: (f: F_1, params?: Parameters["get"]>[1] | undefined) => ReturnType["get"]>; + actionFeature: (f: F_2, t: Parameters["action"]>[1], p?: Parameters["action"]>[2] | undefined) => ReturnType["action"]>; +}>; diff --git a/lib/index.js b/lib/index.js index 3ed5ff88..58cf2136 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,42 +1,55 @@ "use strict"; -var __importDefault = (this && this.__importDefault) || function (mod) { - return (mod && mod.__esModule) ? mod : { "default": mod }; -}; Object.defineProperty(exports, "__esModule", { value: true }); -exports.initialize = exports.getSentry = exports.getToken = void 0; -const toolkit_1 = require("@reduxjs/toolkit"); -const token_1 = __importDefault(require("./features/token")); -const sentry_1 = __importDefault(require("./features/sentry")); -const debugStore_1 = require("./dataStore/debugStore"); -const reducer = { - t: token_1.default.reducer, - s: sentry_1.default.reducer, -}; -const getToken = (state) => state.t; -exports.getToken = getToken; -const getSentry = (state) => state.s; -exports.getSentry = getSentry; -function initialize(storageSchema, triggers = [], initialState = {}, isDebug = true) { - const store = (0, toolkit_1.configureStore)({ - reducer, - preloadedState: initialState, - }); - const dataStore = (0, debugStore_1.createDebugStore)(storageSchema); - for (const trigger of triggers) { - dataStore.registerTrigger(trigger); +exports.initialize = void 0; +const token_1 = require("./features/token"); +const lodash_1 = require("lodash"); +const FrontContext_1 = require("./FrontContext"); +function populateFeatures(featureClazzDict) { + const result = {}; + for (const k in featureClazzDict) { + (0, lodash_1.assign)(result, { + [k]: new featureClazzDict[k](), + }); } + return result; +} +async function initialize(storageSchema, applicationId, featureClazzDict, triggers, aspectDict, initialData) { + const token = new token_1.Token(); + // todo default triggers + const frontContext = new FrontContext_1.FrontContext(storageSchema, triggers, applicationId, () => token.getTokenValue(), aspectDict, initialData); + const featureDict = { + token: token_1.Token, + }; + function ppf() { + return populateFeatures(featureClazzDict); + } + if (featureClazzDict) { + (0, lodash_1.assign)(featureDict, ppf()); + } + const featureDict2 = featureDict; + const subscribe = (features, callback) => { + const unsubscribes = features.map((f) => { + const feature = featureDict2[f]; + return feature.subscribe(callback); + }); + return () => { + unsubscribes.forEach(ele => ele()); + }; + }; + const getFeature = (f, params) => { + // const context = new FrontContext(store, aspectProxy) as any; + const feature = featureDict2[f]; + return feature.get(frontContext, params); // 这里有个类型的转换暂时写不出来,因为populateFeatures没法传递generic types在返回值里 + }; + const actionFeature = (f, t, p) => { + // const context = new FrontContext(store, aspectProxy) as any; + const feature = featureDict2[f]; + return feature.action(frontContext, t, p); + }; return { - store, - actions: { - ...token_1.default.actions, - operate: (entity, operation, context, params) => { - const result = dataStore.operate(entity, operation, context, params); - sentry_1.default.actions.refreshSentry(); - return result; - }, - select: (entity, selection, context, params) => dataStore.select(entity, selection, context, params), - count: (entity, selection, context, params) => dataStore.count(entity, selection, context, params) - }, + subscribe, + getFeature, + actionFeature, }; } exports.initialize = initialize; diff --git a/src/FrontContext.ts b/src/FrontContext.ts index 6dd602ee..de1e021b 100644 --- a/src/FrontContext.ts +++ b/src/FrontContext.ts @@ -26,7 +26,7 @@ class DebugRunningContext extends Context implements } }; -export async function createAspectProxy>>( +export async function createAspectProxy>>( cacheStore: CacheStore, storageSchema: StorageSchema, triggers: Array>, @@ -49,7 +49,7 @@ export async function createAspectProxy>debugStore).select('application', { + const { result: [application] } = await debugStore.select('application', { data: { id: 1, systemId: 1, @@ -60,7 +60,7 @@ export async function createAspectProxy>context); + }, context); const getApplication = () => application as Application; const FullAspectProxy = assign(BaseAspectProxy, aspectDict); @@ -71,7 +71,7 @@ export async function createAspectProxy>debugStore).select('token', { + const { result } = await debugStore.select('token', { data: { id: 1, userId: 1, @@ -80,7 +80,7 @@ export async function createAspectProxy>context2); + }, context2); token = result[0] as Token; // todo 判断 token的合法性 } @@ -91,7 +91,7 @@ export async function createAspectProxy>> extends BaseContext { +export class FrontContext>> extends BaseContext { getAspectProxy: () => Promise>; constructor( diff --git a/src/features/data.ts b/src/features/data.ts index e94dc277..43fa2047 100644 --- a/src/features/data.ts +++ b/src/features/data.ts @@ -1,9 +1,10 @@ import { EntityDict } from 'oak-domain/lib/types/Entity'; +import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-domain/EntityDict'; import { Aspect } from 'oak-domain/lib/types/Aspect'; import { Feature } from '../types/Feature'; import { FrontContext } from '../FrontContext'; -export class Data>> extends Feature { +export class Data>> extends Feature { async get(context: FrontContext, params: { entity: T, selection: ED[T]['Selection'] }) { const { result } = await context.rowStore.select(params.entity, params.selection, context); return result; diff --git a/src/features/sentry.ts b/src/features/sentry.ts deleted file mode 100644 index 82666f3c..00000000 --- a/src/features/sentry.ts +++ /dev/null @@ -1,60 +0,0 @@ -import { createSlice, PayloadAction, Slice, ThunkAction } from '@reduxjs/toolkit'; -import { EntityDef, OperationResult, SelectionResult } from 'oak-domain/lib/types/Entity'; -import { StorageSchema } from 'oak-domain/lib/types/Storage'; -import { Trigger } from 'oak-domain/lib/types/Trigger'; -import { Context, createDebugStore } from '../dataStore/debugStore'; - -// Define the initial state using that type -const initialState: number = 0; - -export function initialize(storageSchema: StorageSchema, triggers?: Array>) { - const dataStore = createDebugStore(storageSchema); - if (triggers) { - for (const trigger of triggers) { - dataStore.registerTrigger(trigger); - } - } - - const slice = createSlice({ - name: 'sentry', - // `createSlice` will infer the state type from the `initialState` argument - initialState, - reducers: { - // Use the PayloadAction type to declare the contents of `action.payload` - refreshSentry: (state) => { - state = state + 1; - }, - } - }); - - const actions = { - operate( - entity: T, - operation: ED[T]['Operation'], - context: Context, - params?: Object - ): ThunkAction>, any, any, ReturnType> { - return async (dispatch) => { - const result = await dataStore.operate(entity, operation, context, params); - dispatch(slice.actions.refreshSentry()); - return result; - }; - }, - }; - - return { - slice, - actions, - - selectData( - entity: T, - selection: ED[T]['Selection'], - context: Context, - params?: Object - ) { - return dataStore.select(entity, selection, context, params); - }, - } -} diff --git a/src/features/token.ts b/src/features/token.ts index 6a92a6e4..944b9902 100644 --- a/src/features/token.ts +++ b/src/features/token.ts @@ -1,9 +1,10 @@ import { EntityDict } from 'oak-domain/lib/types/Entity'; +import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-domain/EntityDict'; import { Aspect } from 'oak-domain/lib/types/Aspect'; import { Feature } from '../types/Feature'; import { FrontContext } from '../FrontContext'; -export class Token>> extends Feature { +export class Token>> extends Feature { tokenValue?: string; async get(context: FrontContext) { throw new Error('Method not implemented.'); diff --git a/src/index.ts b/src/index.ts index 57393655..625aa633 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ import { StorageSchema } from 'oak-domain/lib/types/Storage'; import { Trigger } from "oak-domain/lib/types/Trigger"; -import { EntityDict as BaseEntityDict } from 'oak-domain/src/base-domain/EntityDict'; +import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-domain/EntityDict'; import { Aspect } from 'oak-domain/lib/types/Aspect'; import { Feature } from './types/Feature'; @@ -11,7 +11,7 @@ import { EntityDict } from 'oak-domain/lib/types/Entity'; import { FrontContext } from './FrontContext'; -function populateFeatures>, FD extends Record>>() => Feature>>(featureClazzDict: FD) +function populateFeatures>, FD extends Record>>() => Feature>>(featureClazzDict: FD) : { [T in keyof FD]: InstanceType; } { @@ -25,9 +25,9 @@ function populateFeatures>, - FD extends Record>>() => Feature>>( + FD extends Record>>() => Feature>>( storageSchema: StorageSchema, applicationId: string, featureClazzDict?: FD, diff --git a/src/types/Feature.ts b/src/types/Feature.ts index 85dd813c..6f7a36cf 100644 --- a/src/types/Feature.ts +++ b/src/types/Feature.ts @@ -4,7 +4,7 @@ import { EntityDict } from 'oak-domain/lib/types/entity'; import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-domain/EntityDict'; import { FrontContext } from '../FrontContext'; -export abstract class Feature>> { +export abstract class Feature>> { private callbackSet: Array<() => void>; constructor() { this.callbackSet = [];