Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: player - push library item time every 90s or more #586

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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