|
| 1 | +/* --------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Applied Eng & Design All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.md in the project root for license information. |
| 4 | + * -------------------------------------------------------------------------------------------- */ |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +import { ExtensionContext } from 'vscode'; |
| 8 | +import { constants } from './constants'; |
| 9 | +import { LocalStorageService } from './localStorageService'; |
| 10 | +import { Version } from './version'; |
| 11 | + |
| 12 | +export interface IState extends Record<string, unknown> { |
| 13 | + previousVersion: string; |
| 14 | + version: string; |
| 15 | +} |
| 16 | + |
| 17 | +const enum Loc { |
| 18 | + Global = 'global', |
| 19 | + WS = 'workspace', |
| 20 | +} |
| 21 | + |
| 22 | +export class StateControl { |
| 23 | + private defaults: IState = { |
| 24 | + previousVersion: '0.0.0', |
| 25 | + version: '0.0.0', |
| 26 | + }; |
| 27 | + |
| 28 | + private _state: IState; |
| 29 | + private _storageManager: LocalStorageService; |
| 30 | + |
| 31 | + constructor(context: ExtensionContext) { |
| 32 | + this._storageManager = new LocalStorageService(context); |
| 33 | + this._state = this.getState(); |
| 34 | + } |
| 35 | + |
| 36 | + private getState(): IState { |
| 37 | + return this._storageManager.getValue<IState>(constants.configId, Loc.Global) ?? this.defaults; |
| 38 | + } |
| 39 | + |
| 40 | + async deleteState(): Promise<void> { |
| 41 | + await this._storageManager.deleteValue(constants.configId, Loc.Global); |
| 42 | + } |
| 43 | + |
| 44 | + private async writeState(): Promise<void> { |
| 45 | + await this._storageManager.setValue<IState>(constants.configId, this._state, Loc.Global); |
| 46 | + } |
| 47 | + |
| 48 | + getVersion(): Version { |
| 49 | + return new Version(this._state.version); |
| 50 | + } |
| 51 | + |
| 52 | + async updateVer(ver: Version | string): Promise<void> { |
| 53 | + let newVer: Version; |
| 54 | + if (typeof ver === 'string') { |
| 55 | + newVer = new Version(ver); |
| 56 | + } else { |
| 57 | + newVer = ver; |
| 58 | + } |
| 59 | + |
| 60 | + this._state.previousVersion = this._state.version; |
| 61 | + this._state.version = newVer.getVersionAsString(); |
| 62 | + |
| 63 | + await this.writeState(); |
| 64 | + } |
| 65 | +} |
0 commit comments