docs/concept/effect.md
Self-First-Broadcast。
/// one style of writing
FutureOr<Object> sideEffect(Action action, Context<String> ctx) async {
if (action.type == Lifecycle.initState) {
//do something on initState
return true;
} else if (action.type == 'onShare') {
//do something on onShare
await Future<void>.delayed(Duration(milliseconds: 1000));
ctx.dispatch(const Action('shared'));
return true;
}
return null;
}
class MessageComponent extends Component<String> {
MessageComponent(): super(
view: buildMessageView,
effect: sideEffect,
);
}
/// another style of writing
Effect<String> buildEffect() {
return combineEffects(<Object, Effect<String>>{
Lifecycle.initState: _initState,
'onShare': _onShare,
});
}
void _initState(Action action, Context<String> ctx) {
//do something on initState
}
void _onShare(Action action, Context<String> ctx) async {
//do something on onShare
await Future<void>.delayed(Duration(milliseconds: 1000));
ctx.dispatch(const Action('shared'));
}
class MessageComponent extends Component<String> {
MessageComponent(): super(
view: buildMessageView,
effect: buildEffect(),
);
}