Skip to content

Commit

Permalink
chore: player - push library item time every 90s or more
Browse files Browse the repository at this point in the history
Signed-off-by: Lachezar Lechev <[email protected]>
  • Loading branch information
elpiel committed Dec 12, 2023
1 parent c1ac3ff commit 6e6b2b0
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions src/models/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ use derivative::Derivative;
use itertools::Itertools;
use serde::{Deserialize, Serialize};

use lazy_static::lazy_static;
use once_cell::sync::Lazy;

lazy_static! {
/// The duration that must have passed in order for a library item to be updated.
pub static ref PUSH_TO_LIBRARY_EVERY: Duration = Duration::seconds(30);
}
/// The duration that must have passed in order for a library item to be updated.
pub static PUSH_TO_LIBRARY_EVERY: Lazy<Duration> = Lazy::new(|| Duration::seconds(90));

#[derive(Clone, Default, PartialEq, Eq, Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -438,16 +436,7 @@ impl<E: Env + 'static> UpdateWithCtx<E> for Player {
};

let push_to_library_effects =
if E::now() - self.push_library_item_time >= *PUSH_TO_LIBRARY_EVERY {
self.push_library_item_time = E::now();

Effects::msg(Msg::Internal(Internal::UpdateLibraryItem(
library_item.to_owned(),
)))
.unchanged()
} else {
Effects::none().unchanged()
};
push_to_library::<E>(&mut self.push_library_item_time, library_item);

trakt_event_effects.join(push_to_library_effects)
}
Expand Down Expand Up @@ -630,6 +619,24 @@ impl<E: Env + 'static> UpdateWithCtx<E> for Player {
}
}

/// We will push an [`Internal::UpdateLibraryItem`] message only if
/// at least [`PUSH_TO_LIBRARY_EVERY`] time has passed since the last update.
fn push_to_library<E: Env + 'static>(
push_library_item_time: &mut DateTime<Utc>,
library_item: &mut LibraryItem,
) -> Effects {
if E::now() - *push_library_item_time >= *PUSH_TO_LIBRARY_EVERY {
*push_library_item_time = E::now();

Effects::msg(Msg::Internal(Internal::UpdateLibraryItem(
library_item.to_owned(),
)))
.unchanged()
} else {
Effects::none().unchanged()
}
}

fn switch_to_next_video(
library_item: &mut Option<LibraryItem>,
next_video: &Option<Video>,
Expand Down

0 comments on commit 6e6b2b0

Please sign in to comment.