diff --git a/packages/core/src/dx/create-plugin.ts b/packages/core/src/dx/create-plugin.ts new file mode 100644 index 0000000..1596a24 --- /dev/null +++ b/packages/core/src/dx/create-plugin.ts @@ -0,0 +1,45 @@ +/* + * @Author: 欧阳鑫 + * @Date: 2020-08-10 08:52:15 + * @Last Modified by: 欧阳鑫 + * @Last Modified time: 2020-10-14 16:59:38 + */ + +import { store } from '../helper/store'; +import { is } from '../utils'; + +export type Hook = + | 'beforeDispatch' + | 'afterDispatch' + | 'beforeEffect' + | 'effect' + | 'afterEffect' + | 'beforeReducer' + | 'reducer' + | 'afterReducer'; + +const PluginContext = { + hooks(hook: Hook, handler: unknown) { + const hooks = store.plugins.get(hook) ?? []; + hooks.push(handler); + store.plugins.set(hook, hooks); + }, +}; + +export type ClassPlugin = { + apply(ctx: typeof PluginContext): void; +}; + +export type DxPlugin = { new (): ClassPlugin } | ((ctx: typeof PluginContext) => void); + +// store plugin +export default function createPlugin(plugins?: DxPlugin[]) { + if (!Array.isArray(plugins)) return; + plugins.forEach(plugin => { + if (is.isClass<{ new (): ClassPlugin }>(plugin)) { + new plugin().apply(PluginContext); + return; + } + plugin(PluginContext); + }); +} diff --git a/packages/core/src/dx/create-store/index.ts b/packages/core/src/dx/create-store/index.ts index 14ce419..756856c 100644 --- a/packages/core/src/dx/create-store/index.ts +++ b/packages/core/src/dx/create-store/index.ts @@ -2,11 +2,11 @@ import { Action, Store } from 'redux'; import { store } from '../../helper/store'; import { storeModel } from './store-model'; import { combinStore } from './create-store'; -import storePlugins from '../plugins/create-plugin'; +import storePlugins from '../create-plugin'; import { CreateOption } from '../exports/create'; export function createStoreFactory() { - return (options: CreateOption = {}): Store<{}, Action> => { + return (options: CreateOption = {}): Store<{}, Action> => { if (store.reduxStore) return store.reduxStore; // 收集 plugins diff --git a/packages/core/src/dx/exports/create.ts b/packages/core/src/dx/exports/create.ts index 341ded5..7761846 100644 --- a/packages/core/src/dx/exports/create.ts +++ b/packages/core/src/dx/exports/create.ts @@ -4,12 +4,10 @@ import { createStoreFactory } from '../create-store'; import { Middleware } from 'redux'; import { EffectMiddleware } from 'redux-saga'; import { DxModelContstructor } from '../../dx-model/model'; -import { DxPlugin } from '../plugins/create-plugin'; +import { DxPlugin } from '../create-plugin'; -// eslint-disable-next-line @typescript-eslint/no-explicit-any -export interface CreateOption { +export interface CreateOption { models?: DxModelContstructor[] | { [key: string]: DxModelContstructor }; - injects?: T[]; middlewares?: Middleware[]; sagaMiddlewares?: EffectMiddleware[]; plugins?: DxPlugin[]; @@ -22,10 +20,8 @@ export interface CreateOption { } export function createFactory() { - return (options?: CreateOption): React.FunctionComponent => { + return (options?: CreateOption): React.FunctionComponent => { const store = createStoreFactory()(options); - return ({ children }: React.PropsWithChildren): React.ReactElement => { - return React.createElement(Provider, { store }, children); - }; + return ({ children }: React.PropsWithChildren<{}>) => React.createElement(Provider, { store }, children); }; } diff --git a/packages/core/src/dx/exports/models.ts b/packages/core/src/dx/exports/models.ts index 9afee99..a4d43ff 100644 --- a/packages/core/src/dx/exports/models.ts +++ b/packages/core/src/dx/exports/models.ts @@ -10,6 +10,7 @@ export function modelsFactory() { function getModels(): { [key: string]: DxModelContstructor }; function getModels(match: RegExp): DxModelContstructor[]; function getModels(match: string): DxModelContstructor; + function getModels(match?: string | RegExp): GetModels { const map = store.getModels()?.map || {}; // eslint-disable-next-line @typescript-eslint/no-non-null-assertion @@ -21,7 +22,6 @@ export function modelsFactory() { '请传入有效的参数,当前参数类型为 %s, 但只接受 string、undefined、regexp 的类型', typeof match, ); - require('invariant')(store.reduxStore, 'store 还没有创建,请先调用 Dx.createStore 或 Dx.create 创建 store'); } @@ -33,6 +33,7 @@ export function modelsFactory() { if (Reflect.has(map, match)) return Reflect.get(map, match); return [...set].find(model => getModelName(model).startsWith(match)); } + return [...set].filter(model => match.test(getModelName(model))); } return getModels; diff --git a/packages/core/src/dx/plugins/context.ts b/packages/core/src/dx/plugins/context.ts deleted file mode 100644 index 97daf63..0000000 --- a/packages/core/src/dx/plugins/context.ts +++ /dev/null @@ -1,25 +0,0 @@ -/* - * @Author: 欧阳鑫 - * @Date: 2020-08-10 08:52:56 - * @Last Modified by: 欧阳鑫 - * @Last Modified time: 2020-08-10 16:17:10 - */ - -import { store } from '../../helper/store'; - -export type Hook = - | 'beforeDispatch' - | 'afterDispatch' - | 'beforeEffect' - | 'effect' - | 'afterEffect' - | 'beforeReducer' - | 'reducer' - | 'afterReducer'; - -export default { - hooks(hook: Hook, callback: unknown): void { - const hooks = store.plugins.get(hook) || store.plugins.set(hook, []).get(hook); - hooks?.push(callback); - }, -}; diff --git a/packages/core/src/dx/plugins/create-plugin.ts b/packages/core/src/dx/plugins/create-plugin.ts deleted file mode 100644 index 2787477..0000000 --- a/packages/core/src/dx/plugins/create-plugin.ts +++ /dev/null @@ -1,31 +0,0 @@ -/* - * @Author: 欧阳鑫 - * @Date: 2020-08-10 08:52:15 - * @Last Modified by: 欧阳鑫 - * @Last Modified time: 2020-08-10 11:56:00 - */ - -import { is } from '../../utils'; -import context, { Hook } from './context'; - -interface Context { - hooks(hook: Hook, callback: unknown): void; -} - -interface ClassPlugin { - apply(ctx: Context): void; -} - -export type DxPlugin = { new (): ClassPlugin } | ((ctx: Context) => void); - -export default function storePlugins(plugins?: DxPlugin[]): void { - if (!plugins || !plugins.length) return; - - plugins.forEach(Plugin => { - if (is.isClassPlugin(Plugin)) { - return new Plugin().apply(context); - } - - Plugin(context); - }); -} diff --git a/packages/core/src/helper/store.ts b/packages/core/src/helper/store.ts index d124d47..aa691d4 100644 --- a/packages/core/src/helper/store.ts +++ b/packages/core/src/helper/store.ts @@ -1,7 +1,7 @@ import { Store } from 'redux'; import { SymbolType } from '@dxjs/shared/symbol'; import { DxModelContstructor } from '../dx-model/model'; -import { Hook } from '../dx/plugins/context'; +import { Hook } from '../dx/create-plugin'; interface ModelRefs { set: Set; diff --git a/packages/core/src/utils/is.ts b/packages/core/src/utils/is.ts index 6d773ff..7e38a6d 100644 --- a/packages/core/src/utils/is.ts +++ b/packages/core/src/utils/is.ts @@ -2,7 +2,7 @@ interface IsGeneratorExtend extends Function { isGenerator(): boolean; } -export function isClassPlugin(fn: unknown): boolean { +export function isClass(fn: unknown): fn is T { return typeof fn === 'function' && !!fn.toString().match(/^class/); }