|
| 1 | +using ElectronNET.API.Entities; |
| 2 | +using Newtonsoft.Json.Linq; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Text; |
| 6 | +using System.Threading; |
| 7 | +using System.Threading.Tasks; |
| 8 | + |
| 9 | +namespace ElectronNET.API |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Enable apps to automatically update themselves. Based on electron-updater. |
| 13 | + /// </summary> |
| 14 | + public sealed class AutoUpdater |
| 15 | + { |
| 16 | + private static AutoUpdater _autoUpdater; |
| 17 | + private static object _syncRoot = new object(); |
| 18 | + |
| 19 | + internal AutoUpdater() { } |
| 20 | + |
| 21 | + internal static AutoUpdater Instance |
| 22 | + { |
| 23 | + get |
| 24 | + { |
| 25 | + if (_autoUpdater == null) |
| 26 | + { |
| 27 | + lock (_syncRoot) |
| 28 | + { |
| 29 | + if (_autoUpdater == null) |
| 30 | + { |
| 31 | + _autoUpdater = new AutoUpdater(); |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + return _autoUpdater; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Asks the server whether there is an update. |
| 42 | + /// </summary> |
| 43 | + /// <returns></returns> |
| 44 | + public Task<UpdateCheckResult> CheckForUpdatesAsync() |
| 45 | + { |
| 46 | + var taskCompletionSource = new TaskCompletionSource<UpdateCheckResult>(); |
| 47 | + string guid = Guid.NewGuid().ToString(); |
| 48 | + |
| 49 | + BridgeConnector.Socket.On("autoUpdaterCheckForUpdatesComplete" + guid, (updateCheckResult) => |
| 50 | + { |
| 51 | + BridgeConnector.Socket.Off("autoUpdaterCheckForUpdatesComplete" + guid); |
| 52 | + taskCompletionSource.SetResult(JObject.Parse(updateCheckResult.ToString()).ToObject<UpdateCheckResult>()); |
| 53 | + }); |
| 54 | + |
| 55 | + BridgeConnector.Socket.Emit("autoUpdaterCheckForUpdates", guid); |
| 56 | + |
| 57 | + return taskCompletionSource.Task; |
| 58 | + } |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Asks the server whether there is an update. |
| 62 | + /// |
| 63 | + /// This will immediately download an update, then install when the app quits. |
| 64 | + /// </summary> |
| 65 | + /// <returns></returns> |
| 66 | + public Task<UpdateCheckResult> CheckForUpdatesAndNotifyAsync() |
| 67 | + { |
| 68 | + var taskCompletionSource = new TaskCompletionSource<UpdateCheckResult>(); |
| 69 | + string guid = Guid.NewGuid().ToString(); |
| 70 | + |
| 71 | + BridgeConnector.Socket.On("autoUpdaterCheckForUpdatesAndNotifyComplete" + guid, (updateCheckResult) => |
| 72 | + { |
| 73 | + BridgeConnector.Socket.Off("autoUpdaterCheckForUpdatesAndNotifyComplete" + guid); |
| 74 | + taskCompletionSource.SetResult(JObject.Parse(updateCheckResult.ToString()).ToObject<UpdateCheckResult>()); |
| 75 | + }); |
| 76 | + |
| 77 | + BridgeConnector.Socket.Emit("autoUpdaterCheckForUpdatesAndNotify", guid); |
| 78 | + |
| 79 | + return taskCompletionSource.Task; |
| 80 | + } |
| 81 | + |
| 82 | + /// <summary> |
| 83 | + /// Restarts the app and installs the update after it has been downloaded. |
| 84 | + /// It should only be called after `update-downloaded` has been emitted. |
| 85 | + /// |
| 86 | + /// Note: QuitAndInstall() will close all application windows first and only emit `before-quit` event on `app` after that. |
| 87 | + /// This is different from the normal quit event sequence. |
| 88 | + /// </summary> |
| 89 | + /// <param name="isSilent">*windows-only* Runs the installer in silent mode. Defaults to `false`.</param> |
| 90 | + /// <param name="isForceRunAfter">Run the app after finish even on silent install. Not applicable for macOS. Ignored if `isSilent` is set to `false`.</param> |
| 91 | + public void QuitAndInstall(bool isSilent = false, bool isForceRunAfter = false) |
| 92 | + { |
| 93 | + BridgeConnector.Socket.Emit("autoUpdaterQuitAndInstall", isSilent, isForceRunAfter); |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments