Skip to content

Instantly share code, notes, and snippets.

@adamterlson
Created April 21, 2020 19:05
Show Gist options
  • Select an option

  • Save adamterlson/2a8317e74c305718847b4ce98c05c8bb to your computer and use it in GitHub Desktop.

Select an option

Save adamterlson/2a8317e74c305718847b4ce98c05c8bb to your computer and use it in GitHub Desktop.

Revisions

  1. adamterlson created this gist Apr 21, 2020.
    78 changes: 78 additions & 0 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,78 @@
    const transactionMachine = Machine({
    id: 'transaction',
    initial: 'draft',
    states: {
    draft: {
    on: {
    AUTHORIZE_PAYMENT: {
    target: 'authorizing',
    },
    ADD_ITEM: {
    target: 'draft',
    },
    ADD_CUSTOMER: {
    target: 'draft',
    },
    TRANSFER: 'transferred',
    },
    },
    authorizing: {
    on: {
    AUTHORIZED: 'finalizing'
    }
    },
    finalizing: {
    on: {
    COMPLETE: 'completed',
    }
    },
    error: {
    on: {
    RETRY: {
    target: 'draft',
    },
    TRANSFER: 'transferred',
    },
    },
    transferred: {
    type: 'final',
    },
    completed: {
    type: 'final',
    },
    },
    })

    const activityStates = {
    id: 'activity',
    initial: 'idle',
    states: {
    idle: {
    on: {
    CREATE_TRANSACTION: 'transaction'
    }
    },
    transaction: {
    invoke: {
    src: transactionMachine,
    onDone: 'idle',
    onError: 'idle',
    }
    }
    }
    }

    const socketMachine = Machine({
    id: 'socket',
    initial: 'disconnected',
    states: {
    disconnected: {
    on: {
    CONNECT: 'connected'
    }
    },
    connected: {
    ...activityStates,
    }
    }
    });