Skip to content

Instantly share code, notes, and snippets.

@benediktarnold
Created October 5, 2021 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benediktarnold/3b65f4ca0db4c04dc8f659672bc4bb61 to your computer and use it in GitHub Desktop.
Save benediktarnold/3b65f4ca0db4c04dc8f659672bc4bb61 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 fetchStates = (key)=> ({
initial: 'idle',
states: {
idle: {
after: {
1: {
target: 'success',
cond: {
type: 'hasData',
key
}
},
2: {target: 'loading'}
}
},
loading: {
on: {
RESOLVE: {
target:'success',
actions: assign({[key]:(context, event) =>"foo"})
},
REJECT: 'failure'
}
},
success: {
type: 'final',
},
failure: {
on: {
RETRY: {
target: 'loading'
}
}
}
}
})
const fetchMachine = Machine({
id: 'fetch',
initial: 'a',
context: {
retries: 0
},
states: {
a: {
...fetchStates('a'),
on: {
B: 'b'
}
},
b: {
...fetchStates('b'),
on: {
A: 'a'
}
}
},
}, {
guards: {
hasData: (context, event, {cond}) => {
return context[cond.key] !== undefined;
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment