Skip to content

Patterns for sharing a machine as a lib #1495

Discussion options

You must be logged in to vote

This has me thinking that maybe sendParent shouldn't throw if there is no parent, but rather gracefully continue...

Anyway, is your plan to have this consumable as an XState machine, or a general-purpose utility?

As an XState machine:

Use sendParent, as you have described above. The assumption is that this machine is to be invoked.

As a general-purpose utility:

You can try this pattern:

export function exponentialBackoff(getPromise, { onSuccess, onFailure, onRetry }) {
  const machine = createMachine(/* ... */)
    .withConfig({
      actions: { onSuccess, onFailure, onRetry }
    });

  interpret(machine).start();
}

Which can be generally consumed like this:

import { exponentialBackoff } f…

Replies: 4 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Answer selected by cristiano-belloni
Comment options

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

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants