Skip to content

Commit

Permalink
feat: get a lock when downloading / removing a version
Browse files Browse the repository at this point in the history
  • Loading branch information
erikian committed Sep 8, 2023
1 parent 666cada commit fd6d0f4
Showing 1 changed file with 51 additions and 23 deletions.
74 changes: 51 additions & 23 deletions src/renderer/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
}
}
},
);
}

/**
Expand Down Expand Up @@ -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);
},
);
}

/**
Expand Down

0 comments on commit fd6d0f4

Please sign in to comment.