Last active
March 20, 2020 13:51
-
-
Save revgum/eaabdce88087c41e8391818c92140ca3 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 userMessageStates = { | |
initial: 'idle', | |
states: { | |
idle: { | |
on: { | |
FETCH: 'getPreferences' | |
} | |
}, | |
getPreferences: { | |
on: { | |
RESOLVE: 'createMessage', | |
REJECT: 'failure' | |
} | |
}, | |
createMessage: { | |
on: { | |
RESOLVE: 'sendPendingMessages', | |
REJECT: 'failure' | |
} | |
}, | |
sendPendingMessages: { | |
on: { | |
RESOLVE: 'messageSent', | |
REJECT: 'failure' | |
} | |
}, | |
messageSent: { | |
type: 'final' | |
}, | |
failure: { | |
on: { | |
RETRY: { | |
target: 'getPreferences', | |
actions: assign({ | |
retries: (context, event) => context.retries + 1 | |
}) | |
} | |
} | |
} | |
} | |
}; | |
const messageStates = { | |
initial: 'idle', | |
states: { | |
idle: { | |
on: { | |
FETCH: 'getPendingMessages' | |
} | |
}, | |
getPendingMessages: { | |
on: { | |
PROCESS: 'getPopulation', | |
NONE_FOUND: 'finished', | |
FAILED: 'failure' | |
} | |
}, | |
getPopulation: { | |
on: { | |
PROCESS: 'createUserMessages', | |
NONE_FOUND: 'finished', | |
FAILED: 'failure' | |
} | |
}, | |
createUserMessages: { | |
on: { | |
CREATED: 'finished', | |
FAILED: 'failure' | |
} | |
}, | |
finished: { | |
type: 'final' | |
}, | |
failure: { | |
on: { | |
RETRY: { | |
target: 'getPendingMessages', | |
actions: assign({ | |
retries: (context, event) => context.retries + 1 | |
}) | |
} | |
} | |
} | |
} | |
} | |
const messageRequestStates = { | |
initial: 'idle', | |
states: { | |
idle: { | |
on: { | |
PERSIST: 'persisting' | |
} | |
}, | |
persisting: { | |
on: { | |
RESOLVE: 'finished', | |
REJECT: 'failure' | |
} | |
}, | |
finished: { | |
type: 'final' | |
}, | |
failure: { | |
on: { | |
RETRY: { | |
target: 'persisting', | |
actions: assign({ | |
retries: (context, event) => context.retries + 1 | |
}) | |
} | |
} | |
} | |
} | |
} | |
const messageMachine = Machine({ | |
id: 'messageEngine', | |
type: 'parallel', | |
initial: 'events.idle', | |
context: { | |
messageRequests: [] | |
}, | |
states: { | |
messageRequest: { | |
...messageRequestStates | |
}, | |
messages: { | |
...messageStates | |
}, | |
userMessages: { | |
...userMessageStates | |
} | |
}, | |
on: { | |
PROCESS_MESSAGE_REQUEST: 'messageRequest.idle', | |
PROCESS_MESSAGES: 'messages.idle', | |
PROCESS_USER_MESSAGES: 'userMessages.idle' | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment