Skip to content

Commit

Permalink
refactor(app): remove notification event emitters
Browse files Browse the repository at this point in the history
  • Loading branch information
mjhuff committed Feb 15, 2024
1 parent 9dc8f2d commit d0d4c4b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 22 deletions.
12 changes: 5 additions & 7 deletions app/src/redux/shell/remote.ts
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -40,16 +39,15 @@ export function appShellRequestor<Data>(

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)

Check warning on line 49 in app/src/redux/shell/remote.ts

View check run for this annotation

Codecov / codecov/patch

app/src/redux/shell/remote.ts#L49

Added line #L49 was not covered by tests
}
}
)
return eventEmitter
}
10 changes: 9 additions & 1 deletion app/src/redux/shell/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 4 additions & 14 deletions app/src/resources/useNotifyService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TData, TError = Error>
extends UseQueryOptions<TData, TError> {
forceHttpPolling?: boolean
}

interface NotifyRefetchData {
refetchUsingHTTP: boolean
statusCode: never
}

export type NotifyNetworkError = 'ECONNFAILED' | 'ECONNREFUSED'

type NotifyResponseData = NotifyRefetchData | NotifyNetworkError

interface UseNotifyServiceProps<TData, TError = Error> {
topic: NotifyTopic
refetchUsingHTTP: () => void
Expand Down Expand Up @@ -55,12 +46,11 @@ export function useNotifyService<TData, TError = Error>({
hostname != null &&
staleTime !== Infinity
) {
const eventEmitter = appShellListener(hostname, topic)
eventEmitter.on('data', onDataListener)
const listenerCb = (data: NotifyResponseData): void => onDataEvent(data)
appShellListener(hostname, topic, listenerCb)

Check warning on line 50 in app/src/resources/useNotifyService.ts

View check run for this annotation

Codecov / codecov/patch

app/src/resources/useNotifyService.ts#L49-L50

Added lines #L49 - L50 were not covered by tests
dispatch(notifySubscribeAction(hostname, topic))

return () => {
eventEmitter.off('data', onDataListener)
if (hostname != null) {
dispatch(notifyUnsubscribeAction(hostname, topic))
}
Expand All @@ -77,7 +67,7 @@ export function useNotifyService<TData, TError = Error>({

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
Expand Down

0 comments on commit d0d4c4b

Please sign in to comment.