updateData<D2> method
MessageActionDescriptor<M, D, C>
updateData<D2>(
- DataStateKey<
D2> forState, - D2 update(
- MessageHandlerContext<
M, D, C> ctx, - D2 data
- MessageHandlerContext<
- String? label,
Updates state data of type D while a message is being handled.
When update function is called, it is provided a MessageHandlerContext, and the current
state data value.
enum Messages { increment }
var countingState = DataStateKey<int>('counting');
var builder = new StateTreeBuilder(initialChild: countingState);
builder.dataState<int>(
countingState,
InitialData.value(1),
(b) {
b.onMessageValue<Messages>(Messages.increment, (b) {
// Updates state data as a side effect while the message is handled.
b.stay(action: b.act.updateData((ctx, counter) => counter + 1));
});
});
This action can be labeled when formatting a state tree by providing a label.
Implementation
MessageActionDescriptor<M, D, C> updateData<D2>(
DataStateKey<D2> forState,
D2 Function(MessageHandlerContext<M, D, C> ctx, D2 data) update, {
String? label,
}) {
var info = MessageActionInfo(ActionType.updateData, null, D2, label);
return MessageActionDescriptor(info, (ctx) {
_log.fine(() => "State '$_forState' is updating data of type $D2");
ctx.messageContext
.data(forState)
.update((current) => update(ctx, current));
});
}