-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MermaidJS to Superstate #1
Comments
Hey! This is fascinating! Can you tell me more about how this can be applied? I imagine this can be used to initialize a state chart, but then Superstate will lose all the type safety. If it's to generate code, I'm curious if there's a way to synchronize and define substrates separately from the root statechart? |
By the way, I plan to do the vice-versa translation out of the box, so you can get Mermaid code from any statechart, which is admittedly much easier. |
Yea, it was weird enough to add branching & "actions" to MermaidJS, & I still don't have context. + I'm quoting "actions" because there are no functions in MermaidJS, so I only allow to tag where the actions & filters get called. I expect folks have to hand-code actual code after conversion.
Well, I didn't think of OSM to Mermaid transcoding, but here is other example of the intervening pseudo-AST: Maybe there is a better way to deal with actions & guards better in the OSM? If there was a common AST-ish for all things StateMachine, then not only documentation would be easier for all StateMachines (eg SuperState -> Mermaid), but different SM implementations can be used in the same workflow. (Prior art for front-end development: Mitosis) |
I'd be happy working towards this goal. I have started a statechart schema based on XState here: https://github.com/statelyai/specification/blob/main/schemas/statechart.json |
@tomByrer I just shipped I don't think I have access to the repo (https://github.com/tomByrer/markdown2statemachine), so I can't check how close it is to what you got right now. Even though it could be beneficial for Superstate to render events as @davidkpiano I'm not sure if you plan to change it or expand it, but I would appreciate your input. I'm talking about those (plus guarded events
That's a fascinating idea. I'm unsure if @davidkpiano designed the spec to be universal, but we can at least use a subset of it. Here's how the Mermaid rendering works: import { superstate } from "superstate";
import { toMermaid } from "superstate/mermaid";
type VolumeState = "low" | "medium" | "high";
const volumeState = superstate<VolumeState>("volume")
.state("low", "up() -> medium")
.state("medium", ["up() -> high", "down() -> low"])
.state("high", "down() -> medium");
type PlayerState = "stopped" | "playing" | "paused";
const playerState = superstate<PlayerState>("player")
.state("stopped", "play() -> playing")
.state("playing", ["pause() -> paused", "stop() -> stopped"], ($) =>
$.sub("volume", volumeState)
)
.state("paused", ["play() -> playing", "stop() -> stopped"]);
const mermaid = toMermaid(playerState); And produces: %% Generated with Superstate
stateDiagram-v2
state "player" as player {
[*] --> player.stopped
player.stopped --> player.playing : play
player.playing --> player.paused : pause
player.playing --> player.stopped : stop
player.paused --> player.playing : play
player.paused --> player.stopped : stop
state "stopped" as player.stopped
state "playing" as player.playing {
[*] --> player.playing.low
player.playing.low --> player.playing.medium : up
player.playing.medium --> player.playing.high : up
player.playing.medium --> player.playing.low : down
player.playing.high --> player.playing.medium : down
state "low" as player.playing.low
state "medium" as player.playing.medium
state "high" as player.playing.high
}
state "paused" as player.paused
}
Mermaid code:
|
Yes, I intend for the specification to be universal 👍 |
FYI I might make another target to translate Mermaid JS to Superstate.
I'm already 1/2 way there; since I knew there would be multiple types of sources & targets for statecharts, I made an intervening output (sort of simple AST), so the only step is to go from that Object notation to Superstate.
EG:
source:
to OSM:
Which in tern is used to make a XState machine
The text was updated successfully, but these errors were encountered: