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

v1.1.4

Published

Reduce Boilerplate for Redux Async Actions and Reducers

Downloads

8

Readme

Write More Organizable Actions and Reducers with Less Code

This package redux-declare allows you to create Actions and Reducers with objects like {type: function} and {type: {status: function}}.

Feature

  1. Promise-like Async Actions: Dispatch Async Actions with Status Change.
  2. Declarative Reducers and Actions: As easy as Redux-act/Redux-actions for Synchronic operations.
  3. Neater thunk code: Do not need to rewrite type and status in thunk code.

Install

Just like other packages by npm:

npm i --save redux-declare

Document

A Sync Counter Example

Following is an redux store reprsenting a counter with sync actions (sub and add).

import { createStore, applyMiddleware } from "redux";
import { wrapReducers, wrapActions } from "redux-declare";
import assert from "assert";

let nestedReducers = {
  add: (state, action) => ({
    count: state.count + action.count
  }),
  sub: (state, action) => ({
    count: state.count - action.count
  })
};

let reducer = wrapReducers(nestedReducers);
let actionCreators = wrapActions(nestedReducers, {});

let { add, sub } = actionCreators;

store.dispatch(add({ count: 9 }));
assert(store.getState().count === 9);

store.dispatch(sub( { count: 9 }));
assert(store.getState().count === 0);

An Async Counter Example

Following we create a redux store representing a counter with async actions. In this counter, add action is always synchronic. sub action is synchronic when action.status !== success , and is asynchronous when action.status === success.

import { createStore, applyMiddleware } from "redux";
import thunk from "redux-thunk";
import { wrapReducers, wrapActions } from "redux-declare";
import assert from "assert";

let nestedReducers = {
  add: (state, action) => ({
    count: state.count + action.count
  }),
  sub: {
    // Activated when sub action is pending and state.paused is true
    success: (state, action) => ({
      count: state.count - action.count
    }),
    // Activated when sub action is pending and state.paused is true
    error: (state, action) => ({
      count: state.count + 0.01
    })
  },
  // state.paused controls whether sub async action 
  // would render a success result or an error result
  pause: (state, action) => ({
    paused: action.paused || !state.paused
  })
};

let nestedActions = {
  sub: {
    pending: payload => (dispatch, getState) => {
      setTimeout(() => {
      // If state.paused is false, async dispatch sub action with success
      // status; otherwise, async dispatch sub action with error status.
        if (!getState().paused) {
          dispatch({
            status: "success",
            count: payload.count
          });
        } else {
          dispatch({ status: "error" });
        }
      }, payload.delay);
    }
  }
};

// Options default options
let reducer = wrapReducers(nestedReducers);
let store = createStore(
  reducer,
  { count: 0, paused: false },
  applyMiddleware(thunk)
);

Then we could examine the synchronic and asynchronous actions and reducers by the code below


let actionCreators = wrapActions(nestedReducers, nestedActions);
let { add, sub, pause } = actionCreators;

// Test Sync Actions
store.dispatch(add({ count: 9 }));
assert(store.getState().count === 9);

store.dispatch(sub("success", { count: 9 }));
assert(store.getState().count === 0);

// Test Async Actions
store.dispatch(sub("pending", { count: 9, delay: 1 }));
setTimeout(() => {
  assert(store.getState().count === -9);
}, 10);

setTimeout(() => {
  store.dispatch(pause());
  assert(store.getState().paused === true);
  store.dispatch(sub("pending", { delay: 10 }));
  setTimeout(() => {
    assert(store.getState().count === -8.99);
  }, 100);
}, 100);

FAQ:

  • Do I need to rewrite type or status when customizing action creators?

    No. As long as you are trying to dispatching the type and status by the node indexed by the same type and status, you need not to retype the type and status. Even you are dispatch action in thunk, it is not necessary to rewrite the type and status.
    Details could be seen in src/composeAutoFix.js.

Maybe In the Plan:

  • [ ] Webpack bundle compile
  • [X] Compose Dispatch for avoid retype action.type
  • [ ] Support FSA by appending rules
  • [ ] BindAll like in the `redux-act`