Last active
August 2, 2018 20:33
-
-
Save dpalita/1844245b3e572b4c191f47b1294796ba to your computer and use it in GitHub Desktop.
Use a Injectable() class as a factory for your reducers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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