From f25e9d628622f04ed3b5ba747b78ae9ad1278c9a Mon Sep 17 00:00:00 2001 From: Automated Release Test Date: Tue, 5 Mar 2024 20:42:25 -0500 Subject: [PATCH] moves log state up to App so it persists across route changes, moves wait during setup for webview to start to the async runtime so it doesn't block the setup fn from starting the webview. --- src-tauri/src/child_process.rs | 12 +++++++- src-tauri/src/main.rs | 7 ++--- src/App.tsx | 56 +++++++++++++++++++++++++++++++++- src/pages/Logs.tsx | 55 ++------------------------------- 4 files changed, 70 insertions(+), 60 deletions(-) diff --git a/src-tauri/src/child_process.rs b/src-tauri/src/child_process.rs index c1aed56..349d9bc 100644 --- a/src-tauri/src/child_process.rs +++ b/src-tauri/src/child_process.rs @@ -4,6 +4,7 @@ use std::{ io::{BufRead, BufReader}, path::PathBuf, process::{Child, Command, Stdio}, + time::Duration, }; use tauri::{utils, AppHandle, Manager}; @@ -94,8 +95,17 @@ pub fn run_zebrad() -> (Child, Receiver) { (zebrad_child, output_receiver) } -pub fn spawn_logs_emitter(mut output_receiver: Receiver, app_handle: AppHandle) { +pub fn spawn_logs_emitter( + mut output_receiver: Receiver, + app_handle: AppHandle, + should_wait_for_webview: bool, +) { tauri::async_runtime::spawn(async move { + // Wait for webview to start + if should_wait_for_webview { + tokio::time::sleep(Duration::from_secs(3)).await; + } + // Exit the task once the channel is closed and empty. while let Some(output) = output_receiver.recv().await { if let Err(error) = app_handle.emit("log", output) { diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 85740c4..0a9eb6d 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -37,7 +37,7 @@ async fn save_config(app_handle: AppHandle, new_config: String) -> Result() .insert_zebrad_child(zebrad_child); - spawn_logs_emitter(zebrad_output_receiver, app_handle); + spawn_logs_emitter(zebrad_output_receiver, app_handle, false); Ok(old_config_contents) } @@ -54,10 +54,7 @@ fn main() { tauri::Builder::default() .manage(AppState::new(zebrad_child)) .setup(|app| { - // Wait for webview to start - std::thread::sleep(Duration::from_secs(3)); - - spawn_logs_emitter(zebrad_output_receiver, app.handle().clone()); + spawn_logs_emitter(zebrad_output_receiver, app.handle().clone(), true); Ok(()) }) .invoke_handler(tauri::generate_handler![save_config, read_config]) diff --git a/src/App.tsx b/src/App.tsx index c1f8698..b2f8314 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,6 +1,13 @@ import { Router, Route, RouteSectionProps, A, useMatch } from "@solidjs/router"; import { css, styled } from "solid-styled-components"; +import { listen, Event, UnlistenFn } from "@tauri-apps/api/event"; + +import { createSignal, onCleanup, onMount } from "solid-js"; + +import { EXAMPLE_LOGS } from "./tests/example_data"; +import { MAX_NUM_LOG_LINES } from "./constants"; + import { NAVIGATION_BAR_HEIGHT } from "./constants"; import Logs from "./pages/Logs"; import Configuration from "./pages/Configure"; @@ -68,9 +75,56 @@ const AppContainer = ({ children }: RouteSectionProps) => ( ); function App() { + const is_tauri_app = window.hasOwnProperty("__TAURI_INTERNALS__"); + const [logs, set_logs] = createSignal>([]); + + const is_at_bottom = () => { + const y_bottom = Math.ceil(window.scrollY) + window.innerHeight; + return y_bottom >= document.body.scrollHeight; + }; + + const scroll_to_bottom = () => { + window.scroll(0, document.body.scrollHeight); + }; + + if (is_tauri_app) { + let stop_listening: UnlistenFn; + + onMount(async () => { + stop_listening = await listen("log", (event: Event) => { + const was_at_bottom = is_at_bottom(); + + set_logs([...logs().slice(-MAX_NUM_LOG_LINES), event.payload]); + + if (was_at_bottom) { + scroll_to_bottom(); + } + }); + }); + + onCleanup(() => stop_listening()); + } else { + let example_log_index = 0; + setInterval(() => { + const was_at_bottom = is_at_bottom(); + + set_logs([ + ...logs().slice(-MAX_NUM_LOG_LINES), + EXAMPLE_LOGS[example_log_index], + ]); + + // TODO: check if it's the logs page? May be easier to do if this logic is moved to `AppContainer`. + if (was_at_bottom) { + scroll_to_bottom(); + } + + example_log_index = (example_log_index + 1) % EXAMPLE_LOGS.length; + }, 100); + } + return ( - + } /> ); diff --git a/src/pages/Logs.tsx b/src/pages/Logs.tsx index 07c672c..6a48981 100644 --- a/src/pages/Logs.tsx +++ b/src/pages/Logs.tsx @@ -1,11 +1,6 @@ -import { listen, Event, UnlistenFn } from "@tauri-apps/api/event"; - -import { createSignal, onCleanup, onMount, For } from "solid-js"; +import { For, Accessor } from "solid-js"; import { styled } from "solid-styled-components"; -import { EXAMPLE_LOGS } from "../tests/example_data"; -import { MAX_NUM_LOG_LINES } from "../constants"; - const LogContainer = styled("div")` display: flex; flex-grow: 1; @@ -68,53 +63,7 @@ const Log = ({ children }: { children: string }) => { } }; -const Logs = () => { - const is_tauri_app = window.hasOwnProperty("__TAURI_INTERNALS__"); - const [logs, set_logs] = createSignal>([]); - - const is_at_bottom = () => { - const y_bottom = Math.ceil(window.scrollY) + window.innerHeight; - return y_bottom >= document.body.scrollHeight; - }; - - const scroll_to_bottom = () => { - window.scroll(0, document.body.scrollHeight); - }; - - if (is_tauri_app) { - let stop_listening: UnlistenFn; - - onMount(async () => { - stop_listening = await listen("log", (event: Event) => { - const was_at_bottom = is_at_bottom(); - - set_logs([...logs().slice(-MAX_NUM_LOG_LINES), event.payload]); - - if (was_at_bottom) { - scroll_to_bottom(); - } - }); - }); - - onCleanup(() => stop_listening()); - } else { - let example_log_index = 0; - setInterval(() => { - const was_at_bottom = is_at_bottom(); - - set_logs([ - ...logs().slice(-MAX_NUM_LOG_LINES), - EXAMPLE_LOGS[example_log_index], - ]); - - if (was_at_bottom) { - scroll_to_bottom(); - } - - example_log_index = (example_log_index + 1) % EXAMPLE_LOGS.length; - }, 100); - } - +const Logs = ({ logs }: { logs: Accessor }) => { return ( Waiting for zebrad to start...}>