Skip to content

Instantly share code, notes, and snippets.

@jonathanstiansen
Last active December 9, 2019 17:51
Show Gist options
  • Save jonathanstiansen/b5e1e0d3576620a6ac114a3cac56b223 to your computer and use it in GitHub Desktop.
Save jonathanstiansen/b5e1e0d3576620a6ac114a3cac56b223 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const fetchMachine = Machine({
id: 'permissions',
initial: 'checkLocalPerms',
context: {
remotePermission: 'undetermined'
},
states: {
checkLocalPerms: {
on: {
GRANTED: {
target: 'checkRemote',
actions: 'assignEventType'
},
UNDETERMINED: {target: 'primer',
actions: 'assignEventType'},
REJECTED: {target: 'checkRemote',
actions: 'assignEventType'}
},
},
checkRemote: {
on: {
matches: 'noop',
differs: 'updateServer'
}
},
primer: {
on: {
accept: 'askPermission',
notYet: 'updateServer'
},
exit: ['updatePrimerResponse']
},
askPermission: {
on: {
whenUsing: 'updateServer',
justOnce: 'noop',
reject: 'updateServer'
}
},
noop: {
type: 'final'
},
updateServer: {
type: 'final',
entry: ['updateServer']
},
}
},{
actions: {
assignEventType : assign({
remotePermission: (_, event) => event.type
}),
// action implementations
updateServer: (context, event) => {
console.log('sending status...' + context.remotePermission);
},
updatePrimerResponse: (context, event) => {
console.log('sending event...' , event.type)
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment