Merge from vscode 8e0f348413f4f616c23a88ae30030efa85811973 (#6381)

* Merge from vscode 8e0f348413f4f616c23a88ae30030efa85811973

* disable strict null check
This commit is contained in:
Anthony Dresser
2019-07-15 22:35:46 -07:00
committed by GitHub
parent f720ec642f
commit 0b7e7ddbf9
2406 changed files with 59140 additions and 35464 deletions

View File

@@ -11,14 +11,16 @@ import { isUndefined, isUndefinedOrNull } from 'vs/base/common/types';
import { IStateService } from 'vs/platform/state/common/state';
import { ILogService } from 'vs/platform/log/common/log';
type StorageDatebase = { [key: string]: any; };
export class FileStorage {
private _database: object | null = null;
private _database: StorageDatebase | null = null;
private lastFlushedSerializedDatabase: string | null = null;
constructor(private dbPath: string, private onError: (error: Error) => void) { }
private get database(): object {
private get database(): StorageDatebase {
if (!this._database) {
this._database = this.loadSync();
}
@@ -27,27 +29,36 @@ export class FileStorage {
}
async init(): Promise<void> {
try {
const contents = await readFile(this.dbPath);
if (this._database) {
return; // return if database was already loaded
}
try {
this.lastFlushedSerializedDatabase = contents.toString();
this._database = JSON.parse(this.lastFlushedSerializedDatabase);
} catch (error) {
this._database = {};
}
const database = await this.loadAsync();
if (this._database) {
return; // return if database was already loaded
}
this._database = database;
}
private loadSync(): StorageDatebase {
try {
this.lastFlushedSerializedDatabase = fs.readFileSync(this.dbPath).toString();
return JSON.parse(this.lastFlushedSerializedDatabase);
} catch (error) {
if (error.code !== 'ENOENT') {
this.onError(error);
}
this._database = {};
return {};
}
}
private loadSync(): object {
private async loadAsync(): Promise<StorageDatebase> {
try {
this.lastFlushedSerializedDatabase = fs.readFileSync(this.dbPath).toString();
this.lastFlushedSerializedDatabase = (await readFile(this.dbPath)).toString();
return JSON.parse(this.lastFlushedSerializedDatabase);
} catch (error) {