|
| 1 | +const {ArcBase} = require('./arc-base'); |
| 2 | +const {autoUpdater} = require('electron-updater'); |
| 3 | +const log = require('electron-log'); |
| 4 | +autoUpdater.logger = log; |
| 5 | +autoUpdater.logger.transports.file.level = 'info'; |
| 6 | + |
| 7 | +/** |
| 8 | + * A module responsible for storing / restoring user settings. |
| 9 | + */ |
| 10 | +class UpdateStatus extends ArcBase { |
| 11 | + constructor(windowManager) { |
| 12 | + super(); |
| 13 | + this.wm = windowManager; |
| 14 | + this.state = 0; |
| 15 | + this.lastInfoObject = undefined; |
| 16 | + this._ensureScope(); |
| 17 | + this._addListeners(); |
| 18 | + } |
| 19 | + /** |
| 20 | + * Checks for app update. |
| 21 | + * This function **must** be called after the app ready event. |
| 22 | + */ |
| 23 | + start() { |
| 24 | + log.info('Initializing Auto Updater...'); |
| 25 | + setTimeout(() => { |
| 26 | + this.check(); |
| 27 | + }, 1000); |
| 28 | + } |
| 29 | + |
| 30 | + _ensureScope() { |
| 31 | + this._checkingHandler = this._checkingHandler.bind(this); |
| 32 | + this._updateAvailableHandler = this._updateAvailableHandler.bind(this); |
| 33 | + this._updateNotAvailableHandler = this._updateNotAvailableHandler.bind(this); |
| 34 | + this._updateErrorHandler = this._updateErrorHandler.bind(this); |
| 35 | + this._downloadProgressHandler = this._downloadProgressHandler.bind(this); |
| 36 | + this._downloadReadyHandler = this._downloadReadyHandler.bind(this); |
| 37 | + } |
| 38 | + |
| 39 | + _addListeners() { |
| 40 | + // Auto updater |
| 41 | + autoUpdater.on('checking-for-update', this._checkingHandler); |
| 42 | + autoUpdater.on('update-available', this._updateAvailableHandler); |
| 43 | + autoUpdater.on('update-not-available', this._updateNotAvailableHandler); |
| 44 | + autoUpdater.on('error', this._updateErrorHandler); |
| 45 | + autoUpdater.on('download-progress', this._downloadProgressHandler); |
| 46 | + autoUpdater.on('update-downloaded', this._downloadReadyHandler); |
| 47 | + } |
| 48 | + |
| 49 | + check() { |
| 50 | + log.info('Checking for update'); |
| 51 | + autoUpdater.checkForUpdates(); |
| 52 | + } |
| 53 | + |
| 54 | + notifyWindows(type, ...args) { |
| 55 | + var data = [type]; |
| 56 | + data = data.concat(args); |
| 57 | + this.wm.notifyAll(type, data); |
| 58 | + } |
| 59 | + |
| 60 | + _checkingHandler() { |
| 61 | + this.state = 1; |
| 62 | + this.lastInfoObject = undefined; |
| 63 | + this.notifyWindows('autoupdate-checking-for-update'); |
| 64 | + } |
| 65 | + |
| 66 | + _updateAvailableHandler(info) { |
| 67 | + this.state = 2; |
| 68 | + this.lastInfoObject = info; |
| 69 | + this.notifyWindows('autoupdate-update-available', info); |
| 70 | + } |
| 71 | + |
| 72 | + _updateNotAvailableHandler(info) { |
| 73 | + this.state = 3; |
| 74 | + this.lastInfoObject = info; |
| 75 | + this.notifyWindows('autoupdate-update-not-available', info); |
| 76 | + } |
| 77 | + |
| 78 | + _updateErrorHandler(error) { |
| 79 | + this.state = 4; |
| 80 | + this.lastInfoObject = error; |
| 81 | + this.notifyWindows('autoupdate-error', error); |
| 82 | + } |
| 83 | + |
| 84 | + _downloadProgressHandler(progressObj) { |
| 85 | + this.state = 5; |
| 86 | + this.lastInfoObject = progressObj; |
| 87 | + this.notifyWindows('autoupdate-download-progress', progressObj); |
| 88 | + } |
| 89 | + _downloadReadyHandler(info) { |
| 90 | + this.state = 6; |
| 91 | + this.lastInfoObject = info; |
| 92 | + this.notifyWindows('autoupdate-update-downloaded', info); |
| 93 | + // autoUpdater.quitAndInstall(); |
| 94 | + } |
| 95 | +} |
| 96 | +exports.UpdateStatus = UpdateStatus; |
0 commit comments