Redux
Rules API forMake your code more easy to reason about with a more natural language for your logic, using rules fired by actions and reacting to a given set of facts.
Based on the forward-chaining rules in Clojure called Clara and a discussion from an issue of Choko core.
Where Redux gets in?
[ACTION] => ([RULES] -> [CONDITIONS] -> [REACTIONS]) => [STATE]
Obs.: Reactions are dispatched actions / side-effects.
Redux gives us the tools to build functional rules systems using it's middleware API. So basically each rule behaves like a Redux's middleware.
Vanilla Redux:
const LOGIN_SUCCESS = 'user/login/SUCCESS' const welcomeAuthUserMiddleware = { return } const reducer = { if actiontype === LOGIN_SUCCESS return ...state user: actionpayloaduser if actiontype === FLASH_MESSAGE return ...state message: actionpayload } const store = store console // Good to see you Manolo!
Using Redux Rules:
const LOGIN_SUCCESS = 'user/login/SUCCESS' const isAuthUser = actionpayload userrolesconst isComingBackUser = !!actionpayloaduserlastLogin const welcomeAuthUserMessageRule = type: 'messages/user/login/SUCCESS' actionTypes: LOGIN_SUCCESS condition: { const name = actionpayloaduser store return } const rulesMiddleware = const reducer = { if actiontype === LOGIN_SUCCESS return ...state user: actionpayloaduser if actiontype === FLASH_MESSAGE return ...state message: actionpayload } const store = store console // Good to see you Manolo!
Usage:
- Tests.
- Real World.
- Docs coming soon...