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:
@ -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();
|
||||
|
@ -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.
|
||||
*/
|
||||
|
@ -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.
|
||||
*/
|
||||
|
@ -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;
|
||||
|
Reference in New Issue
Block a user