Skip to content

Commit ab56d7e

Browse files
author
Benjamin Pasero
authored
storage - add isNew method (microsoft#101217)
1 parent 7923112 commit ab56d7e

5 files changed

Lines changed: 38 additions & 17 deletions

File tree

src/vs/editor/contrib/multicursor/test/multicursor.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ suite('Multicursor selection', () => {
7070
remove: (key) => undefined,
7171
logStorage: () => undefined,
7272
migrate: (toWorkspace) => Promise.resolve(undefined),
73-
flush: () => undefined
73+
flush: () => undefined,
74+
isNew: () => true
7475
} as IStorageService);
7576

7677
test('issue #8817: Cursor position changes when you cancel multicursor', () => {

src/vs/platform/storage/browser/storageService.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import { Disposable, IDisposable, dispose } from 'vs/base/common/lifecycle';
77
import { Emitter } from 'vs/base/common/event';
8-
import { IWorkspaceStorageChangeEvent, IStorageService, StorageScope, IWillSaveStateEvent, WillSaveStateReason, logStorage, WorkspaceStorageSettings } from 'vs/platform/storage/common/storage';
8+
import { IWorkspaceStorageChangeEvent, IStorageService, StorageScope, IWillSaveStateEvent, WillSaveStateReason, logStorage } from 'vs/platform/storage/common/storage';
99
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
1010
import { IWorkspaceInitializationPayload } from 'vs/platform/workspaces/common/workspaces';
1111
import { IFileService, FileChangeType } from 'vs/platform/files/common/files';
@@ -20,6 +20,8 @@ export class BrowserStorageService extends Disposable implements IStorageService
2020

2121
declare readonly _serviceBrand: undefined;
2222

23+
private static readonly WORKSPACE_IS_NEW_KEY = '__$__isNewStorageMarker';
24+
2325
private readonly _onDidChangeStorage = this._register(new Emitter<IWorkspaceStorageChangeEvent>());
2426
readonly onDidChangeStorage = this._onDidChangeStorage.event;
2527

@@ -81,12 +83,11 @@ export class BrowserStorageService extends Disposable implements IStorageService
8183
]);
8284

8385
// Check to see if this is the first time we are "opening" this workspace
84-
const firstOpen = this.workspaceStorage.getBoolean(WorkspaceStorageSettings.WORKSPACE_FIRST_OPEN);
86+
const firstOpen = this.workspaceStorage.getBoolean(BrowserStorageService.WORKSPACE_IS_NEW_KEY);
8587
if (firstOpen === undefined) {
86-
// NOTE@eamodio We can't reliably check to see if a workspace was added before this setting was introduced, so just pretend it is the first time
87-
this.workspaceStorage.set(WorkspaceStorageSettings.WORKSPACE_FIRST_OPEN, true);
88+
this.workspaceStorage.set(BrowserStorageService.WORKSPACE_IS_NEW_KEY, true);
8889
} else if (firstOpen) {
89-
this.workspaceStorage.set(WorkspaceStorageSettings.WORKSPACE_FIRST_OPEN, false);
90+
this.workspaceStorage.set(BrowserStorageService.WORKSPACE_IS_NEW_KEY, false);
9091
}
9192

9293
// In the browser we do not have support for long running unload sequences. As such,
@@ -188,6 +189,10 @@ export class BrowserStorageService extends Disposable implements IStorageService
188189
this.dispose();
189190
}
190191

