Skip to content

Commit

Permalink
streamline worker helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
fl0rek committed Sep 30, 2024
1 parent 2fd3aeb commit 989501e
Showing 1 changed file with 6 additions and 25 deletions.
31 changes: 6 additions & 25 deletions node-wasm/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,46 +82,27 @@ pub(crate) fn js_value_from_display<D: fmt::Display>(value: D) -> JsValue {
}

trait WorkerSelf {
type GlobalScope;

fn worker_self() -> Self::GlobalScope;
fn is_worker_type() -> bool;
}

impl WorkerSelf for SharedWorker {
type GlobalScope = SharedWorkerGlobalScope;
type GlobalScope: JsCast;

fn worker_self() -> Self::GlobalScope {
JsValue::from(js_sys::global()).into()
js_sys::global().unchecked_into()
}

fn is_worker_type() -> bool {
js_sys::global().has_type::<Self::GlobalScope>()
}
}

impl WorkerSelf for SharedWorker {
type GlobalScope = SharedWorkerGlobalScope;
}

impl WorkerSelf for Worker {
type GlobalScope = DedicatedWorkerGlobalScope;

fn worker_self() -> Self::GlobalScope {
JsValue::from(js_sys::global()).into()
}

fn is_worker_type() -> bool {
js_sys::global().has_type::<Self::GlobalScope>()
}
}

impl WorkerSelf for ServiceWorker {
type GlobalScope = ServiceWorkerGlobalScope;

fn worker_self() -> Self::GlobalScope {
JsValue::from(js_sys::global()).into()
}

fn is_worker_type() -> bool {
js_sys::global().has_type::<Self::GlobalScope>()
}
}

/// This type is useful in cases where we want to deal with de/serialising `Result<T, E>`, with
Expand Down

0 comments on commit 989501e

Please sign in to comment.