Skip to content

WASM | Returning struct to js that interacts with a window? #2061

Answered by stevenhuyn
sotrh asked this question in Q&A
Discussion options

You must be logged in to vote

Ok I found the sauce: from a search of event_loop.spawn( in the whole of public github source code.

Make use of CustomEvents

Because EventLoopProxy does not implement Send,Sync work around is thread_local! from my understanding.

#[derive(Debug, Clone, Copy)]
pub enum CustomEvent {
    Foo,
}

thread_local! {
    pub static EVENT_LOOP_PROXY: Mutex<Option<EventLoopProxy<CustomEvent>>> = Mutex::new(None);
}

#[wasm_bindgen(js_name = "fireFoo")]
pub fn fire_foo() {
    EVENT_LOOP_PROXY.with(|proxy| {
        if let Some(event_loop_proxy) = &*proxy.lock().unwrap() {
            event_loop_proxy.send_event(CustomEvent::Foo).ok();
        }
    });
}

#[wasm_bindgen]
pub fn run() {
    console_log

Replies: 4 comments 5 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@ALPHAAAL
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
4 replies
@daxpedda
Comment options

@daxpedda
Comment options

@stevenhuyn
Comment options

@llvm-x86
Comment options

Answer selected by sotrh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
6 participants