mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-18 18:28:08 +08:00
Cleanup
This commit is contained in:
@@ -15,10 +15,7 @@ extern "C" {
|
||||
#[derive(Debug)]
|
||||
pub struct NodeRuntimeIO {
|
||||
// Send to
|
||||
#[cfg(any(not(feature = "tauri"), test))]
|
||||
sender: Sender<GraphRuntimeRequest>,
|
||||
#[cfg(all(feature = "tauri", not(test)))]
|
||||
sender: Sender<NodeGraphUpdate>,
|
||||
receiver: Receiver<NodeGraphUpdate>,
|
||||
}
|
||||
|
||||
@@ -31,25 +28,13 @@ impl Default for NodeRuntimeIO {
|
||||
impl NodeRuntimeIO {
|
||||
/// Creates a new NodeRuntimeIO instance
|
||||
pub fn new() -> Self {
|
||||
#[cfg(any(not(feature = "tauri"), test))]
|
||||
{
|
||||
let (response_sender, response_receiver) = std::sync::mpsc::channel();
|
||||
let (request_sender, request_receiver) = std::sync::mpsc::channel();
|
||||
futures::executor::block_on(replace_node_runtime(NodeRuntime::new(request_receiver, response_sender)));
|
||||
let (response_sender, response_receiver) = std::sync::mpsc::channel();
|
||||
let (request_sender, request_receiver) = std::sync::mpsc::channel();
|
||||
futures::executor::block_on(replace_node_runtime(NodeRuntime::new(request_receiver, response_sender)));
|
||||
|
||||
Self {
|
||||
sender: request_sender,
|
||||
receiver: response_receiver,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(feature = "tauri", not(test)))]
|
||||
{
|
||||
let (response_sender, response_receiver) = std::sync::mpsc::channel();
|
||||
Self {
|
||||
sender: response_sender,
|
||||
receiver: response_receiver,
|
||||
}
|
||||
Self {
|
||||
sender: request_sender,
|
||||
receiver: response_receiver,
|
||||
}
|
||||
}
|
||||
#[cfg(test)]
|
||||
@@ -59,44 +44,11 @@ impl NodeRuntimeIO {
|
||||
|
||||
/// Sends a message to the NodeRuntime
|
||||
pub fn send(&self, message: GraphRuntimeRequest) -> Result<(), String> {
|
||||
#[cfg(any(not(feature = "tauri"), test))]
|
||||
{
|
||||
self.sender.send(message).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[cfg(all(feature = "tauri", not(test)))]
|
||||
{
|
||||
let serialized = ron::to_string(&message).map_err(|e| e.to_string()).unwrap();
|
||||
wasm_bindgen_futures::spawn_local(async move {
|
||||
let js_message = create_message_object(&serialized);
|
||||
invoke("runtime_message", js_message).await;
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
self.sender.send(message).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// Receives any pending updates from the NodeRuntime
|
||||
pub fn receive(&self) -> impl Iterator<Item = NodeGraphUpdate> + use<'_> {
|
||||
// TODO: This introduces extra latency
|
||||
#[cfg(all(feature = "tauri", not(test)))]
|
||||
{
|
||||
let sender = self.sender.clone();
|
||||
// In the Tauri case, responses are handled separately via poll_node_runtime_updates
|
||||
wasm_bindgen_futures::spawn_local(async move {
|
||||
let messages = invoke_without_arg("poll_node_graph").await;
|
||||
let vec: Vec<_> = ron::from_str(&messages.as_string().unwrap()).unwrap();
|
||||
for message in vec {
|
||||
sender.send(message).unwrap();
|
||||
}
|
||||
});
|
||||
}
|
||||
self.receiver.try_iter()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(feature = "tauri", not(test)))]
|
||||
pub fn create_message_object(message: &str) -> JsValue {
|
||||
let obj = js_sys::Object::new();
|
||||
js_sys::Reflect::set(&obj, &JsValue::from_str("message"), &JsValue::from_str(message)).unwrap();
|
||||
obj.into()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user