Skip to content

Instantly share code, notes, and snippets.

@dpalita
Last active August 2, 2018 20:33
Show Gist options
  • Save dpalita/1844245b3e572b4c191f47b1294796ba to your computer and use it in GitHub Desktop.
Save dpalita/1844245b3e572b4c191f47b1294796ba to your computer and use it in GitHub Desktop.
Use a Injectable() class as a factory for your reducers
@Injectable()
export class AccountReducer {
// with real DI inside
constructor (private usefulService: UsefulService) {}
createReducer() {
// handle specific action with the help of your injected service and return new states, this is the pure reducer function
return (state: AccountState, action: Action) => {
if(action.type === 'SomethingUseful' /* or better still action instanceof SomethingUsefulAction, but this makes for another post ;-)*/) {
return {...state, {useful: this.usefulService.something(action.payload)}}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment