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

@rebean/async

v0.0.3

Published

Redux async bean

Downloads

4

Readme

@rebean/async

Async utilities for Redux

Installation

This module is available as a npm package. To start using it, you have to install it first.

npm install --save @rebean/async

After installing it, you have to add snackbar reducer to your root reducer.

import { createStore, combineReducers, applyMiddleware } from 'redux';
import {
  asyncReducer,
  asyncMiddleware,
  asyncActionSanitizer
} from '@rebean/async';
import thunkMiddleware from 'redux-thunk';
// if you are using Redux Devtools
import { composeWithDevTools } from 'redux-devtools-extension';

const rootReducer = combineReducers({
  // ...your other reducers here
  // you have to pass asyncReducer under 'async' key,
  async: asyncReducer
});

// to block actions that are not in proper chronology
const middlewareEnhancer = applyMiddleware(
  thunkMiddleware,
  asyncMiddleware
  // ...other middleware
);

// to see async action status and parameters in Redux Devtools
const composeEnhancers = composeWithDevTools({
  actionSanitizer: asyncActionSanitizer
});

const enhancer = composeEnhancers(middlewareEnhancer);
const initialState = {};

const store = createStore(rootReducer, initialState, enhancer);

Now you can connect it to your components - redux knows how to handle async actions.

Usage

To define async action you need redux-thunk installed and connected to the store. The easiest way to create new async action creator is to use async function from this package. Then you have to create reducer - you can use handle* higher order reducers to create your own.

Example of fetching list of users:

// store/user/userAction.ts

import { async, AsyncAction } from '@rebean/async';

export const LIST_USERS = 'LIST_USERS';
export type ListUsersAction = AsyncAction<User[]>;
export const listUsers = (): ThunkAction<Promise<User[]>> => dispatch =>
  dispatch(async(LIST_USERS, UserApi.list()));
// store/user/userReducer.ts
import { handleAsync } from '@rebean/async';
import { combineReducers } from 'redux';
import { LIST_USERS, ListUsersAction } from './userAction';

const listUsersReducer = handleResolved<User[], ListUsersAction>(
  LIST_USERS,
  (state, action) => action.payload,
  []
);

export const userReducer = combineReducers({
  users: listUsersReducer
});
// component/UserList/UserList.tsx

import { isAsyncPending } from '@rebean/async';
import { LIST_USERS } from '../../store/user/userAction.ts';

export namespace UserList {
  export type StateProps = {
    isLoading: boolean;
    users: User[];
  };
  export type OwnProps = {};
  export type Props = StateProps & OwnProps;
}

const UserListDumb: FC<UserList.Props> = ({ isLoading, users }) => {
  if (isLoading) {
    return 'Loading...';
  }

  return (
    <ul>
      {users.map(user => (
        <li key={user.id}>{user.name}</li>
      ))}
    </ul>
  );
};

export const UserList = connect(state => ({
  isLoading: isAsyncPending(LIST_USERS)(state),
  users: getUsers(state)
}))(UserListDumb);

License

MIT