Skip to content

Commit

Permalink
Broadcast a reconnected message on reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
tarkah committed Jun 23, 2023
1 parent d14469d commit 8b87dc3
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 23 deletions.
4 changes: 2 additions & 2 deletions data/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ impl Connection {
pub struct Map(BTreeMap<Server, State>);

impl Map {
pub fn disconnected(&mut self, server: Server) -> Option<State> {
self.0.insert(server, State::Disconnected)
pub fn disconnected(&mut self, server: Server) {
self.0.insert(server, State::Disconnected);
}

pub fn ready(&mut self, server: Server, client: Connection) {
Expand Down
4 changes: 4 additions & 0 deletions data/src/history/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ impl Manager {
Broadcast::Disconnected => {
message::broadcast::disconnected(channels, queries).collect::<Vec<_>>()
}
Broadcast::Reconnected => {
message::broadcast::reconnected(channels, queries).collect::<Vec<_>>()
}
};

messages.into_iter().for_each(|message| {
Expand Down Expand Up @@ -370,4 +373,5 @@ impl Data {
#[derive(Debug, Clone, Copy)]
pub enum Broadcast {
Disconnected,
Reconnected,
}
42 changes: 33 additions & 9 deletions data/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,22 @@ pub(crate) mod broadcast {

use super::{Direction, Message, Sender, Source};

fn expand(
channels: impl IntoIterator<Item = String>,
queries: impl IntoIterator<Item = Nick>,
f: fn(source: Source) -> Message,
) -> impl Iterator<Item = Message> {
channels
.into_iter()
.map(move |channel| f(Source::Channel(channel, Sender::Server)))
.chain(
queries
.into_iter()
.map(move |nick| f(Source::Query(nick, Sender::Server))),
)
.chain(Some(f(Source::Server)))
}

pub fn disconnected(
channels: impl IntoIterator<Item = String>,
queries: impl IntoIterator<Item = Nick>,
Expand All @@ -375,14 +391,22 @@ pub(crate) mod broadcast {
}
}

channels
.into_iter()
.map(|channel| message(Source::Channel(channel, Sender::Server)))
.chain(
queries
.into_iter()
.map(|nick| message(Source::Query(nick, Sender::Server))),
)
.chain(Some(message(Source::Server)))
expand(channels, queries, message)
}

pub fn reconnected(
channels: impl IntoIterator<Item = String>,
queries: impl IntoIterator<Item = Nick>,
) -> impl Iterator<Item = Message> {
fn message(source: Source) -> Message {
Message {
datetime: Utc::now(),
direction: Direction::Received,
source,
text: " ∙ connection to server restored".into(),
}
}

expand(channels, queries, message)
}
}
34 changes: 29 additions & 5 deletions data/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,15 @@ pub enum Error {

#[derive(Debug)]
pub enum Update {
Connected(Server, Connection),
Disconnected(Server),
Connected {
server: Server,
connection: Connection,
is_initial: bool,
},
Disconnected {
server: Server,
is_initial: bool,
},
MessagesReceived(Server, Vec<message::Encoded>),
}

Expand All @@ -45,9 +52,15 @@ pub async fn run(server: server::Entry, mut sender: mpsc::Sender<Update>) -> Nev

let server::Entry { server, config } = server;

let mut is_initial = true;
let mut state = State::Disconnected { last_retry: None };
// Notify app of initial disconnected state
let _ = sender.send(Update::Disconnected(server.clone())).await;
let _ = sender
.send(Update::Disconnected {
server: server.clone(),
is_initial,
})
.await;

loop {
match &mut state {
Expand All @@ -65,9 +78,15 @@ pub async fn run(server: server::Entry, mut sender: mpsc::Sender<Update>) -> Nev
log::info!("[{server}] connected");

let _ = sender
.send(Update::Connected(server.clone(), connection))
.send(Update::Connected {
server: server.clone(),
connection,
is_initial,
})
.await;

is_initial = false;

state = State::Connected {
stream,
batch: Batch::new(),
Expand All @@ -92,7 +111,12 @@ pub async fn run(server: server::Entry, mut sender: mpsc::Sender<Update>) -> Nev
}
Input::IrcMessage(Err(e)) => {
log::warn!("[{server}] disconnected: {e}");
let _ = sender.send(Update::Disconnected(server.clone())).await;
let _ = sender
.send(Update::Disconnected {
server: server.clone(),
is_initial,
})
.await;
state = State::Disconnected {
last_retry: Some(Instant::now()),
};
Expand Down
22 changes: 15 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ mod stream;
mod theme;
mod widget;

use data::client;
use data::config::{self, Config};
use iced::widget::container;
use iced::{executor, Application, Command, Length, Subscription};
Expand Down Expand Up @@ -136,18 +135,27 @@ impl Application for Halloy {
}
},
Message::Stream(update) => match update {
stream::Update::Disconnected(server) => {
if let Some(client::State::Ready(_)) = self.clients.disconnected(server.clone())
// Previously connected but now disconnected
{
stream::Update::Disconnected { server, is_initial } => {
self.clients.disconnected(server.clone());

if !is_initial {
let Screen::Dashboard(dashboard) = &mut self.screen;
dashboard.disconnected(&server);
}

Command::none()
}
stream::Update::Connected(server, client) => {
self.clients.ready(server, client);
stream::Update::Connected {
server,
connection,
is_initial,
} => {
self.clients.ready(server.clone(), connection);

if !is_initial {
let Screen::Dashboard(dashboard) = &mut self.screen;
dashboard.reconnected(&server);
}

Command::none()
}
Expand Down
4 changes: 4 additions & 0 deletions src/screen/dashboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,10 @@ impl Dashboard {
self.history.broadcast(server, Broadcast::Disconnected);
}

pub fn reconnected(&mut self, server: &Server) {
self.history.broadcast(server, Broadcast::Reconnected);
}

fn get_focused_mut(&mut self) -> Option<(pane_grid::Pane, &mut Pane)> {
let pane = self.focus?;
self.panes.get_mut(&pane).map(|state| (pane, state))
Expand Down

0 comments on commit 8b87dc3

Please sign in to comment.