-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GraphQL plugin #53
base: main
Are you sure you want to change the base?
GraphQL plugin #53
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,16 @@ | ||
use std::collections::HashMap; | ||
use std::error::Error; | ||
use std::fmt::{Display, Formatter}; | ||
use std::sync::Arc; | ||
|
||
use crossterm::event::KeyEvent; | ||
use ratatui::widgets::ScrollbarState; | ||
use serde::{Deserialize, Serialize}; | ||
use tokio::sync::mpsc; | ||
use tokio::sync::mpsc::UnboundedSender; | ||
use tokio::sync::Mutex; | ||
|
||
use crate::components::component::Component; | ||
use crate::components::handlers::HandlerMetadata; | ||
use crate::components::home::{Home, WebSockerInternalState}; | ||
use crate::components::home::Home; | ||
use crate::services::websocket::{Client, Trace}; | ||
use crate::tui::{Event, Tui}; | ||
use crate::wss::client; | ||
|
@@ -78,6 +76,12 @@ pub struct UIState { | |
pub horizontal_scroll_state: ScrollbarState, | ||
} | ||
|
||
#[derive(Default, Clone, PartialEq, Eq, Debug, Hash)] | ||
pub struct WssClient { | ||
pub path: String, | ||
pub address: String, | ||
} | ||
|
||
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] | ||
pub enum Action { | ||
#[serde(skip)] | ||
|
@@ -121,25 +125,27 @@ pub enum Action { | |
#[serde(skip)] | ||
SetGeneralStatus(String), | ||
#[serde(skip)] | ||
SetWebsocketStatus(WebSockerInternalState), | ||
#[serde(skip)] | ||
MarkTraceAsTimedOut(String), | ||
#[serde(skip)] | ||
ClearStatusMessage, | ||
#[serde(skip)] | ||
AddTrace(Trace), | ||
#[serde(skip)] | ||
AddClient(WssClient), | ||
#[serde(skip)] | ||
RemoveClient(WssClient), | ||
AddTraceError, | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct Services { | ||
websocket_client: Arc<Mutex<Client>>, | ||
websocket_client: Client, | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct App { | ||
pub action_tx: Option<UnboundedSender<Action>>, | ||
pub components: Vec<Arc<Mutex<dyn Component>>>, | ||
pub components: Vec<Box<dyn Component>>, | ||
pub services: Services, | ||
pub is_first_render: bool, | ||
pub logs: Vec<String>, | ||
|
@@ -152,12 +158,12 @@ impl App { | |
pub fn new() -> Result<App, Box<dyn Error>> { | ||
let config = crate::config::Config::new()?; | ||
|
||
let home = Arc::new(Mutex::new(Home::new()?)); | ||
let home = Home::new()?; | ||
|
||
let websocket_client = Arc::new(Mutex::new(Client::new())); | ||
let websocket_client = Client::new(); | ||
|
||
let app = App { | ||
components: vec![home], | ||
components: vec![Box::new(home)], | ||
services: Services { websocket_client }, | ||
key_map: config.mapping.0, | ||
..Self::default() | ||
|
@@ -182,39 +188,34 @@ impl App { | |
|
||
self.services | ||
.websocket_client | ||
.lock() | ||
.await | ||
.register_action_handler(action_tx.clone())?; | ||
|
||
self.services.websocket_client.lock().await.start(); | ||
self.services.websocket_client.start(); | ||
|
||
let mut t = Tui::new(); | ||
|
||
t.enter()?; | ||
|
||
for component in self.components.iter() { | ||
component | ||
.lock() | ||
.await | ||
.register_action_handler(action_tx.clone())?; | ||
for component in self.components.iter_mut() { | ||
component.register_action_handler(action_tx.clone())?; | ||
} | ||
|
||
self.services.websocket_client.lock().await.init(); | ||
self.services.websocket_client.init(); | ||
|
||
let action_to_clone = self.action_tx.as_ref().unwrap().clone(); | ||
|
||
tokio::spawn(async move { | ||
client(Some(action_to_clone)).await; | ||
let _ = client(Some(action_to_clone)).await; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you can also do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll check that out! |
||
}); | ||
|
||
loop { | ||
let event = t.next().await; | ||
|
||
if let Some(Event::Render) = event { | ||
for component in self.components.iter() { | ||
let c = component.lock().await; | ||
t.terminal.draw(|frame| { | ||
let r = c.render(frame); | ||
let r = component.render(frame); | ||
|
||
if let Err(e) = r { | ||
action_tx | ||
.send(Action::Error(format!("Failed to draw: {:?}", e))) | ||
|
@@ -225,8 +226,8 @@ impl App { | |
}; | ||
|
||
if let Some(Event::OnMount) = event { | ||
for component in self.components.iter() { | ||
if let Some(action) = component.lock().await.on_mount()? { | ||
for component in self.components.iter_mut() { | ||
if let Some(action) = component.on_mount()? { | ||
action_tx.send(action.clone())?; | ||
} | ||
} | ||
|
@@ -245,8 +246,8 @@ impl App { | |
} | ||
}; | ||
|
||
for component in self.components.iter() { | ||
if let Some(action) = component.lock().await.handle_events(event)? { | ||
for component in self.components.iter_mut() { | ||
if let Some(action) = component.handle_events(event)? { | ||
action_tx.send(action.clone())?; | ||
} | ||
} | ||
|
@@ -256,18 +257,13 @@ impl App { | |
self.should_quit = true; | ||
} | ||
|
||
for component in self.components.iter() { | ||
if let Some(action) = component.lock().await.update(action.clone())? { | ||
for component in self.components.iter_mut() { | ||
if let Some(action) = component.update(action.clone())? { | ||
action_tx.send(action.clone())?; | ||
} | ||
} | ||
|
||
self.services | ||
.websocket_client | ||
.clone() | ||
.lock() | ||
.await | ||
.update(action.clone()); | ||
self.services.websocket_client.update(action.clone()); | ||
} | ||
|
||
if self.should_quit { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1090,8 +1090,28 @@ pub fn handle_delete_item(app: &mut Home) { | |
let _ = &app.items.remove(current_trace); | ||
} | ||
|
||
pub fn handle_general_status(app: &mut Home, s: String) { | ||
app.status_message = Some(s); | ||
pub fn handle_general_status( | ||
app: &mut Home, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The variable name is |
||
message: String, | ||
sender: Option<UnboundedSender<Action>>, | ||
) { | ||
app.status_message = Some(message); | ||
|
||
app.abort_handlers.iter().for_each(|handler| { | ||
handler.abort(); | ||
}); | ||
Comment on lines
+1100
to
+1102
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It took me a minute to wrap my head around this, so I'll summarize here to make sure I understand. When you're showing a status message, you want to set a 5-second clear action. If the status message is changed during that time, you abort the prior clear and reset the timer to 5 seconds anew so that the new message isn't cleared out too soon? |
||
|
||
app.abort_handlers.clear(); | ||
|
||
if let Some(sender) = sender { | ||
let thread_handler = tokio::spawn(async move { | ||
sleep(Duration::from_millis(5000)).await; | ||
|
||
sender.send(Action::ClearStatusMessage) | ||
}); | ||
Comment on lines
+1107
to
+1111
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm guessing this is a |
||
|
||
app.abort_handlers.push(thread_handler.abort_handle()); | ||
} | ||
} | ||
|
||
pub fn handle_select(app: &mut Home) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is really minor and you definitely don't have to do it, but a style guideline I follow is to prevent what they call "module-name prefixes", which essentially means repeating the module name within your type names, like
components::home::Home
orcomponents::component::Component
orcomponents::handlers::HandlerMetadata
. The Go community recommends against this as well. It's generally more concise and readable to use alternatives likecomponents::Home
,components::Component
, orcomponents::handlers::Metadata
.To do this, I'll often do public exports of specific types within my
lib.rs
ormod.rs
files, like this. With things likepub use hooks::Hook;
andpub use tags::Tag;
, it allows me to use more concise imports likeinject::Hook
, andinject::Tag
everywhere.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, you can collapse a lot of these imports together into one statement, like this. The end result after adopting these two style guidelines would look something like this: