mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-09-26 21:41:48 +02:00
refactor(updater): migrate to tauri's new resource table (#899)
* refactor(updater): migrate to tauri's new resource table * fmt * fmt * Create updater-js-started-event.md
This commit is contained in:
@@ -2,15 +2,12 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
use crate::{PendingUpdate, Result, UpdaterExt};
|
||||
use crate::{Result, Update, UpdaterExt};
|
||||
|
||||
use serde::Serialize;
|
||||
use tauri::{ipc::Channel, AppHandle, Runtime, State};
|
||||
use tauri::{ipc::Channel, AppHandle, Manager, ResourceId, Runtime};
|
||||
|
||||
use std::{
|
||||
sync::atomic::{AtomicBool, Ordering},
|
||||
time::Duration,
|
||||
};
|
||||
use std::time::Duration;
|
||||
use url::Url;
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
@@ -30,6 +27,7 @@ pub enum DownloadEvent {
|
||||
#[derive(Serialize, Default)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub(crate) struct Metadata {
|
||||
rid: Option<ResourceId>,
|
||||
available: bool,
|
||||
current_version: String,
|
||||
version: String,
|
||||
@@ -40,7 +38,6 @@ pub(crate) struct Metadata {
|
||||
#[tauri::command]
|
||||
pub(crate) async fn check<R: Runtime>(
|
||||
app: AppHandle<R>,
|
||||
pending: State<'_, PendingUpdate>,
|
||||
headers: Option<Vec<(String, String)>>,
|
||||
timeout: Option<u64>,
|
||||
proxy: Option<String>,
|
||||
@@ -72,7 +69,7 @@ pub(crate) async fn check<R: Runtime>(
|
||||
metadata.version = update.version.clone();
|
||||
metadata.date = update.date.map(|d| d.to_string());
|
||||
metadata.body = update.body.clone();
|
||||
pending.0.lock().await.replace(update);
|
||||
metadata.rid = Some(app.resources_table().add(update));
|
||||
}
|
||||
|
||||
Ok(metadata)
|
||||
@@ -80,30 +77,28 @@ pub(crate) async fn check<R: Runtime>(
|
||||
|
||||
#[tauri::command]
|
||||
pub(crate) async fn download_and_install<R: Runtime>(
|
||||
_app: AppHandle<R>,
|
||||
pending: State<'_, PendingUpdate>,
|
||||
app: AppHandle<R>,
|
||||
rid: ResourceId,
|
||||
on_event: Channel,
|
||||
) -> Result<()> {
|
||||
if let Some(pending) = &*pending.0.lock().await {
|
||||
let first_chunk = AtomicBool::new(false);
|
||||
let on_event_c = on_event.clone();
|
||||
pending
|
||||
.download_and_install(
|
||||
move |chunk_length, content_length| {
|
||||
if first_chunk.swap(false, Ordering::Acquire) {
|
||||
on_event
|
||||
.send(DownloadEvent::Started { content_length })
|
||||
.unwrap();
|
||||
}
|
||||
on_event
|
||||
.send(DownloadEvent::Progress { chunk_length })
|
||||
.unwrap();
|
||||
},
|
||||
move || {
|
||||
on_event_c.send(&DownloadEvent::Finished).unwrap();
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
let update = app.resources_table().get::<Update>(rid)?;
|
||||
|
||||
let mut first_chunk = true;
|
||||
|
||||
update
|
||||
.download_and_install(
|
||||
|chunk_length, content_length| {
|
||||
if first_chunk {
|
||||
first_chunk = !first_chunk;
|
||||
let _ = on_event.send(DownloadEvent::Started { content_length });
|
||||
}
|
||||
let _ = on_event.send(DownloadEvent::Progress { chunk_length });
|
||||
},
|
||||
|| {
|
||||
let _ = on_event.send(&DownloadEvent::Finished);
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user