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

v0.0.6

Published

Spark is a thin wrapper around redux and redux-saga. It significantly reduces boilerplate code in react/redux/saga applications.

Downloads

22

Readme

Redux Spark

npm version npm downloads

What is this?

Spark is a thin wrapper around redux and redux-saga. We tried to reduce boilerplate code in react/redux/saga applications. Instead of writing action types, action creators, sagas and reducer, Spark allows you to call a single function and generate everything under the hood.

If you want to customize anything, only thing you need to write is a reducer (again, everything else is generated). There is a lot of customizable options, so we think it covers a lot of use cases. If you still need something that is not supported, it plays nicely with a standard reducer/saga/types/creators setup.

Example

Spark comes with pre built generator. generateAsyncReducer accepts two params - reducer's name and function that returns a promise. Hopefully live example will make it clearer.

import { generateAsyncReducer } from 'redux-spark';
import api from '../api';

// api.getUsers returns a promise
const actionCreators = generateAsyncReducer('users', api.getUsers);

export const {
  get,
  reset,
} = actionCreators;

Code above generates:

  • Four actions types - one for reset action and three for async get action
  • Two action creators - get and reset
  • Saga and watcher - for async get action using getLatest
  • Reducer function - with the following state:
    {
      // data returned from api.getUsers
      data: object,
      // error if api.getUsers fails
      error: object,
      // loading indicators
      loading: boolean,
      // all params passed to "get" action (i.g. pagination offset and per page)
      params: object,
    }

You can write your generators, we advise you to check the source in generate-async-reducer.ts, it should be pretty self explanatory.

Custom reducer

import { Reducer } from 'redux-spark';
import api from './api';

// Create app reducer
const global = new Reducer('global', {
  isModalActive: false,
  settings: null,
  settingsError: null,
  settingsLoading: false,
});

// Add sync action
export const toggleModal = global.addAction('toggleModal', (state:any, action:any) => {
  return {
    ...state,
    isModalActive: !state.isModalActive,
  };
});

// Add async action
export const getSettings = global.addAsyncAction('getSettings', api.getSettings, {
  start: (state:any, action:any) => {
    return {
      ...state,
      settings: null,
      settingsError: null,
      settingsLoading: true,
    };
  },
  error: (state:any, action:any) => {
    return {
      ...state,
      settingsError: action.error,
      settingsLoading: false,
    };
  },
  success: (state:any, action:any) => {
    return {
      ...state,
      settings: action.data,
      settingsLoading: false,
    };
  },
});

API

Before diving into API, be sure to check examples above.

Spark core

Reducers

getAllReducers method will return array containing all of the generated reducers. You need to add it to your root reducer (only once).

import { combineReducers } from 'redux';
import spark from 'redux-spark';

export default combineReducers({
  // You can put any additional reducers here
  ...spark.getAllReducers(),
});

PLEASE NOTE you'll need to import your reducer files somewhere, so they are executed and registered in the spark core. If you want to do it manually, you can always get a single reducer using it's built in method .getReducerFunction()

import { combineReducers } from 'redux';

// Import spark reducer classes
import users from './users';
import groups from './groups';

export default combineReducers({
  // You can put any additional reducers here

  // Call "getReducerFunction" to get reducer function from spark
  users: users.getReducerFunction(),
  groups: groups.getReducerFunction(),
});

Sagas

Same thing for sagas. getAllSagas will return an array containing all of the generated sagas. You need to yield them in your root saga.

import { all } from 'redux-saga/effects';
import spark from 'redux-spark';

export default function* rootSaga() {
  yield all([
    // You can put any additional sagas here
    ...spark.getAllSagas(),
  ]);
}

Async Reducer Generator

Reducer

  • Constructor

    • name string
    • initialState any type (you can use plain object or for example Immutable instance)

    Returns Reducer instance.

  • addAction

    • actionName string, camel cased (e.g. toggleModal, generated action type will be TOGGLE_MODAL).
    • handler reducer function with two params state and action.

    Returns action creator function.

  • addAsyncAction

    • actionName string, camel cased (e.g. getUsers, generated action types will be GET_USERS_START, GET_USERS_SUCCESS, GET_USERS_ERROR).
    • asyncMethod function that returns a promise. This promise will be resolved in the generated saga creator, and data returned to the handler.
    • handlers map (object) with start/success/end keys, each being reducer function with two params state and action. Each handler responds to one of the three generated actions.
    • sagaOptions object, if you want to customize saga effect or pass a custom saga, you need to pass it in this object.
      • Custom effect:
        { effect: takeEvery }
      • Custom saga creator (it needs to be a function, so Spark can pass object with generated action types).
        { sagaCreator: (actionsTypes) => yourCustomSaga }
        actionsTypes is an object with start/success/end keys, each one corresponding to the action type. (e.g. GET_USERS_START, GET_USERS_SUCCESS, GET_USERS_ERROR)

    Returns action creator function.

    By default generated saga will use takeLatest effect.

Development

Quick start:

npm start

This project was bootstrapped with Create React App. Check their default readme.

Contributors