Skip to content

Commit

Permalink
Annotate return types
Browse files Browse the repository at this point in the history
  • Loading branch information
Hal-9k1 committed Oct 17, 2024
1 parent b7c3835 commit 43ddb25
Show file tree
Hide file tree
Showing 22 changed files with 205 additions and 154 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module.exports = {
],
'no-redeclare': 'off', // False positives on typescript's function overloads
'no-undef': 'off', // False positives and typescript won't build if actually undefined vars
'@typescript-eslint/explicit-function-return-type': 'error',
},
parserOptions: {
ecmaVersion: 2022,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"build:renderer": "cross-env NODE_ENV=production TS_NODE_TRANSPILE_ONLY=true webpack --config ./.erb/configs/webpack.config.renderer.prod.ts",
"doc": "typedoc --name Dawn --excludePrivate false --exclude src/node_modules/** --exclude src/__tests__/** src/**/*.ts src/**/*.tsx --validation",
"postinstall": "ts-node .erb/scripts/check-native-dep.js && electron-builder install-app-deps && npm run build:dll",
"lint": "cross-env NODE_ENV=development eslint . --ext .js,.jsx,.ts,.tsx",
"lint": "cross-env NODE_ENV=development eslint src --ext .js,.jsx,.ts,.tsx",
"package": "ts-node ./.erb/scripts/clean.js dist && npm run build && electron-builder build --publish never && npm run build:dll",
"rebuild": "electron-rebuild --parallel --types prod,dev,optional --module-dir release/app",
"start": "ts-node ./.erb/scripts/check-port-in-use.js && npm run start:renderer",
Expand Down
7 changes: 5 additions & 2 deletions src/common/IpcEventTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,10 @@ export interface RendererFileControlPromptDownloadData {
* Data for a specialization of the renderer-file-control event, sent when the main process wants to
* close the file open in the editor.
*/
interface RendererFileControlPromptCreateNewFile {
export interface RendererFileControlPromptCreateNewFileData {
/**
* The subtype of file control event.
*/
type: 'promptCreateNewFile';
}
/**
Expand All @@ -174,7 +177,7 @@ export type RendererFileControlData =
| RendererFileControlExtChangeData
| RendererFileControlPromptUploadData
| RendererFileControlPromptDownloadData
| RendererFileControlPromptCreateNewFile;
| RendererFileControlPromptCreateNewFileData;
/**
* Data for the renderer-post-console event sent by the main process to add a console message to the
* AppConsole.
Expand Down
75 changes: 52 additions & 23 deletions src/main/MainApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ export default class MainApp implements MenuHandler, RuntimeCommsListener {
* happen more than once in the program's lifetime (e.g. if the BrowserWindow is somehow
* reloaded).
*/
onPresent() {
onPresent(): void {
this.#watcher?.close();
this.#savePath = null;
this.#sendToRenderer('renderer-init', {
Expand All @@ -237,7 +237,11 @@ export default class MainApp implements MenuHandler, RuntimeCommsListener {
});
}

onReceiveRobotLogs(msgs: string[]) {
/**
* Called when the robot emits log messages.
* @param msgs - an array of new log messages.
*/
onReceiveRobotLogs(msgs: string[]): void {
msgs.forEach((msg) => {
this.#sendToRenderer(
'renderer-post-console',
Expand All @@ -246,15 +250,28 @@ export default class MainApp implements MenuHandler, RuntimeCommsListener {
});
}

onReceiveLatency(latency: number) {
/**
* Called when a latency check completes.
* @param latency - the measured robot connection latency.
*/
onReceiveLatency(latency: number): void {
this.#sendToRenderer('renderer-latency-update', latency);
}

onReceiveDevices(deviceInfoState: DeviceInfoState[]) {
/**
* Called when the robot sends lowcar device state.
* @param deviceInfoState - an array of the devices currently connected to the robot and their
* currently measured parameters.
*/
onReceiveDevices(deviceInfoState: DeviceInfoState[]): void {
this.#sendToRenderer('renderer-devices-update', deviceInfoState);
}

onRuntimeTcpError(err: Error) {
/**
* Called when an error is encountered in the TCP connection to the robot.
* @param err - the error.
*/
onRuntimeTcpError(err: Error): void {
this.#sendToRenderer(
'renderer-post-console',
new AppConsoleMessage(
Expand All @@ -264,7 +281,12 @@ export default class MainApp implements MenuHandler, RuntimeCommsListener {
);
}

onRuntimeUdpError(err: Error) {
/**
* Called when the UDP socket encounters an error or data received by the UDP socket is malformed.
* @param err - the error. Protobuf ProtocolErrors are likely the result of a UDP transmission
* error.
*/
onRuntimeUdpError(err: Error): void {
this.#sendToRenderer(
'renderer-post-console',
new AppConsoleMessage(
Expand All @@ -274,7 +296,11 @@ export default class MainApp implements MenuHandler, RuntimeCommsListener {
);
}

onRuntimeError(err: Error) {
/**
* Called when a generic Runtime communications error is encountered.
* @param err - the error.
*/
onRuntimeError(err: Error): void {
this.#sendToRenderer(
'renderer-post-console',
new AppConsoleMessage(
Expand All @@ -284,7 +310,10 @@ export default class MainApp implements MenuHandler, RuntimeCommsListener {
);
}

onRuntimeDisconnect() {
/**
* Called when the TCP connection to the robot is lost for any reason.
*/
onRuntimeDisconnect(): void {
this.#sendToRenderer(
'renderer-post-console',
new AppConsoleMessage('dawn-info', 'Disconnected from robot.'),
Expand All @@ -296,7 +325,7 @@ export default class MainApp implements MenuHandler, RuntimeCommsListener {
* @param forceDialog - whether the save path selection dialog should be shown even if there is
* a currently opened file that may be saved to.
*/
promptSaveCodeFile(forceDialog: boolean) {
promptSaveCodeFile(forceDialog: boolean): void {
// We need a round trip to the renderer because that's where the code in the editor actually
// lives
this.#sendToRenderer('renderer-file-control', {
Expand All @@ -309,31 +338,31 @@ export default class MainApp implements MenuHandler, RuntimeCommsListener {
* Requests that the renderer process start to load code from a file into the editor. The renderer
* may delay or ignore this request (e.g. if the editor has unsaved changes).
*/
promptLoadCodeFile() {
promptLoadCodeFile(): void {
this.#sendToRenderer('renderer-file-control', { type: 'promptLoad' });
}

/**
* Requests that the renderer process start to upload the last loaded file to the robot. The round
* trip is needed to notify the user that unsaved changes in the editor will not be uploaded.
*/
promptUploadCodeFile() {
promptUploadCodeFile(): void {
this.#sendToRenderer('renderer-file-control', { type: 'promptUpload' });
}

/**
* Requests that the renderer process start to download code from the robot into the editor. The
* renderer may delay or ignore this request (e.g. if the editor has unsaved changes).
*/
promptDownloadCodeFile() {
promptDownloadCodeFile(): void {
this.#sendToRenderer('renderer-file-control', { type: 'promptDownload' });
}

/**
* Requests that the renderer process close the open file in the editor. The renderer may delay or
* ignore this request (e.g. if there are unsaved changes).
*/
promptCreateNewCodeFile() {
promptCreateNewCodeFile(): void {
this.#sendToRenderer('renderer-file-control', {
type: 'promptCreateNewFile',
});
Expand All @@ -345,7 +374,7 @@ export default class MainApp implements MenuHandler, RuntimeCommsListener {
* @param forceDialog - whether the user should be prompted for a save path even if there is a
* currently open file.
*/
#saveCodeFile(code: string, forceDialog: boolean) {
#saveCodeFile(code: string, forceDialog: boolean): void {
let success = true;
if (this.#savePath === null || forceDialog) {
success = this.#showCodePathDialog('save');
Expand Down Expand Up @@ -377,9 +406,9 @@ export default class MainApp implements MenuHandler, RuntimeCommsListener {
}

/**
* Tries to load code from a file into the editor. Fails is the user does not choose a path.
* Tries to load code from a file into the editor. Fails if the user does not choose a path.
*/
#openCodeFile() {
#openCodeFile(): void {
const success = this.#showCodePathDialog('load');
if (success) {
try {
Expand All @@ -403,7 +432,7 @@ export default class MainApp implements MenuHandler, RuntimeCommsListener {
* contents) to the robot at the given IP. Does nothing if no file has been opened yet.
* @param ip - the IP address to connect to via SSH
*/
#uploadCodeFile(ip: string) {
#uploadCodeFile(ip: string): void {
if (this.#savePath) {
if (
fs
Expand Down Expand Up @@ -453,7 +482,7 @@ export default class MainApp implements MenuHandler, RuntimeCommsListener {
* Downloads code from the robot at the given IP into the editor.
* @param ip - the IP address to connect to via SSH
*/
#downloadCodeFile(ip: string) {
#downloadCodeFile(ip: string): void {
this.#codeTransfer
.download(ip)
.then((content: string) => {
Expand Down Expand Up @@ -486,9 +515,9 @@ export default class MainApp implements MenuHandler, RuntimeCommsListener {
* @param e - the value
* @returns A string representation of the value.
*/
#getErrorDetails(e: any) {
let msg = e instanceof Error ? e.stack : String(e);
if (e.cause) {
#getErrorDetails(e: any): string {
let msg = e instanceof Error && e.stack ? e.stack : String(e);
if (e instanceof Error && e.cause) {
msg += `\nCaused by: ${this.#getErrorDetails(e.cause)}`;
}
return msg;
Expand All @@ -500,7 +529,7 @@ export default class MainApp implements MenuHandler, RuntimeCommsListener {
* @param mode - the type of dialog that should be shown
* @returns Whether a new path was chosen successfully.
*/
#showCodePathDialog(mode: 'save' | 'load') {
#showCodePathDialog(mode: 'save' | 'load'): boolean {
let result: string | string[] | undefined;
if (mode === 'save') {
result = dialog.showSaveDialogSync(this.#mainWindow, {
Expand Down Expand Up @@ -532,7 +561,7 @@ export default class MainApp implements MenuHandler, RuntimeCommsListener {
* Attaches a file watcher to listen for external changes to the last saved or loaded code path,
* destroying the previous one if it exists.
*/
#watchCodeFile() {
#watchCodeFile(): void {
this.#watcher?.close();
this.#watchDebounce = true;
this.#watcher = fs.watch(
Expand Down
4 changes: 2 additions & 2 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ if (isDebug) {
require('electron-debug')();
}

const installExtensions = async () => {
const installExtensions = async (): Promise<string> => {
const installer = require('electron-devtools-installer');
const forceDownload = !!process.env.UPGRADE_EXTENSIONS;
const extensions = ['REACT_DEVELOPER_TOOLS'];
Expand All @@ -51,7 +51,7 @@ const installExtensions = async () => {
.catch(console.log);
};

const createWindow = async () => {
const createWindow = async (): Promise<void> => {
if (isDebug) {
await installExtensions();
}
Expand Down
Loading

0 comments on commit 43ddb25

Please sign in to comment.