项目作者: mc-petry

项目描述 :
Redux reduce boilerplate library
高级语言: TypeScript
项目地址: git://github.com/mc-petry/redux-handler.git
创建时间: 2017-02-21T09:54:21Z
项目社区:https://github.com/mc-petry/redux-handler

开源协议:MIT License

下载


redux-handler

Powerful :muscle: and simple :point_left: redux middleware to handle async actions.

Table of Contents

Requirements

peer dependencies: redux: ^4\
optional dependencies: rxjs: ^6

Installation

store/index.ts

  1. // Define your inner stores
  2. interface RootStore {
  3. inner: InnerStore
  4. }
  5. // Combine it
  6. const reducer = combineHandlers<RootStore>({
  7. inner: innerHandler
  8. })
  9. const store = createStore(reducer.buildReducer(), applyMiddleware(handlerMiddleware()))

store/handler.ts

  1. export const { handler } = create<RootStore>()

Usage

Define store & handler:

  1. interface Store {
  2. counter: number
  3. }
  4. const myHandler = handler<Store>({ counter: 0 })
  5. // Create action
  6. const myAction = myHandler
  7. .action() // For arguments use `.action<TArgs>`
  8. .pipe(
  9. // Operators
  10. )

Operators

Each pipe must contain single main operator

Main

sync

Handles standard action handler.
Compatible operators: Common

  1. sync((state, { args, type }) => typeof state)

rx

Handles rxjs observable.
Compatible operators: Common, Async

  1. rx((args, { action$, getState, type }) => Observable)

promise

Handles promise.
Compatible operators: Common, Async

  1. promise((args, { getState, type }) => Promise)

thunk

Handles async dispatch.
Compatible operators: Common

  1. thunk({ dispatch, getState, args } => void)

Async

pending

Occurs before async method is called.

  1. pending((state, { args, type }) => void)

fulfilled

Occurs on async method succeeds.

  1. fulfilled((state, { payload, args, type }) => typeof state)

rejected

Occurs on async method failed.

  1. rejected((state, { error, args, type }) => typeof state)

completed

Occurs after async method is completed.

  1. completed((state, { args, type }) => typeof state)

loading

Sets the property = true on pending.
Sets the property = false on completed.

  1. loading(prop: keyof state)

Common

available

Prevents calling actions based on state.
Can be used only before main operator.

  1. available((getState, { args, type }) => boolean)

Advanced

Handle action in another handler

  1. myHandler.handle(
  2. myAction,
  3. // ...operators[]
  4. )

Redux devtools

  1. import { actionSanitizer } from 'redux-handler/utils'
  2. const composeEnhancers: typeof compose = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
  3. ? window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({ actionSanitizer })
  4. : compose
  5. export const store = createStore(reducer, composeEnhancers(...composes))

Examples

Simple users fetch:

  1. export interface UsersStore {
  2. data?: any[]
  3. loading?: boolean
  4. }
  5. export const usersHandler = handler<UsersStore>({})
  6. export const fetchUsers = usersHandler
  7. .action<{ limit: number }>()
  8. .pipe(
  9. available((getState, { args }) => getState().users.data),
  10. rx(({ limit }) => ajax({ url: `/users?limit=${limit}` })),
  11. fulfilled((s, { payload }) => ({ ...s, data: payload })),
  12. loading('loading')
  13. )

Dispatch another action inside action:

  1. export const updateUsersBalance = usersHandler
  2. .action()
  3. .pipe(
  4. sync(s => ({ ...s, balance: s.users.reduce((a, b) => a + b.balance, 0) }))
  5. )
  6. export const fetchUsers = usersHandler
  7. .action()
  8. .pipe(
  9. rx(
  10. () => concat(
  11. ajax(),
  12. of(updateUsersBalance())
  13. )
  14. )
  15. )

Development

Publishing

npm run build\
npm publish dist