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

saga-action-creator

v1.1.9

Published

A toolkit to auto generate actions for the redux-saga.

Downloads

2,627

Readme

Saga action creator

npm version codecov npm Build Status TypeScript Tested with Jest MIT License

The Saga-action-creator is made to simplify the writing of Redux-saga, it can auto-generate Action Creators for dispatch from your writes Saga effects directly.

This toolkit supports plugins and It can help to make some actions before and after effect convenient, which means it will be much simpler to set up the loading state, handle the error, etc. when you executing the asynchronous effect.

Adds automated loading indicators for effects to Saga-action-creator. Inspired by Rematch.

Why we still need this toolkit when we have rematch already? That is because the cost of migration from Redux's historical project to Rematch is high, and Redux-Saga has many excellent designs (i.e: such as take, fork, channel, etc.).

Features

  • Auto-generate actions from passed effect
  • Completely retain redux-saga features
  • Support plugins
  • Typescript-typings support (including plugins)
  • Testable

Getting started

Install

$ npm install --save saga-action-creator

or

$ yarn add saga-action-creator

Usage Example

Step 1: Define saga effects

import createSagaActions from 'saga-action-creator';
import { takeLatest, call } from 'redux-saga/effects';
import { getUserInfo, updateUser } from '../services/user';

const user = createSagaActions({
  // By default, you can pass the generator functions
  *getUserById(id: string): Generator<any, any, any> {
    yield call(getUserInfo, id);
  },
  // If you need to change the effect take type
  // you can pass the object for the action name
  updateUser: {
    takeType: takeLatest,
    *effect(id: string, name: string): Generator<any, any, any> {
      yield call(updateUser, id, { name });
    },
  },
});

export default user;

Step 2: Connect sagaActions

import { createConnection, getLoadingPlugin } from 'saga-action-creator';
import user from './user';
import { takeLatest } from 'redux-saga/effects';

const creator = createConnection({
  // Combine creators
  creators: {
    user,
  },
  // You can change the default take type here,
  // by default is `takeEvery`
  defaultTakeType: takeLatest,
  // You can pass plugins at there
  plugins: {
    // the plugin name will be map to reducer key
    loading: getLoadingPlugin(),
  },
});

export default creator;

For a more advanced setup, see plugins and Redux-saga

Step 3: Connect to redux and redux-saga

import { createStore, combineReducers, applyMiddleware } from 'redux';
import { all } from 'redux-saga/effects';
import createSagaMiddleware from 'redux-saga';
import creator from '../sagas';

// connect to store
const reducers = combineReducers({
  ...creator.getReducers(),
});

const sagaMiddleware = createSagaMiddleware();

// connect to saga and run
sagaMiddleware.run(function*() {
  yield all(creator.getEffects());
});

const store = createStore(reducers, applyMiddleware(sagaMiddleware));

export type AppState = ReturnType<typeof reducers>;

export default store;

Step 3: Use created actions

import { connect } from 'react-redux';
import { AppState } from '../store';
import userActions from '../sagaActions/user';
import UserList from './UserList';

const mapStateToProps = (state: AppState) => ({
  loading: state.loading.user.getUserById,
});

const mapDispatchToProps = {
  getUserById: userActions.actions.getUserById,
};

export default connect(
  mapStateToProps,
  mapDispatchToProps,
)(UserList);

Examples

TODOs

  • [x] Typescript generic types for plugins export reducer
  • [x] Code remarks
  • [x] Unit tests
  • [x] Local example
  • [ ] Plugin docs
  • [ ] Online examples
  • [ ] API docs
  • [ ] Chinese docs

API

See the APIs

License

For a detailed explanation on how things work, checkout the rollup doc and parcel

Copyright (c) 2019-present, Idler.zhu