Last active
January 6, 2021 16:44
-
-
Save lettertwo/388fc2c10c6112a88372299cd62f175a 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 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