Skip to content

Instantly share code, notes, and snippets.

@samselikoff
Last active May 19, 2020 14:13
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 samselikoff/5e58cf14fa9f11a9098672af19e58afc to your computer and use it in GitHub Desktop.
Save samselikoff/5e58cf14fa9f11a9098672af19e58afc to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const inspectorMachine = Machine(
{
id: "inspector",
context: {
iframeRef: null,
error: null,
errorHandlingRequest: null,
db: {},
},
initial: "loading",
states: {
loading: {
on: {
SUCCESS: "ready",
ERROR: {
target: "error",
actions: assign({
error: (context, event) => event.message,
}),
},
},
},
error: {
on: {
CONFIG_CHANGE: "loading",
},
},
ready: {
entry: ["clearError", "updateDatabase"],
on: {
CONFIG_CHANGE: "loading",
},
initial: "idle",
states: {
idle: {
on: {
REQUEST: "pendingRequest",
},
},
pendingRequest: {
entry: "makeRequest",
on: {
RESPONSE: {
target: "handledRequest",
actions: ["updateResponse", "updateDatabase"],
},
ERROR: {
target: "failedRequest",
actions: assign({
errorHandlingRequest: (context, event) => event.message,
}),
},
},
},
handledRequest: {
on: {
REQUEST: "pendingRequest",
},
},
failedRequest: {
on: {
REQUEST: "pendingRequest",
},
},
},
},
},
},
{
actions: {
makeRequest(context, { method, url, body }) {
context.iframeRef.current.contentWindow.postMessage(
{
fromInspector: true,
type: "mirage:request",
message: {
method,
url,
body: body ? JSON.stringify(body) : undefined,
},
},
"*"
)
},
updateResponse: assign({
response: (context, event) => event.response,
}),
updateDatabase: assign({
db: (context, event) => event.db,
}),
clearError: assign({
error: null,
}),
log(context, event) {
console.log({ context }, { event })
},
},
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment