npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

redux-handler

v0.13.2

Published

Simple and powerful tool for redux effects for medium and large projects

Downloads

25

Readme

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