Skip to content

Commit cd8e5d5

Browse files
committed
feat: Trigger check updates menu item enabling/disabling
1 parent 423109d commit cd8e5d5

3 files changed

Lines changed: 40 additions & 38 deletions

File tree

packages/neuron-wallet/src/controllers/app/menu.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { app, shell, BrowserWindow, dialog, MenuItemConstructorOptions, clipboard, Menu, MenuItem, MessageBoxOptions, MessageBoxReturnValue } from 'electron'
1+
import { app, shell, BrowserWindow, dialog, MenuItemConstructorOptions, clipboard, Menu, MessageBoxOptions, MessageBoxReturnValue } from 'electron'
22
import { bech32Address, AddressPrefix, AddressType } from '@nervosnetwork/ckb-sdk-utils'
33
import i18n from 'utils/i18n'
44
import env from 'env'
@@ -77,10 +77,10 @@ const updateApplicationMenu = (mainWindow: BrowserWindow | null) => {
7777
click: () => { showAbout() },
7878
},
7979
{
80-
enabled: isMainWindow,
8180
label: i18n.t('application-menu.neuron.check-updates'),
82-
click: (menuItem: MenuItem) => {
83-
new UpdateController().checkUpdates(menuItem)
81+
enabled: isMainWindow && !UpdateController.isChecking,
82+
click: () => {
83+
new UpdateController().checkUpdates()
8484
navTo(URL.Preference)
8585
}
8686
},
@@ -228,8 +228,9 @@ const updateApplicationMenu = (mainWindow: BrowserWindow | null) => {
228228
})
229229
helpSubmenu.push({
230230
label: i18n.t('application-menu.neuron.check-updates'),
231-
click: (menuItem: MenuItem) => {
232-
new UpdateController().checkUpdates(menuItem)
231+
enabled: isMainWindow && !UpdateController.isChecking,
232+
click: () => {
233+
new UpdateController().checkUpdates()
233234
navTo(URL.Preference)
234235
}
235236
})

packages/neuron-wallet/src/controllers/app/subscribe.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export const subscribe = (dispatcher: AppResponder) => {
5555
})
5656

5757
AppUpdaterSubject.subscribe(params => {
58+
dispatcher.updateMenu()
5859
dispatcher.sendMessage('app-updater-updated', params)
5960
})
6061
}
Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,44 @@
11
import { dialog } from 'electron'
22
import { autoUpdater, UpdateInfo } from 'electron-updater'
33
import i18n from 'utils/i18n'
4+
import AppUpdaterSubject from 'models/subjects/app-updater'
45

56
export default class UpdateController {
6-
sender: { enabled: boolean } | null
7+
static isChecking = false // One instance is already running and checking
78

89
constructor() {
910
autoUpdater.autoDownload = false
10-
this.sender = null
1111

12-
this.bindEvents()
13-
}
14-
15-
// TODO: refactor: do not require sender
16-
public checkUpdates(sender: { enabled: boolean } | null = null) {
17-
this.sender = sender
18-
if (this.sender) {
19-
this.sender.enabled = false
12+
if (!UpdateController.isChecking) {
13+
this.bindEvents()
2014
}
15+
}
2116

17+
public checkUpdates() {
18+
UpdateController.isChecking = true
2219
autoUpdater.checkForUpdates()
20+
21+
AppUpdaterSubject.next({
22+
checking: true,
23+
downloadProgress: -1,
24+
version: '',
25+
releaseNotes: ''
26+
})
2327
}
2428

2529
bindEvents() {
2630
autoUpdater.removeAllListeners()
2731

2832
autoUpdater.on('error', error => {
2933
dialog.showErrorBox('Error', error == null ? 'unknown' : (error.stack || error).toString())
30-
this.enableSender()
34+
35+
UpdateController.isChecking = false
36+
this.notify()
3137
})
3238

3339
autoUpdater.on('update-available', (info: UpdateInfo) => {
34-
const { version } = info
35-
dialog
36-
.showMessageBox({
37-
type: 'info',
38-
title: version,
39-
message: i18n.t('updater.updates-found-do-you-want-to-update', { version }),
40-
buttons: [i18n.t('updater.update-now'), i18n.t('common.no')],
41-
})
42-
.then(returnValue => {
43-
if (returnValue.response === 0) {
44-
autoUpdater.downloadUpdate()
45-
} else {
46-
this.enableSender()
47-
}
48-
})
40+
UpdateController.isChecking = false
41+
this.notify(-1, info.version, 'todo')
4942
})
5043

5144
autoUpdater.on('update-not-available', () => {
@@ -54,7 +47,9 @@ export default class UpdateController {
5447
message: i18n.t('updater.update-not-available'),
5548
buttons: [i18n.t('common.ok')],
5649
})
57-
this.enableSender()
50+
51+
UpdateController.isChecking = false
52+
this.notify()
5853
})
5954

6055
autoUpdater.on('update-downloaded', () => {
@@ -67,13 +62,18 @@ export default class UpdateController {
6762
.then(() => {
6863
setImmediate(() => autoUpdater.quitAndInstall())
6964
})
65+
66+
UpdateController.isChecking = false
67+
this.notify(1, 'toto', '')
7068
})
7169
}
7270

73-
enableSender() {
74-
if (this.sender) {
75-
this.sender.enabled = true
76-
}
77-
this.sender = null
71+
private notify(downloadProgress: number = -1, version = '', releaseNotes = '') {
72+
AppUpdaterSubject.next({
73+
checking: UpdateController.isChecking,
74+
downloadProgress,
75+
version,
76+
releaseNotes
77+
})
7878
}
7979
}

0 commit comments

Comments
 (0)