Last active
May 22, 2019 08:06
-
-
Save rrousselGit/cee059b8d9519d64598f8143e670e24e to your computer and use it in GitHub Desktop.
This file contains 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
enum CounterEvent { | |
increment, | |
decrement, | |
} | |
class Counter extends Store<int, CounterEvent> { | |
Counter() : super(initialState: 0); | |
@override | |
int reducer(int previousState, CounterEvent action) { | |
switch (action) { | |
case CounterEvent.increment: | |
return previousState + 1; | |
case CounterEvent.decrement: | |
return previousState - 1; | |
} | |
} | |
} |
This file contains 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
abstract class Store<T, A> implements Listenable { | |
Store({T initialState}) : _state = ValueNotifier<T>(initialState); | |
final ValueNotifier<T> _state; | |
void dispatch(A action) { | |
_state.value = reducer(_state.value, action); | |
} | |
T reducer(T previousState, A action); | |
@override | |
void addListener(VoidCallback listener) => _state.addListener(listener); | |
@override | |
void removeListener(VoidCallback listener) => _state.removeListener(listener); | |
void dispose() { | |
_state.dispose(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment