Replies: 4 comments
-
Thanks so much for filing this @alavkx! I'm eager to watch this thread - I need to do some more research before I can chime in a meaningful way but this topic is is certainly worth an open discussion. Tagging @markerikson who might want to chime in or lurk. |
Beta Was this translation helpful? Give feedback.
-
Former Redux fanatic and functional programmer who absolutely loves to solve everything using function composition here. I haven't missed middle-ware so far. State machines manage both data and effects so any effect related middle like redux-saga or thunks became irrelevant. To log events and transitions I just use a wrapper around useMachine. Authentication state (so far the only state that I've had to use across machines and components) is shared using a push based stream. This is handy because streams can be converted to promises allowing me to either subscribe to the stream in the machine or even use them in promise chains when I make network calls. |
Beta Was this translation helpful? Give feedback.
-
Excellent question! What exactly are you trying to do with middleware in XState (e.g., logging, etc.)? Keep in mind that the main mechanism for how XState (and the actor model, in general) works is message passing (with events), so the only real opportunity to "do something" with an event is to intercept it before it reaches an invoked machine. For example, if you wanted to add logging before an event reaches a machine: const service = interpret(someMachine).start();
const sendWithLog = (event) => {
console.log(event);
service.send(event);
}
const wrappedService = {
subscribe: service.subscribe,
send: sendWithLog
}
wrappedService.send('HELLO');
// => { type: "HELLO" } Note that you can also use a reactive approach instead of using middleware, by adding observers: // Logs the event after it was sent to the service
service.subscribe(state => console.log(state.event)); But I'd like to know more of your use-cases. There are advanced use-cases in which machines themselves can be "middleware", and use |
Beta Was this translation helpful? Give feedback.
-
I agree that I'm in the process of plugging in an I also use middleware to normalize the raw data served from the api (ready-payloads for the reducers), for logging and augmenting the messaging capacity when things go wrong. - E |
Beta Was this translation helpful? Give feedback.
-
The middleware pattern solves a problem
I've been posed a question that boils down to—"How can I do middlewares in XState? I need middlewares."
How do you solve for "middlewares" in XState? Is this sort of logic re-use a non-problem? Do you compose? Does XState not attempt to solve for logging, crash reporting, etc..?
I've attached my answer below, but I'm curious of everyone else's thoughts❗ 🙏 💟
Beta Was this translation helpful? Give feedback.
All reactions