Skip to content
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

[Question] Can I send an event to my statemachine from an action? #31

Open
joachimBurket opened this issue Nov 23, 2024 · 0 comments
Open

Comments

@joachimBurket
Copy link

joachimBurket commented Nov 23, 2024

Hi,

I'm trying to make a state machine that has a state which sends an Event from itself to the state machine. So this state is only doing an action, and then directly going to the next state.

Here is an example of what I would like to do:

enum Event {
    SongDownloaded,
}

struct MyStateMachine {
    player: MusicPlayer,
}

#[state_machine(
    initial = "State::idle()",
)]
impl MyStateMachine {
    pub fn new() -> Self {
        Self {
            player: MusicPlayer::new(),
        }
    }

    #[action]
    fn enter_download_song(&mut self) {
        // [ Do other actions here ]
        
        self.player.download_song();
        
        // TODO: Here I want to go to the next state when the song is downloaded.
        // self.handle(&Event::SongDownloaded);
    }
    #[state]
    fn download_song(event: &Event) -> Response<State> {
        match event {
            Event::SongDownloaded => Transition(State::playing())
            _ => Super,
        }
    }

   //  [ Other States ]
}

Is it possible to send an event from a state? Or maybe am I doing something not very conventional with the library / the states machine in general? 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant