Skip to content

Commit

Permalink
refactor: refactor plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
taixw2 committed Oct 14, 2020
1 parent 5e9e703 commit c86cf57
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 69 deletions.
45 changes: 45 additions & 0 deletions packages/core/src/dx/create-plugin.ts
Original file line number Diff line number Diff line change
@@ -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);
});
}
4 changes: 2 additions & 2 deletions packages/core/src/dx/create-store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T>(options: CreateOption<T> = {}): Store<{}, Action> => {
return (options: CreateOption = {}): Store<{}, Action> => {
if (store.reduxStore) return store.reduxStore;

// 收集 plugins
Expand Down
12 changes: 4 additions & 8 deletions packages/core/src/dx/exports/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T = any> {
export interface CreateOption {
models?: DxModelContstructor[] | { [key: string]: DxModelContstructor };
injects?: T[];
middlewares?: Middleware[];
sagaMiddlewares?: EffectMiddleware[];
plugins?: DxPlugin[];
Expand All @@ -22,10 +20,8 @@ export interface CreateOption<T = any> {
}

export function createFactory() {
return <T>(options?: CreateOption<T>): React.FunctionComponent => {
return (options?: CreateOption): React.FunctionComponent => {
const store = createStoreFactory()(options);
return <T extends { store?: unknown }>({ children }: React.PropsWithChildren<T>): React.ReactElement => {
return React.createElement(Provider, { store }, children);
};
return ({ children }: React.PropsWithChildren<{}>) => React.createElement(Provider, { store }, children);
};
}
3 changes: 2 additions & 1 deletion packages/core/src/dx/exports/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,7 +22,6 @@ export function modelsFactory() {
'请传入有效的参数,当前参数类型为 %s, 但只接受 string、undefined、regexp 的类型',
typeof match,
);

require('invariant')(store.reduxStore, 'store 还没有创建,请先调用 Dx.createStore 或 Dx.create 创建 store');
}

Expand All @@ -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;
Expand Down
25 changes: 0 additions & 25 deletions packages/core/src/dx/plugins/context.ts

This file was deleted.

31 changes: 0 additions & 31 deletions packages/core/src/dx/plugins/create-plugin.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/core/src/helper/store.ts
Original file line number Diff line number Diff line change
@@ -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<DxModelContstructor>;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/utils/is.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ interface IsGeneratorExtend extends Function {
isGenerator(): boolean;
}

export function isClassPlugin(fn: unknown): boolean {
export function isClass<T>(fn: unknown): fn is T {
return typeof fn === 'function' && !!fn.toString().match(/^class/);
}

Expand Down

0 comments on commit c86cf57

Please sign in to comment.