101101//! .setup(|app| {
102102//! let handle = app.handle();
103103//! tauri::async_runtime::spawn(async move {
104- //! let response = handle.check_for_updates ().await;
104+ //! let response = handle.updater().check ().await;
105105//! });
106106//! Ok(())
107107//! });
165165//! .setup(|app| {
166166//! let handle = app.handle();
167167//! tauri::async_runtime::spawn(async move {
168- //! match handle.check_for_updates ().await {
168+ //! match handle.updater().check ().await {
169169//! Ok(update) => {
170170//! if update.is_update_available() {
171171//! update.download_and_install().await.unwrap();
@@ -499,6 +499,115 @@ struct UpdateManifest {
499499 body : String ,
500500}
501501
502+ /// An update check builder.
503+ #[ derive( Debug ) ]
504+ pub struct UpdateBuilder < R : Runtime > {
505+ inner : core:: UpdateBuilder < R > ,
506+ events : bool ,
507+ }
508+
509+ impl < R : Runtime > UpdateBuilder < R > {
510+ /// Do not use the event system to emit information or listen to install the update.
511+ pub fn skip_events ( mut self ) -> Self {
512+ self . events = false ;
513+ self
514+ }
515+
516+ /// Set the target name. Represents the string that is looked up on the updater API or response JSON.
517+ pub fn target ( mut self , target : impl Into < String > ) -> Self {
518+ self . inner = self . inner . target ( target) ;
519+ self
520+ }
521+
522+ /// Sets a closure that is invoked to compare the current version and the latest version returned by the updater server.
523+ /// The first argument is the current version, and the second one is the latest version.
524+ ///
525+ /// The closure must return `true` if the update should be installed.
526+ ///
527+ /// # Examples
528+ ///
529+ /// - Always install the version returned by the server:
530+ ///
531+ /// ```no_run
532+ /// tauri::Builder::default()
533+ /// .setup(|app| {
534+ /// tauri::updater::builder(app.handle()).should_install(|_current, _latest| true);
535+ /// Ok(())
536+ /// });
537+ /// ```
538+ pub fn should_install < F : FnOnce ( & str , & str ) -> bool + Send + ' static > ( mut self , f : F ) -> Self {
539+ self . inner = self . inner . should_install ( f) ;
540+ self
541+ }
542+
543+ /// Check if an update is available.
544+ ///
545+ /// # Examples
546+ ///
547+ /// ```no_run
548+ /// tauri::Builder::default()
549+ /// .setup(|app| {
550+ /// let handle = app.handle();
551+ /// tauri::async_runtime::spawn(async move {
552+ /// match tauri::updater::builder(handle).check().await {
553+ /// Ok(update) => {}
554+ /// Err(error) => {}
555+ /// }
556+ /// });
557+ /// Ok(())
558+ /// });
559+ /// ```
560+ pub async fn check ( self ) -> Result < UpdateResponse < R > > {
561+ let handle = self . inner . app . clone ( ) ;
562+ let events = self . events ;
563+ // check updates
564+ match self . inner . build ( ) . await {
565+ Ok ( update) => {
566+ if events {
567+ // send notification if we need to update
568+ if update. should_update {
569+ let body = update. body . clone ( ) . unwrap_or_else ( || String :: from ( "" ) ) ;
570+
571+ // Emit `tauri://update-available`
572+ let _ = handle. emit_all (
573+ EVENT_UPDATE_AVAILABLE ,
574+ UpdateManifest {
575+ body : body. clone ( ) ,
576+ date : update. date . clone ( ) ,
577+ version : update. version . clone ( ) ,
578+ } ,
579+ ) ;
580+ let _ = handle. create_proxy ( ) . send_event ( EventLoopMessage :: Updater (
581+ UpdaterEvent :: UpdateAvailable {
582+ body,
583+ date : update. date . clone ( ) ,
584+ version : update. version . clone ( ) ,
585+ } ,
586+ ) ) ;
587+
588+ // Listen for `tauri://update-install`
589+ let update_ = update. clone ( ) ;
590+ handle. once_global ( EVENT_INSTALL_UPDATE , move |_msg| {
591+ crate :: async_runtime:: spawn ( async move {
592+ let _ = download_and_install ( update_) . await ;
593+ } ) ;
594+ } ) ;
595+ } else {
596+ send_status_update ( & handle, UpdaterEvent :: AlreadyUpToDate ) ;
597+ }
598+ }
599+ Ok ( UpdateResponse { update } )
600+ }
601+ Err ( e) => {
602+ if self . events {
603+ send_status_update ( & handle, UpdaterEvent :: Error ( e. to_string ( ) ) ) ;
604+ }
605+ Err ( e)
606+ }
607+ }
608+ }
609+ }
610+
502611/// The response of an updater check.
503612pub struct UpdateResponse < R : Runtime > {
504613 update : core:: Update < R > ,
@@ -582,7 +691,7 @@ pub(crate) fn listener<R: Runtime>(handle: AppHandle<R>) {
582691 handle. listen_global ( EVENT_CHECK_UPDATE , move |_msg| {
583692 let handle_ = handle_. clone ( ) ;
584693 crate :: async_runtime:: spawn ( async move {
585- let _ = check ( handle_. clone ( ) ) . await ;
694+ let _ = builder ( handle_. clone ( ) ) . check ( ) . await ;
586695 } ) ;
587696 } ) ;
588697}
@@ -617,7 +726,8 @@ pub(crate) async fn download_and_install<R: Runtime>(update: core::Update<R>) ->
617726 update_result
618727}
619728
620- pub ( crate ) async fn check < R : Runtime > ( handle : AppHandle < R > ) -> Result < UpdateResponse < R > > {
729+ /// Initializes the [`UpdateBuilder`] using the app configuration.
730+ pub fn builder < R : Runtime > ( handle : AppHandle < R > ) -> UpdateBuilder < R > {
621731 let updater_config = & handle. config ( ) . tauri . updater ;
622732 let package_info = handle. package_info ( ) . clone ( ) ;
623733
@@ -636,47 +746,9 @@ pub(crate) async fn check<R: Runtime>(handle: AppHandle<R>) -> Result<UpdateResp
636746 if let Some ( target) = & handle. updater_settings . target {
637747 builder = builder. target ( target) ;
638748 }
639-
640- // check updates
641- match builder. build ( ) . await {
642- Ok ( update) => {
643- // send notification if we need to update
644- if update. should_update {
645- let body = update. body . clone ( ) . unwrap_or_else ( || String :: from ( "" ) ) ;
646-
647- // Emit `tauri://update-available`
648- let _ = handle. emit_all (
649- EVENT_UPDATE_AVAILABLE ,
650- UpdateManifest {
651- body : body. clone ( ) ,
652- date : update. date . clone ( ) ,
653- version : update. version . clone ( ) ,
654- } ,
655- ) ;
656- let _ = handle. create_proxy ( ) . send_event ( EventLoopMessage :: Updater (
657- UpdaterEvent :: UpdateAvailable {
658- body,
659- date : update. date . clone ( ) ,
660- version : update. version . clone ( ) ,
661- } ,
662- ) ) ;
663-
664- // Listen for `tauri://update-install`
665- let update_ = update. clone ( ) ;
666- handle. once_global ( EVENT_INSTALL_UPDATE , move |_msg| {
667- crate :: async_runtime:: spawn ( async move {
668- let _ = download_and_install ( update_) . await ;
669- } ) ;
670- } ) ;
671- } else {
672- send_status_update ( & handle, UpdaterEvent :: AlreadyUpToDate ) ;
673- }
674- Ok ( UpdateResponse { update } )
675- }
676- Err ( e) => {
677- send_status_update ( & handle, UpdaterEvent :: Error ( e. to_string ( ) ) ) ;
678- Err ( e)
679- }
749+ UpdateBuilder {
750+ inner : builder,
751+ events : true ,
680752 }
681753}
682754
0 commit comments