Last active
January 5, 2021 17:54
-
-
Save 40thieves/2a4981135827a73fe8e2e19a6649c602 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", | |
initial: "idle", | |
context: { | |
messages: [] | |
}, | |
states: { | |
idle: { | |
on: { | |
fetch: "fetching", | |
receive: { | |
target: "idle", | |
actions: assign({ | |
messages: (context, event) => [...context.messages, 'new message'] | |
}) | |
} | |
} | |
}, | |
fetching: { | |
invoke: { | |
src: invokeFetchMessages, | |
onDone: { | |
target: "idle", | |
actions: assign({ | |
messages: (context, event) => [...event.data, ...context.messages] | |
}) | |
} | |
} | |
} | |
} | |
}); | |
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