-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
made button swap play/pause
- Loading branch information
Showing
5 changed files
with
187 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
use crate::types::EventQueues; | ||
use axum::{extract::State, http::StatusCode}; | ||
use maud::{html, Markup}; | ||
|
||
#[derive(askama::Template)] | ||
#[template(path = "index.html")] | ||
pub struct IndexTemplate {} | ||
|
||
#[derive(askama::Template)] | ||
#[template(path = "admin.html")] | ||
pub struct AdminTemplate { | ||
pub enabled: bool, | ||
} | ||
|
||
pub async fn index() -> IndexTemplate { | ||
IndexTemplate {} | ||
} | ||
|
||
pub async fn admin() -> AdminTemplate { | ||
AdminTemplate { | ||
enabled: crate::types::EVENT_QUEUE_ACTIVE.load(std::sync::atomic::Ordering::SeqCst), | ||
} | ||
} | ||
|
||
pub async fn get_latest_unpublished_events( | ||
State(queues): State<EventQueues>, | ||
) -> Result<Markup, (StatusCode, String)> { | ||
let queues = queues.lock().unwrap(); | ||
let range = if queues.unpublished_events.len() < 10 { | ||
0..queues.unpublished_events.len() | ||
} else { | ||
0..10 | ||
}; | ||
let events = queues.unpublished_events.range(range); | ||
|
||
Ok(html! { | ||
ul class="waiting" { | ||
@for event in events { | ||
li { (event.message) } | ||
} | ||
} | ||
}) | ||
} | ||
|
||
pub async fn get_latest_events( | ||
State(queues): State<EventQueues>, | ||
) -> Result<Markup, (StatusCode, String)> { | ||
let queues = queues.lock().unwrap(); | ||
let events = queues.latest_events.clone(); | ||
Ok(html! { | ||
ul { | ||
@for event in events { | ||
li { (event.message) } | ||
} | ||
} | ||
}) | ||
} | ||
|
||
pub async fn pause_events() -> Result<Markup, (StatusCode, String)> { | ||
crate::types::EVENT_QUEUE_ACTIVE.store(false, std::sync::atomic::Ordering::SeqCst); | ||
Ok(html! { | ||
button id="event-queue-toggle" hx-get="/events/start" hx-swap="outerHTML" hx-target="#event-queue-toggle" { "Start" } | ||
}) | ||
} | ||
|
||
pub async fn resume_events() -> Result<Markup, (StatusCode, String)> { | ||
crate::types::EVENT_QUEUE_ACTIVE.store(true, std::sync::atomic::Ordering::SeqCst); | ||
Ok(html! { | ||
button id="event-queue-toggle" hx-get="/events/pause" hx-swap="outerHTML" hx-target="#event-queue-toggle" { "Pause" } | ||
}) | ||
} | ||
|
||
pub async fn get_all_events_in_queue( | ||
State(queues): State<EventQueues>, | ||
) -> Result<Markup, (StatusCode, String)> { | ||
let queues = queues.lock().unwrap(); | ||
let events = queues.unpublished_events.clone(); | ||
Ok(html! { | ||
ul { | ||
@for event in events { | ||
li { (event.message) } | ||
} | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use futures_channel::mpsc::UnboundedSender; | ||
use messages::DisplayMessage; | ||
use std::{ | ||
collections::{HashMap, VecDeque}, | ||
net::SocketAddr, | ||
sync::{Arc, Mutex}, | ||
}; | ||
use tokio_tungstenite::tungstenite::Message; | ||
pub type Tx = UnboundedSender<Message>; | ||
pub type ConnectionMap = Arc<Mutex<HashMap<SocketAddr, Tx>>>; | ||
pub type EventQueues = Arc<Mutex<Queues>>; | ||
|
||
pub struct Queues { | ||
pub unpublished_events: VecDeque<DisplayMessage>, | ||
pub tts: VecDeque<DisplayMessage>, | ||
pub latest_events: VecDeque<DisplayMessage>, | ||
pub last_sub: Option<DisplayMessage>, | ||
} | ||
|
||
pub static EVENT_QUEUE_ACTIVE: std::sync::atomic::AtomicBool = | ||
std::sync::atomic::AtomicBool::new(true); | ||
|
||
impl Queues { | ||
pub fn new() -> Queues { | ||
Queues { | ||
unpublished_events: VecDeque::new(), | ||
tts: VecDeque::new(), | ||
latest_events: VecDeque::new(), | ||
last_sub: None, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters