Skip to content

Commit

Permalink
fix(app): fix fallback polling not working on select notifications (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mjhuff authored Aug 29, 2024
1 parent 7459249 commit f401e94
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ describe('useNotifyDataReady', () => {
options: { ...MOCK_OPTIONS, forceHttpPolling: true },
} as any)
)
expect(result.current.shouldRefetch).toEqual(true)
expect(result.current.shouldRefetch).toEqual(false)
expect(result.current.isNotifyEnabled).toEqual(false)
expect(appShellListener).not.toHaveBeenCalled()
expect(mockDispatch).not.toHaveBeenCalled()
})
Expand All @@ -85,7 +86,8 @@ describe('useNotifyDataReady', () => {
options: { ...MOCK_OPTIONS, enabled: false },
} as any)
)
expect(result.current.shouldRefetch).toEqual(true)
expect(result.current.shouldRefetch).toEqual(false)
expect(result.current.isNotifyEnabled).toEqual(false)
expect(appShellListener).not.toHaveBeenCalled()
expect(mockDispatch).not.toHaveBeenCalled()
})
Expand All @@ -97,12 +99,13 @@ describe('useNotifyDataReady', () => {
options: { ...MOCK_OPTIONS, staleTime: Infinity },
} as any)
)
expect(result.current.shouldRefetch).toEqual(true)
expect(result.current.shouldRefetch).toEqual(false)
expect(result.current.isNotifyEnabled).toEqual(false)
expect(appShellListener).not.toHaveBeenCalled()
expect(mockDispatch).not.toHaveBeenCalled()
})

