1
1
import * as vscode from 'vscode'
2
2
import { readFile , writeFile } from '../node'
3
- import { SESSION_FILE_PATH } from '../../environment'
3
+ import { SESSION_STORAGE_PATH } from '../../environment'
4
4
5
5
// NOTE: localStorage is not available on client
6
6
// and must be stored in editor
@@ -10,37 +10,47 @@ import { SESSION_FILE_PATH } from '../../environment'
10
10
// forcing it to be passed in through activation and down to other tools
11
11
class Storage < T > {
12
12
private key : string
13
+ private filePath : string
13
14
private storage : vscode . Memento
14
15
private defaultValue : T
15
- constructor ( { key, storage, defaultValue } : { key : string ; storage : vscode . Memento ; defaultValue : T } ) {
16
+ constructor ( {
17
+ key,
18
+ filePath,
19
+ storage,
20
+ defaultValue,
21
+ } : {
22
+ key : string
23
+ filePath : string
24
+ storage : vscode . Memento
25
+ defaultValue : T
26
+ } ) {
16
27
this . storage = storage
17
28
this . key = key
29
+ this . filePath = filePath
18
30
this . defaultValue = defaultValue
19
31
}
20
32
public get = async ( ) : Promise < T > => {
21
33
const value : string | undefined = await this . storage . get ( this . key )
22
34
if ( value ) {
23
35
return JSON . parse ( value )
24
- } else if ( SESSION_FILE_PATH ) {
36
+ } else if ( SESSION_STORAGE_PATH ) {
25
37
try {
26
38
// optionally read from file as a fallback to local storage
27
- const sessionFile = await readFile ( SESSION_FILE_PATH )
39
+ const sessionFile = await readFile ( SESSION_STORAGE_PATH , ` ${ this . filePath } .json` )
28
40
if ( ! sessionFile ) {
29
41
throw new Error ( 'No session file found' )
30
42
}
31
- const session = JSON . parse ( sessionFile )
43
+ const data : T = JSON . parse ( sessionFile )
32
44
33
- if ( session ) {
34
- const keys = Object . keys ( session )
45
+ if ( data ) {
35
46
// validate session
47
+ const keys = Object . keys ( data )
36
48
if ( keys . length ) {
37
- // should only be one
38
- this . key = keys [ 0 ]
39
- return session [ this . key ]
49
+ return data
40
50
}
41
51
}
42
52
} catch ( err ) {
43
- console . warn ( `Failed to read or parse session file: ${ SESSION_FILE_PATH } ` )
53
+ console . warn ( `Failed to read or parse session file: ${ SESSION_STORAGE_PATH } / ${ this . filePath } .json ` )
44
54
}
45
55
}
46
56
return this . defaultValue
@@ -61,12 +71,12 @@ class Storage<T> {
61
71
this . writeToSessionFile ( next )
62
72
}
63
73
public writeToSessionFile ( data : string ) {
64
- // optionally write to file
65
- if ( SESSION_FILE_PATH ) {
74
+ // optionally write state to file, useful when state cannot be controlled across containers
75
+ if ( SESSION_STORAGE_PATH ) {
66
76
try {
67
- writeFile ( { [ this . key ] : data } , SESSION_FILE_PATH )
77
+ writeFile ( data , SESSION_STORAGE_PATH , ` ${ this . filePath } .json` )
68
78
} catch ( err : any ) {
69
- console . warn ( `Failed to write coderoad session to path: ${ SESSION_FILE_PATH } ` )
79
+ console . warn ( `Failed to write coderoad session to path: ${ SESSION_STORAGE_PATH } / ${ this . filePath } .json ` )
70
80
}
71
81
}
72
82
}
0 commit comments