mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-02-16 10:58:30 -05:00
Merge VS Code 1.31.1 (#4283)
This commit is contained in:
@@ -10,18 +10,52 @@ import { writeFileAndFlushSync } from 'vs/base/node/extfs';
|
||||
import { isUndefined, isUndefinedOrNull } from 'vs/base/common/types';
|
||||
import { IStateService } from 'vs/platform/state/common/state';
|
||||
import { ILogService } from 'vs/platform/log/common/log';
|
||||
import { readFile } from 'vs/base/node/pfs';
|
||||
|
||||
export class FileStorage {
|
||||
|
||||
private _lazyDatabase: object | null = null;
|
||||
private _database: object | null = null;
|
||||
private lastFlushedSerializedDatabase: string | null = null;
|
||||
|
||||
constructor(private dbPath: string, private onError: (error) => void) { }
|
||||
constructor(private dbPath: string, private onError: (error: Error) => void) { }
|
||||
|
||||
private get database(): object {
|
||||
if (!this._lazyDatabase) {
|
||||
this._lazyDatabase = this.loadSync();
|
||||
if (!this._database) {
|
||||
this._database = this.loadSync();
|
||||
}
|
||||
|
||||
return this._database;
|
||||
}
|
||||
|
||||
init(): Promise<void> {
|
||||
return readFile(this.dbPath).then(contents => {
|
||||
try {
|
||||
this.lastFlushedSerializedDatabase = contents.toString();
|
||||
this._database = JSON.parse(this.lastFlushedSerializedDatabase);
|
||||
} catch (error) {
|
||||
this._database = {};
|
||||
}
|
||||
}, error => {
|
||||
if (error.code !== 'ENOENT') {
|
||||
this.onError(error);
|
||||
}
|
||||
|
||||
this._database = {};
|
||||
});
|
||||
}
|
||||
|
||||
private loadSync(): object {
|
||||
try {
|
||||
this.lastFlushedSerializedDatabase = fs.readFileSync(this.dbPath).toString();
|
||||
|
||||
return JSON.parse(this.lastFlushedSerializedDatabase);
|
||||
} catch (error) {
|
||||
if (error.code !== 'ENOENT') {
|
||||
this.onError(error);
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
return this._lazyDatabase;
|
||||
}
|
||||
|
||||
getItem<T>(key: string, defaultValue: T): T;
|
||||
@@ -36,6 +70,7 @@ export class FileStorage {
|
||||
}
|
||||
|
||||
setItem(key: string, data: any): void {
|
||||
|
||||
// Remove an item when it is undefined or null
|
||||
if (isUndefinedOrNull(data)) {
|
||||
return this.removeItem(key);
|
||||
@@ -53,28 +88,23 @@ export class FileStorage {
|
||||
}
|
||||
|
||||
removeItem(key: string): void {
|
||||
|
||||
// Only update if the key is actually present (not undefined)
|
||||
if (!isUndefined(this.database[key])) {
|
||||
this.database[key] = void 0;
|
||||
this.database[key] = undefined;
|
||||
this.saveSync();
|
||||
}
|
||||
}
|
||||
|
||||
private loadSync(): object {
|
||||
try {
|
||||
return JSON.parse(fs.readFileSync(this.dbPath).toString()); // invalid JSON or permission issue can happen here
|
||||
} catch (error) {
|
||||
if (error && error.code !== 'ENOENT') {
|
||||
this.onError(error);
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
private saveSync(): void {
|
||||
const serializedDatabase = JSON.stringify(this.database, null, 4);
|
||||
if (serializedDatabase === this.lastFlushedSerializedDatabase) {
|
||||
return; // return early if the database has not changed
|
||||
}
|
||||
|
||||
try {
|
||||
writeFileAndFlushSync(this.dbPath, JSON.stringify(this.database, null, 4)); // permission issue can happen here
|
||||
writeFileAndFlushSync(this.dbPath, serializedDatabase); // permission issue can happen here
|
||||
this.lastFlushedSerializedDatabase = serializedDatabase;
|
||||
} catch (error) {
|
||||
this.onError(error);
|
||||
}
|
||||
@@ -85,10 +115,19 @@ export class StateService implements IStateService {
|
||||
|
||||
_serviceBrand: any;
|
||||
|
||||
private static STATE_FILE = 'storage.json';
|
||||
|
||||
private fileStorage: FileStorage;
|
||||
|
||||
constructor(@IEnvironmentService environmentService: IEnvironmentService, @ILogService logService: ILogService) {
|
||||
this.fileStorage = new FileStorage(path.join(environmentService.userDataPath, 'storage.json'), error => logService.error(error));
|
||||
constructor(
|
||||
@IEnvironmentService environmentService: IEnvironmentService,
|
||||
@ILogService logService: ILogService
|
||||
) {
|
||||
this.fileStorage = new FileStorage(path.join(environmentService.userDataPath, StateService.STATE_FILE), error => logService.error(error));
|
||||
}
|
||||
|
||||
init(): Promise<void> {
|
||||
return this.fileStorage.init();
|
||||
}
|
||||
|
||||
getItem<T>(key: string, defaultValue: T): T;
|
||||
|
||||
@@ -41,7 +41,7 @@ suite('StateService', () => {
|
||||
service.setItem('some.other.key', 'some.other.value');
|
||||
assert.equal(service.getItem('some.other.key'), 'some.other.value');
|
||||
|
||||
service.setItem('some.undefined.key', void 0);
|
||||
service.setItem('some.undefined.key', undefined);
|
||||
assert.equal(service.getItem('some.undefined.key', 'some.default'), 'some.default');
|
||||
|
||||
service.setItem('some.null.key', null);
|
||||
|
||||
Reference in New Issue
Block a user