reducers

  1. import { Action, AnyAction } from './actions'
  2. /* reducers = 减速器 */
  3. // 处理各种action类型
  4. // 参数
  5. // function user(state, action) {
  6. // return state
  7. // }
  8. // 接收A类型,A继承Action类型如果没有继承Action,默认为AnyAction类型,
  9. export type Reducer<S = any, A extends Action = AnyAction> = (
  10. state: S | undefined,
  11. action: A
  12. ) => S
  13. // 定义一个类型 接收 S = any类型,接收A类型,A继承Action类型如果没有继承Action,默认为AnyAction类型,
  14. export type ReducersMapObject<S = any, A extends Action = AnyAction> = {
  15. [K in keyof S]: Reducer<S[K], A>
  16. }
  17. // 如果M是继承至ReducersMapObject的类型 则构造state
  18. // reducer树
  19. export type StateFromReducersMapObject<M> = M extends ReducersMapObject
  20. ? { [P in keyof M]: M[P] extends Reducer<infer S, any> ? S : never }
  21. : never
  22. export type ReducerFromReducersMapObject<M> = M extends {
  23. [P in keyof M]: infer R
  24. }
  25. ? R extends Reducer<any, any>
  26. ? R
  27. : never
  28. : never
  29. export type ActionFromReducer<R> = R extends Reducer<any, infer A> ? A : never
  30. export type ActionFromReducersMapObject<M> = M extends ReducersMapObject<
  31. any,
  32. any
  33. >
  34. ? ActionFromReducer<ReducerFromReducersMapObject<M>>
  35. : never

actions

  1. // 定义Action 接收类型T,T = any
  2. // eg:
  3. {
  4. type: 'fetch-userinfo'
  5. }
  6. export interface Action<T = any> {
  7. type: T
  8. }
  9. // eg:
  10. {
  11. update: true
  12. }
  13. export interface AnyAction extends Action {
  14. // Allows any extra properties to be defined in an action.
  15. // 允许修改action属性名
  16. [extraProps: string]: any
  17. }
  18. export interface ActionCreator<A, P extends any[] = any[]> {
  19. // 方法定义 rest参数 P参数数组 需要发挥一个A
  20. (...args: P): A
  21. }
  22. //
  23. export interface ActionCreatorsMapObject<A = any, P extends any[] = any[]> {
  24. [key: string]: ActionCreator<A, P>
  25. }

middleware

  1. import { Dispatch } from './store'
  2. export interface MiddlewareAPI<D extends Dispatch = Dispatch, S = any> {
  3. dispatch: D
  4. getState(): S // 返回值S(state)状态树
  5. }
  6. export interface Middleware<
  7. _DispatchExt = {}, // TODO: remove unused component (breaking change)
  8. S = any,
  9. D extends Dispatch = Dispatch
  10. > {
  11. (api: MiddlewareAPI<D, S>): (
  12. next: D
  13. ) => (action: D extends Dispatch<infer A> ? A : never) => any
  14. // 返回一个方法(action: D extends Dispatch<infer A> ? A : never) => any
  15. // 着整个都是返回值类型 这个类型是一个方法 参数是next 继续返回一个方法
  16. // (
  17. // next: D
  18. // ) => (action: D extends Dispatch<infer A> ? A : never) => any
  19. }
  20. function (api) {
  21. return (next) => {
  22. return (action) {
  23. return any
  24. }
  25. }
  26. }
  27. logger = (store) => { // middleware(middlewareAPI) 执行生成一个已dispatch为参数的方法
  28. return next => { // compose执行 生成一个已action为参数的方法
  29. return action => { // 在用户执行外层dispatch之后,触发以内层dispatch为参数(next)的方法
  30. return next(action)
  31. }
  32. }
  33. }
  34. const logger = store => next => action => {
  35. console.group(action.type)
  36. console.info('dispatching', action)
  37. let result = next(action)
  38. console.log('next state', store.getState())
  39. console.groupEnd(action.type)
  40. return result
  41. }
  42. /**
  43. * 用 { meta: { delay: N } } 来让 action 延迟 N 毫秒。
  44. * 在这个案例中,让 `dispatch` 返回一个取消 timeout 的函数。
  45. */
  46. const timeoutScheduler = store => next => action => {
  47. if (!action.meta || !action.meta.delay) {
  48. return next(action)
  49. }
  50. const timeoutId = setTimeout(() => next(action), action.meta.delay)
  51. return function cancel() {
  52. clearTimeout(timeoutId)
  53. }
  54. }

