Skip to content

Share state between events #247

Answered by yavko
Dregorio1 asked this question in Q&A
Discussion options

You must be logged in to vote

can I share state between events. Like I want to create in main function vector where I can store data from one async event and use it in another? Any code snippet will be welcome.

With the limited information you've provided, I think the best way to do this would be to use a static Mutex if using a AsyncEventListener, maybe like so. I haven't tested it, but it should compile.

use hyprland::event_listener::EventListener;
use std::sync::Mutex;

static TEST_THING: Mutex<Vec<String>> = Mutex::new(Vec::new());

#[tokio::main]
async fn main() -> hyprland::Result<()> {
    let mut listener = EventListener::new();
    listener.add_workspace_change_handler(move |workspace| {
        TEST_THING.l…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Dregorio1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants