NgRx Reset/UNDO Actions

To reset a sequence of actions, like when user selects multiple items (each of which dispatch SELECT__ITEM ) in filter and then clicks cancel, triggering reset of all SELECT__ITEM actions, we need to be able to either:

  • Not update the store until Apply is clicked:
    • To do this, we need to create an Effect/Meta-Reducer (or ReplaySubject can subscribe to all actions and on Apply action, a new temp observable can be made to subscribe to this replay subject which will then replay ie emit all the actions which this temp observable can simply dispatch), that catches all the SELECT_ITEM actions and on APPLY_FILTER action, the Effect dispatches the select_item actions (or 1 action with payload of all items collated) and on completion, dispatches the Apply_Filter Action to the Item Reducer and on CANCEL_FILTER, discards all items
    • REF: Todo with Undo/Redo – Using Meta-Reducers
      • Details here:
  • Update the store with every item click and then Revert to original state when cancel is clicked:
    • The way to do this without keeping a local copy of anything, is to write a meta reducer with a replaySubject so that it replays the TOGGLE_ITEM action which will toggle the previously toggled items, hence reverting the state of the items.
    • Another way is to keep a local copy of the items identifier (eg. item_id) before any actions are dispatched, and on CANCEL_FILTER action, trigger new action to RESTORE_FITER_CHECKBOX with this item_id list to toggle filters (NOTE: If the item obj is tiny, or if it doesn’t have identifier, you may send the entire obj and the reducer will just replace the item in store with this item for all changed items).

also see:


PS: Note that this decision needs to keep the observers in mind. So if the requirement is such that no-observers should be triggered until apply is clicked, then it makes sense to NOT update the store till then. But if observers want to be notified of item selection irrespective of Apply Clicked, then Store has to be updated in real time…


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s