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

Init state machine with async transitions #202

Open
bbird1980 opened this issue Dec 13, 2020 · 0 comments
Open

Init state machine with async transitions #202

bbird1980 opened this issue Dec 13, 2020 · 0 comments

Comments

@bbird1980
Copy link

I'm using state machine for telegram bot, when webhook received message from telegram i get user state from store (redis), next creating state machine:

//example of user state from store
fsmState = {state: 'initial', data: {}}

const fsm = new StateMachine({
  data: fsmState.data,
  init: fsmState.state,
  transitions: [
    //some transitions
  ],
  methods: {
    onAfterTransition: async lifecycle => {
        if (fsmState.state !== lifecycle.to) {
            fsmState = await this.setUserFsmState(userId, {...fsmState, state: lifecycle.to});
        }
    },
    //some other transitions and state lifecycle methods
  },
});

next i'm parsing telegram message based on user state and wanna make some transition:
fsm[camelCase(transition)]();
but received pending transition error, because of promised onAfterTransition.

How can i wait while StateMachine is ready to make transition? Now i'm using awfull code:

const waitFor = () => setTimeout(() => {
  if (fsm.can(transition)) {
    fsm[camelCase(transition)]();
  } else {
    waitFor();
  }
}, 10);
waitFor();
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

1 participant