Skip to content

Commit

Permalink
refactor: tick process in application state
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjackwills committed Dec 21, 2022
1 parent 4a5e1c9 commit dce8cae
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 51 deletions.
2 changes: 1 addition & 1 deletion src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 28 additions & 8 deletions src-tauri/src/application_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use tokio::{sync::broadcast::Sender, task::JoinHandle};
use crate::{
app_error::AppError,
db::{self, ModelSettings},
internal_message_handler::InternalMessage,
internal_message_handler::{InternalMessage, Emitter, BreakMessage},
setup_tracing,
};

Expand Down Expand Up @@ -156,11 +156,11 @@ impl ApplicationState {

/// Toggle the pause status
pub fn toggle_pause(&mut self) {
self.timer = self.timer.toggle()
self.timer = self.timer.toggle();
}

/// Check if the timer (tick process) is paused
pub fn get_paused(&self) -> bool {
pub const fn get_paused(&self) -> bool {
match self.timer {
Timer::Paused(_) => true,
Timer::Work(_) => false,
Expand Down Expand Up @@ -215,11 +215,6 @@ impl ApplicationState {
"next break in {}",
Self::format_sec_to_min(self.current_timer_left())
)
// let sec = self.current_timer_left();
// format!(
// "seconds::{}, minutes::{}",
// sec, Self::format_sec_to_min(sec)
// )
}

/// Return ModelSettings object
Expand Down Expand Up @@ -262,6 +257,31 @@ impl ApplicationState {
self.settings.short_break_as_sec = i;
}


pub fn tick_process(&self) {
// let paused = spawn_state.lock().get_paused();
if !self.get_paused() {

match self.session_status {
SessionStatus::Break(_) => {
self.sx.send(InternalMessage::Emit(Emitter::OnBreak))
.unwrap_or_default();
if self.current_timer_left() < 1 {
self.sx.send(InternalMessage::Break(BreakMessage::End))
.unwrap_or_default();
}
}
SessionStatus::Work => {
self.sx.send(InternalMessage::UpdateMenuTimer)
.unwrap_or_default();
if self.current_timer_left() < 1 {
self.sx.send(InternalMessage::Break(BreakMessage::Start))
.unwrap_or_default();
}
}
}
}
}
// /// close the sql connection in a tokio thead
// /// Honestly think this is pointless
// pub fn close_sql(&mut self) {
Expand Down
8 changes: 4 additions & 4 deletions src-tauri/src/internal_message_handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,9 @@ fn update_menu(
}

/// Stop the tick process, and start a new one
fn reset_timer(state: &Arc<Mutex<ApplicationState>>, sx: &Sender<InternalMessage>) {
fn reset_timer(state: &Arc<Mutex<ApplicationState>>) {
state.lock().reset_timer();
tick_process(state, sx.clone());
tick_process(state);
}

/// Update the database setting data, and self.setting, and if necessary reset timers etc
Expand Down Expand Up @@ -269,7 +269,7 @@ async fn handle_settings(
let sqlite = state.lock().sqlite.clone();
let settings = ModelSettings::reset_settings(&sqlite).await?;
state.lock().reset_settings(settings);
reset_timer(state, sx);
reset_timer(state);
sx.send(InternalMessage::Emit(Emitter::Settings))
.unwrap_or_default();
sx.send(InternalMessage::Emit(Emitter::Paused))
Expand All @@ -287,7 +287,7 @@ async fn handle_settings(
let sqlite = state.lock().sqlite.clone();
ModelSettings::update_session(&sqlite, value).await?;
state.lock().set_session_as_sec(value);
reset_timer(state, sx);
reset_timer(state);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ async fn main() -> Result<(), ()> {
let internal_state = Arc::clone(&state);

let event_sx = sx.clone();
let timer_sx = sx.clone();
// let timer_sx = sx.clone();
let close_sx = sx.clone();
let handler_sx = sx.clone();
let tray_sx = sx.clone();
Expand Down Expand Up @@ -171,7 +171,7 @@ async fn main() -> Result<(), ()> {
.build(tauri::generate_context!())
{
Ok(s) => {
tick_process(&init_state, timer_sx);
tick_process(&init_state);
start_message_handler(&s, internal_state, rx, handler_sx);
s.run(move |_app, event| {
if let tauri::RunEvent::ExitRequested { api, .. } = event {
Expand Down
48 changes: 12 additions & 36 deletions src-tauri/src/tick/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use parking_lot::Mutex;
use std::sync::Arc;
use tokio::sync::broadcast::Sender;

use crate::{
application_state::{ApplicationState, SessionStatus},
internal_message_handler::{BreakMessage, Emitter, InternalMessage},
application_state::ApplicationState,
internal_message_handler::InternalMessage,
};

// const ONE_SECOND_AS_MS: u64 = 1000;
Expand All @@ -13,12 +12,12 @@ use crate::{
/// Spawn off a tokio thread, that loops continually, well with a 250ms pause between each loop
/// The timer checking is then spawned off into another thread
/// The outer tread is saved into ApplicationState, so that it can be cancelled at any time
pub fn tick_process(state: &Arc<Mutex<ApplicationState>>, sx: Sender<InternalMessage>) {
pub fn tick_process(state: &Arc<Mutex<ApplicationState>>) {
if let Some(x) = &state.lock().tick_process {
x.abort();
}

let menu_updated = Arc::new(Mutex::new(std::time::Instant::now()));
// let menu_updated = Arc::new(Mutex::new(std::time::Instant::now()));
let spawn_state = Arc::clone(state);

state
Expand All @@ -28,37 +27,14 @@ pub fn tick_process(state: &Arc<Mutex<ApplicationState>>, sx: Sender<InternalMes
.unwrap_or_default();
state.lock().tick_process = Some(tokio::task::spawn(async move {
loop {
let paused = spawn_state.lock().get_paused();
if !paused {
let spawn_state = Arc::clone(&spawn_state);
let sx = sx.clone();
let menu_updated = Arc::clone(&menu_updated);

// TODO do all of this in the state itself!
tokio::spawn(async move {
let to_run = spawn_state.lock().session_status;
let session_left_in_sec = spawn_state.lock().current_timer_left();
match to_run {
SessionStatus::Break(_) => {
sx.send(InternalMessage::Emit(Emitter::OnBreak))
.unwrap_or_default();
if session_left_in_sec < 1 {
sx.send(InternalMessage::Break(BreakMessage::End))
.unwrap_or_default();
}
}
SessionStatus::Work => {
sx.send(InternalMessage::UpdateMenuTimer)
.unwrap_or_default();
*menu_updated.lock() = std::time::Instant::now();
if session_left_in_sec < 1 {
sx.send(InternalMessage::Break(BreakMessage::Start))
.unwrap_or_default();
}
}
}
});
}
// let paused = spawn_state.lock().get_paused();
// if !paused {
spawn_state.lock().tick_process();
// let spawn_state = Arc::clone(&spawn_state);
// tokio::spawn(async move ({
// spawn_state.lock().tick_process()
// }});
// }
// change to 500ms?
tokio::time::sleep(std::time::Duration::from_millis(1000)).await;
}
Expand Down

0 comments on commit dce8cae

Please sign in to comment.