Archived
1
0

Some cleanup

- Use whateverEmitter.event for the onWhatever methods.
- Add readonly to a bunch of things.
- Remove some redundancy in types.
- Move initializations out of the constructor and into the declarations
  where it was reasonable to do so.
- Disable a few no-any violations.
This commit is contained in:
Asher
2019-02-06 11:53:23 -06:00
parent ddf96077a3
commit 588da0443c
16 changed files with 98 additions and 164 deletions

View File

@ -1,6 +1,5 @@
import { Event } from "@coder/events";
import { field, logger, time, Time } from "@coder/logger";
import { InitData, ISharedProcessData } from "@coder/protocol";
import { ISharedProcessData } from "@coder/protocol";
import { retry } from "./retry";
import { upload } from "./upload";
import { client } from "./fill/client";
@ -24,14 +23,16 @@ export abstract class IdeClient {
private readonly tasks = <string[]>[];
private finishedTaskCount = 0;
private readonly loadTime: Time;
private readonly sharedProcessDataPromise: Promise<ISharedProcessData>;
public readonly initData = client.initData;
public readonly sharedProcessData: Promise<ISharedProcessData>;
public readonly onSharedProcessActive = client.onSharedProcessActive;
public constructor() {
logger.info("Loading IDE");
this.loadTime = time(2500);
this.sharedProcessDataPromise = new Promise((resolve): void => {
this.sharedProcessData = new Promise((resolve): void => {
client.onSharedProcessActive(resolve);
});
@ -96,27 +97,6 @@ export abstract class IdeClient {
}
}
/**
* A promise that resolves with initialization data.
*/
public get initData(): Promise<InitData> {
return client.initData;
}
/**
* An event that fires every time the shared process (re-)starts.
*/
public get onSharedProcessActive(): Event<ISharedProcessData> {
return client.onSharedProcessActive;
}
/**
* A promise that resolves with *initial* shared process data.
*/
public get sharedProcessData(): Promise<ISharedProcessData> {
return this.sharedProcessDataPromise;
}
public set notificationService(service: INotificationService) {
this.retry.notificationService = service;
this.upload.notificationService = service;

View File

@ -9,36 +9,28 @@ import { retry } from "../retry";
*/
class Connection implements ReadWriteConnection {
private activeSocket: WebSocket | undefined;
private readonly messageEmitter: Emitter<Uint8Array> = new Emitter();
private readonly closeEmitter: Emitter<void> = new Emitter();
private readonly upEmitter: Emitter<void> = new Emitter();
private readonly downEmitter: Emitter<void> = new Emitter();
private readonly messageBuffer: Uint8Array[] = [];
private socketTimeoutDelay = 60 * 1000;
private retryName = "Socket";
private readonly messageBuffer = <Uint8Array[]>[];
private readonly socketTimeoutDelay = 60 * 1000;
private readonly retryName = "Socket";
private isUp: boolean = false;
private closed: boolean = false;
private readonly messageEmitter = new Emitter<Uint8Array>();
private readonly closeEmitter = new Emitter<void>();
private readonly upEmitter = new Emitter<void>();
private readonly downEmitter = new Emitter<void>();
public readonly onUp = this.upEmitter.event;
public readonly onClose = this.closeEmitter.event;
public readonly onDown = this.downEmitter.event;
public readonly onMessage = this.messageEmitter.event;
public constructor() {
retry.register(this.retryName, () => this.connect());
retry.block(this.retryName);
retry.run(this.retryName);
}
/**
* Register a function to be called when the connection goes up.
*/
public onUp(cb: () => void): void {
this.upEmitter.event(cb);
}
/**
* Register a function to be called when the connection goes down.
*/
public onDown(cb: () => void): void {
this.downEmitter.event(cb);
}
public send(data: Buffer | Uint8Array): void {
if (this.closed) {
throw new Error("web socket is closed");
@ -50,14 +42,6 @@ class Connection implements ReadWriteConnection {
}
}
public onMessage(cb: (data: Uint8Array | Buffer) => void): void {
this.messageEmitter.event(cb);
}
public onClose(cb: () => void): void {
this.closeEmitter.event(cb);
}
public close(): void {
this.closed = true;
this.dispose();

View File

@ -1,11 +1,11 @@
import { IDisposable } from "@coder/disposable";
import { Emitter } from "@coder/events";
/**
* Wrapper around the native clipboard with some fallbacks.
*/
export class Clipboard {
private readonly enableEmitter: Emitter<boolean> = new Emitter();
private readonly enableEmitter = new Emitter<boolean>();
public readonly onPermissionChange = this.enableEmitter.event;
private _isEnabled: boolean = false;
/**
@ -70,14 +70,6 @@ export class Clipboard {
// tslint:enable no-any
}
/**
* Register a function to be called when the native clipboard is
* enabled/disabled.
*/
public onPermissionChange(cb: (enabled: boolean) => void): IDisposable {
return this.enableEmitter.event(cb);
}
/**
* Read text from the clipboard.
*/

View File

@ -1,4 +1,3 @@
import { IDisposable } from "@coder/disposable";
import { Emitter } from "@coder/events";
import "./dialog.scss";
@ -27,19 +26,16 @@ export enum IKey {
}
export class Dialog {
private options: IDialogOptions;
private overlay: HTMLElement;
private readonly overlay: HTMLElement;
private cachedActiveElement: HTMLElement | undefined;
private input: HTMLInputElement | undefined;
private actionEmitter: Emitter<IDialogAction>;
private errors: HTMLElement;
private buttons: HTMLElement[] | undefined;
public constructor(options: IDialogOptions) {
this.options = options;
this.actionEmitter = new Emitter();
private actionEmitter = new Emitter<IDialogAction>();
public onAction = this.actionEmitter.event;
public constructor(private readonly options: IDialogOptions) {
const msgBox = document.createElement("div");
msgBox.classList.add("msgbox");
@ -108,13 +104,6 @@ export class Dialog {
});
}
/**
* Register a function to be called when the user performs an action.
*/
public onAction(callback: (action: IDialogAction) => void): IDisposable {
return this.actionEmitter.event(callback);
}
/**
* Input value if this dialog has an input.
*/

View File

@ -13,7 +13,7 @@ class OS {
public homedir(): string {
if (typeof this._homedir === "undefined") {
throw new Error("not initialized");
throw new Error("trying to access homedir before it has been set");
}
return this._homedir;
@ -21,7 +21,7 @@ class OS {
public tmpdir(): string {
if (typeof this._tmpdir === "undefined") {
throw new Error("not initialized");
throw new Error("trying to access tmpdir before it has been set");
}
return this._tmpdir;

View File

@ -21,7 +21,7 @@ interface IRetryItem {
* to the user explaining what is happening with an option to immediately retry.
*/
export class Retry {
private items: Map<string, IRetryItem>;
private items = new Map<string, IRetryItem>();
// Times are in seconds.
private readonly retryMinDelay = 1;
@ -31,17 +31,15 @@ export class Retry {
private blocked: string | boolean | undefined;
private notificationHandle: INotificationHandle | undefined;
private updateDelay = 1;
private readonly updateDelay = 1;
private updateTimeout: number | NodeJS.Timer | undefined;
private notificationThreshold = 3;
private readonly notificationThreshold = 3;
// Time in milliseconds to wait before restarting a service. (See usage below
// for reasoning.)
private waitDelay = 50;
private readonly waitDelay = 50;
public constructor(private _notificationService: INotificationService) {
this.items = new Map();
}
public constructor(private _notificationService: INotificationService) {}
public set notificationService(service: INotificationService) {
this._notificationService = service;

View File

@ -1,7 +1,7 @@
import { exec } from "child_process";
import { appendFile } from "fs";
import { promisify } from "util";
import { logger, Logger } from "@coder/logger";
import { logger } from "@coder/logger";
import { escapePath } from "@coder/protocol";
import { NotificationService, INotificationService, ProgressService, IProgressService, IProgress, Severity } from "./fill/notification";
@ -40,27 +40,20 @@ export class Upload {
private readonly maxParallelUploads = 100;
private readonly readSize = 32000; // ~32kb max while reading in the file.
private readonly packetSize = 32000; // ~32kb max when writing.
private readonly logger: Logger;
private readonly currentlyUploadingFiles: Map<string, File>;
private readonly queueByDirectory: Map<string, IUploadableDirectory>;
private readonly logger = logger.named("Upload");
private readonly currentlyUploadingFiles = new Map<string, File>();
private readonly queueByDirectory = new Map<string, IUploadableDirectory>();
private progress: IProgress | undefined;
private uploadPromise: Promise<string[]> | undefined;
private resolveUploadPromise: (() => void) | undefined;
private finished: number;
private uploadedFilePaths: string[];
private total: number;
private finished = 0;
private uploadedFilePaths = <string[]>[];
private total = 0;
public constructor(
private _notificationService: INotificationService,
private _progressService: IProgressService,
) {
this.logger = logger.named("Upload");
this.currentlyUploadingFiles = new Map();
this.queueByDirectory = new Map();
this.uploadedFilePaths = [];
this.finished = 0;
this.total = 0;
}
) {}
public set notificationService(service: INotificationService) {
this._notificationService = service;