@@ -45,6 +45,7 @@ class Update extends Resource {
4545 version : string ;
4646 date ?: string ;
4747 body ?: string ;
48+ private downloadedBytes ?: Resource ;
4849
4950 constructor ( metadata : UpdateMetadata ) {
5051 super ( metadata . rid ) ;
@@ -55,6 +56,34 @@ class Update extends Resource {
5556 this . body = metadata . body ;
5657 }
5758
59+ /** Download the updater package */
60+ async download ( onEvent ?: ( progress : DownloadEvent ) => void ) : Promise < void > {
61+ const channel = new Channel < DownloadEvent > ( ) ;
62+ if ( onEvent ) {
63+ channel . onmessage = onEvent ;
64+ }
65+ const downloadedBytesRid = await invoke < number > ( "plugin:updater|download" , {
66+ onEvent : channel ,
67+ rid : this . rid ,
68+ } ) ;
69+ this . downloadedBytes = new Resource ( downloadedBytesRid ) ;
70+ }
71+
72+ /** Install downloaded updater package */
73+ async install ( ) : Promise < void > {
74+ if ( ! this . downloadedBytes ) {
75+ throw "Update.install called before Update.download" ;
76+ }
77+
78+ await invoke ( "plugin:updater|install" , {
79+ updateRid : this . rid ,
80+ bytesRid : this . downloadedBytes . rid ,
81+ } ) ;
82+
83+ // Don't need to call close, we did it in rust side already
84+ this . downloadedBytes = undefined ;
85+ }
86+
5887 /** Downloads the updater package and installs it */
5988 async downloadAndInstall (
6089 onEvent ?: ( progress : DownloadEvent ) => void ,
@@ -68,6 +97,11 @@ class Update extends Resource {
6897 rid : this . rid ,
6998 } ) ;
7099 }
100+
101+ async close ( ) : Promise < void > {
102+ await this . downloadedBytes ?. close ( ) ;
103+ await super . close ( ) ;
104+ }
71105}
72106
73107/** Check for updates, resolves to `null` if no updates are available */
0 commit comments