Platform: createReducer should support intercept default return value

Created on 29 Aug 2019  路  5Comments  路  Source: ngrx/platform

I try to implement an undo-redo app with meta-reducer, in previous ngrx version, we could write a reducer like

const undoRedo = (reducer) => {
  let history = {
    past: [], 
    present: initialState, 
    future: []
  }
  return (state, action) => {
      switch (action.type) {
        case 'UNDO':
          // use first past state as next present ...
          const previous = history.past[0]
          // ... and remove from past
          const newPast = history.past.slice(1)
          history = {
            past: newPast,
            present: previous,
            // push present into future for redo
            future: [history.present, ...history.future]
          }
          return previous
        case 'REDO':
          // use first future state as next present ...
          const next = history.future[0]
          // ... and remove from future
          const newFuture = history.future.slice(1)
          history = {
            // push present into past for undo
            past: [history.present, ...history.past],
            present: next,
            future: newFuture
          }
          return next
        default:
          // derive next state
          const newPresent = reducer(state, action)
          history = {
              // push previous present into past for undo
              past: [history.present, ...history.past],
              present: newPresent,
              future: [] // clear future
          }
          return newPresent
      }
  }
}

and we could not do it with new createReducer api, since there is no way to intercept the default state.

Describe any alternatives/workarounds you're currently using

Other information:

If accepted, I would be willing to submit a PR for this feature

[ ] Yes (Assistance is provided if you need help submitting a pull request)
[x] No

Store

Most helpful comment

Same as with using on(types, reducer) to replace case clauses, there should exist something like onAnythingElse(reducer) to replace the default clause for when you want a reducer to operate in any other type of action not covered. And so, consistently and thoroughly replace usage of the switch statement.

All 5 comments

I want to mention that using the switch-case reducers is still a valid way to create your reducers, and in my opinion the preferred way to create meta-reducers.

yes, switch-case is ok, but the switch-case has been removed from the official doc, it would better to add some operators like 'on' to handle this

We discussed this and decided we will not add this feature.
The reason is that we don't have a good use-case for it.
We might re-evaluate later.

Same as with using on(types, reducer) to replace case clauses, there should exist something like onAnythingElse(reducer) to replace the default clause for when you want a reducer to operate in any other type of action not covered. And so, consistently and thoroughly replace usage of the switch statement.

Same as with using on(types, reducer) to replace case clauses, there should exist something like onAnythingElse(reducer) to replace the default clause for when you want a reducer to operate in any other type of action not covered. And so, consistently and thoroughly replace usage of the switch statement.

Hi, do you mean creating a reducer using createReducer and put it in the MetaReducer? Could you give an example?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mappedinn picture mappedinn  路  3Comments

brandonroberts picture brandonroberts  路  3Comments

bhaidar picture bhaidar  路  3Comments

sandangel picture sandangel  路  3Comments

RichardMisiak picture RichardMisiak  路  3Comments