11import * as vscode from 'vscode'
22import { readFile , writeFile } from '../node'
3- import { SESSION_FILE_PATH } from '../../environment'
3+ import { SESSION_STORAGE_PATH } from '../../environment'
44
55// NOTE: localStorage is not available on client
66// and must be stored in editor
@@ -10,37 +10,47 @@ import { SESSION_FILE_PATH } from '../../environment'
1010// forcing it to be passed in through activation and down to other tools
1111class Storage < T > {
1212 private key : string
13+ private filePath : string
1314 private storage : vscode . Memento
1415 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+ } ) {
1627 this . storage = storage
1728 this . key = key
29+ this . filePath = filePath
1830 this . defaultValue = defaultValue
1931 }
2032 public get = async ( ) : Promise < T > => {
2133 const value : string | undefined = await this . storage . get ( this . key )
2234 if ( value ) {
2335 return JSON . parse ( value )
24- } else if ( SESSION_FILE_PATH ) {
36+ } else if ( SESSION_STORAGE_PATH ) {
2537 try {
2638 // 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` )
2840 if ( ! sessionFile ) {
2941 throw new Error ( 'No session file found' )
3042 }
31- const session = JSON . parse ( sessionFile )
43+ const data : T = JSON . parse ( sessionFile )
3244
33- if ( session ) {
34- const keys = Object . keys ( session )
45+ if ( data ) {
3546 // validate session
47+ const keys = Object . keys ( data )
3648 if ( keys . length ) {
37- // should only be one
38- this . key = keys [ 0 ]
39- return session [ this . key ]
49+ return data
4050 }
4151 }
4252 } 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 ` )
4454 }
4555 }
4656 return this . defaultValue
@@ -61,12 +71,12 @@ class Storage<T> {
6171 this . writeToSessionFile ( next )
6272 }
6373 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 ) {
6676 try {
67- writeFile ( { [ this . key ] : data } , SESSION_FILE_PATH )
77+ writeFile ( data , SESSION_STORAGE_PATH , ` ${ this . filePath } .json` )
6878 } 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 ` )
7080 }
7181 }
7282 }
0 commit comments