handleStateAction method
Run the handler registered in stateActions for a state action.
An action sent with stateAction, StateActions.call or
NyController.updatePageState carries a String name and one data
value: the handler is called with the value when it takes a parameter,
and without it when it takes none. A typed action, sent by a bodiless
method on a PageStateActions subclass, carries the method's Symbol
with its positional and named arguments, which are applied as given.
Implementation
Future<void> handleStateAction(dynamic data) async {
if (data is! Map || !data.containsKey('action')) return;
final Object? action = data['action'];
if (action == null) return;
final Function? handler = stateActions[action];
if (handler == null) {
/// A typed action names a method the developer wrote, so nothing
/// answering it is a mistake worth reporting. A string action may be a
/// built-in that [stateUpdated] has already handled.
if (action is Symbol) {
NyLogger.error(
'$runtimeType has no handler for $action in stateActions. '
'Register one, e.g. { $action: yourMethod }.',
);
}
return;
}
if (action is Symbol) {
final List<dynamic> args = data['args'] as List<dynamic>? ?? const [];
final Map<Symbol, dynamic> named =
data['named'] as Map<Symbol, dynamic>? ?? const {};
await Function.apply(handler, args, named);
return;
}
final dynamic actionData = data['data'];
if (handler is Function(Never)) {
await Function.apply(handler, [actionData]);
} else if (handler is Function()) {
await Function.apply(handler, []);
} else {
NyLogger.error(
'The "$action" handler in $runtimeType.stateActions must take one '
'positional parameter or none.',
);
}
}