Skip to content

Commit

Permalink
Diagnostics: Fix diagnostics-event
Browse files Browse the repository at this point in the history
TypeScript can't compute the type of the payload correctly without using a
normal discriminated union; this doesn't look as nice, but at least it
seems to work.

Signed-off-by: Mark Yen <[email protected]>
  • Loading branch information
mook-as committed Sep 19, 2024
1 parent fb97993 commit b5a5385
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
16 changes: 12 additions & 4 deletions pkg/rancher-desktop/integrations/pathManagerImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,13 @@ export class RcFilePathManager implements PathManager {
protected async manageLinesInFile(fileName: string, filePath: string, lines: string[], desiredPresent: boolean) {
try {
await manageLinesInFile(filePath, lines, desiredPresent);
mainEvents.emit('diagnostics-event', 'path-management', { fileName, error: undefined });
mainEvents.emit('diagnostics-event', {
id: 'path-management', fileName, error: undefined,
});
} catch (error: any) {
mainEvents.emit('diagnostics-event', 'path-management', { fileName, error });
mainEvents.emit('diagnostics-event', {
id: 'path-management', fileName, error,
});
throw error;
}
}
Expand Down Expand Up @@ -96,10 +100,14 @@ export class RcFilePathManager implements PathManager {
} catch (error: any) {
if (error.code === 'ENOENT') {
// If the file does not exist, it is not an error.
mainEvents.emit('diagnostics-event', 'path-management', { fileName, error: undefined });
mainEvents.emit('diagnostics-event', {
id: 'path-management', fileName, error: undefined,
});
continue;
}
mainEvents.emit('diagnostics-event', 'path-management', { fileName, error });
mainEvents.emit('diagnostics-event', {
id: 'path-management', fileName, error,
});
throw error;
}
await this.manageLinesInFile(fileName, filePath, [pathLine], !linesAdded);
Expand Down
7 changes: 3 additions & 4 deletions pkg/rancher-desktop/main/diagnostics/pathManagement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@ const CheckPathManagement: DiagnosticsChecker = {
},
};

mainEvents.on('diagnostics-event', (id, state) => {
if (id !== 'path-management') {
mainEvents.on('diagnostics-event', (payload) => {
if (payload.id !== 'path-management') {
return;
}
const typedState: { fileName: string, error: Error | undefined } = state;
const { fileName, error } = typedState;
const { fileName, error } = payload;

cachedResults[fileName] = {
description: error?.message ?? error?.toString() ?? `Unknown error managing ${ fileName }`,
Expand Down
7 changes: 3 additions & 4 deletions pkg/rancher-desktop/main/mainEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ interface MainEventNames {
* @param id The diagnostic identifier.
* @param state The new state for the diagnostic.
*/
'diagnostics-event'<K extends keyof DiagnosticsEventPayload>(id: K, state: DiagnosticsEventPayload[K]): void;
'diagnostics-event'(payload: DiagnosticsEventPayload): void;

/**
* Emitted when an extension is uninstalled via the extension manager.
Expand Down Expand Up @@ -148,9 +148,8 @@ interface MainEventNames {
* DiagnosticsEventPayload defines the data that will be passed on a
* 'diagnostics-event' event.
*/
type DiagnosticsEventPayload = {
'path-management': { fileName: string; error: Error | undefined };
};
type DiagnosticsEventPayload =
{ id: 'path-management', fileName: string; error: Error | undefined };

/**
* Helper type definition to check if the given event name is a handler (i.e.
Expand Down

0 comments on commit b5a5385

Please sign in to comment.