store

  1. import { Action, AnyAction } from './actions'
  2. import { Reducer } from './reducers'
  3. import '../utils/symbol-observable'
  4. export type ExtendState<State, Extension> = [Extension] extends [never]
  5. ? State
  6. : State & Extension
  7. declare const $CombinedState: unique symbol
  8. export type CombinedState<S> = { readonly [$CombinedState]?: undefined } & S
  9. export type PreloadedState<S> = Required<S> extends {
  10. [$CombinedState]: undefined
  11. }
  12. // S是否继承了CombinedState
  13. ? S extends CombinedState<infer S1>
  14. ? {
  15. // 如果是object 则应该会在PreloadedState中找到
  16. [K in keyof S1]?: S1[K] extends object ? PreloadedState<S1[K]> : S1[K]
  17. }
  18. : never
  19. : {
  20. [K in keyof S]: S[K] extends string | number | boolean | symbol
  21. ? S[K]
  22. : PreloadedState<S[K]>
  23. }
  24. export interface Dispatch<A extends Action = AnyAction> {
  25. <T extends A>(action: T, ...extraArgs: any[]): T
  26. }
  27. /**
  28. * Function to remove listener added by `Store.subscribe()`.
  29. */
  30. export interface Unsubscribe {
  31. (): void
  32. }
  33. export type Observable<T> = {
  34. subscribe: (observer: Observer<T>) => { unsubscribe: Unsubscribe }
  35. [Symbol.observable](): Observable<T>
  36. }
  37. export type Observer<T> = {
  38. next?(value: T): void
  39. }
  40. export interface Store<
  41. S = any,
  42. A extends Action = AnyAction,
  43. StateExt = never,
  44. Ext = {}
  45. > {
  46. dispatch: Dispatch<A>
  47. getState(): S
  48. subscribe(listener: () => void): Unsubscribe
  49. replaceReducer<NewState, NewActions extends Action>(
  50. nextReducer: Reducer<NewState, NewActions>
  51. ): Store<ExtendState<NewState, StateExt>, NewActions, StateExt, Ext> & Ext
  52. /**
  53. * Interoperability point for observable/reactive libraries.
  54. * @returns {observable} A minimal observable of state changes.
  55. * For more information, see the observable proposal:
  56. * https://github.com/tc39/proposal-observable
  57. */
  58. [Symbol.observable](): Observable<S>
  59. }
  60. export interface StoreCreator {
  61. <S, A extends Action, Ext = {}, StateExt = never>(
  62. reducer: Reducer<S, A>,
  63. enhancer?: StoreEnhancer<Ext, StateExt>
  64. ): Store<ExtendState<S, StateExt>, A, StateExt, Ext> & Ext
  65. <S, A extends Action, Ext = {}, StateExt = never>(
  66. reducer: Reducer<S, A>,
  67. preloadedState?: PreloadedState<S>,
  68. enhancer?: StoreEnhancer<Ext>
  69. ): Store<ExtendState<S, StateExt>, A, StateExt, Ext> & Ext
  70. }
  71. export type StoreEnhancer<Ext = {}, StateExt = never> = (
  72. next: StoreEnhancerStoreCreator<Ext, StateExt>
  73. ) => StoreEnhancerStoreCreator<Ext, StateExt>
  74. export type StoreEnhancerStoreCreator<Ext = {}, StateExt = never> = <
  75. S = any,
  76. A extends Action = AnyAction
  77. >(
  78. reducer: Reducer<S, A>,
  79. preloadedState?: PreloadedState<S>
  80. ) => Store<ExtendState<S, StateExt>, A, StateExt, Ext> & Ext