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-optimistic-manager

v3.0.2

Published

A transaction based optimistic base manager for redux Edit

Downloads

18

Readme

redux-optimistic-manager

building status Coverage Status NPM version

redux-optimistic-manager is a lib aimed to simplify optimistic UI implement in redux environment. this lib uses a transaction based method to handle actual and optimistic actions, rolling back optimistic ones in a future point.

How to use

Using redux-optimistic-manager is simple.

  1. Install it:

    npm install --save redux-optimistic-manager
  2. Wrap your reducer with createOptimisticReducer higher order function, then create a manager for your store, the createOptimisticManager returns postAction and rollback function:

    // store.js
    import {createStore} from 'redux';
    import {createOptimisticManager, createOptimisticReducer} from 'redux-manager';
    import reducer from './reducer';
    
    export let store = createStore(createOptimisticReducer(reducer));
    export let {postAction, rollback} = createOptimisticManager(store);
    • postAction(action, [transactionId]) tells transaction to save a action, if transactionId is provided then the action is treated as optimistic, transactionId can be any unique value except null and undefined, we recommend using an new object({}) as transaction id. The postAction function returns whatever you provide as action argument.
    • rollback(transactionId, [replay = store.dispatch]) is to rollback all optimistic actions in certain transaction, you can provide an extra replay function to rollback, all saved actions will be dispatch through replay function.
  3. Create a transactionId in your business logic, before you dispatch any action, call postAction to save it in transaction, you can rollback optimistic ones by calling rollback(transactionId):

    let newTodo = todo => ({type: 'NEW_TODO', payload: todo});
    
    let notify = message => ({type: 'NOTIFY', payload: message});
    
    let saveTodo = async todo => {
        // Begin a transaction
        let transactionId = {};
    
        // Actual action will be saved and re-dispatched on rollback
        postAction(postAction(notify('Saving todo')));
    
        let newTodoAction = newTodo(todo);
        // Save and dispatch an optimistic action, this action will be dismissed on rollback
        dispatch(postAction(newTodo(todo)), transactionId);
    
        let createdTodo = await service.post('/todos', todo);
    
        // Rollback to dismiss all optimistic actions
        rollback(transactionId);
    
        // Dispatch final actual action, this should also be saved
        dispatch(postAction(newTodo(createdTodo)));
    };

Integrate with middleware

redux-optimistic-thunk is an optimistic middleware based on this lib, the code is quite easy to read.

Change Log

2.0.0

  • Simplified interfaces, now we need only postAction and rollback functions.
  • No longer manage transaction id, you should provide an unique transactionId to postAction and rollback function.

3.0.0

  • Refreshed build with single rollup.
  • The es module version is now located at dist/index.mjs.
  • Add "sideEffects": false to package.json.