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-super-thunk

v1.0.0

Published

Redux middleware that returns a function instead of an action but with a twist.

Downloads

559

Readme

Redux Super Thunk

Thunk middleware for Redux that adds the store as an argument.

npm install --save redux-super-thunk

Apply Middleware

To use redux super thunk, you will need to use the super thunk version of the applyMiddleware enhancer, instead of the out of the box version from the redux library.

import { createStore, combineReducers } from 'redux'
import thunk, { applyMiddleware } from 'redux-super-thunk';
import * as reducers from './reducers'

let reducer = combineReducers(reducers)

// using super thunk applyMiddleware instead of
// the one from redux library
let store = createStore(reducer, applyMiddleware(thunk))

Why Redux Super Thunk?

Redux Super Thunk is a super-set of what is provided by Redux Thunk. Redux Thunk supplies the redux store's dispatch and getState methods to a higher-order action creator (as well as an addition argument of your choosing).

While thunk is a powerful too for being able to create almost any manner of asynchronous actions, it would be so much more powerful to supply the store as well, making it possible to subscribe to a store inside of a higher order action creator. This is where Super Thunk comes in.

import axios from 'axios';

export default {
    subscribe: params => (dispatch, getState, store) => {
        let unsubscribe = store.subscribe(() => {
            let ival = setInterval(() => {
                let state = getState()['subscription'];
                if ( state === 'on' ) {
                     axios.get('http://someapi/my-data').then((result) => {
                        dispatch({ type: 'MY_UPDATE', result: result});                
                    });
                }
                else {
                    clearInterval(ival);
                    unsubscribe();
                }
            }, 8000);
        });
    },

    unsubscribe: () => ({type: 'UNSUBSCRIBE'}),
};

Super Thunk with extra argument

For redux thunk users that are currently using an extra argument when adding thunk as middleware, you can still do this, and the store becomes a fourth argument for your thunk action creator.

Setup with extra argument:

import { createStore, combineReducers } from 'redux'
import thunk, { applyMiddleware } from 'redux-super-thunk';
import * as reducers from './reducers'
import axios from 'axios';

let reducer = combineReducers(reducers)

const api = axios.create({ baseUrl: 'http://someapi' });
let store = createStore(reducer, applyMiddleware(thunk.withExtraArgument(api)))

Your thunk will now be:

export default {
    subscribe: params => (dispatch, getState, api, store) => {
        let unsubscribe = store.subscribe(() => {
            let ival = setInterval(() => {
                let state = getState()['subscription'];
                if ( state === 'on' ) {
                     api.get('/my-data').then((result) => {
                        dispatch({ type: 'MY_UPDATE', result: result});                
                    });
                }
                else {
                    clearInterval(ival);
                    unsubscribe();
                }
            }, 8000);
        });
    },

    unsubscribe: () => ({type: 'UNSUBSCRIBE'}),
};

**For more information on thunks go here