Skip to content

Commit

Permalink
move storage from browser => common
Browse files Browse the repository at this point in the history
  • Loading branch information
bpasero committed Jan 11, 2016
1 parent 528380c commit 4b83109
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ export interface IStorage {
}

export class Storage extends EventEmitter implements IStorageService {

public serviceId = IStorageService;

private static COMMON_PREFIX = 'storage://';
private static GLOBAL_PREFIX = Storage.COMMON_PREFIX + 'global/';
private static WORKSPACE_PREFIX = Storage.COMMON_PREFIX + 'workspace/';
Expand All @@ -35,28 +37,13 @@ export class Storage extends EventEmitter implements IStorageService {
private toUnbind: { (): void; }[];
private workspaceKey: string;

constructor(contextService: IWorkspaceContextService, storageImpl?: IStorage) {
constructor(contextService: IWorkspaceContextService, globalStorage: IStorage, workspaceStorage = globalStorage) {
super();

let workspace = contextService.getWorkspace();

// Take provided storage impl if any
if (!!storageImpl) {
this.globalStorage = storageImpl;
this.workspaceStorage = storageImpl;
}

// Otherwise use browser storage
else {
this.globalStorage = window.localStorage;

const env = contextService.getConfiguration().env;
if (env.pluginTestsPath || (!workspace && !env.pluginDevelopmentPath)) {
this.workspaceStorage = inMemoryLocalStorageInstance; // without workspace or in any plugin test, we use inMemory storage unless we develop a plugin where we want to preserve state
} else {
this.workspaceStorage = window.localStorage;
}
}
this.globalStorage = globalStorage;
this.workspaceStorage = workspaceStorage;

this.toUnbind = [];

Expand All @@ -76,7 +63,17 @@ export class Storage extends EventEmitter implements IStorageService {
workspaceUri = workspace.resource.toString();
}

return workspaceUri ? Storage.calculateWorkspaceKey(workspaceUri) : Storage.NO_WORKSPACE_IDENTIFIER;
return workspaceUri ? this.calculateWorkspaceKey(workspaceUri) : Storage.NO_WORKSPACE_IDENTIFIER;
}

private calculateWorkspaceKey(workspaceUrl: string): string {
let root = 'file:///';
let index = workspaceUrl.indexOf(root);
if (index === 0) {
return strings.rtrim(workspaceUrl.substr(root.length), '/') + '/';
}

return workspaceUrl;
}

private cleanupWorkspaceScope(workspaceId: number, workspaceName: string): void {
Expand Down Expand Up @@ -225,16 +222,6 @@ export class Storage extends EventEmitter implements IStorageService {

return Storage.WORKSPACE_PREFIX + this.workspaceKey + key.toLowerCase();
}

private static calculateWorkspaceKey(workspaceUrl: string): string {
let root = window.location.protocol + '//' + window.location.host + '/';
let index = workspaceUrl.indexOf(root);
if (index === 0) {
return strings.rtrim(workspaceUrl.substr(root.length), '/') + '/';
}

return workspaceUrl;
}
}

// In-Memory Local Storage Implementation
Expand Down
6 changes: 4 additions & 2 deletions src/vs/workbench/electron-browser/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {ContextMenuService} from 'vs/workbench/services/contextview/electron-bro
import {Preferences} from 'vs/workbench/common/constants';
import timer = require('vs/base/common/timer');
import {Workbench} from 'vs/workbench/browser/workbench';
import {Storage} from 'vs/workbench/browser/storage';
import {Storage, inMemoryLocalStorageInstance} from 'vs/workbench/common/storage';
import {ITelemetryService} from 'vs/platform/telemetry/common/telemetry';
import {ElectronTelemetryService} from 'vs/platform/telemetry/electron-browser/electronTelemetryService';
import {ElectronIntegration} from 'vs/workbench/electron-browser/integration';
Expand Down Expand Up @@ -269,7 +269,9 @@ export class WorkbenchShell {
];

this.windowService = new WindowService();
this.storageService = new Storage(this.contextService);

let disableWorkspaceStorage = this.configuration.env.pluginTestsPath || (!this.workspace && !this.configuration.env.pluginDevelopmentPath); // without workspace or in any plugin test, we use inMemory storage unless we develop a plugin where we want to preserve state
this.storageService = new Storage(this.contextService, window.localStorage, disableWorkspaceStorage ? inMemoryLocalStorageInstance : window.localStorage);

// no telemetry in a window for plugin development!
let enableTelemetry = this.configuration.env.isBuilt && !this.configuration.env.pluginDevelopmentPath ? !!this.configuration.env.enableTelemetry : false;
Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/test/browser/memento.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {BaseWorkspaceContextService} from 'vs/platform/workspace/common/baseWork
import {StorageScope} from 'vs/platform/storage/common/storage';
import * as TestUtils from 'vs/workbench/test/browser/servicesTestUtils';
import {Memento, Scope} from 'vs/workbench/common/memento';
import {Storage, InMemoryLocalStorage} from 'vs/workbench/browser/storage';
import {Storage, InMemoryLocalStorage} from 'vs/workbench/common/storage';

suite("Workbench Memento", () => {
let context;
Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/test/browser/part.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import * as TestUtils from 'vs/workbench/test/browser/servicesTestUtils';
import {BaseWorkspaceContextService} from 'vs/platform/workspace/common/baseWorkspaceContextService';
import {IWorkspaceContextService} from 'vs/platform/workspace/common/workspace';
import {IStorageService} from 'vs/platform/storage/common/storage';
import {Storage, InMemoryLocalStorage} from 'vs/workbench/browser/storage';
import {Storage, InMemoryLocalStorage} from 'vs/workbench/common/storage';

class MyPart extends Part {

Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/test/browser/servicesTestUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import Paths = require('vs/base/common/paths');
import Env = require('vs/base/common/flags');
import URI from 'vs/base/common/uri';
import MainTelemetryService = require('vs/platform/telemetry/browser/mainTelemetryService');
import Storage = require('vs/workbench/browser/storage');
import Storage = require('vs/workbench/common/storage');
import WorkbenchEditorCommon = require('vs/workbench/common/editor');
import Viewlet = require('vs/workbench/browser/viewlet');
import InstantiationService = require('vs/platform/instantiation/common/instantiationService');
Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/test/browser/storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as assert from 'assert';
import {clone} from 'vs/base/common/objects';
import {StorageEventType, StorageScope} from 'vs/platform/storage/common/storage';
import {TestContextService, TestWorkspace} from 'vs/workbench/test/browser/servicesTestUtils';
import {Storage, InMemoryLocalStorage} from 'vs/workbench/browser/storage';
import {Storage, InMemoryLocalStorage} from 'vs/workbench/common/storage';

suite("Workbench Storage", () => {
test("Store Data", () => {
Expand Down

0 comments on commit 4b83109

Please sign in to comment.