@@ -10,18 +10,48 @@ import { writeFileAndFlushSync } from 'vs/base/node/extfs';
1010import { isUndefined , isUndefinedOrNull } from 'vs/base/common/types' ;
1111import { IStateService } from 'vs/platform/state/common/state' ;
1212import { ILogService } from 'vs/platform/log/common/log' ;
13+ import { readFile } from 'vs/base/node/pfs' ;
1314
1415export class FileStorage {
1516
16- private _lazyDatabase : object | null = null ;
17+ private _database : object | null = null ;
1718
18- constructor ( private dbPath : string , private onError : ( error ) => void ) { }
19+ constructor ( private dbPath : string , private onError : ( error : Error ) => void ) { }
1920
2021 private get database ( ) : object {
21- if ( ! this . _lazyDatabase ) {
22- this . _lazyDatabase = this . loadSync ( ) ;
22+ if ( ! this . _database ) {
23+ this . _database = this . loadSync ( ) ;
24+ }
25+
26+ return this . _database ;
27+ }
28+
29+ init ( ) : Promise < void > {
30+ return readFile ( this . dbPath ) . then ( contents => {
31+ try {
32+ this . _database = JSON . parse ( contents . toString ( ) ) ;
33+ } catch ( error ) {
34+ this . _database = { } ;
35+ }
36+ } , error => {
37+ if ( error . code !== 'ENOENT' ) {
38+ this . onError ( error ) ;
39+ }
40+
41+ this . _database = { } ;
42+ } ) ;
43+ }
44+
45+ private loadSync ( ) : object {
46+ try {
47+ return JSON . parse ( fs . readFileSync ( this . dbPath ) . toString ( ) ) ;
48+ } catch ( error ) {
49+ if ( error . code !== 'ENOENT' ) {
50+ this . onError ( error ) ;
51+ }
52+
53+ return { } ;
2354 }
24- return this . _lazyDatabase ;
2555 }
2656
2757 getItem < T > ( key : string , defaultValue : T ) : T ;
@@ -36,6 +66,7 @@ export class FileStorage {
3666 }
3767
3868 setItem ( key : string , data : any ) : void {
69+
3970 // Remove an item when it is undefined or null
4071 if ( isUndefinedOrNull ( data ) ) {
4172 return this . removeItem ( key ) ;
@@ -53,25 +84,14 @@ export class FileStorage {
5384 }
5485
5586 removeItem ( key : string ) : void {
87+
5688 // Only update if the key is actually present (not undefined)
5789 if ( ! isUndefined ( this . database [ key ] ) ) {
5890 this . database [ key ] = void 0 ;
5991 this . saveSync ( ) ;
6092 }
6193 }
6294
63- private loadSync ( ) : object {
64- try {
65- return JSON . parse ( fs . readFileSync ( this . dbPath ) . toString ( ) ) ; // invalid JSON or permission issue can happen here
66- } catch ( error ) {
67- if ( error && error . code !== 'ENOENT' ) {
68- this . onError ( error ) ;
69- }
70-
71- return { } ;
72- }
73- }
74-
7595 private saveSync ( ) : void {
7696 try {
7797 writeFileAndFlushSync ( this . dbPath , JSON . stringify ( this . database , null , 4 ) ) ; // permission issue can happen here
@@ -85,10 +105,19 @@ export class StateService implements IStateService {
85105
86106 _serviceBrand : any ;
87107
108+ private static STATE_FILE = 'storage.json' ;
109+
88110 private fileStorage : FileStorage ;
89111
90- constructor ( @IEnvironmentService environmentService : IEnvironmentService , @ILogService logService : ILogService ) {
91- this . fileStorage = new FileStorage ( path . join ( environmentService . userDataPath , 'storage.json' ) , error => logService . error ( error ) ) ;
112+ constructor (
113+ @IEnvironmentService environmentService : IEnvironmentService ,
114+ @ILogService logService : ILogService
115+ ) {
116+ this . fileStorage = new FileStorage ( path . join ( environmentService . userDataPath , StateService . STATE_FILE ) , error => logService . error ( error ) ) ;
117+ }
118+
119+ init ( ) : Promise < void > {
120+ return this . fileStorage . init ( ) ;
92121 }
93122
94123 getItem < T > ( key : string , defaultValue : T ) : T ;
0 commit comments