updater.cjs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. 'use strict';
  2. var event = require('./event.cjs');
  3. // Copyright 2019-2023 Tauri Programme within The Commons Conservancy
  4. // SPDX-License-Identifier: Apache-2.0
  5. // SPDX-License-Identifier: MIT
  6. /**
  7. * Customize the auto updater flow.
  8. *
  9. * This package is also accessible with `window.__TAURI__.updater` when [`build.withGlobalTauri`](https://tauri.app/v1/api/config/#buildconfig.withglobaltauri) in `tauri.conf.json` is set to `true`.
  10. * @module
  11. */
  12. /**
  13. * Listen to an updater event.
  14. * @example
  15. * ```typescript
  16. * import { onUpdaterEvent } from "@tauri-apps/api/updater";
  17. * const unlisten = await onUpdaterEvent(({ error, status }) => {
  18. * console.log('Updater event', error, status);
  19. * });
  20. *
  21. * // you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
  22. * unlisten();
  23. * ```
  24. *
  25. * @returns A promise resolving to a function to unlisten to the event.
  26. * Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.
  27. *
  28. * @since 1.0.2
  29. */
  30. async function onUpdaterEvent(handler) {
  31. return event.listen(event.TauriEvent.STATUS_UPDATE, (data) => {
  32. handler(data === null || data === void 0 ? void 0 : data.payload);
  33. });
  34. }
  35. /**
  36. * Install the update if there's one available.
  37. * @example
  38. * ```typescript
  39. * import { checkUpdate, installUpdate } from '@tauri-apps/api/updater';
  40. * const update = await checkUpdate();
  41. * if (update.shouldUpdate) {
  42. * console.log(`Installing update ${update.manifest?.version}, ${update.manifest?.date}, ${update.manifest.body}`);
  43. * await installUpdate();
  44. * }
  45. * ```
  46. *
  47. * @return A promise indicating the success or failure of the operation.
  48. *
  49. * @since 1.0.0
  50. */
  51. async function installUpdate() {
  52. let unlistenerFn;
  53. function cleanListener() {
  54. if (unlistenerFn) {
  55. unlistenerFn();
  56. }
  57. unlistenerFn = undefined;
  58. }
  59. return new Promise((resolve, reject) => {
  60. function onStatusChange(statusResult) {
  61. if (statusResult.error) {
  62. cleanListener();
  63. reject(statusResult.error);
  64. return;
  65. }
  66. // install complete
  67. if (statusResult.status === 'DONE') {
  68. cleanListener();
  69. resolve();
  70. }
  71. }
  72. // listen status change
  73. onUpdaterEvent(onStatusChange)
  74. .then((fn) => {
  75. unlistenerFn = fn;
  76. })
  77. .catch((e) => {
  78. cleanListener();
  79. // dispatch the error to our checkUpdate
  80. throw e;
  81. });
  82. // start the process we dont require much security as it's
  83. // handled by rust
  84. event.emit(event.TauriEvent.INSTALL_UPDATE).catch((e) => {
  85. cleanListener();
  86. // dispatch the error to our checkUpdate
  87. throw e;
  88. });
  89. });
  90. }
  91. /**
  92. * Checks if an update is available.
  93. * @example
  94. * ```typescript
  95. * import { checkUpdate } from '@tauri-apps/api/updater';
  96. * const update = await checkUpdate();
  97. * // now run installUpdate() if needed
  98. * ```
  99. *
  100. * @return Promise resolving to the update status.
  101. *
  102. * @since 1.0.0
  103. */
  104. async function checkUpdate() {
  105. let unlistenerFn;
  106. function cleanListener() {
  107. if (unlistenerFn) {
  108. unlistenerFn();
  109. }
  110. unlistenerFn = undefined;
  111. }
  112. return new Promise((resolve, reject) => {
  113. function onUpdateAvailable(manifest) {
  114. cleanListener();
  115. resolve({
  116. manifest,
  117. shouldUpdate: true
  118. });
  119. }
  120. function onStatusChange(statusResult) {
  121. if (statusResult.error) {
  122. cleanListener();
  123. reject(statusResult.error);
  124. return;
  125. }
  126. if (statusResult.status === 'UPTODATE') {
  127. cleanListener();
  128. resolve({
  129. shouldUpdate: false
  130. });
  131. }
  132. }
  133. // wait to receive the latest update
  134. event.once(event.TauriEvent.UPDATE_AVAILABLE, (data) => {
  135. onUpdateAvailable(data === null || data === void 0 ? void 0 : data.payload);
  136. }).catch((e) => {
  137. cleanListener();
  138. // dispatch the error to our checkUpdate
  139. throw e;
  140. });
  141. // listen status change
  142. onUpdaterEvent(onStatusChange)
  143. .then((fn) => {
  144. unlistenerFn = fn;
  145. })
  146. .catch((e) => {
  147. cleanListener();
  148. // dispatch the error to our checkUpdate
  149. throw e;
  150. });
  151. // start the process
  152. event.emit(event.TauriEvent.CHECK_UPDATE).catch((e) => {
  153. cleanListener();
  154. // dispatch the error to our checkUpdate
  155. throw e;
  156. });
  157. });
  158. }
  159. exports.checkUpdate = checkUpdate;
  160. exports.installUpdate = installUpdate;
  161. exports.onUpdaterEvent = onUpdaterEvent;