I'm finally integrating xstate into my work project because I love it and I'm excited to show off what well-structured states can do for performance and reasoning, but I'm struggling with a bit of modeling. I have a weatherMachine
that gets the current temperature based on lat/long and is connected to a map in my app, so when the user moves it, the weather loads. I have that all working beautifully but if the user zooms out too much, I don't want to show weather, as it's not all that useful when you're viewing a large area. My modeling question is whether or not hiding the weather belongs in the weather machine. Part of me thinks it should just get weather when asked to but the point at which I dispatch the event to load weather (onMapRegionChange
) is also the place I'd check the zoom level and show/hide the weather. It'd be simplest to add some context to the weather machine for hideWeather
but is that "correct?" Any feedback on the approach you all would use would be greatly appreciated.
Asked
Active
Viewed 71 times
0

sunny-mittal
- 499
- 5
- 12
-
I think that's what you would use guard for; you can programmatically decide whether you transition to another state or not. – customcommander Jun 18 '22 at 06:49
-
@customcommander I don't think a guard would work in this instance because, while I could prevent an event dispatch from loading data for a particular lat/long, it won't do anything to hide weather that's already been loaded. I suppose my `on: { LOAD_WEATHER }` event could have multiple transitions with guards that go into a `loading` or `hiding` state but I dunno how much I like that either. I'm thinking just using a small piece of `hideWeather` context will do the trick. – sunny-mittal Jun 19 '22 at 01:17