love 0.1.0-beta.6 love: ^0.1.0-beta.6 copied to clipboard
A state management library that is declarative, predictable and elegant.
import 'package:love/love.dart';
// typedef CounterState = int;
abstract class CounterEvent {}
class CounterEventIncrease implements CounterEvent {}
class CounterEventDecrease implements CounterEvent {}
void main() async {
final counterSystem = System<int, CounterEvent>
.create(initialState: 0)
.on<CounterEventIncrease>(
reduce: (state, event) => state + 1,
effect: (state, event, dispatch) async {
await Future<void>.delayed(Duration(seconds: 3));
dispatch(CounterEventDecrease());
},
)
.on<CounterEventDecrease>(
reduce: (state, event) => state - 1,
)
.log()
.reactState(
effect: (state, dispatch) {
print('Simulate persistence save call with state: $state');
},
)
.onRun(effect: (initialState, dispatch) {
dispatch(CounterEventIncrease());
},);
final dispose = counterSystem.run();
await Future<void>.delayed(Duration(seconds: 3));
dispose();
}