use js_sys::Promise;
use wasm_bindgen::prelude::*;
use web_sys::{Request, window};
#[derive(Debug)]
pub(crate) enum WebContext {
Window(web_sys::Window),
Worker(web_sys::WorkerGlobalScope),
}
impl WebContext {
pub(crate) fn new() -> Option<Self> {
match window() {
Some(window) => Some(Self::Window(window)),
None => {
#[wasm_bindgen]
extern "C" {
type Global;
#[wasm_bindgen(method, getter, js_name = WorkerGlobalScope)]
fn worker(this: &Global) -> JsValue;
}
let global: Global = js_sys::global().unchecked_into();
if !global.worker().is_undefined() {
Some(Self::Worker(global.unchecked_into()))
} else {
None
}
}
}
}
pub(crate) fn fetch_with_request(&self, request: &Request) -> Promise {
match self {
WebContext::Window(w) => w.fetch_with_request(request),
WebContext::Worker(w) => w.fetch_with_request(request),
}
}
}