Skip to content

Commit

Permalink
streamline worker helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
fl0rek committed Oct 1, 2024
1 parent 2fd3aeb commit c68d43c
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ you need to compile wasm node manually. Follow these additional steps:
cargo install wasm-pack

# compile lumina to wasm
wasm-pack build node-wasm
wasm-pack build --target web node-wasm

# install lumina-cli
cargo install --path cli --features browser-node
Expand Down
2 changes: 1 addition & 1 deletion node-wasm/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { NodeClient } from "lumina-node-wasm"
* Spawn a worker running lumina node and get the `NodeClient` connected to it.
*/
export async function spawnNode() {
let worker = new Worker(new URL("worker.js", import.meta.url));
let worker = new Worker(new URL("worker.js", import.meta.url), { type: "module" });
let client = await new NodeClient(worker);
return client;
}
Expand Down
2 changes: 1 addition & 1 deletion node-wasm/js/worker.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NodeWorker, NodeClient } from "lumina-node-wasm"
import { NodeWorker } from "lumina-node-wasm"

Error.stackTraceLimit = 99;

Expand Down
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 c68d43c

Please sign in to comment.