No threads on wasm

This commit is contained in:
elvis
2025-10-31 16:35:49 +01:00
parent c0517f7e4a
commit ea05a8d99e
2 changed files with 89 additions and 43 deletions

View File

@ -2,7 +2,6 @@ use std::borrow::Cow;
use std::collections::HashMap; use std::collections::HashMap;
use std::hash::Hash; use std::hash::Hash;
use std::sync::{Arc, Mutex, RwLock}; use std::sync::{Arc, Mutex, RwLock};
use std::thread::JoinHandle;
use eframe::egui::text::LayoutJob; use eframe::egui::text::LayoutJob;
use eframe::egui::{self, Color32, TextFormat}; use eframe::egui::{self, Color32, TextFormat};
@ -1509,6 +1508,9 @@ type EditorState = GraphEditorState<
GlobalState, GlobalState,
>; >;
#[cfg(not(target_arch = "wasm32"))]
use std::thread::JoinHandle;
#[derive(Default)] #[derive(Default)]
pub struct AppHandle { pub struct AppHandle {
// The top-level object. "register" all custom types by specifying it as // The top-level object. "register" all custom types by specifying it as
@ -1523,6 +1525,7 @@ pub struct AppHandle {
cached_last_value: Option<LayoutJob>, cached_last_value: Option<LayoutJob>,
#[cfg(not(target_arch = "wasm32"))]
app_logic_thread: Option<JoinHandle<anyhow::Result<()>>>, app_logic_thread: Option<JoinHandle<anyhow::Result<()>>>,
} }
@ -1536,6 +1539,7 @@ const TRANSLATOR_KEY: &str = "egui_node_graph_translator";
const CACHE_KEY: &str = "egui_node_graph_cache"; const CACHE_KEY: &str = "egui_node_graph_cache";
#[cfg(feature = "persistence")] #[cfg(feature = "persistence")]
#[cfg(not(target_arch = "wasm32"))]
const VERSION_NUMBER: u64 = 1; const VERSION_NUMBER: u64 = 1;
#[cfg(feature = "persistence")] #[cfg(feature = "persistence")]
@ -1562,6 +1566,7 @@ impl AppHandle {
} }
#[cfg(feature = "persistence")] #[cfg(feature = "persistence")]
#[cfg(not(target_arch = "wasm32"))]
fn write_state( fn write_state(
state: &str, state: &str,
translator: &str, translator: &str,
@ -1587,6 +1592,7 @@ fn write_state(
} }
#[cfg(feature = "persistence")] #[cfg(feature = "persistence")]
#[cfg(not(target_arch = "wasm32"))]
fn read_state( fn read_state(
path: &std::path::PathBuf path: &std::path::PathBuf
) -> Result<(EditorState, rsprocess::translator::Translator, OutputsCache), String> { ) -> Result<(EditorState, rsprocess::translator::Translator, OutputsCache), String> {
@ -1870,8 +1876,10 @@ impl eframe::App for AppHandle {
if let Some(l_v) = &self.cached_last_value { if let Some(l_v) = &self.cached_last_value {
text = l_v.clone(); text = l_v.clone();
} else { } else {
#[cfg(not(target_arch = "wasm32"))] {
// wasm does not support threads :-(
// ------------------------------------------------------------- // -------------------------------------------------------------
// did we start a thread? // did we already start a thread?
if self.app_logic_thread.is_none() { if self.app_logic_thread.is_none() {
let thread_join_handle = { let thread_join_handle = {
let arc_state = Arc::clone(&self.user_state); let arc_state = Arc::clone(&self.user_state);
@ -1927,6 +1935,39 @@ impl eframe::App for AppHandle {
} }
} }
#[cfg(target_arch = "wasm32")] {
let err = create_output(
Arc::clone(&self.user_state),
self.state.graph.clone(),
&self.cache,
Arc::clone(&self.translator),
&ctx
);
if let Err(e) = err {
let text = get_layout(Err(e), &self.translator.lock().unwrap(), ctx);
self.cached_last_value = Some(text.clone());
} else if let Some(l_b_v) = self.cache.get_last_state() {
if let BasicValue::SaveString { path, value } = &l_b_v {
use std::io::Write;
let mut f = match std::fs::File::create(path) {
Ok(f) => f,
Err(e) => {
println!("Error creating file {path}: {e}");
return;
}
};
if let Err(e) = write!(f, "{}", value) {
println!("Error writing to file {path}: {e}");
return;
}
}
text = get_layout(Ok(l_b_v), &self.translator.lock().unwrap(), ctx);
self.cached_last_value = Some(text.clone());
}
spin = false;
}
}
let window = egui::SidePanel::right("Results").resizable(true); let window = egui::SidePanel::right("Results").resizable(true);
if spin { if spin {

View File

@ -2473,11 +2473,12 @@ fn process_template(
} }
}, },
| NodeInstruction::Sleep => { | NodeInstruction::Sleep => {
#[cfg(not(target_arch = "wasm32"))] {
let input_seconds = retrieve_from_cache![1]; let input_seconds = retrieve_from_cache![1];
let hash_inputs = hash_inputs!(input_seconds); let hash_inputs = hash_inputs!(input_seconds);
if let BasicValue::PositiveInt { value } = input_seconds { if let BasicValue::PositiveInt { value: _value } = input_seconds {
std::thread::sleep(std::time::Duration::from_secs(value as u64)); std::thread::sleep(std::time::Duration::from_secs(_value as u64));
set_cache_output!(( set_cache_output!((
output_names.first().unwrap(), output_names.first().unwrap(),
@ -2488,6 +2489,10 @@ fn process_template(
anyhow::bail!("Not an integer"); anyhow::bail!("Not an integer");
} }
} }
#[cfg(target_arch = "wasm32")] {
anyhow::bail!("Cannot sleep on wams");
}
}
} }
Ok(None) Ok(None)
} }