0.13.2 • Published 5 years ago

redux-handler v0.13.2

Weekly downloads
21
License
MIT
Repository
github
Last release
5 years ago

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

// Define your inner stores
interface RootStore {
  inner: InnerStore
}

// Combine it
const reducer = combineHandlers<RootStore>({
  inner: innerHandler
})

const store = createStore(reducer.buildReducer(), applyMiddleware(handlerMiddleware()))

store/handler.ts

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

Usage

Define store & handler:

interface Store {
  counter: number
}

const myHandler = handler<Store>({ counter: 0 })

// Create action
const myAction = myHandler
  .action() // For arguments use `.action<TArgs>`
  .pipe(
    // Operators
  )

Operators

Each pipe must contain single main operator

Main

sync

Handles standard action handler. Compatible operators: Common

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

rx

Handles rxjs observable. Compatible operators: Common, Async

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

promise

Handles promise. Compatible operators: Common, Async

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

thunk

Handles async dispatch. Compatible operators: Common

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

Async

pending

Occurs before async method is called.

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

fulfilled

Occurs on async method succeeds.

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

rejected

Occurs on async method failed.

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

completed

Occurs after async method is completed.

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

loading

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

loading(prop: keyof state)

Common

available

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

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

Advanced

Handle action in another handler

myHandler.handle(
  myAction,
  // ...operators[]
)

Redux devtools

import { actionSanitizer } from 'redux-handler/utils'

const composeEnhancers: typeof compose = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
  ? window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({ actionSanitizer })
  : compose

export const store = createStore(reducer, composeEnhancers(...composes))

Examples

Simple users fetch:

export interface UsersStore {
  data?: any[]
  loading?: boolean
}

export const usersHandler = handler<UsersStore>({})

export const fetchUsers = usersHandler
  .action<{ limit: number }>()
  .pipe(
    available((getState, { args }) => getState().users.data),
    rx(({ limit }) => ajax({ url: `/users?limit=${limit}` })),
    fulfilled((s, { payload }) => ({ ...s, data: payload })),
    loading('loading')
  )

Dispatch another action inside action:

export const updateUsersBalance = usersHandler
  .action()
  .pipe(
    sync(s => ({ ...s, balance: s.users.reduce((a, b) => a + b.balance, 0) }))
  )

export const fetchUsers = usersHandler
  .action()
  .pipe(
    rx(
      () => concat(
        ajax(),
        of(updateUsersBalance())
      )
    )
  )

Development

Publishing

npm run build\ npm publish dist

0.13.2

5 years ago

0.13.1

5 years ago

0.13.0

5 years ago

0.12.0-alpha.0

5 years ago

0.11.2

5 years ago

0.11.1

5 years ago

0.10.8

6 years ago

0.10.7

6 years ago

0.10.6

6 years ago

0.10.5

6 years ago

0.10.4

6 years ago

0.10.3

6 years ago

0.10.2

6 years ago

0.10.1

6 years ago

0.10.0

6 years ago

0.9.0

6 years ago

0.8.5

6 years ago

0.8.4

6 years ago

0.8.3

6 years ago

0.8.2

6 years ago

0.8.1

6 years ago

0.8.0

6 years ago

0.7.2

6 years ago

0.7.1

6 years ago

0.7.0

6 years ago

0.6.0

6 years ago

0.5.5

7 years ago

0.5.4

7 years ago

0.5.3

7 years ago

0.5.2

7 years ago

0.5.1

7 years ago

0.5.0

7 years ago

0.4.2

7 years ago

0.4.1

7 years ago

0.4.0

7 years ago

0.3.3

7 years ago

0.3.2

7 years ago

0.3.1

7 years ago

0.3.0

7 years ago

0.2.5

7 years ago

0.2.4

7 years ago

0.2.3

7 years ago

0.2.2

7 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.0

7 years ago