Skip to content
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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 14 additions & 17 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ pub struct Services {
#[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>,
Expand All @@ -160,12 +160,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 app = App {
components: vec![home],
components: vec![Box::new(home)],
services: Services { websocket_client },
key_map: config.mapping.0,
..Self::default()
Expand Down Expand Up @@ -200,29 +200,26 @@ impl App {

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();

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can also do client(Some(action_to_clone)).await?; which would allow the error to surface from the result

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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)))
Expand All @@ -233,8 +230,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())?;
}
}
Expand All @@ -253,8 +250,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())?;
}
}
Expand All @@ -264,8 +261,8 @@ 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())?;
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/components/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,25 @@ pub trait Component {
) -> Result<(), Box<dyn Error>> {
Ok(())
}

fn handle_events(&mut self, event: Option<Event>) -> Result<Option<Action>, Box<dyn Error>> {
let r = match event {
Some(Event::Key(key_event)) => self.handle_key_events(key_event)?,
_ => None,
};
Ok(r)
}

#[allow(unused_variables)]
fn handle_key_events(&mut self, key: KeyEvent) -> Result<Option<Action>, Box<dyn Error>> {
Ok(None)
}

#[allow(unused_variables)]
fn update(&mut self, action: Action) -> Result<Option<Action>, Box<dyn Error>> {
Ok(None)
}

fn render(&self, f: &mut Frame<'_>) -> Result<(), Box<dyn Error>>;

fn on_mount(&mut self) -> Result<Option<Action>, Box<dyn Error>>;
Expand Down