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-entry

v0.1.4

Published

[Redux Middleware] process dispatched action before it reaches the store

Downloads

13

Readme

Redux-Entry

npm

Middleware for Redux.

Definition

Entry is a function that:

  • Target one type of action
  • Will be called with (store, action), after the action is dispatched, but before it reaches the store reducer
  • Can block current dispatched action by return true
  • Has access to store. Hence you can access current store state by store.getState(), or dispatch some action by store.dispatch. From there we have some feature like:
    • Block and Redirect action to another action type
    • Dispatch multiple action from one Entry
    • dispatch to another Entry (beware of dead loops)

Usage

import { createReduxEntry, createStateStore, createStateStoreReducer } from 'redux-entry'

ReduxEntry

import { createStore, applyMiddleware, combineReducers } from 'redux'
import { createReduxEntry } from 'redux-entry'

const reducer = (state = {}, action) => state // some sample reducer

const { middleware: reduxEntryMiddleware, setEntry, setEntryMap } = createReduxEntry() // get ReduxEntry instance

// add reduxEntry to middleware
const store = createStore(
  reducer,
  // null, // no initialState
  applyMiddleware(reduxEntryMiddleware)
)

// define some entryFunction
const entryFunction0 = (store, action) => {}
const entryFunction1 = ({dispatch, getState}, {type, payload}) => {}
// and set
setEntry('action-type-to-precess-0', entryFunction0)
setEntry('action-type-to-precess-1', entryFunction1)

// or pack them up
const entryMap = {
  'action-type-to-precess-0': entryFunction0,
  'action-type-to-precess-1': entryFunction1
}
// and set
setEntryMap(entryMap)

createStateStore & createStateStoreReducer

These are packed handy functions to create mini-state-store, if needed to.

mini-state-store can be used to offload Redux store reducer codes.

Suppose we have a store state like this:

const storeState = {
  'mini-state-a': {},
  'mini-state-b': {},
  'mini-state-c': {}
}

and for each mini state, we have reducers like:

const miniStateReducer = (state, action) => {
  if (action.type === 'mini-state:set-key-0') return { ...state, key0: 0 }
  else if (action.type === 'mini-state:set-key-1') return { ...state, key1: 1 }
  else if (action.type === 'mini-state:set-key-2') return { ...state, key2: 2 }
  else return state
}

We might want to 'reduce' the reducer code to a single merge operation:

const miniStateReducer = (state, action) => {
  if (action.type === 'mini-state:update') return { ...state, ...action.payload }
  else return state
}

...and put the each reduce action to one entryFunction:

const entryMap = {
  'mini-state:set-key-0': ({ dispatch, getState }, action) => {
    const miniState = getState()['mini-state-a']
    dispatch({ type: 'mini-state:update', payload: { ...miniState, key0: 0 } })
  },
  'mini-state:set-key-1': ({ dispatch, getState }, action) => {
    const miniState = getState()['mini-state-a']
    dispatch({ type: 'mini-state:update', payload: { ...miniState, key1: 1 } })
  },
  'mini-state:set-key-2': ({ dispatch, getState }, action) => {
    const miniState = getState()['mini-state-a']
    dispatch({ type: 'mini-state:update', payload: { ...miniState, key2: 2 } })
  }
}

And with createStateStore and createStateStoreReducer, we get a mini-state-store:

import { createStateStore, createStateStoreReducer } from 'redux-entry'

const initialState = {}
const stateStore = createStateStore(initialState)
const { getState, setState, wrapEntry } = stateStore
const reducer = createStateStoreReducer('mini-state:update', stateStore)
const entryMap = {
  'mini-state:set-key-0': wrapEntry((state, { dispatch }, action) => { dispatch({ type: 'mini-state:update', payload: { ...state, key0: 0 } }) }),
  'mini-state:set-key-1': wrapEntry((state, { dispatch }, action) => { dispatch({ type: 'mini-state:update', payload: { ...state, key1: 1 } }) }),
  'mini-state:set-key-2': wrapEntry((state, { dispatch }, action) => { dispatch({ type: 'mini-state:update', payload: { ...state, key2: 2 } }) })
}

export { stateStore, reducer }

License

MIT. Issues and Pull Requests are welcomed.