Skip to content

Instantly share code, notes, and snippets.

@tmbtech
Created June 10, 2021 22:01
Show Gist options
  • Save tmbtech/c84ca5be312a061b88ca1c078f3752ed to your computer and use it in GitHub Desktop.
Save tmbtech/c84ca5be312a061b88ca1c078f3752ed to your computer and use it in GitHub Desktop.

Revisions

  1. tmbtech created this gist Jun 10, 2021.
    47 changes: 47 additions & 0 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@

    // Available variables:
    // - Machine
    // - interpret
    // - assign
    // - send
    // - sendParent
    // - spawn
    // - raise
    // - actions
    // - XState (all XState exports)

    const fetchMachine = Machine({
    id: 'fetch',
    initial: 'idle',
    context: {
    retries: 0
    },
    states: {
    idle: {
    on: {
    FETCH: 'loading'
    }
    },
    loading: {
    invoke: {
    src: () => new Promise((resolve, reject) => setTimeout(reject, 1000)),
    onDone: "success",
    onError: "failure"
    }
    },
    success: {
    type: 'final'
    },
    failure: {
    on: {
    RETRY: {
    target: 'loading',
    actions: assign({
    retries: (context, event) => context.retries + 1
    })
    }
    }
    }
    }
    });