Skip to content

Expectation about activities! #1652

Discussion options

You must be logged in to vote

Can you share code?

If you want some external entity (an "activity" or whatever you want to call it) to signal to the machine that it should update context on an interval, you can do it like this:

const machine = createMachine({
  // ...
  invoke: {
    src: () => sendBack => {
      const i = setInterval(() => {
        sendBack('UPDATE');
      }, 1000);
      
      return () => clearInterval(i);
    }
  },
  on: {
    UPDATE: { actions: assign({ count: ctx => ctx.count + 1 }) }
  }
});

Replies: 2 comments 4 replies

Comment options

You must be logged in to vote
1 reply
@mjangir
Comment options

Answer selected by Ali-Hussein-dev
Comment options

You must be logged in to vote
3 replies
@Andarist
Comment options

@davidkpiano
Comment options

@Ali-Hussein-dev
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants