cerebral / overmind

Overmind - Frictionless state management

Home Page:https://overmindjs.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

React on actions [possible improvement suggestion]

ssijak opened this issue · comments

Is it possible to react to actions? Something like

reaction(
  (actions) => actions.uberAction,

  (state, actions, actionParams) => {
    //do something after the action was called? 
  },
)

@ssijak You could just make a new action that calls both

export const newAction = ({actions}) => {
   actions.uberAction();
   
   // do something after the action was called.
}

// Call the new action rather then actions.uberAction
actions.newAction();

or if you always want the extra work, just add it to uberAction directly

Yeah. I was just thinking that I could have some general utility actions, like log functions which would log action calls somewhere, and then I would not need to change the other actions code and could just add reactions to ones I am interested to "decorate".

For that, you can try to use events

https://overmindjs.org/api-1/events

All good suggestions here, you could also just use functional patterns, like factory:

const log = (context, action) => {
  console.log('Calling ', action.name)
  return action(context)
}
const uberAction = log(() => {

})

Or even just Overminds own functional API:

const ubAction = pipe(
  log('uberAction'),
  () => { /* uberAction */}
)

NOTE! Shown with next API, coming soon