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

v1.0.5

Published

Lightweight decorations for redux

Downloads

19

Readme

Redux Decorators

Requirements

Of course you can use only functions from this library, but I recommend to enable decorators support. See https://www.npmjs.com/package/babel-plugin-transform-decorators-legacy

Usage

// file 'MyModel.js'
import { Storage, Transient, AsyncAction, Action, selector } from 'redux-decor'
    
// Make reducer, inject it into redux store.
// Also mix into the class static actions and static 'getState()' - returns current redux state for that reducer.
// Add to the instance object methods 'getState()', 'dispatch()' - from redux store object.
// By default it uses class name as reducer name, but you can give alias like this '@Storage("TheirsModel")'
@Storage
export default class MyModel {
    
    // Provide properties for state and create initial state.
    forPerson = undefined;
    secretSauce = null;
    fromPerson = null;
    toPerson = null;
    error = null;
    loading = false;
    // Not sure if it really needed, but you can mark property as transient - it shall not pass to redux store
    @Transient
    transientField = 'should be ignored';
    
    // Mark class method as async action.
    // Any changes to 'this' will be ignored, only 'Promise.resolve' result will be used to update redux state.
    @AsyncAction({ // override default callbacks
        request: { loading: true }, // as plain object, just set 'this.loading = true'
        ok: function() { this.loading = false }, // as function, 'this' will be bound to redux state
        error: selector('fetchSecretSauceError') // use 'selector' helper, target method of class ( see below )
    })
    makeASandwichWithSecretSauce(forPerson) {
        var self = this;

        return this.fetchSecretSauce().then(
            sauce => self.makeASandwich(forPerson, sauce),
            error => {
                self.apologize('The Sandwich Shop', forPerson, error);
                throw error; //continue promise chain
            }
        );
    }
    
    // Used as error callback from 'makeASandwichWithSecretSauce' action
    fetchSecretSauceError(error) {
        this.loading = false;
        this.error = error;
    }
    
    // Mark class method as plain action.
    // You don't need to return anything, but changes to 'this' will be used to update redux state
    @Action
    makeASandwich(forPerson, secretSauce) {
        this.forPerson = forPerson;
        this.secretSauce = secretSauce;
        return {
            forPerson,
            secretSauce
        };
    }
    
    @Action
    apologize(fromPerson, toPerson, error) {
        this.fromPerson = fromPerson;
        this.toPerson = toPerson;
        this.error = error;
    }
    
    // Just method
    fetchSecretSauce() {
        return fetch('https://www.google/search?q=secret+sauce').then(r => r.status);
    }
}
    
    
const logger = createLogger({
    collapsed: true,
    duration: true,
    diff: true
});
    
setDefaultOptions({
    asyncAction: {
        request: function () {
            this.error = null;
            this.isLoading = true;
        },
        ok: function (payload) {
            Object.assign(this, payload);
            this.isLoading = false;
        },
        error: function (error) {
            this.error = error;
            this.isLoading = false;
        }
    }
});
    
    
    
// file init-store.js
import { applyMiddleware, combineReducers } from 'redux';
import { createLogger } from 'redux-logger';
import { createStore, setDefaultOptions } from 'redux-decor';
import MyModel from './MyModel';
    
// override default callbacks
setDefaultOptions({
    asyncAction: {
        request: function () {
            this.error = null;
            this.isLoading = true;
        },
        ok: function (payload) {
            Object.assign(this, payload);
            this.isLoading = false;
        },
        error: function (error) {
            this.error = error;
            this.isLoading = false;
        }
    }
});
    
const logger = createLogger({
    collapsed: true,
    duration: true,
    diff: true
});
    
const navReducer = (state = initialState, action) => {
    const nextState = ...
    // Simply return the original `state` if `nextState` is null or undefined.
    return nextState || state;
};
    
const store = createStore(
    combineReducers({Navigation: navReducer}), // provide custom reducer
    {Navigation: initialState}, // and custom initial state
    applyMiddleware(logger) // middlewares
);
    
    
 console.log("call action makeASandwich = ", MyModel.makeASandwich('for me', 'no sauce'));
 console.log("actual state for MyModel = ", MyModel.getState());
 console.log("also call action as instance method = ", new MyModel().makeASandwichWithSecretSauce('for you'));