use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern "C" {
type This;
#[wasm_bindgen(method, getter, structural, js_name = self)]
fn self_(me: &This) -> Option<Scope>;
type Scope;
#[wasm_bindgen(method, getter, structural)]
fn constructor(me: &Scope) -> Constructor;
type Constructor;
#[wasm_bindgen(method, getter, structural)]
fn name(me: &Constructor) -> String;
}
pub fn detect() -> Runtime {
match js_sys::global().unchecked_into::<This>().self_() {
Some(scope) => match scope.constructor().name().as_str() {
"DedicatedWorkerGlobalScope" | "SharedWorkerGlobalScope" => Runtime::Worker,
_ => Runtime::Browser,
},
None => Runtime::Node,
}
}
pub enum Runtime {
Browser,
Node,
Worker,
}