From ae662617a2bc49d025adaf9c4a8ff2dfa557d36c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 16 Mar 2020 19:23:38 +0100 Subject: Separate persistent mutable state from config That way, we clearly see which things are not change, and we also clearly see which things are persistent. --- editors/code/src/installation/extension.ts | 16 +++++++++------- editors/code/src/installation/server.ts | 21 +++++++++++---------- 2 files changed, 20 insertions(+), 17 deletions(-) (limited to 'editors/code/src/installation') diff --git a/editors/code/src/installation/extension.ts b/editors/code/src/installation/extension.ts index eea6fded2..a1db96f05 100644 --- a/editors/code/src/installation/extension.ts +++ b/editors/code/src/installation/extension.ts @@ -7,6 +7,7 @@ import { Config, UpdatesChannel } from "../config"; import { ArtifactReleaseInfo, ArtifactSource } from "./interfaces"; import { downloadArtifactWithProgressUi } from "./downloads"; import { fetchArtifactReleaseInfo } from "./fetch_artifact_release_info"; +import { PersistentState } from "../persistent_state"; const HEURISTIC_NIGHTLY_RELEASE_PERIOD_IN_HOURS = 25; @@ -14,7 +15,7 @@ const HEURISTIC_NIGHTLY_RELEASE_PERIOD_IN_HOURS = 25; * Installs `stable` or latest `nightly` version or does nothing if the current * extension version is what's needed according to `desiredUpdateChannel`. */ -export async function ensureProperExtensionVersion(config: Config): Promise { +export async function ensureProperExtensionVersion(config: Config, state: PersistentState): Promise { // User has built lsp server from sources, she should manage updates manually if (config.serverSource?.type === ArtifactSource.Type.ExplicitPath) return; @@ -23,7 +24,7 @@ export async function ensureProperExtensionVersion(config: Config): Promise { + await tryDownloadNightlyExtension(config, state, releaseInfo => { assert( - currentExtReleaseDate.getTime() === config.installedNightlyExtensionReleaseDate.get()?.getTime(), + currentExtReleaseDate.getTime() === state.installedNightlyExtensionReleaseDate.get()?.getTime(), "Other active VSCode instance has reinstalled the extension" ); @@ -111,6 +112,7 @@ async function askToDownloadProperExtensionVersion(config: Config, reason = "") */ const tryDownloadNightlyExtension = notReentrant(async ( config: Config, + state: PersistentState, shouldDownload: (releaseInfo: ArtifactReleaseInfo) => boolean = () => true ): Promise => { const vsixSource = config.nightlyVsixSource; @@ -124,7 +126,7 @@ const tryDownloadNightlyExtension = notReentrant(async ( const vsixPath = path.join(vsixSource.dir, vsixSource.file); await vscodeInstallExtensionFromVsix(vsixPath); - await config.installedNightlyExtensionReleaseDate.set(releaseInfo.releaseDate); + await state.installedNightlyExtensionReleaseDate.set(releaseInfo.releaseDate); await fs.unlink(vsixPath); await vscodeReloadWindow(); // never returns diff --git a/editors/code/src/installation/server.ts b/editors/code/src/installation/server.ts index 05730a778..05d326131 100644 --- a/editors/code/src/installation/server.ts +++ b/editors/code/src/installation/server.ts @@ -7,8 +7,9 @@ import { fetchArtifactReleaseInfo } from "./fetch_artifact_release_info"; import { downloadArtifactWithProgressUi } from "./downloads"; import { log, assert, notReentrant } from "../util"; import { Config, NIGHTLY_TAG } from "../config"; +import { PersistentState } from "../persistent_state"; -export async function ensureServerBinary(config: Config): Promise { +export async function ensureServerBinary(config: Config, state: PersistentState): Promise { const source = config.serverSource; if (!source) { @@ -37,7 +38,7 @@ export async function ensureServerBinary(config: Config): Promise return null; } case ArtifactSource.Type.GithubRelease: { - if (!shouldDownloadServer(source, config)) { + if (!shouldDownloadServer(state, source)) { return path.join(source.dir, source.file); } @@ -50,24 +51,24 @@ export async function ensureServerBinary(config: Config): Promise if (userResponse !== "Download now") return null; } - return await downloadServer(source, config); + return await downloadServer(state, source); } } } function shouldDownloadServer( + state: PersistentState, source: ArtifactSource.GithubRelease, - config: Config ): boolean { if (!isBinaryAvailable(path.join(source.dir, source.file))) return true; const installed = { - tag: config.serverReleaseTag.get(), - date: config.serverReleaseDate.get() + tag: state.serverReleaseTag.get(), + date: state.serverReleaseDate.get() }; const required = { tag: source.tag, - date: config.installedNightlyExtensionReleaseDate.get() + date: state.installedNightlyExtensionReleaseDate.get() }; log.debug("Installed server:", installed, "required:", required); @@ -86,16 +87,16 @@ function shouldDownloadServer( * Enforcing no reentrancy for this is best-effort. */ const downloadServer = notReentrant(async ( + state: PersistentState, source: ArtifactSource.GithubRelease, - config: Config, ): Promise => { try { const releaseInfo = await fetchArtifactReleaseInfo(source.repo, source.file, source.tag); await downloadArtifactWithProgressUi(releaseInfo, source.file, source.dir, "language server"); await Promise.all([ - config.serverReleaseTag.set(releaseInfo.releaseName), - config.serverReleaseDate.set(releaseInfo.releaseDate) + state.serverReleaseTag.set(releaseInfo.releaseName), + state.serverReleaseDate.set(releaseInfo.releaseDate) ]); } catch (err) { log.downloadError(err, "language server", source.repo.name); -- cgit v1.2.3