fix updater module

This commit is contained in:
Jonas Kruckenberg 2022-11-16 13:13:47 +01:00
parent 96b82451e7
commit 4876386825
2 changed files with 105 additions and 32 deletions

View file

@ -25,7 +25,7 @@ tauri-sys = { path = ".", features = ["all"] }
all-features = true all-features = true
[features] [features]
all = ["app", "clipboard", "event", "mocks", "tauri", "window", "process", "dialog", "os", "notification", "path"] all = ["app", "clipboard", "event", "mocks", "tauri", "window", "process", "dialog", "os", "notification", "path", "updater"]
app = ["dep:semver"] app = ["dep:semver"]
clipboard = [] clipboard = []
dialog = [] dialog = []
@ -37,6 +37,7 @@ process = []
os = ["dep:semver"] os = ["dep:semver"]
notification = [] notification = []
path = [] path = []
updater = []
[workspace] [workspace]
members = ["examples/test", "examples/test/src-tauri"] members = ["examples/test", "examples/test/src-tauri"]

View file

@ -1,38 +1,33 @@
use serde::Deserialize; use serde::Deserialize;
use wasm_bindgen::{prelude::Closure, JsValue};
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug, Clone)]
pub struct UpdateManifest { pub struct UpdateManifest {
pub body: String, pub body: String,
pub date: String, pub date: String,
pub version: String pub version: String
} }
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug, Clone)]
pub struct UpdateResult { pub struct UpdateResult {
pub manifest: Option<UpdateManifest>, pub manifest: Option<UpdateManifest>,
pub should_update: bool pub should_update: bool
} }
#[derive(Deserialize)] #[derive(Deserialize)]
pub enum UpdateStatus { struct UpdateStatusResult {
PENDING, error: Option<String>,
ERROR, status: UpdateStatus
DONE,
UPTODATE
} }
/// Install the update if there's one available. #[derive(Deserialize)]
/// pub enum UpdateStatus {
/// # Example #[serde(rename = "PENDING")]
/// Pending,
/// ```rust,no_run #[serde(rename = "DONE")]
/// use tauri_api::updater::install_update; Done,
/// #[serde(rename = "UPTODATE")]
/// install_update().await; UpToDate
/// ```
#[inline(always)]
pub async fn install_update() {
inner::installUpdate().await
} }
/// Checks if an update is available. /// Checks if an update is available.
@ -40,22 +35,99 @@ pub async fn install_update() {
/// # Example /// # Example
/// ///
/// ```rust,no_run /// ```rust,no_run
/// use tauri_api::updater::{check_update, UpdateResult}; /// use tauri_sys::updater::check_update;
/// ///
/// let update: UpdateResult = check_update().await; /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let update = check_update().await?;
/// // now run installUpdate() if needed
/// # Ok(())
/// # }
/// ``` /// ```
pub async fn check_update() -> crate::Result<UpdateResult> {
let raw = inner::checkUpdate().await?;
Ok(serde_wasm_bindgen::from_value(raw)?)
}
/// Install the update if there's one available.
///
/// # Example
///
/// ```rust,no_run
/// use tauri_sys::updater::{check_update, install_update};
///
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let update = check_update().await?;
///
/// if update.should_update {
/// log::info("Installing update {:?}", update.manifest);
/// install_update().await?;
/// }
/// # Ok(())
/// # }
/// ```
pub async fn install_update() -> crate::Result<()> {
inner::installUpdate().await?;
Ok(())
}
/// Listen to an updater event.
///
/// # Example
///
/// ```rust,no_run
/// use tauri_sys::updater::on_updater_event;
///
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let unlisten = on_updater_event(|event| {
/// log::debug!("Updater event {:?}", event);
/// }).await?;
///
/// // you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
/// unlisten();
/// # Ok(())
/// # }
/// ```
/// Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.
#[inline(always)] #[inline(always)]
pub async fn check_update() -> UpdateResult { pub async fn on_updater_event<H>(mut handler: H) -> crate::Result<impl FnOnce()>
let update = inner::checkUpdate().await; where
serde_wasm_bindgen::from_value(update).unwrap() H: FnMut(Result<UpdateStatus, String>) + 'static,
{
let closure = Closure::<dyn FnMut(JsValue)>::new(move |raw| {
let raw: UpdateStatusResult = serde_wasm_bindgen::from_value(raw).unwrap();
let result = if let Some(error) = raw.error {
Err(error)
} else {
Ok(raw.status)
};
(handler)(result)
});
let unlisten = inner::onUpdaterEvent(&closure).await?;
closure.forget();
let unlisten = js_sys::Function::from(unlisten);
Ok(move || {
unlisten.call0(&wasm_bindgen::JsValue::NULL).unwrap();
})
} }
mod inner { mod inner {
use wasm_bindgen::{prelude::wasm_bindgen, JsValue}; use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "/dist/updater.js")] #[wasm_bindgen(module = "/src/updater.js")]
extern "C" { extern "C" {
pub async fn installUpdate(); #[wasm_bindgen(catch)]
pub async fn checkUpdate() -> JsValue; pub async fn checkUpdate() -> Result<JsValue, JsValue>;
#[wasm_bindgen(catch)]
pub async fn installUpdate() -> Result<JsValue, JsValue>;
#[wasm_bindgen(catch)]
pub async fn onUpdaterEvent(
handler: &Closure<dyn FnMut(JsValue)>,
) -> Result<JsValue, JsValue>;
} }
} }