Skip to content

Commit 0fe9a42

Browse files
committed
Strict null check state service
1 parent 9cf442e commit 0fe9a42

3 files changed

Lines changed: 12 additions & 13 deletions

File tree

src/tsconfig.strictNullChecks.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,7 @@
502502
"./vs/platform/search/common/search.ts",
503503
"./vs/platform/search/test/common/replace.test.ts",
504504
"./vs/platform/state/common/state.ts",
505+
"./vs/platform/state/node/stateService.ts",
505506
"./vs/platform/statusbar/common/statusbar.ts",
506507
"./vs/platform/storage/common/storage.ts",
507508
"./vs/platform/storage/common/storageLegacyService.ts",

src/vs/platform/state/common/state.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ export const IStateService = createDecorator<IStateService>('stateService');
1010
export interface IStateService {
1111
_serviceBrand: any;
1212

13-
getItem<T>(key: string, defaultValue?: T): T;
13+
getItem<T>(key: string, defaultValue: T): T;
14+
getItem<T>(key: string, defaultValue?: T): T | undefined;
1415
setItem(key: string, data: any): void;
1516
removeItem(key: string): void;
1617
}

src/vs/platform/state/node/stateService.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ import { ILogService } from 'vs/platform/log/common/log';
1313

1414
export class FileStorage {
1515

16-
private database: object | null = null;
16+
private _lazyDatabase: object | null = null;
1717

1818
constructor(private dbPath: string, private onError: (error) => void) { }
1919

20-
private ensureLoaded(): void {
21-
if (!this.database) {
22-
this.database = this.loadSync();
20+
private get database(): object {
21+
if (!this._lazyDatabase) {
22+
this._lazyDatabase = this.loadSync();
2323
}
24+
return this._lazyDatabase;
2425
}
2526

26-
getItem<T>(key: string, defaultValue?: T): T {
27-
this.ensureLoaded();
28-
27+
getItem<T>(key: string, defaultValue: T): T;
28+
getItem<T>(key: string, defaultValue?: T): T | undefined {
2929
const res = this.database[key];
3030
if (isUndefinedOrNull(res)) {
3131
return defaultValue;
@@ -35,8 +35,6 @@ export class FileStorage {
3535
}
3636

3737
setItem(key: string, data: any): void {
38-
this.ensureLoaded();
39-
4038
// Remove an item when it is undefined or null
4139
if (isUndefinedOrNull(data)) {
4240
return this.removeItem(key);
@@ -54,8 +52,6 @@ export class FileStorage {
5452
}
5553

5654
removeItem(key: string): void {
57-
this.ensureLoaded();
58-
5955
// Only update if the key is actually present (not undefined)
6056
if (!isUndefined(this.database[key])) {
6157
this.database[key] = void 0;
@@ -94,7 +90,8 @@ export class StateService implements IStateService {
9490
this.fileStorage = new FileStorage(path.join(environmentService.userDataPath, 'storage.json'), error => logService.error(error));
9591
}
9692

97-
getItem<T>(key: string, defaultValue?: T): T {
93+
getItem<T>(key: string, defaultValue: T): T;
94+
getItem<T>(key: string, defaultValue?: T): T | undefined {
9895
return this.fileStorage.getItem(key, defaultValue);
9996
}
10097

0 commit comments

Comments
 (0)