Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(app): Remove notification event emitters #14504

Merged
merged 3 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 appShellListener(
hostname: string | null,
topic: NotifyTopic
): EventEmitter {
const eventEmitter = new EventEmitter()
topic: NotifyTopic,
callback: (data: NotifyResponseData) => void
): void {
remote.ipcRenderer.on(
'notify',
(_, shellHostname, shellTopic, shellMessage) => {
if (hostname === shellHostname && topic === shellTopic) {
eventEmitter.emit('data', shellMessage)
callback(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
17 changes: 3 additions & 14 deletions app/src/resources/useNotifyService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,13 @@
} 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,10 @@
hostname != null &&
staleTime !== Infinity
) {
const eventEmitter = appShellListener(hostname, topic)
eventEmitter.on('data', onDataListener)
appShellListener(hostname, topic, onDataEvent)

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

View check run for this annotation

Codecov / codecov/patch

app/src/resources/useNotifyService.ts#L49

Added line #L49 was not covered by tests
dispatch(notifySubscribeAction(hostname, topic))

return () => {
eventEmitter.off('data', onDataListener)
if (hostname != null) {
dispatch(notifyUnsubscribeAction(hostname, topic))
}
Expand All @@ -77,7 +66,7 @@

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
Loading