it('should set HTTP refetch to always if there is an error', () => {
it('should set isNotifyEnabled to false if there is an error', () => {
vi.mocked(useHost).mockReturnValue({ hostname: null } as any)
const errorSpy = vi.spyOn(console, 'error')
errorSpy.mockImplementation(() => {})
Expand All @@ -115,10 +118,11 @@ describe('useNotifyDataReady', () => {
} as any)
)

expect(result.current.shouldRefetch).toEqual(true)
expect(result.current.shouldRefetch).toEqual(false)
expect(result.current.isNotifyEnabled).toEqual(false)
})

it('should return set HTTP refetch to always and fire an analytics reporting event if the connection was refused', () => {
it('should return set isNotifyEnabled to false and fire an analytics reporting event if the connection was refused', () => {
vi.mocked(appShellListener).mockImplementation(function ({
callback,
}): any {
Expand All @@ -134,7 +138,7 @@ describe('useNotifyDataReady', () => {
)
expect(mockTrackEvent).toHaveBeenCalled()
rerender()
expect(result.current.shouldRefetch).toEqual(true)
expect(result.current.isNotifyEnabled).toEqual(false)
})

it('should trigger a single HTTP refetch if the refetch flag was returned', () => {
Expand All @@ -153,6 +157,7 @@ describe('useNotifyDataReady', () => {
)
rerender()
expect(result.current.shouldRefetch).toEqual(true)
expect(result.current.isNotifyEnabled).toEqual(true)
})

it('should trigger a single HTTP refetch if the unsubscribe flag was returned', () => {
Expand All @@ -170,6 +175,7 @@ describe('useNotifyDataReady', () => {
)
rerender()
expect(result.current.shouldRefetch).toEqual(true)
expect(result.current.isNotifyEnabled).toEqual(true)
})

it('should clean up the listener on dismount', () => {
Expand Down Expand Up @@ -210,7 +216,8 @@ describe('useNotifyDataReady', () => {
} as any)
)

expect(result.current.shouldRefetch).toEqual(true)
expect(result.current.shouldRefetch).toEqual(false)
expect(result.current.isNotifyEnabled).toEqual(false)
expect(appShellListener).not.toHaveBeenCalled()
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ export function useNotifyClientDataRecovery(
AxiosError
> = {}
): UseQueryResult<ClientDataResponse<ClientDataRecovery>, AxiosError> {
const { notifyOnSettled, shouldRefetch } = useNotifyDataReady({
const {
notifyOnSettled,
shouldRefetch,
isNotifyEnabled,
} = useNotifyDataReady({
topic: `robot-server/clientData/${KEYS.ERROR_RECOVERY}`,
options,
})
Expand All @@ -23,7 +27,8 @@ export function useNotifyClientDataRecovery(
KEYS.ERROR_RECOVERY,
{
...options,
enabled: options?.enabled !== false && shouldRefetch,
enabled:
options?.enabled !== false && (shouldRefetch || !isNotifyEnabled),
onSettled: notifyOnSettled,
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ import type { QueryOptionsWithPolling } from '../useNotifyDataReady'
export function useNotifyDeckConfigurationQuery(
options: QueryOptionsWithPolling<DeckConfiguration, unknown> = {}
): UseQueryResult<DeckConfiguration> {
const { notifyOnSettled, shouldRefetch } = useNotifyDataReady({
const {
notifyOnSettled,
shouldRefetch,
isNotifyEnabled,
} = useNotifyDataReady({
topic: 'robot-server/deck_configuration',
options,
})

const httpQueryResult = useDeckConfigurationQuery({
...options,
enabled: options?.enabled !== false && shouldRefetch,
enabled: options?.enabled !== false && (shouldRefetch || !isNotifyEnabled),
onSettled: notifyOnSettled,
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ import type { QueryOptionsWithPolling } from '../useNotifyDataReady'
export function useNotifyCurrentMaintenanceRun(
options: QueryOptionsWithPolling<MaintenanceRun, Error> = {}
): UseQueryResult<MaintenanceRun> | UseQueryResult<MaintenanceRun, Error> {
const { notifyOnSettled, shouldRefetch } = useNotifyDataReady({
const {
notifyOnSettled,
shouldRefetch,
isNotifyEnabled,
} = useNotifyDataReady({
topic: 'robot-server/maintenance_runs/current_run',
options,
})

const httpQueryResult = useCurrentMaintenanceRun({
...options,
enabled: options?.enabled !== false && shouldRefetch,
enabled: options?.enabled !== false && (shouldRefetch || !isNotifyEnabled),
onSettled: notifyOnSettled,
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@ export function useNotifyAllCommandsAsPreSerializedList(
params?: GetCommandsParams | null,
options: QueryOptionsWithPolling<CommandsData, AxiosError> = {}
): UseQueryResult<CommandsData, AxiosError> {
const { notifyOnSettled, shouldRefetch } = useNotifyDataReady({
const {
notifyOnSettled,
shouldRefetch,
isNotifyEnabled,
} = useNotifyDataReady({
topic: `robot-server/runs/pre_serialized_commands/${runId}`,
options,
})

const httpResponse = useAllCommandsAsPreSerializedList(runId, params, {
...options,
enabled: options?.enabled !== false && shouldRefetch,
enabled: options?.enabled !== false && (shouldRefetch || !isNotifyEnabled),
onSettled: notifyOnSettled,
})

Expand Down
8 changes: 6 additions & 2 deletions app/src/resources/runs/useNotifyAllCommandsQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ export function useNotifyAllCommandsQuery<TError = Error>(
// running to succeeded, that may change the useAllCommandsQuery response, but it
// will not necessarily change the command links. We might need an MQTT topic
// covering "any change in `GET /runs/{id}/commands`".
const { notifyOnSettled, shouldRefetch } = useNotifyDataReady({
const {
notifyOnSettled,
shouldRefetch,
isNotifyEnabled,
} = useNotifyDataReady({
topic: 'robot-server/runs/commands_links',
options,
})

const httpResponse = useAllCommandsQuery(runId, params, {
...options,
enabled: options?.enabled !== false && shouldRefetch,
enabled: options?.enabled !== false && (shouldRefetch || !isNotifyEnabled),
onSettled: notifyOnSettled,
})

Expand Down

0 comments on commit f401e94

Please sign in to comment.