Skip to content

Instantly share code, notes, and snippets.

@christiannwamba
Created May 14, 2020 18:22
Show Gist options
  • Save christiannwamba/8a2a28c5113c2ce469c4a4307f421168 to your computer and use it in GitHub Desktop.
Save christiannwamba/8a2a28c5113c2ce469c4a4307f421168 to your computer and use it in GitHub Desktop.

Revisions

  1. christiannwamba created this gist May 14, 2020.
    35 changes: 35 additions & 0 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    // DEMO: Null or Transient Transitions with ''

    // Cick TRY 3 times and it will get to success but will keep coming back to idle at 1 and 2 time
    // You can see the state increment in the STATE tab when you click TRY
    const ifAtFirstYouDontSucceed = Machine(
    {
    id: 'tryTryAgain',
    initial: 'idle',
    context: {
    tries: 0,
    },
    states: {
    idle: {
    on: { TRY: 'trying' },
    },
    trying: {
    entry: ['incTries'],
    on: {
    '': [{ target: 'success', cond: 'triedEnough' }, { target: 'idle' }],
    },
    },
    success: {},
    },
    },
    {
    actions: {
    incTries: assign({
    tries: (ctx) => ctx.tries + 1,
    }),
    },
    guards: {
    triedEnough: (ctx) => ctx.tries > 2,
    },
    }
    );