hexagonal refactor
This commit is contained in:
+11
-29
@@ -1,19 +1,11 @@
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use tokio::sync::{Mutex, broadcast};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct PresenceUser {
|
||||
pub user_id: i64,
|
||||
pub display_name: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum HubEvent {
|
||||
ListChanged { list_id: i64, revision: i64 },
|
||||
PresenceChanged { list_id: i64 },
|
||||
}
|
||||
use crate::domain::PresenceUser;
|
||||
use crate::ports::{HubEvent, RealtimeNotifier, Subscription};
|
||||
|
||||
#[derive(Debug)]
|
||||
struct ConnectionInfo {
|
||||
@@ -26,24 +18,14 @@ struct Room {
|
||||
connections: HashMap<String, ConnectionInfo>,
|
||||
}
|
||||
|
||||
pub struct Subscription {
|
||||
pub connection_id: String,
|
||||
pub receiver: broadcast::Receiver<HubEvent>,
|
||||
pub presence: Vec<PresenceUser>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct Hub {
|
||||
pub struct InMemoryHub {
|
||||
rooms: Arc<Mutex<HashMap<i64, Room>>>,
|
||||
}
|
||||
|
||||
impl Hub {
|
||||
pub async fn join(
|
||||
&self,
|
||||
list_id: i64,
|
||||
user_id: i64,
|
||||
display_name: String,
|
||||
) -> Subscription {
|
||||
#[async_trait]
|
||||
impl RealtimeNotifier for InMemoryHub {
|
||||
async fn join(&self, list_id: i64, user_id: i64, display_name: String) -> Subscription {
|
||||
let mut rooms = self.rooms.lock().await;
|
||||
let room = rooms.entry(list_id).or_insert_with(|| {
|
||||
let (sender, _) = broadcast::channel(64);
|
||||
@@ -53,7 +35,7 @@ impl Hub {
|
||||
}
|
||||
});
|
||||
|
||||
let connection_id = crate::db::new_secret();
|
||||
let connection_id = crate::security::new_secret();
|
||||
let already_present = room
|
||||
.connections
|
||||
.values()
|
||||
@@ -79,7 +61,7 @@ impl Hub {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn leave(&self, list_id: i64, connection_id: &str) {
|
||||
async fn leave(&self, list_id: i64, connection_id: &str) {
|
||||
let mut rooms = self.rooms.lock().await;
|
||||
let mut remove_room = false;
|
||||
if let Some(room) = rooms.get_mut(&list_id) {
|
||||
@@ -100,7 +82,7 @@ impl Hub {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn publish_list_changed(&self, list_id: i64, revision: i64) {
|
||||
async fn publish_list_changed(&self, list_id: i64, revision: i64) {
|
||||
let rooms = self.rooms.lock().await;
|
||||
if let Some(room) = rooms.get(&list_id) {
|
||||
let _ = room
|
||||
@@ -109,7 +91,7 @@ impl Hub {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn presence(&self, list_id: i64) -> Vec<PresenceUser> {
|
||||
async fn presence(&self, list_id: i64) -> Vec<PresenceUser> {
|
||||
let rooms = self.rooms.lock().await;
|
||||
rooms
|
||||
.get(&list_id)
|
||||
|
||||
Reference in New Issue
Block a user