Skip to content

Commit 588da04

Browse files
committed
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.
1 parent ddf9607 commit 588da04

16 files changed

Lines changed: 100 additions & 166 deletions

File tree

packages/ide/src/client.ts

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { Event } from "@coder/events";
21
import { field, logger, time, Time } from "@coder/logger";
3-
import { InitData, ISharedProcessData } from "@coder/protocol";
2+
import { ISharedProcessData } from "@coder/protocol";
43
import { retry } from "./retry";
54
import { upload } from "./upload";
65
import { client } from "./fill/client";
@@ -24,14 +23,16 @@ export abstract class IdeClient {
2423
private readonly tasks = <string[]>[];
2524
private finishedTaskCount = 0;
2625
private readonly loadTime: Time;
27-
private readonly sharedProcessDataPromise: Promise<ISharedProcessData>;
26+
27+
public readonly initData = client.initData;
28+
public readonly sharedProcessData: Promise<ISharedProcessData>;
29+
public readonly onSharedProcessActive = client.onSharedProcessActive;
2830

2931
public constructor() {
3032
logger.info("Loading IDE");
31-
3233
this.loadTime = time(2500);
3334

34-
this.sharedProcessDataPromise = new Promise((resolve): void => {
35+
this.sharedProcessData = new Promise((resolve): void => {
3536
client.onSharedProcessActive(resolve);
3637
});
3738

@@ -96,27 +97,6 @@ export abstract class IdeClient {
9697
}
9798
}
9899

99-
/**
100-
* A promise that resolves with initialization data.
101-
*/
102-
public get initData(): Promise<InitData> {
103-
return client.initData;
104-
}
105-
106-
/**
107-
* An event that fires every time the shared process (re-)starts.
108-
*/
109-
public get onSharedProcessActive(): Event<ISharedProcessData> {
110-
return client.onSharedProcessActive;
111-
}
112-
113-
/**
114-
* A promise that resolves with *initial* shared process data.
115-
*/
116-
public get sharedProcessData(): Promise<ISharedProcessData> {
117-
return this.sharedProcessDataPromise;
118-
}
119-
120100
public set notificationService(service: INotificationService) {
121101
this.retry.notificationService = service;
122102
this.upload.notificationService = service;

packages/ide/src/fill/client.ts

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,28 @@ import { retry } from "../retry";
99
*/
1010
class Connection implements ReadWriteConnection {
1111
private activeSocket: WebSocket | undefined;
12-
private readonly messageEmitter: Emitter<Uint8Array> = new Emitter();
13-
private readonly closeEmitter: Emitter<void> = new Emitter();
14-
private readonly upEmitter: Emitter<void> = new Emitter();
15-
private readonly downEmitter: Emitter<void> = new Emitter();
16-
private readonly messageBuffer: Uint8Array[] = [];
17-
private socketTimeoutDelay = 60 * 1000;
18-
private retryName = "Socket";
12+
private readonly messageBuffer = <Uint8Array[]>[];
13+
private readonly socketTimeoutDelay = 60 * 1000;
14+
private readonly retryName = "Socket";
1915
private isUp: boolean = false;
2016
private closed: boolean = false;
2117

18+
private readonly messageEmitter = new Emitter<Uint8Array>();
19+
private readonly closeEmitter = new Emitter<void>();
20+
private readonly upEmitter = new Emitter<void>();
21+
private readonly downEmitter = new Emitter<void>();
22+
23+
public readonly onUp = this.upEmitter.event;
24+
public readonly onClose = this.closeEmitter.event;
25+
public readonly onDown = this.downEmitter.event;
26+
public readonly onMessage = this.messageEmitter.event;
27+
2228
public constructor() {
2329
retry.register(this.retryName, () => this.connect());
2430
retry.block(this.retryName);
2531
retry.run(this.retryName);
2632
}
2733

28-
/**
29-
* Register a function to be called when the connection goes up.
30-
*/
31-
public onUp(cb: () => void): void {
32-
this.upEmitter.event(cb);
33-
}
34-
35-
/**
36-
* Register a function to be called when the connection goes down.
37-
*/
38-
public onDown(cb: () => void): void {
39-
this.downEmitter.event(cb);
40-
}
41-
4234
public send(data: Buffer | Uint8Array): void {
4335
if (this.closed) {
4436
throw new Error("web socket is closed");
@@ -50,14 +42,6 @@ class Connection implements ReadWriteConnection {
5042
}
5143
}
5244

53-
public onMessage(cb: (data: Uint8Array | Buffer) => void): void {
54-
this.messageEmitter.event(cb);
55-
}
56-
57-
public onClose(cb: () => void): void {
58-
this.closeEmitter.event(cb);
59-
}
60-
6145
public close(): void {
6246
this.closed = true;
6347
this.dispose();

packages/ide/src/fill/clipboard.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { IDisposable } from "@coder/disposable";
21
import { Emitter } from "@coder/events";
32

43
/**
54
* Wrapper around the native clipboard with some fallbacks.
65
*/
76
export class Clipboard {
8-
private readonly enableEmitter: Emitter<boolean> = new Emitter();
7+
private readonly enableEmitter = new Emitter<boolean>();
8+
public readonly onPermissionChange = this.enableEmitter.event;
99
private _isEnabled: boolean = false;
1010

1111
/**
@@ -70,14 +70,6 @@ export class Clipboard {
7070
// tslint:enable no-any
7171
}
7272

73-
/**
74-
* Register a function to be called when the native clipboard is
75-
* enabled/disabled.
76-
*/
77-
public onPermissionChange(cb: (enabled: boolean) => void): IDisposable {
78-
return this.enableEmitter.event(cb);
79-
}
80-
8173
/**
8274
* Read text from the clipboard.
8375
*/

packages/ide/src/fill/dialog.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { IDisposable } from "@coder/disposable";
21
import { Emitter } from "@coder/events";
32

43
import "./dialog.scss";
@@ -27,19 +26,16 @@ export enum IKey {
2726
}
2827

2928
export class Dialog {
30-
private options: IDialogOptions;
31-
private overlay: HTMLElement;
29+
private readonly overlay: HTMLElement;
3230
private cachedActiveElement: HTMLElement | undefined;
3331
private input: HTMLInputElement | undefined;
34-
private actionEmitter: Emitter<IDialogAction>;
3532
private errors: HTMLElement;
3633
private buttons: HTMLElement[] | undefined;
3734

38-
public constructor(options: IDialogOptions) {
39-
this.options = options;
40-
41-
this.actionEmitter = new Emitter();
35+
private actionEmitter = new Emitter<IDialogAction>();
36+
public onAction = this.actionEmitter.event;
4237

38+
public constructor(private readonly options: IDialogOptions) {
4339
const msgBox = document.createElement("div");
4440
msgBox.classList.add("msgbox");
4541

@@ -108,13 +104,6 @@ export class Dialog {
108104
});
109105
}
110106

111-
/**
112-
* Register a function to be called when the user performs an action.
113-
*/
114-
public onAction(callback: (action: IDialogAction) => void): IDisposable {
115-
return this.actionEmitter.event(callback);
116-
}
117-
118107
/**
119108
* Input value if this dialog has an input.
120109
*/

packages/ide/src/fill/os.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ class OS {
1313

1414
public homedir(): string {
1515
if (typeof this._homedir === "undefined") {
16-
throw new Error("not initialized");
16+
throw new Error("trying to access homedir before it has been set");
1717
}
1818

1919
return this._homedir;
2020
}
2121

2222
public tmpdir(): string {
2323
if (typeof this._tmpdir === "undefined") {
24-
throw new Error("not initialized");
24+
throw new Error("trying to access tmpdir before it has been set");
2525
}
2626

2727
return this._tmpdir;

packages/ide/src/retry.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ interface IRetryItem {
2121
* to the user explaining what is happening with an option to immediately retry.
2222
*/
2323
export class Retry {
24-
private items: Map<string, IRetryItem>;
24+
private items = new Map<string, IRetryItem>();
2525

2626
// Times are in seconds.
2727
private readonly retryMinDelay = 1;
@@ -31,17 +31,15 @@ export class Retry {
3131
private blocked: string | boolean | undefined;
3232

3333
private notificationHandle: INotificationHandle | undefined;
34-
private updateDelay = 1;
34+
private readonly updateDelay = 1;
3535
private updateTimeout: number | NodeJS.Timer | undefined;
36-
private notificationThreshold = 3;
36+
private readonly notificationThreshold = 3;
3737

3838
// Time in milliseconds to wait before restarting a service. (See usage below
3939
// for reasoning.)
40-
private waitDelay = 50;
40+
private readonly waitDelay = 50;
4141

42-
public constructor(private _notificationService: INotificationService) {
43-
this.items = new Map();
44-
}
42+
public constructor(private _notificationService: INotificationService) {}
4543

4644
public set notificationService(service: INotificationService) {
4745
this._notificationService = service;

packages/ide/src/upload.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { exec } from "child_process";
22
import { appendFile } from "fs";
33
import { promisify } from "util";
4-
import { logger, Logger } from "@coder/logger";
4+
import { logger } from "@coder/logger";
55
import { escapePath } from "@coder/protocol";
66
import { NotificationService, INotificationService, ProgressService, IProgressService, IProgress, Severity } from "./fill/notification";
77

@@ -40,27 +40,20 @@ export class Upload {
4040
private readonly maxParallelUploads = 100;
4141
private readonly readSize = 32000; // ~32kb max while reading in the file.
4242
private readonly packetSize = 32000; // ~32kb max when writing.
43-
private readonly logger: Logger;
44-
private readonly currentlyUploadingFiles: Map<string, File>;
45-
private readonly queueByDirectory: Map<string, IUploadableDirectory>;
43+
private readonly logger = logger.named("Upload");
44+
private readonly currentlyUploadingFiles = new Map<string, File>();
45+
private readonly queueByDirectory = new Map<string, IUploadableDirectory>();
4646
private progress: IProgress | undefined;
4747
private uploadPromise: Promise<string[]> | undefined;
4848
private resolveUploadPromise: (() => void) | undefined;
49-
private finished: number;
50-
private uploadedFilePaths: string[];
51-
private total: number;
49+
private finished = 0;
50+
private uploadedFilePaths = <string[]>[];
51+
private total = 0;
5252

5353
public constructor(
5454
private _notificationService: INotificationService,
5555
private _progressService: IProgressService,
56-
) {
57-
this.logger = logger.named("Upload");
58-
this.currentlyUploadingFiles = new Map();
59-
this.queueByDirectory = new Map();
60-
this.uploadedFilePaths = [];
61-
this.finished = 0;
62-
this.total = 0;
63-
}
56+
) {}
6457

6558
public set notificationService(service: INotificationService) {
6659
this._notificationService = service;

packages/logger/src/logger.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ const hashStringToColor = (str: string): string => {
113113
* currently built items and appends to that.
114114
*/
115115
export abstract class Formatter {
116-
protected format: string = "";
117-
protected args: string[] = [];
116+
protected format = "";
117+
protected args = <string[]>[];
118118

119119
/**
120120
* Add a tag.
@@ -250,14 +250,13 @@ export class Logger {
250250
public level = Level.Info;
251251

252252
private readonly nameColor?: string;
253-
private muted: boolean;
253+
private muted: boolean = false;
254254

255255
public constructor(
256256
private _formatter: Formatter,
257257
private readonly name?: string,
258258
private readonly defaultFields?: FieldArray,
259259
) {
260-
this.muted = false;
261260
if (name) {
262261
this.nameColor = hashStringToColor(name);
263262
}
@@ -388,10 +387,6 @@ export class Logger {
388387
times = fields.filter((f) => f.value instanceof Time);
389388
}
390389

391-
// Format is:
392-
// [TAG] [NAME?] MESSAGE TIME?
393-
// field1 (type)?: value
394-
// field2 (type)?: value
395390
this._formatter.tag(options.type.toUpperCase(), options.tagColor);
396391
if (this.name && this.nameColor) {
397392
this._formatter.tag(this.name.toUpperCase(), this.nameColor);

packages/protocol/src/browser/client.ts

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,28 @@ import { EventEmitter } from "events";
99
* Client accepts an arbitrary connection intended to communicate with the Server.
1010
*/
1111
export class Client {
12-
public Socket: typeof ServerSocket;
12+
public readonly Socket: typeof ServerSocket;
1313

14-
private evalId: number = 0;
15-
private evalDoneEmitter: Emitter<EvalDoneMessage> = new Emitter();
16-
private evalFailedEmitter: Emitter<EvalFailedMessage> = new Emitter();
17-
private evalEventEmitter: Emitter<EvalEventMessage> = new Emitter();
14+
private evalId = 0;
15+
private readonly evalDoneEmitter = new Emitter<EvalDoneMessage>();
16+
private readonly evalFailedEmitter = new Emitter<EvalFailedMessage>();
17+
private readonly evalEventEmitter = new Emitter<EvalEventMessage>();
1818

19-
private sessionId: number = 0;
20-
private readonly sessions: Map<number, ServerProcess> = new Map();
19+
private sessionId = 0;
20+
private readonly sessions = new Map<number, ServerProcess>();
2121

22-
private connectionId: number = 0;
23-
private readonly connections: Map<number, ServerSocket> = new Map();
22+
private connectionId = 0;
23+
private readonly connections = new Map<number, ServerSocket>();
2424

25-
private serverId: number = 0;
26-
private readonly servers: Map<number, ServerListener> = new Map();
25+
private serverId = 0;
26+
private readonly servers = new Map<number, ServerListener>();
2727

2828
private _initData: InitData | undefined;
29-
private initDataEmitter = new Emitter<InitData>();
30-
private initDataPromise: Promise<InitData>;
29+
private readonly initDataEmitter = new Emitter<InitData>();
30+
private readonly initDataPromise: Promise<InitData>;
3131

32-
private sharedProcessActiveEmitter = new Emitter<ISharedProcessData>();
32+
private readonly sharedProcessActiveEmitter = new Emitter<ISharedProcessData>();
33+
public readonly onSharedProcessActive = this.sharedProcessActiveEmitter.event;
3334

3435
/**
3536
* @param connection Established connection to the server
@@ -63,10 +64,6 @@ export class Client {
6364
return this.initDataPromise;
6465
}
6566

66-
public get onSharedProcessActive(): Event<ISharedProcessData> {
67-
return this.sharedProcessActiveEmitter.event;
68-
}
69-
7067
public run(func: (ae: ActiveEval) => void | Promise<void>): ActiveEval;
7168
public run<T1>(func: (ae: ActiveEval, a1: T1) => void | Promise<void>, a1: T1): ActiveEval;
7269
public run<T1, T2>(func: (ae: ActiveEval, a1: T1, a2: T2) => void | Promise<void>, a1: T1, a2: T2): ActiveEval;
@@ -95,8 +92,8 @@ export class Client {
9592
});
9693

9794
return {
98-
on: (event: string, cb: (...args: any[]) => void) => eventEmitter.on(event, cb),
99-
emit: (event: string, ...args: any[]) => {
95+
on: (event: string, cb: (...args: any[]) => void): EventEmitter => eventEmitter.on(event, cb),
96+
emit: (event: string, ...args: any[]): void => {
10097
const eventsMsg = new EvalEventMessage();
10198
eventsMsg.setId(doEval.id);
10299
eventsMsg.setEvent(event);

0 commit comments

Comments
 (0)