updater.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright 2019-2021 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. /**
  5. * Customize the auto updater flow.
  6. * This package is also accessible with `window.__TAURI__.updater` when `tauri.conf.json > build > withGlobalTauri` is set to true.
  7. * @packageDocumentation
  8. */
  9. import { once, listen, emit, UnlistenFn } from './event'
  10. type UpdateStatus = 'PENDING' | 'ERROR' | 'DONE' | 'UPTODATE'
  11. interface UpdateStatusResult {
  12. error?: string
  13. status: UpdateStatus
  14. }
  15. interface UpdateManifest {
  16. version: string
  17. date: string
  18. body: string
  19. }
  20. interface UpdateResult {
  21. manifest?: UpdateManifest
  22. shouldUpdate: boolean
  23. }
  24. /**
  25. * Install the update if there's one available.
  26. *
  27. * @return A promise indicating the success or failure of the operation.
  28. */
  29. async function installUpdate(): Promise<void> {
  30. let unlistenerFn: UnlistenFn | undefined
  31. function cleanListener(): void {
  32. if (unlistenerFn) {
  33. unlistenerFn()
  34. }
  35. unlistenerFn = undefined
  36. }
  37. return new Promise((resolve, reject) => {
  38. function onStatusChange(statusResult: UpdateStatusResult): void {
  39. if (statusResult.error) {
  40. cleanListener()
  41. return reject(statusResult.error)
  42. }
  43. // install complete
  44. if (statusResult.status === 'DONE') {
  45. cleanListener()
  46. return resolve()
  47. }
  48. }
  49. // listen status change
  50. listen('tauri://update-status', (data: { payload: any }) => {
  51. onStatusChange(data?.payload as UpdateStatusResult)
  52. })
  53. .then((fn) => {
  54. unlistenerFn = fn
  55. })
  56. .catch((e) => {
  57. cleanListener()
  58. // dispatch the error to our checkUpdate
  59. throw e
  60. })
  61. // start the process we dont require much security as it's
  62. // handled by rust
  63. emit('tauri://update-install').catch((e) => {
  64. cleanListener()
  65. // dispatch the error to our checkUpdate
  66. throw e
  67. })
  68. })
  69. }
  70. /**
  71. * Checks if an update is available.
  72. *
  73. * @return Promise resolving to the update status.
  74. */
  75. async function checkUpdate(): Promise<UpdateResult> {
  76. let unlistenerFn: UnlistenFn | undefined
  77. function cleanListener(): void {
  78. if (unlistenerFn) {
  79. unlistenerFn()
  80. }
  81. unlistenerFn = undefined
  82. }
  83. return new Promise((resolve, reject) => {
  84. function onUpdateAvailable(manifest: UpdateManifest): void {
  85. cleanListener()
  86. return resolve({
  87. manifest,
  88. shouldUpdate: true
  89. })
  90. }
  91. function onStatusChange(statusResult: UpdateStatusResult): void {
  92. if (statusResult.error) {
  93. cleanListener()
  94. return reject(statusResult.error)
  95. }
  96. if (statusResult.status === 'UPTODATE') {
  97. cleanListener()
  98. return resolve({
  99. shouldUpdate: false
  100. })
  101. }
  102. }
  103. // wait to receive the latest update
  104. once('tauri://update-available', (data: { payload: any }) => {
  105. onUpdateAvailable(data?.payload as UpdateManifest)
  106. }).catch((e) => {
  107. cleanListener()
  108. // dispatch the error to our checkUpdate
  109. throw e
  110. })
  111. // listen status change
  112. listen('tauri://update-status', (data: { payload: any }) => {
  113. onStatusChange(data?.payload as UpdateStatusResult)
  114. })
  115. .then((fn) => {
  116. unlistenerFn = fn
  117. })
  118. .catch((e) => {
  119. cleanListener()
  120. // dispatch the error to our checkUpdate
  121. throw e
  122. })
  123. // start the process
  124. emit('tauri://update').catch((e) => {
  125. cleanListener()
  126. // dispatch the error to our checkUpdate
  127. throw e
  128. })
  129. })
  130. }
  131. export type { UpdateStatus, UpdateStatusResult, UpdateManifest, UpdateResult }
  132. export { installUpdate, checkUpdate }