Created
January 5, 2021 17:59
-
-
Save 40thieves/041033922b9c0efa80c7e6d09ad6aa38 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: { | |
on: { | |
receive: { | |
target: "fetching", | |
actions: assign({ | |
messages: (context, event) => [...context.messages, 'new message'] | |
}) | |
} | |
}, | |
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