Skip to content

Commit

Permalink
fix(electron-auto-updater): queue checkForUpdates
Browse files Browse the repository at this point in the history
Closes #1045
  • Loading branch information
develar committed Dec 25, 2016
1 parent a0c0d8e commit 62e0bcb
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
4 changes: 2 additions & 2 deletions nsis-auto-updater/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "electron-auto-updater",
"version": "0.8.2",
"version": "0.8.4",
"description": "NSIS Auto Updater",
"main": "out/nsis-auto-updater/src/main.js",
"author": "Vladimir Krivosheev",
Expand All @@ -17,7 +17,7 @@
"fs-extra-p": "^3.0.3",
"js-yaml": "^3.7.0",
"semver": "^5.3.0",
"source-map-support": "^0.4.6"
"source-map-support": "^0.4.8"
},
"typings": "./out/electron-auto-updater.d.ts"
}
18 changes: 17 additions & 1 deletion nsis-auto-updater/src/NsisUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class NsisUpdater extends EventEmitter {
private clientPromise: Promise<Provider<any>>

private readonly untilAppReady: Promise<boolean>
private checkForUpdatesPromise: Promise<UpdateCheckResult> | null

private readonly app: any

Expand Down Expand Up @@ -72,7 +73,22 @@ export class NsisUpdater extends EventEmitter {
this.clientPromise = BluebirdPromise.resolve(createClient(value))
}

async checkForUpdates(): Promise<UpdateCheckResult> {
checkForUpdates(): Promise<UpdateCheckResult> {
let checkForUpdatesPromise = this.checkForUpdatesPromise
if (checkForUpdatesPromise != null) {
return checkForUpdatesPromise
}

checkForUpdatesPromise = this._checkForUpdates()
this.checkForUpdatesPromise = checkForUpdatesPromise
const nullizePromise = () => this.checkForUpdatesPromise = null
checkForUpdatesPromise
.then(nullizePromise)
.catch(nullizePromise)
return checkForUpdatesPromise
}

private async _checkForUpdates(): Promise<UpdateCheckResult> {
await this.untilAppReady
this.emit("checking-for-update")
try {
Expand Down
16 changes: 16 additions & 0 deletions test/out/__snapshots__/nsisUpdaterTest.js.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
exports[`test checkForUpdates several times 1`] = `
Object {
"name": "TestApp Setup 1.1.0.exe",
"sha2": "f2ca1bb6c7e907d06dafe4687e579fce76b37e4e93b7605022da52e6ccc26fd2",
"url": "https://develar.s3.amazonaws.com/test/TestApp Setup 1.1.0.exe",
}
`;

exports[`test checkForUpdates several times 2`] = `
Array [
"checking-for-update",
"update-available",
"update-downloaded",
]
`;

exports[`test file url 1`] = `
Object {
"name": "TestApp Setup 1.1.0.exe",
Expand Down
24 changes: 24 additions & 0 deletions test/src/nsisUpdaterTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,30 @@ test("file url generic - manual download", async () => {
await assertThat(path.join(await updater.downloadUpdate())).isFile()
})

// https://github.com/electron-userland/electron-builder/issues/1045
test("checkForUpdates several times", async () => {
const tmpDir = new TmpDir()
const testResourcesPath = await tmpDir.getTempFile("update-config")
await outputFile(path.join(testResourcesPath, "app-update.yml"), safeDump(<GenericServerOptions>{
provider: "generic",
url: "https://develar.s3.amazonaws.com/test",
}))
g.__test_resourcesPath = testResourcesPath
const updater: NsisUpdater = new NsisUpdaterClass()

const actualEvents = trackEvents(updater)

for (let i = 0; i < 10; i++) {
//noinspection JSIgnoredPromiseFromCall
updater.checkForUpdates()
}
const updateCheckResult = await updater.checkForUpdates()
expect(updateCheckResult.fileInfo).toMatchSnapshot()
await assertThat(path.join(await updateCheckResult.downloadPromise)).isFile()

expect(actualEvents).toMatchSnapshot()
})

test("file url github", async () => {
const tmpDir = new TmpDir()
const testResourcesPath = await tmpDir.getTempFile("update-config")
Expand Down

0 comments on commit 62e0bcb

Please sign in to comment.