From d0d4c4b320632cc6c5a610b6ec22d9305f81396b Mon Sep 17 00:00:00 2001 From: Jamey Huffnagle Date: Thu, 15 Feb 2024 15:22:57 -0500 Subject: [PATCH] refactor(app): remove notification event emitters --- app/src/redux/shell/remote.ts | 12 +++++------- app/src/redux/shell/types.ts | 10 +++++++++- app/src/resources/useNotifyService.ts | 18 ++++-------------- 3 files changed, 18 insertions(+), 22 deletions(-) diff --git a/app/src/redux/shell/remote.ts b/app/src/redux/shell/remote.ts index 9671f600e5e..093ae1f4b92 100644 --- a/app/src/redux/shell/remote.ts +++ b/app/src/redux/shell/remote.ts @@ -1,10 +1,9 @@ // access main process remote modules via attachments to `global` import assert from 'assert' -import { EventEmitter } from 'events' import type { AxiosRequestConfig } from 'axios' import type { ResponsePromise } from '@opentrons/api-client' -import type { Remote, NotifyTopic } from './types' +import type { Remote, NotifyTopic, NotifyResponseData } from './types' const emptyRemote: Remote = {} as any @@ -40,16 +39,15 @@ export function appShellRequestor( export function appShellListener( hostname: string | null, - topic: NotifyTopic -): EventEmitter { - const eventEmitter = new EventEmitter() + topic: NotifyTopic, + listenerCb: (data: NotifyResponseData) => void +): void { remote.ipcRenderer.on( 'notify', (_, shellHostname, shellTopic, shellMessage) => { if (hostname === shellHostname && topic === shellTopic) { - eventEmitter.emit('data', shellMessage) + listenerCb(shellMessage) } } ) - return eventEmitter } diff --git a/app/src/redux/shell/types.ts b/app/src/redux/shell/types.ts index 874b98296ef..e2baffba65f 100644 --- a/app/src/redux/shell/types.ts +++ b/app/src/redux/shell/types.ts @@ -12,13 +12,21 @@ export interface Remote { event: IpcMainEvent, hostname: string, topic: NotifyTopic, - message: string | Object, + message: NotifyResponseData | NotifyNetworkError, ...args: unknown[] ) => void ) => void } } +interface NotifyRefetchData { + refetchUsingHTTP: boolean + statusCode: never +} + +export type NotifyNetworkError = 'ECONNFAILED' | 'ECONNREFUSED' +export type NotifyResponseData = NotifyRefetchData | NotifyNetworkError + interface File { sha512: string url: string diff --git a/app/src/resources/useNotifyService.ts b/app/src/resources/useNotifyService.ts index 87398f4bc50..05a73337cae 100644 --- a/app/src/resources/useNotifyService.ts +++ b/app/src/resources/useNotifyService.ts @@ -12,22 +12,13 @@ import { } from '../redux/analytics' import type { UseQueryOptions } from 'react-query' -import type { NotifyTopic } from '../redux/shell/types' +import type { NotifyTopic, NotifyResponseData } from '../redux/shell/types' export interface QueryOptionsWithPolling extends UseQueryOptions { forceHttpPolling?: boolean } -interface NotifyRefetchData { - refetchUsingHTTP: boolean - statusCode: never -} - -export type NotifyNetworkError = 'ECONNFAILED' | 'ECONNREFUSED' - -type NotifyResponseData = NotifyRefetchData | NotifyNetworkError - interface UseNotifyServiceProps { topic: NotifyTopic refetchUsingHTTP: () => void @@ -55,12 +46,11 @@ export function useNotifyService({ hostname != null && staleTime !== Infinity ) { - const eventEmitter = appShellListener(hostname, topic) - eventEmitter.on('data', onDataListener) + const listenerCb = (data: NotifyResponseData): void => onDataEvent(data) + appShellListener(hostname, topic, listenerCb) dispatch(notifySubscribeAction(hostname, topic)) return () => { - eventEmitter.off('data', onDataListener) if (hostname != null) { dispatch(notifyUnsubscribeAction(hostname, topic)) } @@ -77,7 +67,7 @@ export function useNotifyService({ return { isNotifyError: isNotifyError.current } - function onDataListener(data: NotifyResponseData): void { + function onDataEvent(data: NotifyResponseData): void { if (!isNotifyError.current) { if (data === 'ECONNFAILED' || data === 'ECONNREFUSED') { isNotifyError.current = true