redux-logic
redux-logic copied to clipboard
Promise/observable returns in validate/transform/process hooks
It appears that hooks don't fully benefit from promise/observable returns. The suggestion is to use promise and observable returns and make callback API optional where possible.
The library already relies on hook functions arity, this could be used to change their behaviour based on function parameters.
This validate hook
async validate({ action }, resolve, reject) {
try {
await ...;
resolve(action);
} catch (err) {
reject(err);
}
}
could become
async validate({ action }) {
await ...;
return action;
}
validateOptions/transformOptions could possibly be useful with an option similar to dispatchReturn, just in case the behaviour needs to be unambiguous.
And this multi-dispatch process hook
async process({ action }, dispatch, done) {
dispatch(...);
await ...;
dispatch(...);
done();
}
could become
async process({ action }, dispatch) {
dispatch(...);
await ...;
dispatch(...);
}
Currently single-dispatch mode is deprecated, call done when finished dispatching warning appears in case process function hook has 2 parameters. Additional doneReturn option (defaults to true for multi-dispatch) could possibly be useful for non-ending logics. I believe this approach supplements #93.
@bisubus Thanks for the suggestions. I like your ideas. I'll give it some thought.
Yeah, let's get rid of done, please. It looks so 2016. :)