Make EventHandlers run on certain conditions.
Closed this issue · 0 comments
SEGVeenstra commented
This idea is to prevent writing unnecessary and repetitive code.
Sometimes you don't want to execute the handler in a specific state.
For example you don't want to load items when the state is not Authenticated.
We could think about separating the condition from the rest of the logic.
maybe a separate function:
class LogoutEventHandler extends EventHandler<AppState, LogoutEvent> {
@override
bool shouldRun(AppState state) => state is AuthenticatedAppState; // you can only logout when you're logged in.
@override
FutureOr<void> call(
LoadTodosEvent event,
StateGetter<AppState> getState,
StateSetter<AppState> setState,
) async {
final authenticatedState = getState() as AuthenticatedAppState; // we can safely do this because shouldRun prevents this.
// do everything to logout
}
}