From fd6d0f4834629c4fba80a1433ad761c3090dc2ba Mon Sep 17 00:00:00 2001 From: Erik Moura Date: Mon, 24 Jul 2023 23:45:20 -0300 Subject: [PATCH] feat: get a lock when downloading / removing a version --- src/renderer/state.ts | 74 +++++++++++++++++++++++++++++-------------- 1 file changed, 51 insertions(+), 23 deletions(-) diff --git a/src/renderer/state.ts b/src/renderer/state.ts index cf68a5d2e7..ac989d6d60 100644 --- a/src/renderer/state.ts +++ b/src/renderer/state.ts @@ -836,29 +836,48 @@ export class AppState { return; } - console.log(`State: Removing Electron ${version}`); - if (source === VersionSource.local) { - if (version in this.versions) { - delete this.versions[version]; - saveLocalVersions(Object.values(this.versions)); - } else { - console.log(`State: Version ${version} already removed, doing nothing`); - } - } else { - if ( - state === InstallState.installed || - state == InstallState.downloaded - ) { - await this.installer.remove(version); - if (this.installer.state(version) === InstallState.missing) { - await window.ElectronFiddle.app.electronTypes.uncache(ver); + await navigator.locks.request( + this.getVersionLockName(version), + { + mode: 'exclusive', + ifAvailable: true, + }, + async (lock) => { + // another window is already removing this version + if (!lock) { + return; + } - this.broadcastVersionStates([ver]); + console.log(`State: Removing Electron ${version}`); + + if (source === VersionSource.local) { + if (version in this.versions) { + delete this.versions[version]; + saveLocalVersions(Object.values(this.versions)); + } else { + console.log( + `State: Version ${version} already removed, doing nothing`, + ); + } + } else { + if ( + state === InstallState.installed || + state == InstallState.downloaded + ) { + await this.installer.remove(version); + if (this.installer.state(version) === InstallState.missing) { + await window.ElectronFiddle.app.electronTypes.uncache(ver); + + this.broadcastVersionStates([ver]); + } + } else { + console.log( + `State: Version ${version} already removed, doing nothing`, + ); + } } - } else { - console.log(`State: Version ${version} already removed, doing nothing`); - } - } + }, + ); } /** @@ -1044,8 +1063,17 @@ export class AppState { } } - // Fetch new binaries, maybe? - await this.downloadVersion(ver); + await navigator.locks.request( + `downloading:${version}`, + { mode: 'exclusive' }, + async (lock) => { + console.log(`exclusive download lock granted:`); + console.log(lock); + + // Fetch new binaries, maybe? + await this.downloadVersion(ver); + }, + ); } /**