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.
[ ] Yes (Assistance is provided if you need help submitting a pull request)
[x] No
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 replacecaseclauses, there should exist something likeonAnythingElse(reducer)to replace thedefaultclause for when you want a reducer to operate in any other type of action not covered. And so, consistently and thoroughly replace usage of theswitchstatement.
Hi, do you mean creating a reducer using createReducer and put it in the MetaReducer? Could you give an example?
Most helpful comment
Same as with using
on(types, reducer)to replacecaseclauses, there should exist something likeonAnythingElse(reducer)to replace thedefaultclause for when you want a reducer to operate in any other type of action not covered. And so, consistently and thoroughly replace usage of theswitchstatement.