Skip to content

Commit

Permalink
fix: Only fetch download speed when opening the Updater tab.
Browse files Browse the repository at this point in the history
  • Loading branch information
okdistribute committed Aug 4, 2020
1 parent 702d430 commit 6e5645f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
5 changes: 1 addition & 4 deletions src/main/auto-updater.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class MapeoUpdater extends events.EventEmitter {
return releaseSummary
}

async _getDownloadSpeed () {
async getDownloadSpeed () {
let downloadSpeed
try {
downloadSpeed = await networkSpeed.download()
Expand Down Expand Up @@ -91,15 +91,12 @@ class MapeoUpdater extends events.EventEmitter {
return logger.info('[UPDATER] Must use an AppImage, dmg, or exe for automatic updates.')
}

var downloadSpeed = await this._getDownloadSpeed()

var args = {
version,
files,
path,
sha512,
releaseDate,
downloadSpeed,
releaseSummary: null // TODO: this._getReleaseSummary(version)
}

Expand Down
4 changes: 4 additions & 0 deletions src/main/ipc.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ module.exports = function (ipcSend) {
ipcSend('update-status', 'update-available', updateInfo)
})

ipcMain.handle('get-download-speed', async function (event) {
await updater.getDownloadSpeed()
})

ipcMain.on('download-update', function (event) {
updater.downloadUpdate()
})
Expand Down
36 changes: 28 additions & 8 deletions src/renderer/components/UpdaterView/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import React from 'react'
import React, { useEffect, useState } from 'react'
import { LinearProgress, Typography, makeStyles } from '@material-ui/core'
import { defineMessages, useIntl } from 'react-intl'
import Button from '@material-ui/core/Button'
import FormattedDuration from 'react-intl-formatted-duration'
import ErrorIcon from '@material-ui/icons/Error'
import Paper from '@material-ui/core/Paper'
import Loader from '../Loader'

import Loader from '../Loader'
import logger from '../../../logger'
import ipc from '../../electron-ipc'
import STATES from './states'
export { default as STATES } from './states'

Expand All @@ -24,6 +26,7 @@ const m = defineMessages({
patchUpdate: 'This update includes changes that fix critical errors. Please update as soon as possible.',
minorUpdate: 'This update includes improvements that may change your experience.',
majorUpdate: 'This update will make your application incompatible with earlier verions.',
calculatingDownloadSpeed: 'Calculating...',
unknownDownloadSpeed: 'Unknown',
estimatedDownloadTime: 'Estimated download time is'
})
Expand Down Expand Up @@ -150,13 +153,27 @@ const DownloadProgressView = ({ cx, update, percent }) => {
const UpdateAvailableView = ({ cx, update, setUpdate }) => {
const { formatMessage: t } = useIntl()

const { size, downloadSpeed, major, minor, patch } = update.updateInfo
const [downloadSpeed, setDownloadSpeed] = useState(Infinity)

useEffect(() => {
ipc.getDownloadSpeed().then((downloadSpeed) => {
setDownloadSpeed(downloadSpeed)
}).catch((err) => {
logger.error(err)
// Could not get download speed
setDownloadSpeed(null)
})
})

const { size, major, minor, patch } = update.updateInfo

const handleDownlaoadClick = setUpdate.downloadUpdate

const estimatedDownloadTime = downloadSpeed
? <FormattedDuration seconds={size / downloadSpeed.bps} />
: t(m.unknownDownloadSpeed)
const estimatedDownloadTime = downloadSpeed === Infinity
? t(m.calculatingDownloadSpeed)
: downloadSpeed === null || downloadSpeed === undefined
? t(m.unknownDownloadSpeed)
: <FormattedDuration seconds={size / downloadSpeed.bps} />

return (
<div className={cx.searchingText}>
Expand All @@ -179,7 +196,10 @@ const UpdateAvailableView = ({ cx, update, setUpdate }) => {
</Typography>

<Typography className={cx.estimatedDownloadTime}>
{t(m.estimatedDownloadTime)} {estimatedDownloadTime}
{t(m.estimatedDownloadTime)}
</Typography>
<Typography>
{estimatedDownloadTime}
</Typography>

<Button
Expand Down Expand Up @@ -272,7 +292,7 @@ const useStyles = makeStyles(theme => ({
marginTop: '15px'
},
estimatedDownloadTime: {
margin: '10px 0px'
margin: '15px 0px'
},
icon: {
fontSize: 48
Expand Down
3 changes: 3 additions & 0 deletions src/renderer/electron-ipc.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ function Api (ipcRenderer) {
},
quitAndInstall: function () {
ipcRenderer.send('quit-and-install')
},
getDownloadSpeed: async function () {
await ipcRenderer.invoke('get-download-speed')
}
}
}

0 comments on commit 6e5645f

Please sign in to comment.