Last active
April 29, 2021 15:31
-
-
Save NoriSte/9536a07f719d4ad291cdf5fc0e3d17d2 to your computer and use it in GitHub Desktop.
RM V2' tracking app
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
const setOnline = assign({ | |
offline: (context, event) => (context.offline = false), | |
}) | |
const setOffline = assign({ | |
offline: (context, event) => (context.offline = true), | |
}) | |
const setLastUpdate = assign({ | |
lastUpdate: (context, event) => (context.lastUpdate = Date.now()), | |
}) | |
const setStatusOk = assign({ | |
status: (context, event) => (context.status = 'ok'), | |
}) | |
const startPollingAfterError = (context, event) => { | |
// if the first load failed, the poll won't start | |
return context.lastUpdate !== 0 | |
} | |
const wasConnected = (context, event) => { | |
return context.lastUpdate !== 0 | |
} | |
const neverConnected = (context, event) => { | |
return context.lastUpdate === 0 | |
} | |
// Available variables: | |
// - Machine | |
// - interpret | |
// - assign | |
// - send | |
// - sendParent | |
// - spawn | |
// - raise | |
// - actions | |
// - XState (all XState exports) | |
const fetchMachine = Machine( | |
{ | |
id: 'Current Tracking App', | |
initial: 'UIPreloadImage', | |
context: { | |
offline: false, | |
lastUpdate: 0, | |
status: 'loading', | |
}, | |
states: { | |
UIPreloadImage: { | |
// starts loading localizations immediately | |
after: { 0: 'loadIntl' }, | |
}, | |
// LOADING INTL | |
loadIntl: { | |
// TODO: speed up machine debugging, then remove it. This transition happens programmatically | |
after: { 500: 'order' }, | |
on: { | |
SUCCESS: 'order', | |
SUCCESS_EMPTY_MESSAGES: 'order.UIOffline', | |
ERROR: 'order.UIOffline', | |
}, | |
}, | |
// LOADING ORDER | |
order: { | |
initial: 'loadOrderData', | |
states: { | |
loadOrderData: { | |
on: { | |
SUCCESS: { | |
target: 'UIShowOrderData', | |
actions: ['setOnline', 'setLastUpdate', 'setStatusOk'], | |
}, | |
SUCCESS_WITH_ERROR: { target: 'serverError' }, | |
ERROR: { target: 'UIOffline', actions: 'setOffline' }, | |
}, | |
}, | |
serverError: { | |
on: { | |
ORDER_NOT_FOUND: 'UIOrderDeleted', | |
TERRITORY_NOT_FOUND: 'UIDeadLink', | |
TOKEN_NOT_FOUND: 'UIDeadLink', | |
TOKEN_NOT_VALID: 'UIDeadLink', | |
TRACKING_BODY_NOT_VALID: 'UIDeadLink', | |
DEFAULT: [ | |
{ | |
target: 'UILostConnection', | |
cond: wasConnected, | |
}, | |
{ | |
target: 'UIOffline', | |
cond: neverConnected, | |
}, | |
], | |
}, | |
}, | |
// UI STATES WITH POLLING | |
UIShowOrderData: { | |
// TODO: long delay polling | |
after: { 1000: 'loadOrderData' }, | |
}, | |
UIOffline: { | |
type: 'final', | |
// start fast polling to exit the offline mode | |
after: { 100: { target: 'loadOrderData', cond: startPollingAfterError } }, | |
}, | |
// FINAL UI STATES | |
UIDeadLink: { type: 'final' }, | |
UIOrderDeleted: { type: 'final' }, | |
UILostConnection: { type: 'final' }, | |
}, | |
// ------------------- | |
}, | |
}, | |
}, | |
{ | |
actions: { setOnline, setOffline, setLastUpdate, setStatusOk }, | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment