From 62e0bcbb204a05edec12a8f3aa02541308dd691a Mon Sep 17 00:00:00 2001 From: develar Date: Sun, 25 Dec 2016 20:54:05 +0100 Subject: [PATCH] fix(electron-auto-updater): queue checkForUpdates Closes #1045 --- nsis-auto-updater/package.json | 4 ++-- nsis-auto-updater/src/NsisUpdater.ts | 18 +++++++++++++- .../out/__snapshots__/nsisUpdaterTest.js.snap | 16 +++++++++++++ test/src/nsisUpdaterTest.ts | 24 +++++++++++++++++++ 4 files changed, 59 insertions(+), 3 deletions(-) diff --git a/nsis-auto-updater/package.json b/nsis-auto-updater/package.json index 791616f0f56..02391194ff0 100644 --- a/nsis-auto-updater/package.json +++ b/nsis-auto-updater/package.json @@ -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", @@ -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" } diff --git a/nsis-auto-updater/src/NsisUpdater.ts b/nsis-auto-updater/src/NsisUpdater.ts index aae34e3b9ad..6fc690c5e44 100644 --- a/nsis-auto-updater/src/NsisUpdater.ts +++ b/nsis-auto-updater/src/NsisUpdater.ts @@ -29,6 +29,7 @@ export class NsisUpdater extends EventEmitter { private clientPromise: Promise> private readonly untilAppReady: Promise + private checkForUpdatesPromise: Promise | null private readonly app: any @@ -72,7 +73,22 @@ export class NsisUpdater extends EventEmitter { this.clientPromise = BluebirdPromise.resolve(createClient(value)) } - async checkForUpdates(): Promise { + checkForUpdates(): Promise { + 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 { await this.untilAppReady this.emit("checking-for-update") try { diff --git a/test/out/__snapshots__/nsisUpdaterTest.js.snap b/test/out/__snapshots__/nsisUpdaterTest.js.snap index ab67cb60ccf..87b787e8956 100644 --- a/test/out/__snapshots__/nsisUpdaterTest.js.snap +++ b/test/out/__snapshots__/nsisUpdaterTest.js.snap @@ -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", diff --git a/test/src/nsisUpdaterTest.ts b/test/src/nsisUpdaterTest.ts index a939ccb9feb..acf9b855416 100644 --- a/test/src/nsisUpdaterTest.ts +++ b/test/src/nsisUpdaterTest.ts @@ -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({ + 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")