From bef3e6ddd3a9439f02b9377714c0c35ef7a4b89f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=94=BF?= <43430109@qq.com> Date: Fri, 16 Oct 2020 14:28:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20typescript=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.d.ts | 123 +++++++++++++++++++++++++++++++++++++++++++++++++++++ index.js | 4 +- 2 files changed, 124 insertions(+), 3 deletions(-) create mode 100644 index.d.ts diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..f543624 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,123 @@ +import { + Reducer, + Action, + AnyAction, + ReducersMapObject, + MiddlewareAPI, + StoreEnhancer, + bindActionCreators, +} from "redux"; + +import { History } from "history"; + +export interface Dispatch { + (action: T): Promise | T; +} + +export interface onActionFunc { + (api: MiddlewareAPI): void; +} + +export interface ReducerEnhancer { + (reducer: Reducer): void; +} + +export interface Hooks { + onError?: (e: Error, dispatch: Dispatch) => void; + onAction?: onActionFunc | onActionFunc[]; + onStateChange?: () => void; + onReducer?: ReducerEnhancer; + onEffect?: () => void; + onHmr?: () => void; + extraReducers?: ReducersMapObject; + extraEnhancers?: StoreEnhancer[]; +} + +export type DvaOption = Hooks & { + namespacePrefixWarning?: boolean; + initialState?: Object; + history?: Object; +}; + +export interface EffectsCommandMap { + put: (action: A) => any; + call: Function; + select: Function; + take: Function; + cancel: Function; + [key: string]: any; +} + +export type Effect = (action: AnyAction, effects: EffectsCommandMap) => void; +export type EffectType = "takeEvery" | "takeLatest" | "watcher" | "throttle"; +export type EffectWithType = [Effect, { type: EffectType }]; +export type Subscription = (api: SubscriptionAPI, done: Function) => void; +export type ReducersMapObjectWithEnhancer = [ + ReducersMapObject, + ReducerEnhancer +]; + +export interface EffectsMapObject { + [key: string]: Effect | EffectWithType; +} + +export interface SubscriptionAPI { + history: History; + dispatch: Dispatch; +} + +export interface SubscriptionsMapObject { + [key: string]: Subscription; +} + +export interface Model { + namespace: string; + state?: any; + reducers?: ReducersMapObject | ReducersMapObjectWithEnhancer; + effects?: EffectsMapObject; + subscriptions?: SubscriptionsMapObject; +} + +export interface DvaInstance { + /** + * Register an object of hooks on the application. + * + * @param hooks + */ + use: (hooks: Hooks) => void; + + /** + * Register a model. + * + * @param model + */ + model: (model: Model) => void; + + /** + * Unregister a model. + * + * @param namespace + */ + unmodel: (namespace: string) => void; + + /** + * Start the application. Selector is optional. If no selector + * arguments, it will return a function that return JSX elements. + * + * @param selector + */ + start: (selector?: React.ReactNode, opts?: { forwardRef: boolean }) => any; +} + +export default function dva(opts?: DvaOption): DvaInstance; + +export { bindActionCreators }; + +export { + connect, + connectAdvanced, + useSelector, + useDispatch, + useStore, + shallowEqual, +} from "react-redux"; diff --git a/index.js b/index.js index 4388194..8ff05e0 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,7 @@ import React from 'react'; import { Provider, connect, connectAdvanced, useSelector, useDispatch, useStore, shallowEqual } from 'react-redux'; import { bindActionCreators } from 'redux'; -import { utils, create, saga } from 'dva-core'; +import { create, saga } from 'dva-core'; export default function(opts = {}) { const createOpts = { @@ -34,8 +34,6 @@ export default function(opts = {}) { return app; } -function getProvider(store, app) {} - export { connect, connectAdvanced, useSelector, useDispatch, useStore, shallowEqual }; export { bindActionCreators }; export { saga };