Skip to content

Instantly share code, notes, and snippets.

@korshunad
Last active April 30, 2021 00:40
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 korshunad/c1ff7b44260d55bf83c6eb4dbee2c493 to your computer and use it in GitHub Desktop.
Save korshunad/c1ff7b44260d55bf83c6eb4dbee2c493 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 FS = {
IDLE: 'idle',
COOLDOWN: 'cooldown',
ERROR: 'error',
NO_RETRY: 'no_retry',
CHECKING_MOMENT: 'checking_moment',
CHECK_ORDER_FOR_INVOCATION: 'check_order_for_invocation',
CANCELING_MOMENT_SALE: 'canceling_moment_sale',
POLLING_LISTING_ORDER_FOR_INVOCATION: 'polling_listing_order_for_invocation',
REDIRECTING_TO_DAPPER: 'redirecting_to_dapper',
SUCCESS: 'success',
}
const IGNORE_ERRORS_DURATION = 3000;
const delistMachine = Machine({
id: 'delistMoment',
initial: FS.IDLE,
states: {
[FS.IDLE]: {
initial: FS.IDLE,
states: {
[FS.IDLE]: {
id: 'idle',
},
[FS.ERROR]: { id: 'error' },
[FS.NO_RETRY]: { id: 'no_retry' },
},
on: {
DELIST_MOMENT: [
{ target: '#cooldown', cond: 'isCooldownEnabled' },
{ target: '#checking_moment' },
],
},
},
[FS.COOLDOWN]: {
id: 'cooldown',
on: {
CONFIRM: '#checking_moment',
DISMISS: '#idle',
},
},
[FS.CHECKING_MOMENT]: {
id: 'checking_moment',
invoke: {
src: 'checkIsMomentStillForSale',
onDone: {
target: '#check_order_for_invocation',
},
onError: [
{
cond: 'isNoRetryError',
target: '#no_retry',
actions: 'setError',
},
{
target: '#error',
actions: 'setError',
},
],
},
},
[FS.CHECK_ORDER_FOR_INVOCATION]: {
id: 'check_order_for_invocation',
invoke: {
src: 'checkListingOrder',
onDone: [
{
target: '#success',
cond: 'isDelisted',
},
{
target: '#canceling_moment_sale',
cond: 'isNoInvocationCreated',
},
{
target: '#redirecting_to_dapper',
cond: 'hasInvocation',
},
{
target: '#polling_listing_order_for_invocation',
cond: 'willCheckInvocation',
},
{
target: '#error',
actions: 'setError',
},
],
onError: {
target: '#error',
actions: 'setError',
},
},
},
[FS.SUCCESS]: {
id: 'success',
type: 'final',
},
[FS.CANCELING_MOMENT_SALE]: {
id: 'canceling_moment_sale',
invoke: {
src: 'placeDelistingOrder',
onDone: '#polling_listing_order_for_invocation',
onError: {
target: '#error',
actions: 'setError',
},
},
},
[FS.POLLING_LISTING_ORDER_FOR_INVOCATION]: {
id: 'polling_listing_order_for_invocation',
after: {
[IGNORE_ERRORS_DURATION]: {
actions: send('STOP', { to: 'pollOrderService' }),
},
},
invoke: {
id: 'pollOrderService',
src: 'pollOrder',
onError: {
target: '#error',
actions: 'setError',
},
},
on: {
POLL_ORDER_SUCCESS: [
{ target: '#success', cond: 'isDelisted' },
{ target: '#redirecting_to_dapper' },
],
POLL_ORDER_ERROR: [
{
target: '#error',
actions: 'setError',
},
],
},
},
[FS.REDIRECTING_TO_DAPPER]: {
id: 'redirecting_to_dapper',
type: 'final',
invoke: {
src: 'redirectToDelist',
},
},
},
}, {
guards: {
isCooldownEnabled: () => true,
isDelisted: () => false,
isNoInvocationCreated: () => true,
hasInvocation: () => false,
willHaveInvocation: () => false,
isNoRetryError: () => false,
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment