mirror of
https://github.com/amitwh/markdown-converter.git
synced 2026-08-03 02:11:07 +05:30
feat(updater): add UpdaterService wrapping electron-updater lifecycle
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
const { EventEmitter } = require('events');
|
||||
const DEBOUNCE_MS = 60_000;
|
||||
|
||||
class UpdaterService extends EventEmitter {
|
||||
constructor(autoUpdater) {
|
||||
super();
|
||||
this.autoUpdater = autoUpdater;
|
||||
this.state = 'idle';
|
||||
this.lastCheckAt = 0;
|
||||
this._wire();
|
||||
}
|
||||
|
||||
_wire() {
|
||||
const au = this.autoUpdater;
|
||||
au.on('checking-for-update', () => this._emit({ state: 'checking' }));
|
||||
au.on('update-available', (info) => this._emit({ state: 'available', version: info.version }));
|
||||
au.on('download-progress', (p) => this._emit({ state: 'downloading', percent: p.percent }));
|
||||
au.on('update-downloaded', (info) => this._emit({ state: 'ready', version: info.version }));
|
||||
au.on('update-not-available', () => this._emit({ state: 'idle' }));
|
||||
au.on('error', (err) => {
|
||||
const code = err && /ENOTFOUND|ETIMEDOUT|ECONNREFUSED/.test(err.message) ? 'NETWORK' : 'UNKNOWN';
|
||||
this._emit({ state: 'error', code });
|
||||
});
|
||||
}
|
||||
|
||||
_emit(payload) {
|
||||
this.state = payload.state;
|
||||
this.emit('status', payload);
|
||||
}
|
||||
|
||||
async check() {
|
||||
if (Date.now() - this.lastCheckAt < DEBOUNCE_MS) return;
|
||||
this.lastCheckAt = Date.now();
|
||||
await this.autoUpdater.checkForUpdates();
|
||||
}
|
||||
|
||||
install() {
|
||||
this.autoUpdater.quitAndInstall();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { UpdaterService };
|
||||
Reference in New Issue
Block a user