Throw errors if accessing paths before set

This commit is contained in:
Asher 2019-02-06 16:55:29 -06:00
parent 5ea1d8b2aa
commit 5d02194048
No known key found for this signature in database
GPG Key ID: 7BB4BA9C783D2BBC
5 changed files with 47 additions and 19 deletions

View File

@ -1,5 +1,5 @@
import { field, logger, time, Time } from "@coder/logger"; import { field, logger, time, Time } from "@coder/logger";
import { ISharedProcessData } from "@coder/protocol"; import { SharedProcessData } from "@coder/protocol";
import { retry } from "./retry"; import { retry } from "./retry";
import { upload } from "./upload"; import { upload } from "./upload";
import { client } from "./fill/client"; import { client } from "./fill/client";
@ -25,7 +25,7 @@ export abstract class IdeClient {
private readonly loadTime: Time; private readonly loadTime: Time;
public readonly initData = client.initData; public readonly initData = client.initData;
public readonly sharedProcessData: Promise<ISharedProcessData>; public readonly sharedProcessData: Promise<SharedProcessData>;
public readonly onSharedProcessActive = client.onSharedProcessActive; public readonly onSharedProcessActive = client.onSharedProcessActive;
public constructor() { public constructor() {

View File

@ -1,4 +1,4 @@
import { ReadWriteConnection, InitData, OperatingSystem, ISharedProcessData } from "../common/connection"; import { ReadWriteConnection, InitData, OperatingSystem, SharedProcessData } from "../common/connection";
import { NewEvalMessage, ServerMessage, EvalDoneMessage, EvalFailedMessage, TypedValue, ClientMessage, NewSessionMessage, TTYDimensions, SessionOutputMessage, CloseSessionInputMessage, WorkingInitMessage, EvalEventMessage } from "../proto"; import { NewEvalMessage, ServerMessage, EvalDoneMessage, EvalFailedMessage, TypedValue, ClientMessage, NewSessionMessage, TTYDimensions, SessionOutputMessage, CloseSessionInputMessage, WorkingInitMessage, EvalEventMessage } from "../proto";
import { Emitter } from "@coder/events"; import { Emitter } from "@coder/events";
import { logger, field } from "@coder/logger"; import { logger, field } from "@coder/logger";
@ -30,7 +30,7 @@ export class Client {
private readonly initDataEmitter = new Emitter<InitData>(); private readonly initDataEmitter = new Emitter<InitData>();
private readonly initDataPromise: Promise<InitData>; private readonly initDataPromise: Promise<InitData>;
private readonly sharedProcessActiveEmitter = new Emitter<ISharedProcessData>(); private readonly sharedProcessActiveEmitter = new Emitter<SharedProcessData>();
public readonly onSharedProcessActive = this.sharedProcessActiveEmitter.event; public readonly onSharedProcessActive = this.sharedProcessActiveEmitter.event;
/** /**

View File

@ -24,7 +24,7 @@ export interface InitData {
readonly builtInExtensionsDirectory: string; readonly builtInExtensionsDirectory: string;
} }
export interface ISharedProcessData { export interface SharedProcessData {
readonly socketPath: string; readonly socketPath: string;
readonly logPath: string; readonly logPath: string;
} }

View File

@ -156,16 +156,10 @@ export class Client extends IdeClient {
protected initialize(): Promise<void> { protected initialize(): Promise<void> {
registerContextMenuListener(); registerContextMenuListener();
const pathSets = this.sharedProcessData.then((data) => {
paths._paths.socketPath = data.socketPath;
process.env.VSCODE_LOGS = data.logPath;
});
this._clipboardContextKey = new RawContextKey("nativeClipboard", this.clipboard.isEnabled); this._clipboardContextKey = new RawContextKey("nativeClipboard", this.clipboard.isEnabled);
return this.task("Start workbench", 1000, async (data) => { return this.task("Start workbench", 1000, async (data, sharedData) => {
paths._paths.appData = data.dataDirectory; paths._paths.initialize(data, sharedData);
paths._paths.defaultUserData = data.dataDirectory;
this._builtInExtensionsDirectory = data.builtInExtensionsDirectory; this._builtInExtensionsDirectory = data.builtInExtensionsDirectory;
process.env.SHELL = data.shell; process.env.SHELL = data.shell;
@ -190,7 +184,7 @@ export class Client extends IdeClient {
bounded.set(enabled); bounded.set(enabled);
}); });
this.clipboard.initialize(); this.clipboard.initialize();
}, this.initData, pathSets); }, this.initData, this.sharedProcessData);
} }
} }

View File

@ -1,8 +1,42 @@
export const _paths = { import { InitData, SharedProcessData } from "@coder/protocol";
appData: "/tmp",
defaultUserData: "/tmp",
socketPath: "/tmp/vscode-remote.sock",
};
class Paths {
private _appData: string | undefined;
private _defaultUserData: string | undefined;
private _socketPath: string | undefined;
public get appData(): string {
if (typeof this._appData === "undefined") {
throw new Error("trying to access appData before it has been set");
}
return this._appData;
}
public get defaultUserData(): string {
if (typeof this._defaultUserData === "undefined") {
throw new Error("trying to access defaultUserData before it has been set");
}
return this._defaultUserData;
}
public get socketPath(): string {
if (typeof this._socketPath === "undefined") {
throw new Error("trying to access socketPath before it has been set");
}
return this._socketPath;
}
public initialize(data: InitData, sharedData: SharedProcessData): void {
process.env.VSCODE_LOGS = sharedData.logPath;
this._appData = data.dataDirectory;
this._defaultUserData = data.dataDirectory;
this._socketPath = sharedData.socketPath;
}
}
export const _paths = new Paths();
export const getAppDataPath = (): string => _paths.appData; export const getAppDataPath = (): string => _paths.appData;
export const getDefaultUserDataPath = (): string => _paths.defaultUserData; export const getDefaultUserDataPath = (): string => _paths.defaultUserData;