Last active
March 11, 2020 18:30
-
-
Save hosmelq/dd2aeeb5d477e2049334cd66f9201e40 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function buildVibrationPattern(seconds) { | |
| return new Array(seconds / 1500) | |
| .fill(null) | |
| .reduce(prev => [...prev, 0, 1000, 500], []) | |
| } | |
| const DEFAULT_WAIT_TIME = 30000 | |
| Machine( | |
| { | |
| id: `courier-state-and-jobs`, | |
| initial: `initializing`, | |
| context: { | |
| online: true, | |
| offer: { | |
| id: null, | |
| receivedAt: null | |
| } | |
| }, | |
| states: { | |
| initializing: { | |
| on: { | |
| '': [ | |
| { | |
| cond: `isOffline`, | |
| target: `offline` | |
| }, | |
| { | |
| cond: `isOnline`, | |
| target: `online` | |
| } | |
| ] | |
| } | |
| }, | |
| offline: { | |
| entry: `setOffline`, | |
| on: { | |
| GO_ONLINE: `goOnline` | |
| } | |
| }, | |
| goOnline: { | |
| invoke: { | |
| id: `goOnline`, | |
| src: `goOnline`, | |
| onDone: `online`, | |
| onError: `offline` | |
| } | |
| }, | |
| goOffline: { | |
| invoke: { | |
| id: `goOffline`, | |
| src: `goOffline`, | |
| onDone: `offline`, | |
| onError: `offline` | |
| } | |
| }, | |
| online: { | |
| entry: `setOnline`, | |
| initial: `waiting`, | |
| on: { | |
| GO_OFFLINE: `goOffline` | |
| }, | |
| states: { | |
| waiting: { | |
| on: { | |
| ORDER_RECEIVED: `fetchingOrder`, | |
| ORDER_ASSIGNED: { | |
| actions: `reloadOrderList` | |
| }, | |
| ORDER_OFFER_RECEIVED: { | |
| actions: `setOffer`, | |
| target: `offer` | |
| } | |
| } | |
| }, | |
| fetchingOrder: { | |
| invoke: { | |
| id: `fetchOrder`, | |
| src: `fetchOrder`, | |
| onDone: [ | |
| { | |
| cond: `isDeliveryOrder`, | |
| target: `offer` | |
| }, | |
| { | |
| cond: `isHiredOrder`, | |
| target: `assignment` | |
| } | |
| ], | |
| onError: `waiting` | |
| } | |
| }, | |
| assignment: {}, | |
| offer: { | |
| onDone: `waiting`, | |
| initial: `validating`, | |
| states: { | |
| validating: { | |
| invoke: { | |
| id: `validateOrder`, | |
| src: `validateOrder`, | |
| onDone: [ | |
| { | |
| cond: `hasEnoughTimeToAcceptOrder`, | |
| target: `waiting` | |
| }, | |
| { | |
| target: `done` | |
| } | |
| ], | |
| onError: `done` | |
| } | |
| }, | |
| waiting: { | |
| entry: `vibrate`, | |
| exit: `cancelVibration`, | |
| on: { | |
| ACCEPT_JOB: `accepting`, | |
| REJECT_JOB: `rejecting` | |
| }, | |
| after: { | |
| ORDER_DELAY: `rejecting` | |
| } | |
| }, | |
| accepting: { | |
| invoke: { | |
| id: `acceptOrder`, | |
| src: `acceptOrder`, | |
| onDone: `done`, | |
| onError: [ | |
| { | |
| target: `done`, | |
| actions: (ctx, event) => { | |
| console.log(`ctx: `, ctx) | |
| console.log(`event: `, event) | |
| } | |
| } | |
| ] | |
| } | |
| }, | |
| rejecting: { | |
| invoke: { | |
| id: `rejectOrder`, | |
| src: `rejectOrder`, | |
| onDone: `done`, | |
| onError: [ | |
| { | |
| target: `done`, | |
| actions: (ctx, event) => { | |
| console.log(`ctx: `, ctx) | |
| console.log(`event: `, event) | |
| } | |
| } | |
| ] | |
| } | |
| }, | |
| done: { | |
| entry: `resetOffer`, | |
| type: `final` | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| }, | |
| { | |
| actions: { | |
| setOffline: assign({ | |
| online: () => false, | |
| }), | |
| setOnline: assign({ | |
| online: () => true, | |
| }), | |
| vibrate: () => { | |
| console.log(`vibrate`) | |
| }, | |
| cancelVibration: () => { | |
| console.log(`cancelVibration`) | |
| }, | |
| setOfferId: assign({ | |
| offer: () => ({ | |
| acceptedAt: new Date(), | |
| id: 1, | |
| receivedAt: new Date(), | |
| }), | |
| }), | |
| resetOffer: assign({ | |
| offer: () => ({ | |
| acceptedAt: null, | |
| id: null, | |
| receivedAt: null, | |
| }), | |
| }), | |
| }, | |
| delays: { | |
| ORDER_DELAY: ctx => { | |
| if (!ctx.time) { | |
| return DEFAULT_WAIT_TIME | |
| } | |
| return Math.floor(ctx.time + DEFAULT_WAIT_TIME - Date.now()) | |
| }, | |
| }, | |
| guards: { | |
| isOffline: ctx => !ctx.online, | |
| isOnline: ctx => ctx.online, | |
| hasEnoughTimeToAcceptOrder: ({offer: {receivedAt}}) => { | |
| return Math.floor(receivedAt + DEFAULT_WAIT_TIME - Date.now()) > 2500 | |
| }, | |
| } | |
| } | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment