Skip to content

Commit

Permalink
Various cleanups etc related to adding troup crate
Browse files Browse the repository at this point in the history
* Don't panic so frivolously
* Remove type restriction on GatheringHall (we could have done this
  before the troupe crate IMO)
* Drop a couple drops that we no longer need thanks to troupe doing it
  for us
* Undo a weird commit that snuck in somehow via rebase
* Derive/Implement `Debug` for a few things that we now need
  • Loading branch information
orblivion committed Jan 11, 2024
1 parent 78f4b0c commit b0b55f4
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 31 deletions.
4 changes: 2 additions & 2 deletions squire_core/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl AppStateBuilder<Uri, DbName> {
let tourn_coll = Arc::from(self.get_tournament_collection_name());
let tourn_db = TournDb::new(db_conn.clone(), tourn_coll);
let tournaments = ActorBuilder::new(TournPersister::new(tourn_db.clone())).launch();
let gatherings = ActorBuilder::new(GatheringHall::<TournPersister>::new(tournaments.clone())).launch();
let gatherings = ActorBuilder::new(GatheringHall::new(tournaments.clone())).launch();
AppState {
sessions: SessionStoreHandle::new(db_conn.clone()),
accounts: AccountStoreHandle::new(db_conn),
Expand All @@ -126,7 +126,7 @@ impl AppStateBuilder<Database, ()> {
let tourn_coll: Arc<str> = Arc::from(self.get_tournament_collection_name());
let tourn_db = TournDb::new(self.db_conn.clone(), tourn_coll);
let tourns = ActorBuilder::new(TournPersister::new(tourn_db.clone())).launch();
let gatherings = ActorBuilder::new(GatheringHall::<TournPersister>::new(tourns.clone())).launch();
let gatherings = ActorBuilder::new(GatheringHall::new(tourns.clone())).launch();
AppState {
sessions: SessionStoreHandle::new(self.db_conn.clone()),
accounts: AccountStoreHandle::new(self.db_conn),
Expand Down
1 change: 1 addition & 0 deletions squire_sdk/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ impl UserInfo {
}
}

#[derive(Debug)]
pub struct SquireClient {
user: SessionWatcher,
client: NetworkClient,
Expand Down
4 changes: 2 additions & 2 deletions squire_sdk/src/client/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl ActorState for NetworkState {
match msg {
NetworkCommand::Request(req, send) => {
let fut = self.client.execute(req.session(self.token.as_ref()));
scheduler.manage_future(async move { drop(send.send(NetworkResponse::new(fut.await))) });
scheduler.manage_future(async move { send.send(NetworkResponse::new(fut.await)) });
}
NetworkCommand::Login(cred, send) => {
let req = self.post_request(Login(cred), []);
Expand Down Expand Up @@ -121,7 +121,7 @@ impl ActorState for NetworkState {
Some(token) => {
let url = format!("/api/v1/tournaments/subscribe/{id}");
scheduler.manage_future(async move {
drop(send.send(init_ws(Websocket::new(&url).await.ok(), token).await));
send.send(init_ws(Websocket::new(&url).await.ok(), token).await)
});
}
None => drop(send.send(None)),
Expand Down
18 changes: 17 additions & 1 deletion squire_sdk/src/client/tournaments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use squire_lib::{
operations::{OpData, OpResult, TournOp},
tournament::TournamentId,
};
use std::fmt::Debug;
use tokio::sync::watch::{channel as watch_channel, Receiver as Watcher, Sender as Broadcaster};
use troupe::{prelude::*, sink::permanent::Tracker};
use uuid::Uuid;
Expand All @@ -22,7 +23,7 @@ use crate::{
};

/// A container for the channels used to communicate with the tournament management task.
#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct TournsClient {
client: SinkClient<Permanent, ManagementCommand>,
}
Expand All @@ -38,6 +39,21 @@ pub(crate) enum ManagementCommand {
Retry(MessageRetry),
}

impl Debug for ManagementCommand {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
// Ignoring some annoying to deal with fields for now
Self::Query(value, _) => write!(f, "Query({value:?})"),
Self::Update((tid, ut), _) => write!(f, "Update({tid:?}, {ut:?})"),
Self::Import(_, _) => write!(f, "Import()"),
Self::Subscribe(value, _) => write!(f, "Subscribe({value:?})"),
Self::Connection(value, _) => write!(f, "Connection({value:?})"),
Self::Remote(value) => write!(f, "Remote({value:?})"),
Self::Retry(value) => write!(f, "Retry({value:?})"),
}
}
}

/// A struct that contains all of the state that the management task maintains
#[allow(unused)]
struct ManagerState {
Expand Down
29 changes: 8 additions & 21 deletions squire_sdk/src/server/gathering/hall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use async_trait::async_trait;
use axum::extract::ws::WebSocket;
use instant::{Duration, Instant};
use squire_lib::tournament::TournamentId;
use std::marker::PhantomData;
use tokio::sync::{
mpsc::{channel, Receiver, Sender},
oneshot::channel as oneshot_channel,
Expand Down Expand Up @@ -39,9 +38,7 @@ use crate::{
*
*/

fn schedule_persist<P>(scheduler: &mut Scheduler<GatheringHall<P>>)
where
P: ActorState<Message = PersistMessage> + Sync,
fn schedule_persist(scheduler: &mut Scheduler<GatheringHall>)
{
scheduler.schedule(
Instant::now() + Duration::from_secs(5),
Expand All @@ -64,18 +61,15 @@ pub enum GatheringHallMessage {
/// users to different gatherings and persisting data to the database. All of this is handled
/// through message passing and tokio tasks.
#[derive(Debug)]
pub struct GatheringHall<P: ActorState<Message = PersistMessage>> {
pub struct GatheringHall {
gatherings: HashMap<TournamentId, SinkClient<Transient, GatheringMessage>>,
persists: Receiver<PersistReadyMessage>,
persist_sender: Sender<PersistReadyMessage>,
persister: SinkClient<Permanent, PersistMessage>,
marker: PhantomData<P>,
}

#[async_trait]
impl<P> ActorState for GatheringHall<P>
where
P: ActorState<Message = PersistMessage> + Sync,
impl ActorState for GatheringHall
{
type Permanence = Permanent;
type ActorType = SinkActor;
Expand All @@ -91,9 +85,7 @@ where
match msg {
GatheringHallMessage::NewGathering(id) => self.process_new_gathering(id).await,
GatheringHallMessage::NewConnection(id, user, ws) => {
if !self.process_new_onlooker(id, user, ws).await {
panic!("process_new_onlooker failed")
}
self.process_new_onlooker(id, user, ws).await
}
GatheringHallMessage::Persist => {
let mut to_persist = HashSet::new();
Expand All @@ -105,9 +97,7 @@ where
let sender = self.gatherings.get_mut(&id).unwrap();
let (send, recv) = oneshot_channel();
let msg = GatheringMessage::GetTournament(send);
if !sender.send(msg) {
panic!("GatheringMessage::GetTournament failed")
}
let _ = sender.send(msg);
let tourn = recv.await.unwrap();
let _ = persist_reqs.insert(id, tourn);
}
Expand All @@ -125,9 +115,7 @@ where
}
}

impl<P> GatheringHall<P>
where
P: ActorState<Message = PersistMessage>,
impl GatheringHall
{
/// Creates a new `GatheringHall` from receiver halves of channels that communicate new
/// gatherings and subscriptions
Expand All @@ -138,7 +126,6 @@ where
persists,
persist_sender,
persister,
marker: PhantomData,
}
}

Expand All @@ -162,10 +149,10 @@ where
id: TournamentId,
user: SessionWatcher,
ws: WebSocket,
) -> bool {
) {
let msg = GatheringMessage::NewConnection(user, ws);
let send = self.get_or_init_gathering(id).await;
send.send(msg)
let _ = send.send(msg);
}

async fn get_or_init_gathering(&mut self, id: TournamentId) -> SinkClient<Transient, GatheringMessage> {
Expand Down
5 changes: 0 additions & 5 deletions squire_web/src/account/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,6 @@ impl Component for Login {
self.error_message = "Login attempt failed!!".to_owned();
let _ = element.show_modal();
}
LoginMessage::LoginResult(Ok(_)) => {
let navigator = ctx.link().navigator().unwrap();
navigator.push(&Route::Create);
}
LoginMessage::LoginResult(_) => panic!("Login attempt failed!!"),
}
true
}
Expand Down

0 comments on commit b0b55f4

Please sign in to comment.