spring-projects / spring-statemachine

Spring Statemachine is a framework for application developers to use state machine concepts with Spring.
1.54k stars 607 forks source link

Reactive Api for doWithAllRegions #1116

Open bhishambajaj1999 opened 1 year ago

bhishambajaj1999 commented 1 year ago

The current doWithAllRegion method doesn't support non reactive api. void doWithAllRegions(Consumer<StateMachineAccess<S, E>> stateMachineAccess); I am using 3.0.1 version of spring state machine. To Restart the stateMachineReactively I have to use subscribe() which is not preferred. Please find the snippet

public Mono<StateMachine<OrderState, OrderEvent>> returnActivatedStateMachine( OrderState orderState, StateMachine<OrderState, OrderEvent> stateMachine) {

return stateMachine.stopReactively()
    .then(Mono.defer(() -> {
          StateMachineContext<OrderState, OrderEvent> context = new DefaultStateMachineContext<>(
              orderState, null, null, null);

          stateMachine.getStateMachineAccessor()
              .doWithAllRegions(access -> access.resetStateMachineReactively(context).subscribe());

          return stateMachine.startReactively();
        })
        .thenReturn(stateMachine));

}

Can you please help with some reactive alternate for doWithAllRegions?

kzander91 commented 11 months ago

You could collect the Monos into a list like this:

List<Mono<Void>> monos = new ArrayList<>();
stateMachine.getStateMachineAccessor()
  .doWithAllRegions(access -> monos.add(access.resetStateMachineReactively(context)));
Mono.when(monos).thenEmpty(stateMachine.startReactively());

But I agree it's not that ideal...