Skip to content

Commit cd39d5d

Browse files
committed
New: Created update checking class.
1 parent 2a2e085 commit cd39d5d

File tree

5 files changed

+141
-35
lines changed

5 files changed

+141
-35
lines changed

app/main.js

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@ const app = electron.app;
33
const ipc = require('electron').ipcMain;
44
const dialog = require('electron').dialog;
55
const {ArcWindowsManager} = require('./scripts/windows-manager');
6-
const log = require('electron-log');
7-
const {autoUpdater} = require('electron-updater');
8-
autoUpdater.logger = log;
9-
autoUpdater.logger.transports.file.level = 'info';
10-
log.info('App starting...');
6+
const {UpdateStatus} = require('./scripts/update-status');
117

128
class Arc {
139
constructor() {
1410
this.wm = new ArcWindowsManager();
11+
this.us = new UpdateStatus(this.wm);
1512
}
1613

1714
attachListeners() {
@@ -22,7 +19,7 @@ class Arc {
2219

2320
_readyHandler() {
2421
this.wm.open();
25-
autoUpdater.checkForUpdates();
22+
this.us.start();
2623
}
2724
// Quits when all windows are closed.
2825
_allClosedHandler() {
@@ -37,10 +34,6 @@ class Arc {
3734
this.wm.open();
3835
}
3936
}
40-
41-
notifyWindows(type, ...args) {
42-
this.wm.notifyAll(type, args);
43-
}
4437
}
4538

4639
const arcApp = new Arc();
@@ -67,24 +60,3 @@ ipc.on('new-window', function() {
6760
ipc.on('toggle-devtools', (event) => {
6861
event.sender.webContents.toggleDevTools();
6962
});
70-
71-
// Auto updater
72-
autoUpdater.on('checking-for-update', () => {
73-
arcApp.notifyWindows('autoupdate-checking-for-update');
74-
});
75-
autoUpdater.on('update-available', (info) => {
76-
arcApp.notifyWindows('autoupdate-update-available', info);
77-
});
78-
autoUpdater.on('update-not-available', (info) => {
79-
arcApp.notifyWindows('autoupdate-update-not-available', info);
80-
});
81-
autoUpdater.on('error', (err) => {
82-
arcApp.notifyWindows('autoupdate-error', err);
83-
});
84-
autoUpdater.on('download-progress', (progressObj) => {
85-
arcApp.notifyWindows('autoupdate-download-progress', progressObj);
86-
});
87-
autoUpdater.on('update-downloaded', (info) => {
88-
arcApp.notifyWindows('autoupdate-update-downloaded', info);
89-
// autoUpdater.quitAndInstall();
90-
});

app/scripts/latest-request.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const path = require('path');
2-
const fs = require('fs-extra');
32
const {ArcPreferences} = require('./arc-preferences');
43
/**
54
* A module responsible for storing / restoring latest request from user FS.

app/scripts/update-status.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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;

app/src/arc-electron.html

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@
252252
</template>
253253
<script>
254254
const {ArcPreferences} = require('./scripts/arc-preferences');
255+
const ipc = require('electron').ipcRenderer;
255256

256257
Polymer({
257258
is: 'arc-electron',
@@ -288,7 +289,15 @@
288289
},
289290

290291
attached: function() {
291-
this.listen(document.body, 'display-license', 'openLicense');
292+
Polymer.RenderStatus.afterNextRender(this, function() {
293+
this.listen(document.body, 'display-license', 'openLicense');
294+
this._observeMainEvents();
295+
});
296+
},
297+
298+
detacheded: function() {
299+
this.unlisten(document.body, 'display-license', 'openLicense');
300+
this._unobserveMainEvents();
292301
},
293302

294303
listeners: {
@@ -313,6 +322,31 @@
313322
}
314323
},
315324

325+
// Starts listening for main script events.
326+
_observeMainEvents: function() {
327+
if (!this.__ueh) {
328+
this.__ueh = this._updateEventHandler.bind(this);
329+
}
330+
ipc.on('checking-for-update', this.__ueh);
331+
ipc.on('update-available', this.__ueh);
332+
ipc.on('update-not-available', this.__ueh);
333+
ipc.on('autoupdate-error', this.__ueh);
334+
ipc.on('download-progress', this.__ueh);
335+
ipc.on('update-downloaded', this.__ueh);
336+
},
337+
338+
_unobserveMainEvents: function() {
339+
if (!this.__ueh) {
340+
return;
341+
}
342+
ipc.removeListener('checking-for-update', this.__ueh);
343+
ipc.removeListener('update-available', this.__ueh);
344+
ipc.removeListener('update-not-available', this.__ueh);
345+
ipc.removeListener('autoupdate-error', this.__ueh);
346+
ipc.removeListener('download-progress', this.__ueh);
347+
ipc.removeListener('update-downloaded', this.__ueh);
348+
},
349+
316350
_navigateHandler: function(e) {
317351
var params = e.detail;
318352
var url;
@@ -458,19 +492,22 @@
458492
},
459493

460494
onNewWindow: function() {
461-
const ipc = require('electron').ipcRenderer;
462495
ipc.send('new-window');
463496
},
464497

465498
openLogs: function() {
466-
const ipc = require('electron').ipcRenderer;
467499
ipc.send('toggle-devtools');
468500
},
469501

470502
_copyContentHandler: function(e) {
471503
const {clipboard} = require('electron');
472504
clipboard.writeText(e.detail.value);
473505
e.preventDefault();
506+
},
507+
508+
_updateEventHandler: function(...args) {
509+
// TODO: add support for update events
510+
console.log('Update event: ', args);
474511
}
475512
});
476513
</script>

app/src/file-export/file-export.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<script>
2+
(function() {
23
const ipc = require('electron').ipcRenderer;
34
const fs = require('fs-extra');
45
Polymer({
@@ -81,4 +82,5 @@
8182
return data;
8283
}
8384
});
85+
})();
8486
</script>

0 commit comments

Comments
 (0)