Skip to content

Commit

Permalink
Makes runtime wasm ready
Browse files Browse the repository at this point in the history
  • Loading branch information
codenikel committed Oct 26, 2023
1 parent 1d2b0d8 commit b53122e
Show file tree
Hide file tree
Showing 23 changed files with 91 additions and 213 deletions.
29 changes: 10 additions & 19 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ members = [
"crates/path",
"crates/runtime",
"crates/winit",
"crates/wasm",
"apps/web/src-tauri"
]
18 changes: 17 additions & 1 deletion crates/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
crate-type = ["cdylib", "rlib"]

[dependencies]
env_logger = "0.10"
wgpu = { version = "0.17", features = ["webgl"]}
Expand All @@ -19,4 +22,17 @@ radiant-core = { path = "../core" }
radiant-image-node = { path = "../image" }
radiant-text-node = { path = "../text" }
radiant-path-node = { path = "../path" }
radiant-winit = { path = "../winit" }
radiant-winit = { path = "../winit" }

[target.'cfg(target_arch = "wasm32")'.dependencies]
console_log = "1.0"
console_error_panic_hook = "0.1.6"
serde-wasm-bindgen = "0.4"
js-sys = "0.3.64"
wasm-bindgen = "0.2"
wasm-bindgen-futures = "0.4.30"
web-sys = { version = "0.3", features = [
"Document",
"Window",
"Element",
]}
3 changes: 3 additions & 0 deletions crates/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ pub use radiant_core::*;
pub use radiant_image_node::RadiantImageNode;
pub use radiant_path_node::RadiantPathNode;
pub use radiant_text_node::RadiantTextNode;

#[cfg(target_arch = "wasm32")]
pub mod wasm;
27 changes: 3 additions & 24 deletions crates/wasm/src/lib.rs → crates/runtime/src/wasm.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::sync::{Arc, RwLock};

use radiant_runtime::RadiantRuntime;
use radiant_runtime::{RadiantNodeType, RadiantRectangleNode, RadiantResponse};
use crate::RadiantRuntime;
use crate::{RadiantNodeType, RadiantRectangleNode, RadiantResponse};
use wasm_bindgen::prelude::*;
use winit::platform::web::EventLoopExtWebSys;

#[wasm_bindgen(js_name = RadiantAppController)]
pub struct RadiantAppController {
Expand Down Expand Up @@ -34,29 +33,9 @@ impl RadiantAppController {
[100.0, 100.0],
)));

let event_loop = std::mem::replace(&mut runtime.app.event_loop, None);

let runtime = Arc::new(RwLock::new(runtime));
let weak_runtime = Arc::downgrade(&runtime);
let f3 = f.clone();

if let Some(event_loop) = event_loop {
event_loop.spawn(move |event, _, control_flow| {
if let Some(runtime) = weak_runtime.upgrade() {
if let Ok(mut runtime) = runtime.write() {
if let Some(message) = runtime.app.handle_event(&event, control_flow) {
if let Some(response) = runtime.handle_message(message) {
let this = JsValue::null();
let _ = f3.call1(
&this,
&serde_wasm_bindgen::to_value(&response).unwrap(),
);
}
}
}
}
});
}
radiant_winit::run_wasm(runtime.clone(), f.clone());

Self { runtime }
}
Expand Down
123 changes: 0 additions & 123 deletions crates/wasm/Cargo.lock

This file was deleted.

29 changes: 0 additions & 29 deletions crates/wasm/Cargo.toml

This file was deleted.

5 changes: 4 additions & 1 deletion crates/winit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,7 @@ web-sys = { version = "0.3", features = [
"Document",
"Window",
"Element",
]}
]}
wasm-bindgen = "0.2"
serde-wasm-bindgen = "0.4"
js-sys = "0.3.64"
38 changes: 38 additions & 0 deletions crates/winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ use winit::{event::*, event_loop::ControlFlow};

pub use winit::event::Event::RedrawRequested;

#[cfg(target_arch = "wasm32")]
use std::sync::{Arc, RwLock};
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;
#[cfg(target_arch = "wasm32")]
use winit::platform::web::EventLoopExtWebSys;

pub struct RadiantApp<M, N: RadiantNode> {
pub window: Window,
pub event_loop: Option<EventLoop<()>>,
Expand Down Expand Up @@ -286,3 +293,34 @@ pub fn run_native<
});
}
}

#[cfg(target_arch = "wasm32")]
pub fn run_wasm<
M: From<InteractionMessage> + TryInto<InteractionMessage> + 'static,
N: RadiantNode + 'static,
R: serde::ser::Serialize,
>(
runtime: Arc<RwLock<impl Runtime<M, N, R> + 'static>>,
f: js_sys::Function,
) {
let Ok(mut runtime_) = runtime.write() else { return; };
let event_loop = std::mem::replace(&mut runtime_.app().event_loop, None);

let weak_runtime = Arc::downgrade(&runtime);

if let Some(event_loop) = event_loop {
event_loop.spawn(move |event, _, control_flow| {
if let Some(runtime) = weak_runtime.upgrade() {
if let Ok(mut runtime) = runtime.write() {
if let Some(message) = runtime.app().handle_event(&event, control_flow) {
if let Some(response) = runtime.handle_runtime_message(message) {
let this = JsValue::null();
let _ =
f.call1(&this, &serde_wasm_bindgen::to_value(&response).unwrap());
}
}
}
}
});
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"private": true,
"workspaces": [
"crates/wasm/pkg",
"crates/runtime/pkg",
"sdk/web",
"apps/web"
]
Expand Down
4 changes: 2 additions & 2 deletions sdk/web/lib/cjs/context/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sdk/web/lib/cjs/context/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b53122e

Please sign in to comment.