Last active
January 6, 2021 09:35
-
-
Save 40thieves/23aa888f7527721be5d138d8446124a7 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
const chatMachine = Machine({ | |
id: "chat", | |
type: "parallel", | |
context: { | |
messages: [] | |
}, | |
states: { | |
http: { | |
initial: "idle", | |
states: { | |
idle: { | |
on: { | |
fetch: "fetching" | |
} | |
}, | |
fetching: { | |
invoke: { | |
src: invokeFetchMessages, | |
onDone: { | |
target: "idle", | |
actions: assign({ | |
messages: (context, event) => { | |
return [...event.data, ...context.messages]; | |
} | |
}) | |
} | |
} | |
} | |
} | |
}, | |
socket: { | |
initial: "idle", | |
states: { | |
idle: { | |
on: { | |
receive: { | |
target: "idle", | |
actions: assign({ | |
messages: (context, event) => [ | |
...context.messages, | |
"new message" | |
] | |
}) | |
} | |
} | |
} | |
} | |
} | |
} | |
}); | |
function invokeFetchMessages() { | |
return new Promise((resolve) => { | |
setTimeout(() => { | |
resolve(["foo", "bar"]); | |
}, 5000); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment