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

v1.1.0

Published

redux middleware; sagas for thunks

Downloads

3

Readme

redux-cosed Build Status

Sagas for thunks; redux-saga meets redux-thunk

Middleware for redux that allows developers to dispatch actions which trigger generator functions. On top of that, these generator functions have an API for describing side-effect as opposed to activating them. Let redux-cosed handle the side-effects, all you need to worry about is describing how the side-effects should look.

Features

  • Don't want the weight of redux-saga (forking model, task cancellation, spawning infinite loops, etc.)
  • Like action creators spawning side-effects
  • Want to describe side-effects as data
  • Testing is incredibly simple
  • Same API as redux-saga (select, put, call, spawn, all, delay)
  • Typescript typings
  • Upgradability from react-cosed and redux-saga

Upgrade plan

Sometimes all we need to do is fetch data from a server and load it into one component. That data lives inside that component and we don't need to share it with anything else. This is where react-cosed shines. However, because requirements change over time, we need to eventually share that data with multiple components and now we need to save the data in redux. We could use redux-thunk to accomplish that goal, but it's difficult to test thunks and the way to fetch the data looks a little different than react-cosed. This is where redux-cosed shines. The function we wrote for react-cosed looks almost identical to the one we use for redux-cosed. The only addition is redux-cosed provides the ability to query redux state and dispatch actions. Sometimes requirements change even more and now we need the ability to listen to multiple events and other more complex flow control mechanisms. This is when we would introduce redux-saga. The cosed API was built with redux-saga in mind. It's the same exact API. All we would need to do is change the import path for call, etc. to redux-saga/effects and it should work exactly the same.

So what do we have? We have an upgrade path to start small (react-cosed), upgrade to redux with simple side-effects (redux-cosed), and then upgrade to really complex flow mechanisms (redux-saga).

react-cosed -> redux-cosed -> redux-saga

Install

yarn add redux-cosed

Usage

import cosedMiddleware, { createEffect, call, select, put } from 'redux-cosed';
import { applyMiddleware, createStore } from 'redux';

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

// action creators
const todosSuccess = (payload) => ({
  type: 'TODO_SUCCESS',
  payload,
});
const uploadTodos = (todos) => createEffect(effect, todos);

// selector
const getApiToken = (state) => state.token;

// effect
function* effect(todos) {
  const token = yield select(getApiToken);

  const result = yield call(fetch, '/todos', {
    method: 'POST',
    headers: {
      'Authorization': `Bearer ${token}`,
    },
    body: JSON.stringify(todos),
  });
  const json = yield call([result, 'json']);

  yield put(todosSuccess(json));
}

const todos = ['drop kids off at the pool', 'make dinner'];
// trigger effect
store.dispatch(uploadTodos(todos));

Testing

See cosed for instructions on how to test an effect function.