Skip to content

Commit 62e0bcb

Browse files
committed
fix(electron-auto-updater): queue checkForUpdates
Closes #1045
1 parent a0c0d8e commit 62e0bcb

File tree

4 files changed

+59
-3
lines changed

4 files changed

+59
-3
lines changed

nsis-auto-updater/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "electron-auto-updater",
3-
"version": "0.8.2",
3+
"version": "0.8.4",
44
"description": "NSIS Auto Updater",
55
"main": "out/nsis-auto-updater/src/main.js",
66
"author": "Vladimir Krivosheev",
@@ -17,7 +17,7 @@
1717
"fs-extra-p": "^3.0.3",
1818
"js-yaml": "^3.7.0",
1919
"semver": "^5.3.0",
20-
"source-map-support": "^0.4.6"
20+
"source-map-support": "^0.4.8"
2121
},
2222
"typings": "./out/electron-auto-updater.d.ts"
2323
}

nsis-auto-updater/src/NsisUpdater.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export class NsisUpdater extends EventEmitter {
2929
private clientPromise: Promise<Provider<any>>
3030

3131
private readonly untilAppReady: Promise<boolean>
32+
private checkForUpdatesPromise: Promise<UpdateCheckResult> | null
3233

3334
private readonly app: any
3435

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

75-
async checkForUpdates(): Promise<UpdateCheckResult> {
76+
checkForUpdates(): Promise<UpdateCheckResult> {
77+
let checkForUpdatesPromise = this.checkForUpdatesPromise
78+
if (checkForUpdatesPromise != null) {
79+
return checkForUpdatesPromise
80+
}
81+
82+
checkForUpdatesPromise = this._checkForUpdates()
83+
this.checkForUpdatesPromise = checkForUpdatesPromise
84+
const nullizePromise = () => this.checkForUpdatesPromise = null
85+
checkForUpdatesPromise
86+
.then(nullizePromise)
87+
.catch(nullizePromise)
88+
return checkForUpdatesPromise
89+
}
90+
91+
private async _checkForUpdates(): Promise<UpdateCheckResult> {
7692
await this.untilAppReady
7793
this.emit("checking-for-update")
7894
try {

test/out/__snapshots__/nsisUpdaterTest.js.snap

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
exports[`test checkForUpdates several times 1`] = `
2+
Object {
3+
"name": "TestApp Setup 1.1.0.exe",
4+
"sha2": "f2ca1bb6c7e907d06dafe4687e579fce76b37e4e93b7605022da52e6ccc26fd2",
5+
"url": "https://develar.s3.amazonaws.com/test/TestApp Setup 1.1.0.exe",
6+
}
7+
`;
8+
9+
exports[`test checkForUpdates several times 2`] = `
10+
Array [
11+
"checking-for-update",
12+
"update-available",
13+
"update-downloaded",
14+
]
15+
`;
16+
117
exports[`test file url 1`] = `
218
Object {
319
"name": "TestApp Setup 1.1.0.exe",

test/src/nsisUpdaterTest.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,30 @@ test("file url generic - manual download", async () => {
111111
await assertThat(path.join(await updater.downloadUpdate())).isFile()
112112
})
113113

114+
// https://github.com/electron-userland/electron-builder/issues/1045
115+
test("checkForUpdates several times", async () => {
116+
const tmpDir = new TmpDir()
117+
const testResourcesPath = await tmpDir.getTempFile("update-config")
118+
await outputFile(path.join(testResourcesPath, "app-update.yml"), safeDump(<GenericServerOptions>{
119+
provider: "generic",
120+
url: "https://develar.s3.amazonaws.com/test",
121+
}))
122+
g.__test_resourcesPath = testResourcesPath
123+
const updater: NsisUpdater = new NsisUpdaterClass()
124+
125+
const actualEvents = trackEvents(updater)
126+
127+
for (let i = 0; i < 10; i++) {
128+
//noinspection JSIgnoredPromiseFromCall
129+
updater.checkForUpdates()
130+
}
131+
const updateCheckResult = await updater.checkForUpdates()
132+
expect(updateCheckResult.fileInfo).toMatchSnapshot()
133+
await assertThat(path.join(await updateCheckResult.downloadPromise)).isFile()
134+
135+
expect(actualEvents).toMatchSnapshot()
136+
})
137+
114138
test("file url github", async () => {
115139
const tmpDir = new TmpDir()
116140
const testResourcesPath = await tmpDir.getTempFile("update-config")

0 commit comments

Comments
 (0)