Skip to content

Instantly share code, notes, and snippets.

@NullVoxPopuli
Created April 16, 2020 01:12
Show Gist options
  • Save NullVoxPopuli/76aa67eadc0d8536e1f4fdce223e5748 to your computer and use it in GitHub Desktop.
Save NullVoxPopuli/76aa67eadc0d8536e1f4fdce223e5748 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const parseScannedData = assign((context, event) => {
let { json } = event;
let isValid = Array.isArray(json);
if (!isValid) {
raise('SCAN_ERROR');
return context;
}
return json;
});
const foo = Machine({
id: 'scan-qr-code',
strict: true,
initial: 'idle',
context: {
intent: undefined,
data: undefined,
},
states: {
idle: {
on: {
SCAN: {
target: 'scanned',
},
},
},
scanned: {
invoke: {
id: 'parse',
src: parseScannedData,
onError: {
target: 'scanError',
},
onDone: {
target: 'loginRequest',
actions: (context, event) => {
return {
...context,
intent: event.data[0],
data: event.data[1],
};
},
},
},
on: {
SCAN_ERROR: {},
},
},
scanError: {
entry: () => console.log('entered scanError'),
},
loginRequest: {
entry: () => console.log('entered login request'),
},
addFriend: {},
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment