aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/persistent_state.ts
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-03-19 08:06:48 +0000
committerGitHub <[email protected]>2020-03-19 08:06:48 +0000
commitaca3c3086ee99f5770a60970e20af640c895d42a (patch)
tree2f0b3233cc4728436ba5e47a6e91e7df33585d43 /editors/code/src/persistent_state.ts
parent55336722b3662cbdcc9e1b92a3e27ed0442d2452 (diff)
parentfb6e655de8a44c65275ad45a27bf5bd684670ba0 (diff)
Merge #3629
3629: Alternative aproach to plugin auto update r=matklad a=matklad This is very much WIP (as in, I haven't run this once), but I like the result so far. cc @Veetaha The primary focus here on simplification: * local simplification of data structures and control-flow: using union of strings instead of an enum, using unwrapped GitHub API responses * global simplification of control flow: all logic is now in `main.ts`, implemented as linear functions without abstractions. This is stateful side-effective code, so arguments from [Carmack](http://number-none.com/blow/john_carmack_on_inlined_code.html) very much apply. We need all user interractions, all mutations, and all network requests to happen in a single file. * as a side-effect of condensing everything to functions, we can get rid of various enums. The enums were basically a reified control flow: ``` enum E { A, B } fn foo() -> E { if cond { E::A } else { E::B } } fn bar(e: E) { match e { E::A => do_a(), E::B => do_b(), } } ==>> fn all() { if cond { do_a() } else { do_b() } } ``` * simplification of model: we don't need to reinstall on settings update, we can just ask the user to reload, we don't need to handle nightly=>stable fallback, we can ask the user to reinstall extension, (todo) we don't need to parse out the date from the version, we can use build id for nightly and for stable we can write the info directly into package.json. Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'editors/code/src/persistent_state.ts')
-rw-r--r--editors/code/src/persistent_state.ts64
1 files changed, 28 insertions, 36 deletions
diff --git a/editors/code/src/persistent_state.ts b/editors/code/src/persistent_state.ts
index 13095b806..138d11b89 100644
--- a/editors/code/src/persistent_state.ts
+++ b/editors/code/src/persistent_state.ts
@@ -1,49 +1,41 @@
1import * as vscode from 'vscode'; 1import * as vscode from 'vscode';
2import { log } from "./util"; 2import { log } from './util';
3 3
4export class PersistentState { 4export class PersistentState {
5 constructor(private readonly ctx: vscode.ExtensionContext) { 5 constructor(private readonly globalState: vscode.Memento) {
6 const { lastCheck, releaseId, serverVersion } = this;
7 log.debug("PersistentState: ", { lastCheck, releaseId, serverVersion });
6 } 8 }
7 9
8 readonly installedNightlyExtensionReleaseDate = new DateStorage( 10 /**
9 "installed-nightly-extension-release-date", 11 * Used to check for *nightly* updates once an hour.
10 this.ctx.globalState 12 */
11 ); 13 get lastCheck(): number | undefined {
12 readonly serverReleaseDate = new DateStorage("server-release-date", this.ctx.globalState); 14 return this.globalState.get("lastCheck");
13 readonly serverReleaseTag = new Storage<null | string>("server-release-tag", this.ctx.globalState, null);
14}
15
16
17export class Storage<T> {
18 constructor(
19 private readonly key: string,
20 private readonly storage: vscode.Memento,
21 private readonly defaultVal: T
22 ) { }
23
24 get(): T {
25 const val = this.storage.get(this.key, this.defaultVal);
26 log.debug(this.key, "==", val);
27 return val;
28 } 15 }
29 async set(val: T) { 16 async updateLastCheck(value: number) {
30 log.debug(this.key, "=", val); 17 await this.globalState.update("lastCheck", value);
31 await this.storage.update(this.key, val);
32 } 18 }
33}
34export class DateStorage {
35 inner: Storage<null | string>;
36 19
37 constructor(key: string, storage: vscode.Memento) { 20 /**
38 this.inner = new Storage(key, storage, null); 21 * Release id of the *nightly* extension.
22 * Used to check if we should update.
23 */
24 get releaseId(): number | undefined {
25 return this.globalState.get("releaseId");
39 } 26 }
40 27 async updateReleaseId(value: number) {
41 get(): null | Date { 28 await this.globalState.update("releaseId", value);
42 const dateStr = this.inner.get();
43 return dateStr ? new Date(dateStr) : null;
44 } 29 }
45 30
46 async set(date: null | Date) { 31 /**
47 await this.inner.set(date ? date.toString() : null); 32 * Version of the extension that installed the server.
33 * Used to check if we need to update the server.
34 */
35 get serverVersion(): string | undefined {
36 return this.globalState.get("serverVersion");
37 }
38 async updateServerVersion(value: string | undefined) {
39 await this.globalState.update("serverVersion", value);
48 } 40 }
49} 41}