updater.ts 3.1 KB

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