@todesktop/runtime
Advanced tools
Comparing version 0.2.6 to 0.2.7
@@ -11,2 +11,2 @@ import AutoUpdater from "./AutoUpdater"; | ||
declare const _default: ToDesktop; | ||
export default _default; | ||
export = _default; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const AutoUpdater_1 = require("./AutoUpdater"); | ||
@@ -34,3 +33,2 @@ const cache_1 = require("./cache"); | ||
} | ||
exports.default = todesktop; | ||
module.exports = todesktop; |
@@ -7,3 +7,3 @@ { | ||
"name": "@todesktop/runtime", | ||
"version": "0.2.6", | ||
"version": "0.2.7", | ||
"license": "MIT", | ||
@@ -10,0 +10,0 @@ "author": "Adam Lynch <contact@adamlynch.com> (http://adamlynch.com/)", |
@@ -5,2 +5,10 @@ # @todesktop/runtime | ||
- [Installation](#installation) | ||
- [Minimal Usage](#minimal-usage) | ||
- [Default Behaviour](#default-behviour) | ||
- [API](#api) | ||
- [Events](#events) | ||
- [Recipes](#recipes) | ||
- [Debugging](#debugging) | ||
- [More documentation](#more-documentation) | ||
@@ -236,2 +244,83 @@ ## Installation | ||
## Recipes | ||
### How do I show a message on my UI to indicate that an update is available? | ||
By default, ToDesktop runtime will show a notification that an update is ready to be installed. However, it is possible that the user may have notifications turned off for your app. For this reason, it's a good idea to notify your users of an update directly on the UI of your app. | ||
ToDesktop runtime is only available in the main process of Electron. So we need to use [IPC](https://www.electronjs.org/docs/api/ipc-main) to tell the UI that an update is available, let's do that: | ||
```js | ||
const todesktop = require("@todesktop/runtime"); | ||
const { ipcMain } = require('electron'); | ||
todesktop.init(); | ||
const win = new BrowserWindow(/* Options here */); | ||
// When we receive an update-downloaded event then | ||
// we forward that event to our UI using IPC | ||
todesktop.autoUpdater.on("update-downloaded", (event) => { | ||
win.webContents.send('update-downloaded', event); | ||
}); | ||
// Listen for the UI to tell us that it wants to | ||
// do a restart and install of the app | ||
ipcMain.on('restart-and-install', () => { | ||
todesktop.autoUpdater.restartAndInstall(); | ||
}); | ||
``` | ||
In our UI we want to show a div when an update is ready to install. Here's the HTML: | ||
```html | ||
<div class="update-available" style="display:none;"> | ||
Version <span class="app-version"></span> is ready to Install. | ||
<a href="#" class="click-to-install"> | ||
Click here to restart the app and install the update | ||
</a>. | ||
</div> | ||
``` | ||
Let's hook it all up. We use IPC on the renderer thread to update our UI: | ||
```js | ||
const { ipcRenderer } = require('electron'); | ||
// First let's hook up our click-to-install link | ||
const installLink = document.querySelector('.click-to-install'); | ||
installLink.addEventListener('click', (e) => { | ||
e.preventDefault(); | ||
ipcRenderer.send('restart-and-install'); | ||
}); | ||
// Now let's listen for updates and show the | ||
// update-available div on our UI | ||
const updateAvailableDiv = document.querySelector('.update-available'); | ||
const appVersionSpan = document.querySelector('.app-version'); | ||
ipcRenderer.on("update-downloaded", (e, { updateInfo }) => { | ||
// Update the version text | ||
appVersionSpan.innerText = updateInfo.version; | ||
// Show the update-available div | ||
updateAvailableDiv.style.display = 'block'; | ||
}); | ||
``` | ||
### How do I force the application to restart when an update has been downloaded? | ||
First up, make sure that this is something that you want to do. For example, a user might be in the middle of typing a message on your UI when you restart the app. | ||
If you still want to go ahead then here is a simple recipe to do just that. | ||
```js | ||
todesktop.autoUpdater.on("update-downloaded", () => { | ||
// Immediately update the app and restart it | ||
todesktop.autoUpdater.restartAndInstall(); | ||
// Return false so that a notification is not displayed | ||
return false; | ||
}); | ||
``` | ||
## Debugging | ||
@@ -244,2 +333,2 @@ | ||
See https://docs.todesktop.com/cli/using-the-todesktop-cli | ||
For documentation on ToDesktop CLI See https://www.npmjs.com/package/@todesktop/cli |
47707
332
813