mod repo;
mod ui;
pub use self::repo::*;
use semver::Version;
use std::{
sync::{Arc, OnceLock},
thread,
};
#[derive(Debug, Clone)]
pub struct Updater {
name: String,
repo: Repository,
current: Version,
latest: Arc<OnceLock<Version>>,
}
impl Updater {
#[inline]
pub fn unchecked(name: impl Into<String>, repo: Repository, current: Version) -> Self {
Self {
name: name.into(),
repo,
current,
latest: Default::default(),
}
}
#[inline]
pub fn new(name: impl Into<String>, repo: Repository, current: Version) -> Self {
let mut updater = Self::unchecked(name, repo, current);
updater.check();
updater
}
#[inline]
pub fn is_outdated(&self) -> bool {
match self.latest.get() {
Some(latest) => latest > &self.current,
None => false,
}
}
#[inline]
pub fn check(&mut self) {
if self.latest.get().is_none() {
let latest = self.latest.clone();
let repo = self.repo.clone();
thread::spawn(move || {
if let Some(version) = repo.latest_version_blocking() {
let _ = latest.set(version);
}
});
}
}
#[inline]
pub fn reset(&mut self) {
self.latest = Default::default();
}
fn latest_if_outdated(&self) -> Option<&Version> {
self.latest.get().filter(|latest| *latest > &self.current)
}
}