reducers
import { Action, AnyAction } from './actions'
/* reducers = 减速器 */
// 处理各种action类型
// 参数
// function user(state, action) {
// return state
// }
// 接收A类型,A继承Action类型如果没有继承Action,默认为AnyAction类型,
export type Reducer<S = any, A extends Action = AnyAction> = (
state: S | undefined,
action: A
) => S
// 定义一个类型 接收 S = any类型,接收A类型,A继承Action类型如果没有继承Action,默认为AnyAction类型,
export type ReducersMapObject<S = any, A extends Action = AnyAction> = {
[K in keyof S]: Reducer<S[K], A>
}
// 如果M是继承至ReducersMapObject的类型 则构造state
// reducer树
export type StateFromReducersMapObject<M> = M extends ReducersMapObject
? { [P in keyof M]: M[P] extends Reducer<infer S, any> ? S : never }
: never
export type ReducerFromReducersMapObject<M> = M extends {
[P in keyof M]: infer R
}
? R extends Reducer<any, any>
? R
: never
: never
export type ActionFromReducer<R> = R extends Reducer<any, infer A> ? A : never
export type ActionFromReducersMapObject<M> = M extends ReducersMapObject<
any,
any
>
? ActionFromReducer<ReducerFromReducersMapObject<M>>
: never
actions
// 定义Action 接收类型T,T = any
// eg:
{
type: 'fetch-userinfo'
}
export interface Action<T = any> {
type: T
}
// eg:
{
update: true
}
export interface AnyAction extends Action {
// Allows any extra properties to be defined in an action.
// 允许修改action属性名
[extraProps: string]: any
}
export interface ActionCreator<A, P extends any[] = any[]> {
// 方法定义 rest参数 P参数数组 需要发挥一个A
(...args: P): A
}
//
export interface ActionCreatorsMapObject<A = any, P extends any[] = any[]> {
[key: string]: ActionCreator<A, P>
}
middleware
import { Dispatch } from './store'
export interface MiddlewareAPI<D extends Dispatch = Dispatch, S = any> {
dispatch: D
getState(): S // 返回值S(state)状态树
}
export interface Middleware<
_DispatchExt = {}, // TODO: remove unused component (breaking change)
S = any,
D extends Dispatch = Dispatch
> {
(api: MiddlewareAPI<D, S>): (
next: D
) => (action: D extends Dispatch<infer A> ? A : never) => any
// 返回一个方法(action: D extends Dispatch<infer A> ? A : never) => any
// 着整个都是返回值类型 这个类型是一个方法 参数是next 继续返回一个方法
// (
// next: D
// ) => (action: D extends Dispatch<infer A> ? A : never) => any
}
function (api) {
return (next) => {
return (action) {
return any
}
}
}
logger = (store) => { // middleware(middlewareAPI) 执行生成一个已dispatch为参数的方法
return next => { // compose执行 生成一个已action为参数的方法
return action => { // 在用户执行外层dispatch之后,触发以内层dispatch为参数(next)的方法
return next(action)
}
}
}
const logger = store => next => action => {
console.group(action.type)
console.info('dispatching', action)
let result = next(action)
console.log('next state', store.getState())
console.groupEnd(action.type)
return result
}
/**
* 用 { meta: { delay: N } } 来让 action 延迟 N 毫秒。
* 在这个案例中,让 `dispatch` 返回一个取消 timeout 的函数。
*/
const timeoutScheduler = store => next => action => {
if (!action.meta || !action.meta.delay) {
return next(action)
}
const timeoutId = setTimeout(() => next(action), action.meta.delay)
return function cancel() {
clearTimeout(timeoutId)
}
}
store
import { Action, AnyAction } from './actions'
import { Reducer } from './reducers'
import '../utils/symbol-observable'
export type ExtendState<State, Extension> = [Extension] extends [never]
? State
: State & Extension
declare const $CombinedState: unique symbol
export type CombinedState<S> = { readonly [$CombinedState]?: undefined } & S
export type PreloadedState<S> = Required<S> extends {
[$CombinedState]: undefined
}
// S是否继承了CombinedState
? S extends CombinedState<infer S1>
? {
// 如果是object 则应该会在PreloadedState中找到
[K in keyof S1]?: S1[K] extends object ? PreloadedState<S1[K]> : S1[K]
}
: never
: {
[K in keyof S]: S[K] extends string | number | boolean | symbol
? S[K]
: PreloadedState<S[K]>
}
export interface Dispatch<A extends Action = AnyAction> {
<T extends A>(action: T, ...extraArgs: any[]): T
}
/**
* Function to remove listener added by `Store.subscribe()`.
*/
export interface Unsubscribe {
(): void
}
export type Observable<T> = {
subscribe: (observer: Observer<T>) => { unsubscribe: Unsubscribe }
[Symbol.observable](): Observable<T>
}
export type Observer<T> = {
next?(value: T): void
}
export interface Store<
S = any,
A extends Action = AnyAction,
StateExt = never,
Ext = {}
> {
dispatch: Dispatch<A>
getState(): S
subscribe(listener: () => void): Unsubscribe
replaceReducer<NewState, NewActions extends Action>(
nextReducer: Reducer<NewState, NewActions>
): Store<ExtendState<NewState, StateExt>, NewActions, StateExt, Ext> & Ext
/**
* Interoperability point for observable/reactive libraries.
* @returns {observable} A minimal observable of state changes.
* For more information, see the observable proposal:
* https://github.com/tc39/proposal-observable
*/
[Symbol.observable](): Observable<S>
}
export interface StoreCreator {
<S, A extends Action, Ext = {}, StateExt = never>(
reducer: Reducer<S, A>,
enhancer?: StoreEnhancer<Ext, StateExt>
): Store<ExtendState<S, StateExt>, A, StateExt, Ext> & Ext
<S, A extends Action, Ext = {}, StateExt = never>(
reducer: Reducer<S, A>,
preloadedState?: PreloadedState<S>,
enhancer?: StoreEnhancer<Ext>
): Store<ExtendState<S, StateExt>, A, StateExt, Ext> & Ext
}
export type StoreEnhancer<Ext = {}, StateExt = never> = (
next: StoreEnhancerStoreCreator<Ext, StateExt>
) => StoreEnhancerStoreCreator<Ext, StateExt>
export type StoreEnhancerStoreCreator<Ext = {}, StateExt = never> = <
S = any,
A extends Action = AnyAction
>(
reducer: Reducer<S, A>,
preloadedState?: PreloadedState<S>
) => Store<ExtendState<S, StateExt>, A, StateExt, Ext> & Ext