192+
isNew(scope: StorageScope.WORKSPACE): boolean {
193+
return this.getBoolean(BrowserStorageService.WORKSPACE_IS_NEW_KEY, scope) === true;
194+
}
195+
191196
dispose(): void {
192197
dispose(this.runWhenIdleDisposable);
193198
this.runWhenIdleDisposable = undefined;

src/vs/platform/storage/common/storage.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ import { Disposable } from 'vs/base/common/lifecycle';
99
import { isUndefinedOrNull } from 'vs/base/common/types';
1010
import { IWorkspaceInitializationPayload } from 'vs/platform/workspaces/common/workspaces';
1111

12-
export enum WorkspaceStorageSettings {
13-
WORKSPACE_FIRST_OPEN = 'workbench.workspaceFirstOpen'
14-
}
15-
1612
export const IStorageService = createDecorator<IStorageService>('storageService');
1713

1814
export enum WillSaveStateReason {
@@ -107,6 +103,14 @@ export interface IStorageService {
107103
*/
108104
migrate(toWorkspace: IWorkspaceInitializationPayload): Promise<void>;
109105

106+
/**
107+
* Wether the storage for the given scope was created during this session or
108+
* existed before.
109+
*
110+
* Note: currently only implemented for `WORKSPACE` scope.
111+
*/
112+
isNew(scope: StorageScope.WORKSPACE): boolean;
113+
110114
/**
111115
* Allows to flush state, e.g. in cases where a shutdown is
112116
* imminent. This will send out the onWillSaveState to ask
@@ -231,6 +235,10 @@ export class InMemoryStorageService extends Disposable implements IStorageServic
231235
flush(): void {
232236
this._onWillSaveState.fire({ reason: WillSaveStateReason.NONE });
233237
}
238+
239+
isNew(): boolean {
240+
return true; // always new when in-memory
241+
}
234242
}
235243

236244
export async function logStorage(global: Map<string, string>, workspace: Map<string, string>, globalPath: string, workspacePath: string): Promise<void> {

src/vs/platform/storage/node/storageService.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import { Disposable, IDisposable, dispose } from 'vs/base/common/lifecycle';
77
import { Emitter } from 'vs/base/common/event';
88
import { ILogService, LogLevel } from 'vs/platform/log/common/log';
9-
import { IWorkspaceStorageChangeEvent, IStorageService, StorageScope, IWillSaveStateEvent, WillSaveStateReason, logStorage, WorkspaceStorageSettings } from 'vs/platform/storage/common/storage';
9+
import { IWorkspaceStorageChangeEvent, IStorageService, StorageScope, IWillSaveStateEvent, WillSaveStateReason, logStorage } from 'vs/platform/storage/common/storage';
1010
import { SQLiteStorageDatabase, ISQLiteStorageDatabaseLoggingOptions } from 'vs/base/parts/storage/node/storage';
1111
import { Storage, IStorageDatabase, IStorage, StorageHint } from 'vs/base/parts/storage/common/storage';
1212
import { mark } from 'vs/base/common/performance';
@@ -25,6 +25,8 @@ export class NativeStorageService extends Disposable implements IStorageService
2525
private static readonly WORKSPACE_STORAGE_NAME = 'state.vscdb';
2626
private static readonly WORKSPACE_META_NAME = 'workspace.json';
2727

28+
private static readonly WORKSPACE_IS_NEW_KEY = '__$__isNewStorageMarker';
29+
2830
private readonly _onDidChangeStorage = this._register(new Emitter<IWorkspaceStorageChangeEvent>());
2931
readonly onDidChangeStorage = this._onDidChangeStorage.event;
3032

@@ -106,11 +108,11 @@ export class NativeStorageService extends Disposable implements IStorageService
106108
await workspaceStorage.init();
107109

108110
// Check to see if this is the first time we are "opening" this workspace
109-
const firstOpen = workspaceStorage.getBoolean(WorkspaceStorageSettings.WORKSPACE_FIRST_OPEN);
111+
const firstOpen = workspaceStorage.getBoolean(NativeStorageService.WORKSPACE_IS_NEW_KEY);
110112
if (firstOpen === undefined) {
111-
workspaceStorage.set(WorkspaceStorageSettings.WORKSPACE_FIRST_OPEN, !result.wasCreated);
113+
workspaceStorage.set(NativeStorageService.WORKSPACE_IS_NEW_KEY, result.wasCreated);
112114
} else if (firstOpen) {
113-
workspaceStorage.set(WorkspaceStorageSettings.WORKSPACE_FIRST_OPEN, false);
115+
workspaceStorage.set(NativeStorageService.WORKSPACE_IS_NEW_KEY, false);
114116
}
115117
} finally {
116118
mark('didInitWorkspaceStorage');
@@ -278,4 +280,8 @@ export class NativeStorageService extends Disposable implements IStorageService
278280
// Recreate and init workspace storage
279281
return this.createWorkspaceStorage(newWorkspaceStoragePath).init();
280282
}
283+
284+
isNew(scope: StorageScope.WORKSPACE): boolean {
285+
return this.getBoolean(NativeStorageService.WORKSPACE_IS_NEW_KEY, scope) === true;
286+
}
281287
}

src/vs/workbench/browser/layout.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { PanelPart } from 'vs/workbench/browser/parts/panel/panelPart';
1616
import { PanelRegistry, Extensions as PanelExtensions } from 'vs/workbench/browser/panel';
1717
import { Position, Parts, IWorkbenchLayoutService, positionFromString, positionToString } from 'vs/workbench/services/layout/browser/layoutService';
1818
import { IWorkspaceContextService, WorkbenchState } from 'vs/platform/workspace/common/workspace';
19-
import { IStorageService, StorageScope, WillSaveStateReason, WorkspaceStorageSettings } from 'vs/platform/storage/common/storage';
19+
import { IStorageService, StorageScope, WillSaveStateReason } from 'vs/platform/storage/common/storage';
2020
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
2121
import { IViewletService } from 'vs/workbench/services/viewlet/browser/viewlet';
2222
import { IPanelService } from 'vs/workbench/services/panel/common/panelService';
@@ -569,7 +569,7 @@ export abstract class Layout extends Disposable implements IWorkbenchLayoutServi
569569
}
570570

571571
// The `firstRun` flag check is a safety-net hack for Codespaces, until we can verify the first open fix
572-
const firstOpen = (storageService.getBoolean(WorkspaceStorageSettings.WORKSPACE_FIRST_OPEN, StorageScope.WORKSPACE) || (defaultLayout as { firstRun: boolean })?.firstRun);
572+
const firstOpen = (storageService.isNew(StorageScope.WORKSPACE) || (defaultLayout as { firstRun: boolean })?.firstRun);
573573
if (!firstOpen) {
574574
return;
575575
}
@@ -791,8 +791,9 @@ export abstract class Layout extends Disposable implements IWorkbenchLayoutServi
791791

792792
private getInitialFilesToOpen(): { filesToOpenOrCreate?: IPath[], filesToDiff?: IPath[] } | undefined {
793793
const defaultLayout = this.environmentService.options?.defaultLayout;
794+
794795
// The `firstRun` flag check is a safety-net hack for Codespaces, until we can verify the first open fix
795-
if (defaultLayout?.editors?.length && (this.storageService.getBoolean(WorkspaceStorageSettings.WORKSPACE_FIRST_OPEN, StorageScope.WORKSPACE) || (defaultLayout as { firstRun: boolean })?.firstRun)) {
796+
if (defaultLayout?.editors?.length && (this.storageService.isNew(StorageScope.WORKSPACE) || (defaultLayout as { firstRun: boolean })?.firstRun)) {
796797
this._openedDefaultEditors = true;
797798

798799
return {

0 commit comments

Comments
 (0)