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-shell, app-shell-odd): Refactor app to use unsubscribe flags #14633

Closed
wants to merge 14 commits into from
Closed
Prev Previous commit
Next Next commit
update the browser layer
  • Loading branch information
mjhuff committed Mar 4, 2024
commit 10c464edcef7caeec51c8d6fc480a190a622b85a
22 changes: 1 addition & 21 deletions app/src/resources/__tests__/useNotifyService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import { useHost } from '@opentrons/react-api-client'
import { useNotifyService } from '../useNotifyService'
import { appShellListener } from '../../redux/shell/remote'
import { useTrackEvent } from '../../redux/analytics'
import {
notifySubscribeAction,
notifyUnsubscribeAction,
} from '../../redux/shell'
import { notifySubscribeAction } from '../../redux/shell'
import { useIsFlex } from '../../organisms/Devices/hooks/useIsFlex'

import type { HostConfig } from '@opentrons/api-client'
Expand Down Expand Up @@ -71,26 +68,9 @@ describe('useNotifyService', () => {
expect(mockDispatch).toHaveBeenCalledWith(
notifySubscribeAction(MOCK_HOST_CONFIG.hostname, MOCK_TOPIC)
)
expect(mockDispatch).not.toHaveBeenCalledWith(
notifyUnsubscribeAction(MOCK_HOST_CONFIG.hostname, MOCK_TOPIC)
)
expect(appShellListener).toHaveBeenCalled()
})

it('should trigger an unsubscribe action on dismount', () => {
const { unmount } = renderHook(() =>
useNotifyService({
topic: MOCK_TOPIC,
setRefetchUsingHTTP: mockHTTPRefetch,
options: MOCK_OPTIONS,
} as any)
)
unmount()
expect(mockDispatch).toHaveBeenCalledWith(
notifyUnsubscribeAction(MOCK_HOST_CONFIG.hostname, MOCK_TOPIC)
)
})

it('should not subscribe to notifications if forceHttpPolling is true', () => {
renderHook(() =>
useNotifyService({
Expand Down
10 changes: 3 additions & 7 deletions app/src/resources/useNotifyService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useDispatch } from 'react-redux'
import { useHost } from '@opentrons/react-api-client'

import { appShellListener } from '../redux/shell/remote'
import { notifySubscribeAction, notifyUnsubscribeAction } from '../redux/shell'
import { notifySubscribeAction } from '../redux/shell'
import {
useTrackEvent,
ANALYTICS_NOTIFICATION_PORT_BLOCK_ERROR,
Expand Down Expand Up @@ -58,12 +58,8 @@ export function useNotifyService<TData, TError = Error>({
appShellListener(hostname, topic, onDataEvent)
dispatch(notifySubscribeAction(hostname, topic))
hasUsedNotifyService.current = true
} else setRefetchUsingHTTP('always')

return () => {
if (hasUsedNotifyService.current && hostname != null) {
dispatch(notifyUnsubscribeAction(hostname, topic))
}
} else {
setRefetchUsingHTTP('always')
}
}, [topic, host, shouldUseNotifications])

Expand Down