Skip to content

Commit a2ada57

Browse files
Implement all electron-updater events
1 parent 0cede61 commit a2ada57

File tree

6 files changed

+302
-4
lines changed

6 files changed

+302
-4
lines changed

ElectronNET.API/AutoUpdater.cs

Lines changed: 175 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
using ElectronNET.API.Entities;
22
using Newtonsoft.Json.Linq;
33
using System;
4-
using System.Collections.Generic;
5-
using System.Text;
6-
using System.Threading;
74
using System.Threading.Tasks;
85

96
namespace ElectronNET.API
@@ -13,6 +10,181 @@ namespace ElectronNET.API
1310
/// </summary>
1411
public sealed class AutoUpdater
1512
{
13+
/// <summary>
14+
/// Emitted when there is an error while updating.
15+
/// </summary>
16+
public event Action<string> OnError
17+
{
18+
add
19+
{
20+
if (_error == null)
21+
{
22+
BridgeConnector.Socket.On("autoUpdater-error" + GetHashCode(), (message) =>
23+
{
24+
_error(message.ToString());
25+
});
26+
27+
BridgeConnector.Socket.Emit("register-autoUpdater-error-event", GetHashCode());
28+
}
29+
_error += value;
30+
}
31+
remove
32+
{
33+
_error -= value;
34+
35+
if (_error == null)
36+
BridgeConnector.Socket.Off("autoUpdater-error" + GetHashCode());
37+
}
38+
}
39+
40+
private event Action<string> _error;
41+
42+
/// <summary>
43+
/// Emitted when checking if an update has started.
44+
/// </summary>
45+
public event Action OnCheckingForUpdate
46+
{
47+
add
48+
{
49+
if (_checkingForUpdate == null)
50+
{
51+
BridgeConnector.Socket.On("autoUpdater-checking-for-update" + GetHashCode(), () =>
52+
{
53+
_checkingForUpdate();
54+
});
55+
56+
BridgeConnector.Socket.Emit("register-autoUpdater-checking-for-update-event", GetHashCode());
57+
}
58+
_checkingForUpdate += value;
59+
}
60+
remove
61+
{
62+
_checkingForUpdate -= value;
63+
64+
if (_checkingForUpdate == null)
65+
BridgeConnector.Socket.Off("autoUpdater-checking-for-update" + GetHashCode());
66+
}
67+
}
68+
69+
private event Action _checkingForUpdate;
70+
71+
/// <summary>
72+
/// Emitted when there is an available update.
73+
/// The update is downloaded automatically if AutoDownload is true.
74+
/// </summary>
75+
public event Action<UpdateInfo> OnUpdateAvailable
76+
{
77+
add
78+
{
79+
if (_updateAvailable == null)
80+
{
81+
BridgeConnector.Socket.On("autoUpdater-update-available" + GetHashCode(), (updateInfo) =>
82+
{
83+
_updateAvailable(JObject.Parse(updateInfo.ToString()).ToObject<UpdateInfo>());
84+
});
85+
86+
BridgeConnector.Socket.Emit("register-autoUpdater-update-available-event", GetHashCode());
87+
}
88+
_updateAvailable += value;
89+
}
90+
remove
91+
{
92+
_updateAvailable -= value;
93+
94+
if (_updateAvailable == null)
95+
BridgeConnector.Socket.Off("autoUpdater-update-available" + GetHashCode());
96+
}
97+
}
98+
99+
private event Action<UpdateInfo> _updateAvailable;
100+
101+
/// <summary>
102+
/// Emitted when there is no available update.
103+
/// </summary>
104+
public event Action<UpdateInfo> OnUpdateNotAvailable
105+
{
106+
add
107+
{
108+
if (_updateNotAvailable == null)
109+
{
110+
BridgeConnector.Socket.On("autoUpdater-update-not-available" + GetHashCode(), (updateInfo) =>
111+
{
112+
_updateNotAvailable(JObject.Parse(updateInfo.ToString()).ToObject<UpdateInfo>());
113+
});
114+
115+
BridgeConnector.Socket.Emit("register-autoUpdater-update-not-available-event", GetHashCode());
116+
}
117+
_updateNotAvailable += value;
118+
}
119+
remove
120+
{
121+
_updateNotAvailable -= value;
122+
123+
if (_updateNotAvailable == null)
124+
BridgeConnector.Socket.Off("autoUpdater-update-not-available" + GetHashCode());
125+
}
126+
}
127+
128+
private event Action<UpdateInfo> _updateNotAvailable;
129+
130+
/// <summary>
131+
/// Emitted on download progress.
132+
/// </summary>
133+
public event Action<ProgressInfo> OnDownloadProgress
134+
{
135+
add
136+
{
137+
if (_downloadProgress == null)
138+
{
139+
BridgeConnector.Socket.On("autoUpdater-download-progress" + GetHashCode(), (progressInfo) =>
140+
{
141+
_downloadProgress(JObject.Parse(progressInfo.ToString()).ToObject<ProgressInfo>());
142+
});
143+
144+
BridgeConnector.Socket.Emit("register-autoUpdater-download-progress-event", GetHashCode());
145+
}
146+
_downloadProgress += value;
147+
}
148+
remove
149+
{
150+
_downloadProgress -= value;
151+
152+
if (_downloadProgress == null)
153+
BridgeConnector.Socket.Off("autoUpdater-download-progress" + GetHashCode());
154+
}
155+
}
156+
157+
private event Action<ProgressInfo> _downloadProgress;
158+
159+
/// <summary>
160+
/// Emitted on download complete.
161+
/// </summary>
162+
public event Action<UpdateInfo> OnUpdateDownloaded
163+
{
164+
add
165+
{
166+
if (_updateDownloaded == null)
167+
{
168+
BridgeConnector.Socket.On("autoUpdater-update-downloaded" + GetHashCode(), (updateInfo) =>
169+
{
170+
_updateDownloaded(JObject.Parse(updateInfo.ToString()).ToObject<UpdateInfo>());
171+
});
172+
173+
BridgeConnector.Socket.Emit("register-autoUpdater-update-downloaded-event", GetHashCode());
174+
}
175+
_updateDownloaded += value;
176+
}
177+
remove
178+
{
179+
_updateDownloaded -= value;
180+
181+
if (_updateDownloaded == null)
182+
BridgeConnector.Socket.Off("autoUpdater-update-downloaded" + GetHashCode());
183+
}
184+
}
185+
186+
private event Action<UpdateInfo> _updateDownloaded;
187+
16188
private static AutoUpdater _autoUpdater;
17189
private static object _syncRoot = new object();
18190

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace ElectronNET.API.Entities
6+
{
7+
/// <summary>
8+
///
9+
/// </summary>
10+
public class ProgressInfo
11+
{
12+
/// <summary>
13+
///
14+
/// </summary>
15+
public string Progress { get; set; }
16+
17+
/// <summary>
18+
///
19+
/// </summary>
20+
public string BytesPerSecond { get; set; }
21+
22+
/// <summary>
23+
///
24+
/// </summary>
25+
public string Percent { get; set; }
26+
27+
/// <summary>
28+
///
29+
/// </summary>
30+
public string Total { get; set; }
31+
32+
/// <summary>
33+
///
34+
/// </summary>
35+
public string Transferred { get; set; }
36+
}
37+
}

ElectronNET.Host/api/autoUpdater.js

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ElectronNET.Host/api/autoUpdater.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ElectronNET.Host/api/autoUpdater.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,46 @@ let electronSocket;
55
export = (socket: SocketIO.Socket) => {
66
electronSocket = socket;
77

8+
// Events ********
9+
10+
socket.on('register-autoUpdater-error-event', (id) => {
11+
autoUpdater.on('error', (error) => {
12+
electronSocket.emit('autoUpdater-error' + id, error.message);
13+
});
14+
});
15+
16+
socket.on('register-autoUpdater-checking-for-update-event', (id) => {
17+
autoUpdater.on('checking-for-update', () => {
18+
electronSocket.emit('autoUpdater-checking-for-update' + id);
19+
});
20+
});
21+
22+
socket.on('register-autoUpdater-update-available-event', (id) => {
23+
autoUpdater.on('update-available', (updateInfo) => {
24+
electronSocket.emit('autoUpdater-update-available' + id, updateInfo);
25+
});
26+
});
27+
28+
socket.on('register-autoUpdater-update-not-available-event', (id) => {
29+
autoUpdater.on('update-not-available', (updateInfo) => {
30+
electronSocket.emit('autoUpdater-update-not-available' + id, updateInfo);
31+
});
32+
});
33+
34+
socket.on('register-autoUpdater-download-progress-event', (id) => {
35+
autoUpdater.on('download-progress', (progressInfo) => {
36+
electronSocket.emit('autoUpdater-download-progress' + id, progressInfo);
37+
});
38+
});
39+
40+
socket.on('register-autoUpdater-update-downloaded-event', (id) => {
41+
autoUpdater.on('update-downloaded', (updateInfo) => {
42+
electronSocket.emit('autoUpdater-update-downloaded' + id, updateInfo);
43+
});
44+
});
45+
46+
// Methods ********
47+
848
socket.on('autoUpdaterCheckForUpdatesAndNotify', async (guid) => {
949
const updateCheckResult = await autoUpdater.checkForUpdatesAndNotify();
1050
electronSocket.emit('autoUpdaterCheckForUpdatesAndNotifyComplete' + guid, updateCheckResult);

ElectronNET.WebApp/Controllers/UpdateController.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,23 @@ public IActionResult Index()
1010
{
1111
if (HybridSupport.IsElectronActive)
1212
{
13+
Electron.AutoUpdater.OnError += (message) => Electron.Dialog.ShowErrorBox("Error", message);
14+
Electron.AutoUpdater.OnCheckingForUpdate += async () => await Electron.Dialog.ShowMessageBoxAsync("Checking for Update");
15+
Electron.AutoUpdater.OnUpdateNotAvailable += async (info) => await Electron.Dialog.ShowMessageBoxAsync("Update not available");
16+
Electron.AutoUpdater.OnUpdateAvailable += async (info) => await Electron.Dialog.ShowMessageBoxAsync("Update available" + info.Version);
17+
Electron.AutoUpdater.OnDownloadProgress += (info) =>
18+
{
19+
var message1 = "Download speed: " + info.BytesPerSecond + "\n<br/>";
20+
var message2 = "Downloaded " + info.Percent + "%" + "\n<br/>";
21+
var message3 = $"({info.Transferred}/{info.Total})" + "\n<br/>";
22+
var message4 = "Progress: " + info.Progress + "\n<br/>";
23+
var information = message1 + message2 + message3 + message4;
24+
25+
var mainWindow = Electron.WindowManager.BrowserWindows.First();
26+
Electron.IpcMain.Send(mainWindow, "auto-update-reply", information);
27+
};
28+
Electron.AutoUpdater.OnUpdateDownloaded += async (info) => await Electron.Dialog.ShowMessageBoxAsync("Update complete!" + info.Version);
29+
1330
Electron.IpcMain.On("auto-update", async (args) =>
1431
{
1532
// Electron.NET CLI Command for deploy:

0 commit comments

Comments
 (0)