Skip to content

Commit ec48598

Browse files
committed
feat: get a lock when downloading / removing a version
1 parent 68cd2a3 commit ec48598

File tree

1 file changed

+54
-26
lines changed

1 file changed

+54
-26
lines changed

src/renderer/state.ts

+54-26
Original file line numberDiff line numberDiff line change
@@ -837,33 +837,52 @@ export class AppState {
837837
return;
838838
}
839839

840-
console.log(`State: Removing Electron ${version}`);
841-
if (source === VersionSource.local) {
842-
if (version in this.versions) {
843-
delete this.versions[version];
844-
saveLocalVersions(Object.values(this.versions));
845-
} else {
846-
console.log(`State: Version ${version} already removed, doing nothing`);
847-
}
848-
} else {
849-
if (
850-
state === InstallState.installed ||
851-
state == InstallState.downloaded
852-
) {
853-
await this.installer.remove(version);
854-
if (this.installer.state(version) === InstallState.missing) {
855-
const typeDefsCleaner = async () => {
856-
window.ElectronFiddle.app.electronTypes.uncache(ver);
857-
};
840+
await navigator.locks.request(
841+
this.getVersionLockName(version),
842+
{
843+
mode: 'exclusive',
844+
ifAvailable: true,
845+
},
846+
async (lock) => {
847+
// another window is already removing this version
848+
if (!lock) {
849+
return;
850+
}
858851

859-
await typeDefsCleaner();
852+
console.log(`State: Removing Electron ${version}`);
860853

861-
this.broadcastVersionStates([ver]);
854+
if (source === VersionSource.local) {
855+
if (version in this.versions) {
856+
delete this.versions[version];
857+
saveLocalVersions(Object.values(this.versions));
858+
} else {
859+
console.log(
860+
`State: Version ${version} already removed, doing nothing`,
861+
);
862+
}
863+
} else {
864+
if (
865+
state === InstallState.installed ||
866+
state == InstallState.downloaded
867+
) {
868+
await this.installer.remove(version);
869+
if (this.installer.state(version) === InstallState.missing) {
870+
const typeDefsCleaner = async () => {
871+
window.ElectronFiddle.app.electronTypes.uncache(ver);
872+
};
873+
874+
await typeDefsCleaner();
875+
876+
this.broadcastVersionStates([ver]);
877+
}
878+
} else {
879+
console.log(
880+
`State: Version ${version} already removed, doing nothing`,
881+
);
882+
}
862883
}
863-
} else {
864-
console.log(`State: Version ${version} already removed, doing nothing`);
865-
}
866-
}
884+
},
885+
);
867886
}
868887

869888
/**
@@ -1049,8 +1068,17 @@ export class AppState {
10491068
}
10501069
}
10511070

1052-
// Fetch new binaries, maybe?
1053-
await this.downloadVersion(ver);
1071+
await navigator.locks.request(
1072+
`downloading:${version}`,
1073+
{ mode: 'exclusive' },
1074+
async (lock) => {
1075+
console.log(`exclusive download lock granted:`);
1076+
console.log(lock);
1077+
1078+
// Fetch new binaries, maybe?
1079+
await this.downloadVersion(ver);
1080+
},
1081+
);
10541082
}
10551083

10561084
/**

0 commit comments

Comments
 (0)