Skip to content

Instantly share code, notes, and snippets.

@lettertwo
Last active January 6, 2021 16:44
Show Gist options
  • Save lettertwo/388fc2c10c6112a88372299cd62f175a to your computer and use it in GitHub Desktop.
Save lettertwo/388fc2c10c6112a88372299cd62f175a 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 createAPIMachine = (name) => Machine({
id: name,
initial: 'idle',
context: {
retries: 0
},
states: {
idle: {
on: {
FETCH: 'pending.load',
UPDATE: 'pending.update',
CREATE: 'pending.create',
}
},
pending: {
states: {
previous: {
type: 'history',
target: 'load'
},
load: {
on: {
RESOLVE: `#${name}.success`,
REJECT: `#${name}.failure`
}
},
update: {
on: {
RESOLVE: `#${name}.success`,
REJECT: `#${name}.failure`
}
},
create: {
on: {
RESOLVE: `#${name}.success`,
REJECT: `#${name}.failure`
}
},
}
},
success: {
on: {
FETCH: 'pending.load',
UPDATE: 'pending.update',
CREATE: 'pending.create',
}
},
failure: {
on: {
RETRY: {
target: 'pending.previous',
actions: assign({
retries: (context, event) => context.retries + 1
})
}
}
}
}
});
const countryListMachine = createAPIMachine('CountryList')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment