5
5
6
6
import { inject , injectable , named } from 'inversify' ;
7
7
import { Memento } from 'vscode' ;
8
+ import { IExtensionSingleActivationService } from '../activation/types' ;
9
+ import { ICommandManager } from './application/types' ;
10
+ import { Commands } from './constants' ;
8
11
import {
9
12
GLOBAL_MEMENTO ,
10
13
IExtensionContext ,
@@ -44,26 +47,72 @@ export class PersistentState<T> implements IPersistentState<T> {
44
47
}
45
48
}
46
49
50
+ const GLOBAL_PERSISTENT_KEYS = 'PYTHON_EXTENSION_GLOBAL_STORAGE_KEYS' ;
51
+ const WORKSPACE_PERSISTENT_KEYS = 'PYTHON_EXTENSION_WORKSPACE_STORAGE_KEYS' ;
52
+ type keysStorage = { key : string ; defaultValue : unknown } ;
53
+
47
54
@injectable ( )
48
- export class PersistentStateFactory implements IPersistentStateFactory {
55
+ export class PersistentStateFactory implements IPersistentStateFactory , IExtensionSingleActivationService {
56
+ private readonly globalKeysStorage = new PersistentState < keysStorage [ ] > (
57
+ this . globalState ,
58
+ GLOBAL_PERSISTENT_KEYS ,
59
+ [ ] ,
60
+ ) ;
61
+ private readonly workspaceKeysStorage = new PersistentState < keysStorage [ ] > (
62
+ this . workspaceState ,
63
+ WORKSPACE_PERSISTENT_KEYS ,
64
+ [ ] ,
65
+ ) ;
49
66
constructor (
50
67
@inject ( IMemento ) @named ( GLOBAL_MEMENTO ) private globalState : Memento ,
51
68
@inject ( IMemento ) @named ( WORKSPACE_MEMENTO ) private workspaceState : Memento ,
69
+ @inject ( ICommandManager ) private cmdManager : ICommandManager ,
52
70
) { }
71
+
72
+ public async activate ( ) : Promise < void > {
73
+ this . cmdManager . registerCommand ( Commands . ClearStorage , this . cleanAllPersistentStates . bind ( this ) ) ;
74
+ }
75
+
53
76
public createGlobalPersistentState < T > (
54
77
key : string ,
55
78
defaultValue ?: T ,
56
79
expiryDurationMs ?: number ,
57
80
) : IPersistentState < T > {
81
+ if ( ! this . globalKeysStorage . value . includes ( { key, defaultValue } ) ) {
82
+ this . globalKeysStorage . updateValue ( [ { key, defaultValue } , ...this . globalKeysStorage . value ] ) . ignoreErrors ( ) ;
83
+ }
58
84
return new PersistentState < T > ( this . globalState , key , defaultValue , expiryDurationMs ) ;
59
85
}
86
+
60
87
public createWorkspacePersistentState < T > (
61
88
key : string ,
62
89
defaultValue ?: T ,
63
90
expiryDurationMs ?: number ,
64
91
) : IPersistentState < T > {
92
+ if ( ! this . workspaceKeysStorage . value . includes ( { key, defaultValue } ) ) {
93
+ this . workspaceKeysStorage
94
+ . updateValue ( [ { key, defaultValue } , ...this . workspaceKeysStorage . value ] )
95
+ . ignoreErrors ( ) ;
96
+ }
65
97
return new PersistentState < T > ( this . workspaceState , key , defaultValue , expiryDurationMs ) ;
66
98
}
99
+
100
+ private async cleanAllPersistentStates ( ) : Promise < void > {
101
+ await Promise . all (
102
+ this . globalKeysStorage . value . map ( async ( keyContent ) => {
103
+ const storage = this . createGlobalPersistentState ( keyContent . key ) ;
104
+ await storage . updateValue ( keyContent . defaultValue ) ;
105
+ } ) ,
106
+ ) ;
107
+ await Promise . all (
108
+ this . workspaceKeysStorage . value . map ( async ( keyContent ) => {
109
+ const storage = this . createWorkspacePersistentState ( keyContent . key ) ;
110
+ await storage . updateValue ( keyContent . defaultValue ) ;
111
+ } ) ,
112
+ ) ;
113
+ await this . globalKeysStorage . updateValue ( [ ] ) ;
114
+ await this . workspaceKeysStorage . updateValue ( [ ] ) ;
115
+ }
67
116
}
68
117
69
118
/////////////////////////////
@@ -79,6 +128,10 @@ interface IPersistentStorage<T> {
79
128
* Build a global storage object for the given key.
80
129
*/
81
130
export function getGlobalStorage < T > ( context : IExtensionContext , key : string ) : IPersistentStorage < T > {
131
+ const globalKeysStorage = new PersistentState < keysStorage [ ] > ( context . globalState , GLOBAL_PERSISTENT_KEYS , [ ] ) ;
132
+ if ( ! globalKeysStorage . value . includes ( { key, defaultValue : undefined } ) ) {
133
+ globalKeysStorage . updateValue ( [ { key, defaultValue : undefined } , ...globalKeysStorage . value ] ) . ignoreErrors ( ) ;
134
+ }
82
135
const raw = new PersistentState < T > ( context . globalState , key ) ;
83
136
return {
84
137
// We adapt between PersistentState and IPersistentStorage.
0 commit comments