Discussion moved from Issue of yed2kingly #10
Replies: 2 comments 2 replies
-
I am actually going to bed, but as you may guess from Kingly I stay away from all those spawned machine and what not, I have never felt the need for them. Anyways, I think you can do what you want simply by setting the SHARE_WAS_SUCCESSFUL event high enough in the state hierarchy. Better than words look at the chess game example, with the timer. You should be able to use the same technique used to implement the timer (so the clock ticked event here would be your SHARE_WAS_SUCCESSFUL event). It involves setting a compound state at the high-enough level, do something then return to whereever you were with a history state. So, Cf. https://brucou.github.io/documentation/v1/tutorials/chess-game-ultimate.html forgot to say that the losange in the graphical representation is used for transient states. So in this graph, it means that when a tick event occurs, it goes to that transient state, executes some action, and then immediately transitions to the history state, that is, where it were before prior to the tick event. alright I am in bed now. I could be wrong, I haven't given it too much thought |
Beta Was this translation helpful? Give feedback.
-
So here is a quick draft illustrating the technique that uses the history state. So App is whatever logic your application is implementing. It can have any number of states, here there is none, as I don't know what else the app is doing and it does not really matter anyways. Hopefully it is self-explanatory. The empty small yellow square that you see is simply to aggregate the arrows as all of them have equal destination. Run the events to see if the logic represented here is correct. Then if it is correct you can implement it with any state machine library that has compound states and history states. There is an alternative design which is to use parallel states. Kingly does not have parallel states, however you can simply create two Kingly machines and pass them the same inputs. Inputs that are not accepted by a machine will simply be ignored. It is simple enough, you have two concurrent machines receiving the same inputs all the time. Or you can use parallel states with XState. In this design, your share modal logic is implemented by a second state machine. The first machine (App) and second (share modal) are gathered into a single machine (don't have a graph there): =====
|
Beta Was this translation helpful? Give feedback.
-
Hi again @brucou , I figured this was a more appropriate place for the discussion we were having, rather than at the bottom of a closed issue in a different repository, lol.
If you have the time I would like to hear how you would implement a specific type of behavior with Kingly:
This is not hypothetical, it is something I am trying to figure out how to implement with statecharts, and whatever architecture or library I end up following needs to be able to handle this use case fairly well...
I'm finding these sorts of architectural best practices for more complex use-cases are hard to glean from the docs from what I've read so far.
Scenario:
Imagine you have a 'share' functionality for a part of your app, which would open up the traditional mobile-style 'share' interface (allows you to share the current page, for example, with instagram, youtube, by email, or just as a link). When the user chooses their method of sharing, and clicks the action button, a request is sent off to some server (perhaps to instagram, for example). You would like to notify the user (maybe with a small popup) when that server responds, to tell them if their share was successful or failed for some reason, but you don't want the entire application to have to wait for that response. So while the response is still pending the user can perform more actions, such as closing the 'share' interface. Or they could send out another 'share' request (either to instagram again, or to somewhere else). And you also want to notify the user in the same way when these other share requests return, without, obviously, preventing the notification from the first request.
My First-Attempt Solution:
Here is how I first thought to implement this, when I was trying XState:
From the main app state-machine I spawn a new 'ShareMachine' which encapsulates the 'share' UI. Then when the user clicks to send the 'share' I spawn a 'ShareRequestResponseMachine' which starts in 'pending' state and ends in either 'success' or 'failure' (sending a notification command to the view layer for each of those final states). Every time the user clicks 'share' I spawn a new 'ShareRequestResponseMachine' to handle that request. Since in XState spawned machines are linked to the parent machine that spawned them, I add something like a 'DoneWithResponsesPending' state to the parent 'ShareMachine' which can tell the view layer to close the 'share' UI, but still allows the 'ShareRequestResponseMachine's to do their thing, and then once they have all finished, then I stop the 'ShareMachine'.
I haven't had time yet to dive into the Kingly API, so I don't have a description for how I would think to solve this using Kingly-specific concepts, unfortunately.
Beta Was this translation helpful? Give feedback.
All reactions