diff --git a/packages/core/src/browser/storage-service.ts b/packages/core/src/browser/storage-service.ts index 1e86823077ffa..4e2bcc9381879 100644 --- a/packages/core/src/browser/storage-service.ts +++ b/packages/core/src/browser/storage-service.ts @@ -35,7 +35,7 @@ interface LocalStorage { export class LocalStorageService implements StorageService { private storage: LocalStorage; - constructor( @inject(ILogger) protected logger: ILogger) { + constructor(@inject(ILogger) protected logger: ILogger) { if (typeof window !== 'undefined' && window.localStorage) { this.storage = window.localStorage; } else { @@ -62,6 +62,6 @@ export class LocalStorageService implements StorageService { } protected prefix(key: string): string { - return 'theia:' + key; + return `theia:${window.location.pathname}:${key}`; } } diff --git a/packages/workspace/src/browser/workspace-storage-service.ts b/packages/workspace/src/browser/workspace-storage-service.ts index d02a288fce2a6..0dcf4c4d0387c 100644 --- a/packages/workspace/src/browser/workspace-storage-service.ts +++ b/packages/workspace/src/browser/workspace-storage-service.ts @@ -7,9 +7,11 @@ import { StorageService } from '@theia/core/lib/browser/storage-service'; import { WorkspaceService } from './workspace-service'; -import { inject, injectable } from 'inversify'; +import { inject, injectable, postConstruct } from 'inversify'; import { ILogger } from '@theia/core/lib/common'; import { LocalStorageService } from '@theia/core/lib/browser/storage-service'; +import URI from '@theia/core/lib/common/uri'; +import { Deferred } from '@theia/core/lib/common/promise-util'; /* * Prefixes any stored data with the current workspace path. @@ -18,36 +20,40 @@ import { LocalStorageService } from '@theia/core/lib/browser/storage-service'; export class WorkspaceStorageService implements StorageService { private prefix: string; - private initialized: Promise; + protected initialized: Deferred; protected storageService: StorageService; - constructor( @inject(WorkspaceService) protected workspaceService: WorkspaceService, - @inject(ILogger) protected logger: ILogger) { - this.initialized = this.workspaceService.root.then(stat => { - if (stat) { - this.prefix = stat.uri; - } else { - this.prefix = '_global_'; - } - }); + @inject(WorkspaceService) + protected workspaceService: WorkspaceService; + + @inject(ILogger) + protected logger: ILogger; + + constructor() { this.storageService = new LocalStorageService(this.logger); + this.initialized = new Deferred(); + } + + @postConstruct() + protected async init(): Promise { + const statFile = await this.workspaceService.root; + this.prefix = statFile ? new URI(statFile.uri).path.toString() : '_global_'; + this.initialized.resolve(); } async setData(key: string, data: T): Promise { - if (!this.prefix) { - await this.initialized; - } + if (!this.prefix) { await this.initialized.promise; } const fullKey = this.prefixWorkspaceURI(key); return this.storageService.setData(fullKey, data); } async getData(key: string, defaultValue?: T): Promise { - await this.initialized; + if (!this.prefix) { await this.initialized.promise; } const fullKey = this.prefixWorkspaceURI(key); return this.storageService.getData(fullKey, defaultValue); } protected prefixWorkspaceURI(originalKey: string): string { - return this.prefix + ":" + originalKey; + return `${this.prefix}:${originalKey}`; } } diff --git a/packages/workspace/src/node/default-workspace-server.ts b/packages/workspace/src/node/default-workspace-server.ts index bbaff6530a646..257e4b6600d1e 100644 --- a/packages/workspace/src/node/default-workspace-server.ts +++ b/packages/workspace/src/node/default-workspace-server.ts @@ -10,7 +10,7 @@ import * as yargs from 'yargs'; import * as fs from 'fs-extra'; import * as os from 'os'; -import { injectable, inject } from "inversify"; +import { injectable, inject, postConstruct } from "inversify"; import { FileUri } from '@theia/core/lib/node'; import { CliContribution } from '@theia/core/lib/node/cli'; import { Deferred } from '@theia/core/lib/common/promise-util'; @@ -50,18 +50,18 @@ export class DefaultWorkspaceServer implements WorkspaceServer { protected root: Promise; - constructor( - @inject(WorkspaceCliContribution) protected readonly cliParams: WorkspaceCliContribution - ) { + @inject(WorkspaceCliContribution) + protected readonly cliParams: WorkspaceCliContribution; + + @postConstruct() + protected async init(): Promise { this.root = this.getRootURIFromCli(); - this.root.then(async root => { - if (!root) { - const data = await this.readFromUserHome(); - if (data && data.recentRoots) { - this.root = Promise.resolve(data.recentRoots[0]); - } + if (!await this.root) { + const data = await this.readFromUserHome(); + if (data && data.recentRoots) { + this.root = Promise.resolve(data.recentRoots[0]); } - }); + } } getRoot(): Promise {