Redux with less boilerplate, actions statuses and controlled side-effects in a single shot.
Redux with less boilerplate, actions statuses and controlled side-effects in a single shot.
Redux with less boilerplate, actions statuses and controlled side-effects in a single shot.
¹ With Actionware, actions have a different meaning: they're just functions which execution generate events. See usage section to better understand.
Wanna have state selectors/getters in a decent way? Use it combined with Stateware lib.
yarn add actionwarenpm i actionware --savecan define custom action types prefix and suffixes:
import * as actionware from 'actionware';
actionware.setup({
store,
defaultPrefix, // default: 'actionware:'
errorSuffix, // default: ':error'
cancelSuffix, // default: ':cancel'
busySuffix // default: ':busy'
});To make Redux store react to busy and error status changes, make sure you add the Actionware reducer into your root reducer.
import { combineReducers } from 'redux';
import { actionwareReducer } from 'actionware';
const rootReducer = combineReducers({
actionware: actionwareReducer,
// your reducers
});export function incrementCounter() { }Whatever you return will be the action payload
// Note that the store is always the last arg
export async function loadUsers(arg1, arg2, argN, store) {
const response = await fetch('/my/api/users');
return response.json();
}Use call to invoke an action and let Actionware handle
the execution lifecycle (managing error and busy statuses, notifying listeners, etc).
import { call } from 'actionware';
call(loadUsers, arg1, arg2, argN);import { call } from 'actionware';
const actionCall = call(loadUsers, arg1, arg2, argN);
actionCall.cancel()To cancel inner calls or other async executions, use setExtra inside an async action
to keep information needed and use them on a cancellation listener:
…import { clearError } from 'actionware'
export async function someAction() {
// ...
}
clearError(someAction)…import { getError, isBusy } from 'actionware';
import { loadUsers } from 'path/to/userActions';
// Whenever needed...
isBusy(loadUsers);
getError(loadUsers);Note that busy listeners are called when busy status changes.
…When you have "complex" flows that depend on some interaction to start or continue,
you can use next to wait for some action completion in this fashion:
import { call, next } from 'actionware';
import { login, showTip, acknowledgeTip } from 'path/to/actions';
export async function appEducationFlow() {
// Wait for the next successful login
await next(login);
call(showTip, 'headerButtons');
await next(acknowledgeTip);
history.redirect('/some/route');
call(showTip, 'sideMenu');
await next(acknowledgeTip);
}
// At some point, start the flow
appEducationFlow();By using withActions to wrap a component, actions are injected into it as props
and can be invoked without using call.
…In case you prefer not injecting actions as props into your component, you can use createActions this way:
import { createActions } from 'actionware'
const actions = createActions('optionalPrefix:', {
someAction,
anotherAction
})
const MyComponent = () => (
)
call and next functionsWhile testing, you're able to replace the call and next functions by custom
spy/stub to simplify tests.
import { mockCallWith, mockNextWith } from 'actionware';
const callSpy = sinon.spy();
const nextStub = sinon.stub().returns(Promise.resolve());
mockCallWith(callSpy);
mockNextWith(nextStub);
// Get back to default behavior
mockCallWith(null);
mockNextWith(null); For testing reducers, you can do the following:
…MIT © Wellington Guimaraes
No open issues yet, or sync has not completed.