Skip to content

How to bind window global object? #3238

Answered by Liamolucko
nikolajevs86 asked this question in Q&A
Discussion options

You must be logged in to vote

It's possible to get it to work the way you're trying to do it, getting the SDK from js_sys::global and then casting it, but I'd recommend doing it this way instead.

You can import methods from SDK by adding js_namespace = SDK to a regular function import:

use wasm_bindgen::prelude::*;

#[wasm_bindgen]
extern "C" {
    #[wasm_bindgen(js_namespace = SDK)]
    fn start();
}

pub fn execute() {
    if js_sys::Reflect::has(&js_sys::global(), &JsValue::from_str("SDK")).unwrap() {
        log!("SDK exists");

        start();
    } else {
        log!("SDK does not exists");
    }
}

Calling start will run SDK.start() on the JS side.

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@nikolajevs86
Comment options

@Liamolucko
Comment options

Answer selected by nikolajevs86
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