Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handlers fired multiple times for same transition #14

Open
adamwasila opened this issue Apr 25, 2014 · 1 comment
Open

Handlers fired multiple times for same transition #14

adamwasila opened this issue Apr 25, 2014 · 1 comment
Assignees

Comments

@adamwasila
Copy link

I'm trying to implement simple FSM to manage UI state in Android application. Here is a problem I found (isolated to an abstract example):

FSM definition (pseudo code):

State { A, B, C }

Event { next }

fsm = from(State.A).transit(
        on(Event.next).to(State.B).transit(
                on(Event.next).to(State.C).transit(
                        on(Event.next).to(State.A)
                        )
                  )
            );
fsm.executor(new UIThreadExecutor()); // same as in android ATM example

Handler on "whenEnter":

        mFlow.whenEnter(new StateHandler<FlowContext>() {
            @Override
            public void call(StateEnum state, FlowContext context) throws Exception {
                println("entering: " + state);
                Thread.sleep(200); // delay handler execution, so that issue become repeatable
            }
        });

Now, last but not least, following code is executed on some event (eg. button press):

mContext.trigger(Event.next);
mContext.trigger(Event.next);
mContext.trigger(Event.next);

Expected result:

entering: A
entering: B
entering: C
entering: A

Observed result:

entering: A
entering: B
entering: B
entering: B

My understanding of what causes an issue here:
After looking briefly into code I see that triggering is implemented in a strange way by splitting it to two runnables: first, handlers are called in separate executor task, but then another executor task is added to do actual state switching (setCurrentState method). So, as a matter of luck, if second task (state change) is interleaved with another trigger, we may observe handlers being called twice for same transition.

@Beh01der Beh01der self-assigned this Apr 27, 2014
@night2tl
Copy link

Is this issue resolved already?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants