Skip to content

Commit

Permalink
⚡ Perf: remove duplicate check of new version, refactor getLatestVers…
Browse files Browse the repository at this point in the history
…ion func
  • Loading branch information
Kuingsmile committed Apr 16, 2023
1 parent 15d34ac commit ef917ce
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
11 changes: 3 additions & 8 deletions src/main/utils/updateChecker.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import db from '~/main/apis/core/datastore'
import { getLatestVersion } from '#/utils/getLatestVersion'
import { autoUpdater } from 'electron-updater'
// const releaseUrl = 'https://api.github.com/repos/Molunerfinn/PicGo/releases'
// const releaseUrlBackup = 'https://picgo-1251750343.cos.ap-chengdu.myqcloud.com'
// const downloadUrl = 'https://github.com/Kuingsmile/PicList/releases/latest'

const checkVersion = async () => {
let showTip = db.get('settings.showUpdateTip')
Expand All @@ -12,10 +8,9 @@ const checkVersion = async () => {
showTip = true
}
if (showTip) {
const res: string = await getLatestVersion()
if (res !== '') {
autoUpdater.checkForUpdatesAndNotify()
} else {
try {
await autoUpdater.checkForUpdatesAndNotify()
} catch (err) {
return false
}
} else {
Expand Down
28 changes: 15 additions & 13 deletions src/universal/utils/getLatestVersion.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import axios from 'axios'
import { RELEASE_URL, RELEASE_URL_BACKUP } from './static'
import yaml from 'js-yaml'
import { RELEASE_URL, RELEASE_URL_BACKUP } from './static'

export const getLatestVersion = async () => {
let res: string = ''
export const getLatestVersion = async (): Promise<string> => {
try {
res = await axios.get(RELEASE_URL).then(r => {
const list = r.data as IStringKeyMap[]
const normalList = list.filter(item => !item.name.includes('beta'))
return normalList[0].name
}).catch(async () => {
const result = await axios.get(`${RELEASE_URL_BACKUP}/latest.yml`)
const r = yaml.load(result.data) as IStringKeyMap
return r.version
})
const { data } = await axios.get(RELEASE_URL)
const releases = data as IStringKeyMap[]
const normalList = releases.filter(item => !item.name.includes('beta'))
const latestRelease = normalList[0]
return latestRelease.name
} catch (err) {
console.log(err)
try {
const { data } = await axios.get(`${RELEASE_URL_BACKUP}/latest.yml`)
const r = yaml.load(data) as IStringKeyMap
return r.version
} catch (err) {
console.log(err)
return ''
}
}
return res
}

0 comments on commit ef917ce

Please sign in to comment.