Last active
April 25, 2020 02:49
-
-
Save NullVoxPopuli/ae6c4b8e7d1c9b05a5510c5bf0303890 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
// Available variables: | |
// - Machine | |
// - interpret | |
// - assign | |
// - send | |
// - sendParent | |
// - spawn | |
// - raise | |
// - actions | |
// - XState (all XState exports) | |
const parseScannedData = assign((context, event) => { | |
let { data } = event; | |
let parsed = JSON.parse(data); | |
let isValid = Array.isArray(parsed); | |
if (!isValid) { | |
throw new Error('Malformed'); | |
} | |
return parsed; | |
}); | |
const fetchMachine = Machine({ | |
id: 'scan-qr-code', | |
strict: true, | |
initial: 'scanner', | |
context: { | |
intent: undefined, | |
data: undefined, | |
error: undefined, | |
parseError: undefined, | |
t: () => '', | |
}, | |
states: { | |
scanner: { | |
id: 'scanner', | |
initial: 'scanning', | |
onDone: [ | |
{ | |
target: '#loginToDevice', | |
cond: 'isQRLogin', | |
}, | |
{ | |
target: '#addFriend', | |
cond: 'isQRAddFriend', | |
}, | |
], | |
states: { | |
scanning: { | |
on: { | |
SCAN: 'parsing', | |
}, | |
}, | |
parsing: { | |
invoke: { | |
id: 'parse', | |
src: 'parseScannedData', | |
onDone: { | |
target: 'scanned', | |
actions: assign({ | |
intent: (_, event) => event.data[0], | |
data: (_, event) => event.data[1], | |
}), | |
}, | |
onError: '#error', | |
}, | |
on: { | |
PARSED: 'scanned', | |
}, | |
}, | |
scanned: { | |
type: 'final', | |
}, | |
}, | |
}, | |
loginToDevice: { | |
id: 'loginToDevice', | |
initial: 'checkLogin', | |
states: { | |
checkLogin: { | |
on: { | |
'': [ | |
{ | |
target: 'askPermission', | |
cond: 'isLoggedIn', | |
}, | |
{ target: '#error' }, | |
], | |
}, | |
}, | |
askPermission: { | |
on: { | |
DENY: '#error', | |
ALLOW: 'transferAllowed', | |
}, | |
}, | |
transferAllowed: { | |
exit: ['destroyConnection'], | |
invoke: { | |
id: 'transfer-data', | |
src: 'transferData', | |
onDone: 'transferComplete', | |
onError: '#scan-qr-code.error', | |
}, | |
}, | |
transferComplete: { | |
type: 'final', | |
}, | |
}, | |
}, | |
addFriend: { | |
id: 'addFriend', | |
initial: 'determineExistence', | |
// context: { | |
// exists: false, | |
// }, | |
states: { | |
determineExistence: { | |
entry: ['doesContactExist', send('HANDLE_EXISTENCE')], | |
on: { | |
HANDLE_EXISTENCE: [ | |
{ | |
target: 'contactExists', | |
cond: 'isContactKnown', | |
}, | |
{ | |
target: 'needToAddContact', | |
actions: ['addContact'], | |
}, | |
], | |
}, | |
}, | |
needToAddContact: {}, | |
contactExists: {}, | |
}, | |
}, | |
error: { id: 'error', on: { RETRY: '#scanner'}}, | |
}, | |
}, { | |
guards: { | |
//isLoggedIn: () => true, | |
//isQRLogin: () => true, | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment