diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 768b1a0..ffb3671 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -10,15 +10,12 @@ use tauri::{Manager, State, Window}; // Structures for the notebook and cells use log::{debug, info}; - -use kernel_sidecar::client::Client; +use kernel_sidecar::{client::Client, handlers::SimpleOutputHandler}; use kernel_sidecar::handlers::{DebugHandler, Handler}; use kernel_sidecar::kernels::JupyterKernel; use kernel_sidecar::notebook::Notebook; - - // AppState that holds the mapping from Window to Notebook struct AppState { // Notebooks are just the document model, and could exist even if there's no underlying kernel @@ -56,7 +53,8 @@ impl AppState { } // Perform the cell execution within the AppState context - async fn execute_cell(&self, window_id: &str, cell_id: &str) -> bool { + async fn execute_cell(&self, window: Window, cell_id: &str) -> bool { + let window_id = window.label(); // Use the window label as a unique identifier debug!( "Executing cell with ID: {} in window with ID: {}", cell_id, window_id @@ -76,10 +74,16 @@ impl AppState { let source = cell.get_source(); let debug_handler = Arc::new(Mutex::new(DebugHandler::new())); - let handlers: Vec>> = vec![debug_handler.clone()]; + let output_handler = Arc::new(Mutex::new(SimpleOutputHandler::new())); + let handlers: Vec>> = + vec![debug_handler.clone(), output_handler.clone()]; - let action = kernel_client.execute_request(source, handlers); + let action = kernel_client.execute_request(source, handlers).await; action.await; + + let finished_output = &output_handler.lock().await.output; + + window.emit("output", finished_output.clone()).unwrap(); return true; } } @@ -130,8 +134,7 @@ async fn execute_cell( window: Window, cell_id: &str, ) -> Result { - let window_id = window.label(); // Use the window label as a unique identifier - Ok(state.execute_cell(window_id, cell_id).await) + Ok(state.execute_cell(window, cell_id).await) } #[tauri::command] diff --git a/src/main.tsx b/src/main.tsx index e83bf95..374b3b8 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -2,7 +2,13 @@ import React from "react"; import ReactDOM from "react-dom/client"; import App from "./App"; import "./index.css"; +import { emit, listen } from '@tauri-apps/api/event' +// listen to the `click` event and get a function to remove the event listener +// there's also a `once` function that subscribes to an event and automatically unsubscribes the listener on the first event +const unlisten = await listen('output', (event) => { + console.log(event) +}) ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(