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-optimist-promise

v0.8.3

Published

Promise middleware for Redux with automatic binding for redux-optimist

Downloads

453

Readme

redux-optimist-promise

build status npm version

FSA-compliant promise middleware middleware for Redux and automatically add necessary for redux-optimist.

npm install --save redux-optimist-promise

Usage in middlewares

First, import the middleware creator and include it in applyMiddleware when creating the Redux store. You need to call it as a function (See later why on configuration section below):

import optimistPromiseMiddleware from 'redux-optimist-promise';

composeStoreWithMiddleware = applyMiddleware(
	optimistPromiseMiddleware()
)(createStore);

To use the middleware, dispatch a promise property and optional additional properties within the meta of the action and specify the action type string as you normally do.

To add the optimist tag, add a optimist field in the meta of the action.

The pending action is dispatched immediately, with type the same as the original dispatching action with all original meta properties apart from the promise as the meta object (those are useful for optimistic updates). The resolve action is dispatched only if the promise is resolved, e.g., if it was successful; and the rejected action is dispatched only if the promise is rejected, e.g., if an error occurred.

Both fulfilled actions (resolved and rejected) will be dispatched with the result of the promise as the payload object and all other remaining properties will be dispatched inside the meta property. More specifically, in the case of a rejected promise, an error is returned in the payload property. Also those fulfilled actions will have the original type added by a suffix (default is _RESOLVED for resolved and _REJECTED for rejected).

Example:

The below action creator, when triggered dispatch(addTodo('use redux-optimist-promise'))

export function addTodo(text) {
	return {
		type: 'ADD_TODO',
		payload: {
			text
		},
		meta: {
			promise: addTodoPromise(text),
			optimist: true
		}
	};
}

will dispatch immediately

{
	type: 'ADD_TODO',
	payload: {
		text: 'use redux-optimist-promise'
	},
	optimist: {type: 'BEGIN', id: transactionID}
}

Assuming promise resolves with { id: '1', name: 'use redux-optimist-promise' }, then it will dispatch

{
	type: 'ADD_TODO_RESOLVED',
	payload: { id: '1', name: 'use redux-optimist-promise' },
	meta: {
		payload: {
			text: 'use redux-optimist-promise'
		}
	},
	optimist: {type: 'COMMIT', id: transactionID}
}

Assuming promise rejects with Error object, then it will dispatch

{
	type: 'ADD_TODO_REJECTED',
	payload: Error,
	meta: {
		payload: {
			text: 'use redux-optimist-promise'
		}
	},
	optimist: {type: 'REVERT', id: transactionID}
}

The middleware also returns the original promise, so you can listen to it and act accordingly from your component if needed (for example redirecting to a new route).

The middleware doesn't include the original promise in the 3 processed actions as it is not useful in the reducers - it is a bad practice to store promises in the state as the state should be serializable.

Configuration

You can configure the string being added to the action type when resolved or rejected by declaring it when initializing the middleware, so considering the example above, if you do

import optimistPromiseMiddleware from 'redux-optimist-promise';

composeStoreWithMiddleware = applyMiddleware(
	optimistPromiseMiddleware({
		throwOnReject: true, // wether to throw when there is an error or not
		resolvedName:'_MY_RESOLVED',
		rejectedName: '_MY_REJECTED'
	})
)(createStore);

then resolved/rejected promised will trigger actions as 'LOAD_USER_MY_RESOLVED' and 'LOAD_USER_MY_REJECTED' instead of the default ones 'LOAD_USER_RESOLVED' and 'LOAD_USER_REJECTED'.

License

MIT