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,37 +1876,73 @@ 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"))] {
// did we start a thread? // wasm does not support threads :-(
if self.app_logic_thread.is_none() { // -------------------------------------------------------------
let thread_join_handle = { // did we already start a thread?
let arc_state = Arc::clone(&self.user_state); if self.app_logic_thread.is_none() {
let arc_translator = Arc::clone(&self.translator); let thread_join_handle = {
let ctx = ctx.clone(); let arc_state = Arc::clone(&self.user_state);
let graph = self.state.graph.clone(); let arc_translator = Arc::clone(&self.translator);
let cache = self.cache.clone(); let ctx = ctx.clone();
std::thread::spawn(move || { let graph = self.state.graph.clone();
create_output( let cache = self.cache.clone();
arc_state, std::thread::spawn(move || {
graph, create_output(
&cache, arc_state,
arc_translator, graph,
&ctx &cache,
) arc_translator,
}) &ctx
}; )
self.app_logic_thread = Some(thread_join_handle); })
};
self.app_logic_thread = Some(thread_join_handle);
}
if self.app_logic_thread.as_ref()
.map(|handle| handle.is_finished())
.unwrap_or(false)
{
let handle = std::mem::take(&mut self.app_logic_thread);
let err = handle.unwrap().join()
.expect("Could not join thread");
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());
}
} else {
spin = true;
}
} }
if self.app_logic_thread.as_ref() #[cfg(target_arch = "wasm32")] {
.map(|handle| handle.is_finished()) let err = create_output(
.unwrap_or(false) Arc::clone(&self.user_state),
{ self.state.graph.clone(),
let handle = std::mem::take(&mut self.app_logic_thread); &self.cache,
Arc::clone(&self.translator),
let err = handle.unwrap().join() &ctx
.expect("Could not join thread"); );
if let Err(e) = err { if let Err(e) = err {
let text = get_layout(Err(e), &self.translator.lock().unwrap(), ctx); let text = get_layout(Err(e), &self.translator.lock().unwrap(), ctx);
self.cached_last_value = Some(text.clone()); self.cached_last_value = Some(text.clone());
@ -1922,8 +1964,7 @@ impl eframe::App for AppHandle {
text = get_layout(Ok(l_b_v), &self.translator.lock().unwrap(), ctx); text = get_layout(Ok(l_b_v), &self.translator.lock().unwrap(), ctx);
self.cached_last_value = Some(text.clone()); self.cached_last_value = Some(text.clone());
} }
} else { spin = false;
spin = true;
} }
} }

View File

@ -2473,19 +2473,24 @@ fn process_template(
} }
}, },
| NodeInstruction::Sleep => { | NodeInstruction::Sleep => {
let input_seconds = retrieve_from_cache![1]; #[cfg(not(target_arch = "wasm32"))] {
let hash_inputs = hash_inputs!(input_seconds); let input_seconds = retrieve_from_cache![1];
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(),
input_seconds, input_seconds,
hash_inputs hash_inputs
)); ));
} else { } else {
anyhow::bail!("Not an integer"); anyhow::bail!("Not an integer");
}
}
#[cfg(target_arch = "wasm32")] {
anyhow::bail!("Cannot sleep on wams");
} }
} }
} }