mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-21 04:18:12 +08:00
Make the dynamic node graph execution asynchronous (#1218)
* Make node graph execution async Make node macro generate async node implementations Start propagating async through the node system Async checkpoint Make Any<'i> Send + Sync Determine node io type using panic node Fix types for raster_node macro Finish porting node registry? Fix lifetime errors Remove Send + Sync requirements and start making node construction async Async MVP Fix tests Clippy fix * Fix nodes * Simplify lifetims for node macro + make node macro more modular * Reenable more nodes * Fix pasting images * Remove http test from brush node * Fix output type for cache node * Fix types for let scope * Fix formatting
This commit is contained in:
committed by
Keavon Chambers
parent
5c7211cb30
commit
4bd9fbd073
@@ -12,12 +12,15 @@ use axum::routing::get;
|
||||
use axum::Router;
|
||||
use fern::colors::{Color, ColoredLevelConfig};
|
||||
use http::{Response, StatusCode};
|
||||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
|
||||
static IMAGES: Mutex<Option<HashMap<String, FrontendImageData>>> = Mutex::new(None);
|
||||
static EDITOR: Mutex<Option<Editor>> = Mutex::new(None);
|
||||
thread_local! {
|
||||
static EDITOR: RefCell<Option<Editor>> = RefCell::new(None);
|
||||
}
|
||||
|
||||
async fn respond_to(id: Path<String>) -> impl IntoResponse {
|
||||
let builder = Response::builder().header("Access-Control-Allow-Origin", "*").status(StatusCode::OK);
|
||||
@@ -55,7 +58,7 @@ async fn main() {
|
||||
|
||||
*(IMAGES.lock().unwrap()) = Some(HashMap::new());
|
||||
graphite_editor::application::set_uuid_seed(0);
|
||||
*(EDITOR.lock().unwrap()) = Some(Editor::new());
|
||||
EDITOR.with(|editor| editor.borrow_mut().replace(Editor::new()));
|
||||
let app = Router::new().route("/", get(|| async { "Hello, World!" })).route("/image/:id", get(respond_to));
|
||||
|
||||
// run it with hyper on localhost:3000
|
||||
@@ -84,9 +87,10 @@ fn handle_message(message: String) -> String {
|
||||
let Ok(message) = ron::from_str::<graphite_editor::messages::message::Message>(&message) else {
|
||||
panic!("Error parsing message: {}", message)
|
||||
};
|
||||
let mut guard = EDITOR.lock().unwrap();
|
||||
let editor = (*guard).as_mut().unwrap();
|
||||
let responses = editor.handle_message(message);
|
||||
let responses = EDITOR.with(|editor| {
|
||||
let mut editor = editor.borrow_mut();
|
||||
editor.as_mut().unwrap().handle_message(message)
|
||||
});
|
||||
|
||||
// Sends a FrontendMessage to JavaScript
|
||||
fn send_frontend_message_to_js(message: FrontendMessage) -> FrontendMessage {
|
||||
|
||||
@@ -59,16 +59,17 @@ fn window() -> web_sys::Window {
|
||||
}
|
||||
|
||||
fn request_animation_frame(f: &Closure<dyn FnMut()>) {
|
||||
//window().request_idle_callback(f.as_ref().unchecked_ref()).unwrap();
|
||||
window().request_animation_frame(f.as_ref().unchecked_ref()).expect("should register `requestAnimationFrame` OK");
|
||||
}
|
||||
|
||||
// Sends a message to the dispatcher in the Editor Backend
|
||||
fn poll_node_graph_evaluation() {
|
||||
async fn poll_node_graph_evaluation() {
|
||||
// Process no further messages after a crash to avoid spamming the console
|
||||
if EDITOR_HAS_CRASHED.load(Ordering::SeqCst) {
|
||||
return;
|
||||
}
|
||||
editor::node_graph_executor::run_node_graph();
|
||||
editor::node_graph_executor::run_node_graph().await;
|
||||
|
||||
// Get the editor instances, dispatch the message, and store the `FrontendMessage` queue response
|
||||
EDITOR_INSTANCES.with(|instances| {
|
||||
@@ -218,7 +219,7 @@ impl JsEditorHandle {
|
||||
let g = f.clone();
|
||||
|
||||
*g.borrow_mut() = Some(Closure::new(move || {
|
||||
poll_node_graph_evaluation();
|
||||
wasm_bindgen_futures::spawn_local(poll_node_graph_evaluation());
|
||||
|
||||
// Schedule ourself for another requestAnimationFrame callback.
|
||||
request_animation_frame(f.borrow().as_ref().unwrap());
|
||||
@@ -788,7 +789,7 @@ impl JsEditorHandle {
|
||||
Some(message_data)
|
||||
})
|
||||
});
|
||||
frontend_messages.unwrap().unwrap_or_default().into()
|
||||
frontend_messages.unwrap().unwrap_or_default()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user