Skip to content

Instantly share code, notes, and snippets.

@peterfarrell
Last active May 28, 2021 22:49
Show Gist options
  • Select an option

  • Save peterfarrell/aa7e534e257201bd182588a88b88096f to your computer and use it in GitHub Desktop.

Select an option

Save peterfarrell/aa7e534e257201bd182588a88b88096f to your computer and use it in GitHub Desktop.

Revisions

  1. peterfarrell revised this gist May 28, 2021. 1 changed file with 57 additions and 40 deletions.
    97 changes: 57 additions & 40 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -9,10 +9,64 @@
    // - raise
    // - actions
    // - XState (all XState exports)
    const fetchMachine = Machine({
    id: 'fetch',
    initial: 'idle',
    context: {
    item: undefined,
    retries: 0
    },
    states: {
    idle: {
    on: {
    FETCH: 'loading',
    },
    after: {
    1000: { target: 'loading' }
    }
    },
    loading: {
    on: {
    RESOLVE: 'item',
    REJECT: 'failure'
    }
    },
    item: {
    on: {
    FETCH: 'loading'
    }
    },
    failure: {
    on: {
    RETRY: {
    target: 'loading',
    actions: assign({
    retries: (context, event) => context.retries + 1
    })
    }
    }
    }
    }
    });


    const orderStates = {
    initial: 'nuu',
    const orderMachine = Machine({
    id: 'order',
    context: {
    item: undefined
    },
    initial: 'loading',
    states: {
    loading: {
    invoke: {
    id: 'fetch',
    src: fetchMachine,
    onDone: {
    target: 'nuu',
    actions: assign({ item: (context, event) => event.data })
    },
    }
    },
    nuu: {
    on: {
    MAKE_PENDING: 'pending',
    @@ -60,42 +114,5 @@
    type: 'final'
    },
    }
    }
    })

    const fetchMachine = Machine({
    id: 'fetch',
    initial: 'idle',
    context: {
    retries: 0
    },
    states: {
    idle: {
    on: {
    FETCH: 'loading'
    }
    },
    loading: {
    on: {
    RESOLVE: 'item',
    REJECT: 'failure'
    }
    },
    item: {
    on: {
    FETCH: 'loading'
    },
    ...orderStates,
    },
    failure: {
    on: {
    RETRY: {
    target: 'loading',
    actions: assign({
    retries: (context, event) => context.retries + 1
    })
    }
    }
    }
    }
    });

  2. peterfarrell revised this gist May 28, 2021. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -22,25 +22,35 @@
    on: {
    ACCEPT: 'accepted',
    CANCEL: 'canceled',
    HOLD: 'held',
    }
    },
    accepted: {
    on: {
    UNPEND: 'pending',
    PROCESS: 'in_process',
    CANCEL: 'canceled',
    HOLD: 'held',
    }
    },
    in_process: {
    on: {
    COMPLETE: 'completed',
    CANCEL: 'canceled',
    HOLD: 'held',
    }
    },
    completed: {
    on: {
    DELIVER: 'delivered',
    PICKUP: 'delivered',
    CANCEL: 'canceled',
    HOLD: 'held',
    }
    },
    held: {
    on: {
    UNHOLD: 'completed'
    }
    },
    delivered: {
  3. peterfarrell revised this gist May 28, 2021. 1 changed file with 23 additions and 7 deletions.
    30 changes: 23 additions & 7 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -21,17 +21,33 @@
    pending: {
    on: {
    ACCEPT: 'accepted',
    CANCEL: 'canceled',
    }
    },
    accepted: {},
    in_process: {},
    completed: {},
    canceled: {
    accepted: {
    on: {
    PROCESS: 'in_process',
    CANCEL: 'canceled',
    }
    },
    in_process: {
    on: {
    COMPLETE: 'completed',
    CANCEL: 'canceled',
    }
    },
    completed: {
    on: {
    DELIVER: 'delivered',
    PICKUP: 'delivered',
    CANCEL: 'canceled',
    }
    },
    delivered: {
    type: 'final'
    },
    hist: {
    type: 'history',
    history: 'shallow' // optional; default is 'shallow'
    canceled: {
    type: 'final'
    },
    }
    }
  4. peterfarrell created this gist May 28, 2021.
    75 changes: 75 additions & 0 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,75 @@

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

    const orderStates = {
    initial: 'nuu',
    states: {
    nuu: {
    on: {
    MAKE_PENDING: 'pending',
    },
    },
    pending: {
    on: {
    ACCEPT: 'accepted',
    }
    },
    accepted: {},
    in_process: {},
    completed: {},
    canceled: {
    type: 'final'
    },
    hist: {
    type: 'history',
    history: 'shallow' // optional; default is 'shallow'
    },
    }
    }

    const fetchMachine = Machine({
    id: 'fetch',
    initial: 'idle',
    context: {
    retries: 0
    },
    states: {
    idle: {
    on: {
    FETCH: 'loading'
    }
    },
    loading: {
    on: {
    RESOLVE: 'item',
    REJECT: 'failure'
    }
    },
    item: {
    on: {
    FETCH: 'loading'
    },
    ...orderStates,
    },
    failure: {
    on: {
    RETRY: {
    target: 'loading',
    actions: assign({
    retries: (context, event) => context.retries + 1
    })
    }
    }
    }
    }
    });