22// SPDX-License-Identifier: Apache-2.0
33// SPDX-License-Identifier: MIT
44
5- use crate :: { PendingUpdate , Result , UpdaterExt } ;
5+ use crate :: { Result , Update , UpdaterExt } ;
66
77use serde:: Serialize ;
8- use tauri:: { ipc:: Channel , AppHandle , Runtime , State } ;
8+ use tauri:: { ipc:: Channel , AppHandle , Manager , ResourceId , Runtime } ;
99
10- use std:: {
11- sync:: atomic:: { AtomicBool , Ordering } ,
12- time:: Duration ,
13- } ;
10+ use std:: time:: Duration ;
1411use url:: Url ;
1512
1613#[ derive( Debug , Serialize ) ]
@@ -30,6 +27,7 @@ pub enum DownloadEvent {
3027#[ derive( Serialize , Default ) ]
3128#[ serde( rename_all = "camelCase" ) ]
3229pub ( crate ) struct Metadata {
30+ rid : Option < ResourceId > ,
3331 available : bool ,
3432 current_version : String ,
3533 version : String ,
@@ -40,7 +38,6 @@ pub(crate) struct Metadata {
4038#[ tauri:: command]
4139pub ( crate ) async fn check < R : Runtime > (
4240 app : AppHandle < R > ,
43- pending : State < ' _ , PendingUpdate > ,
4441 headers : Option < Vec < ( String , String ) > > ,
4542 timeout : Option < u64 > ,
4643 proxy : Option < String > ,
@@ -72,38 +69,36 @@ pub(crate) async fn check<R: Runtime>(
7269 metadata. version = update. version . clone ( ) ;
7370 metadata. date = update. date . map ( |d| d. to_string ( ) ) ;
7471 metadata. body = update. body . clone ( ) ;
75- pending . 0 . lock ( ) . await . replace ( update) ;
72+ metadata . rid = Some ( app . resources_table ( ) . add ( update) ) ;
7673 }
7774
7875 Ok ( metadata)
7976}
8077
8178#[ tauri:: command]
8279pub ( crate ) async fn download_and_install < R : Runtime > (
83- _app : AppHandle < R > ,
84- pending : State < ' _ , PendingUpdate > ,
80+ app : AppHandle < R > ,
81+ rid : ResourceId ,
8582 on_event : Channel ,
8683) -> Result < ( ) > {
87- if let Some ( pending) = & * pending. 0 . lock ( ) . await {
88- let first_chunk = AtomicBool :: new ( false ) ;
89- let on_event_c = on_event. clone ( ) ;
90- pending
91- . download_and_install (
92- move |chunk_length, content_length| {
93- if first_chunk. swap ( false , Ordering :: Acquire ) {
94- on_event
95- . send ( DownloadEvent :: Started { content_length } )
96- . unwrap ( ) ;
97- }
98- on_event
99- . send ( DownloadEvent :: Progress { chunk_length } )
100- . unwrap ( ) ;
101- } ,
102- move || {
103- on_event_c. send ( & DownloadEvent :: Finished ) . unwrap ( ) ;
104- } ,
105- )
106- . await ?;
107- }
84+ let update = app. resources_table ( ) . get :: < Update > ( rid) ?;
85+
86+ let mut first_chunk = true ;
87+
88+ update
89+ . download_and_install (
90+ |chunk_length, content_length| {
91+ if first_chunk {
92+ first_chunk = !first_chunk;
93+ let _ = on_event. send ( DownloadEvent :: Started { content_length } ) ;
94+ }
95+ let _ = on_event. send ( DownloadEvent :: Progress { chunk_length } ) ;
96+ } ,
97+ || {
98+ let _ = on_event. send ( & DownloadEvent :: Finished ) ;
99+ } ,
100+ )
101+ . await ?;
102+
108103 Ok ( ( ) )
109104}
0 commit comments