refactor: refetch on change

This commit is contained in:
Carlos Valente
2024-09-01 12:50:01 +02:00
committed by Carlos Valente
parent 7b932ef0f7
commit 0abaf7724d
6 changed files with 31 additions and 9 deletions
@@ -1,7 +1,7 @@
import { useQuery } from '@tanstack/react-query'; import { useQuery } from '@tanstack/react-query';
import { CustomFields } from 'ontime-types'; import { CustomFields } from 'ontime-types';
import { queryRefetchInterval } from '../../ontimeConfig'; import { queryRefetchIntervalSlow } from '../../ontimeConfig';
import { CUSTOM_FIELDS } from '../api/constants'; import { CUSTOM_FIELDS } from '../api/constants';
import { getCustomFields } from '../api/customFields'; import { getCustomFields } from '../api/customFields';
@@ -14,7 +14,7 @@ export default function useCustomFields() {
placeholderData: (previousData, _previousQuery) => previousData, placeholderData: (previousData, _previousQuery) => previousData,
retry: 5, retry: 5,
retryDelay: (attempt) => attempt * 2500, retryDelay: (attempt) => attempt * 2500,
refetchInterval: queryRefetchInterval, refetchInterval: queryRefetchIntervalSlow,
networkMode: 'always', networkMode: 'always',
}); });
@@ -2,7 +2,7 @@ import { useEffect, useState } from 'react';
import { useQuery } from '@tanstack/react-query'; import { useQuery } from '@tanstack/react-query';
import { NormalisedRundown, OntimeRundown, RundownCached } from 'ontime-types'; import { NormalisedRundown, OntimeRundown, RundownCached } from 'ontime-types';
import { queryRefetchInterval } from '../../ontimeConfig'; import { queryRefetchIntervalSlow } from '../../ontimeConfig';
import { RUNDOWN } from '../api/constants'; import { RUNDOWN } from '../api/constants';
import { fetchNormalisedRundown } from '../api/rundown'; import { fetchNormalisedRundown } from '../api/rundown';
@@ -16,7 +16,7 @@ export default function useRundown() {
placeholderData: (previousData, _previousQuery) => previousData, placeholderData: (previousData, _previousQuery) => previousData,
retry: 5, retry: 5,
retryDelay: (attempt) => attempt * 2500, retryDelay: (attempt) => attempt * 2500,
refetchInterval: queryRefetchInterval, refetchInterval: queryRefetchIntervalSlow,
networkMode: 'always', networkMode: 'always',
}); });
return { data: data ?? cachedRundownPlaceholder, status, isError, refetch, isFetching }; return { data: data ?? cachedRundownPlaceholder, status, isError, refetch, isFetching };
+13 -2
View File
@@ -1,6 +1,6 @@
import { Log, RuntimeStore } from 'ontime-types'; import { Log, RundownCached, RuntimeStore } from 'ontime-types';
import { CLIENT_LIST, isProduction, RUNTIME, websocketUrl } from '../api/constants'; import { CLIENT_LIST, CUSTOM_FIELDS, isProduction, RUNDOWN, RUNTIME, websocketUrl } from '../api/constants';
import { ontimeQueryClient } from '../queryClient'; import { ontimeQueryClient } from '../queryClient';
import { import {
getClientId, getClientId,
@@ -176,6 +176,17 @@ export const connectSocket = () => {
updateDevTools({ auxtimer1: payload }); updateDevTools({ auxtimer1: payload });
break; break;
} }
case 'ontime-refetch': {
// the refetch message signals that the rundown has changed in the server side
const { revision } = payload;
const currentRevision = ontimeQueryClient.getQueryData<RundownCached>(RUNDOWN)?.revision ?? -1;
if (revision > currentRevision) {
ontimeQueryClient.invalidateQueries({ queryKey: RUNDOWN });
ontimeQueryClient.invalidateQueries({ queryKey: CUSTOM_FIELDS });
}
break;
}
} }
} catch (_) { } catch (_) {
// ignore unhandled // ignore unhandled
+2 -2
View File
@@ -2,5 +2,5 @@ export const tooltipDelaySlow = 1000;
export const tooltipDelayMid = 500; export const tooltipDelayMid = 500;
export const tooltipDelayFast = 300; export const tooltipDelayFast = 300;
export const queryRefetchInterval = 10000; export const queryRefetchInterval = 10000; // 10 seconds
export const queryRefetchIntervalSlow = 30000; export const queryRefetchIntervalSlow = 30000; // 30 seconds
@@ -247,7 +247,11 @@ function notifyChanges(options: { timer?: boolean | string[]; external?: boolean
if (options.external) { if (options.external) {
// advice socket subscribers of change // advice socket subscribers of change
sendRefetch(Array.isArray(options.timer) ? options.timer : undefined); const payload = {
changes: Array.isArray(options.timer) ? options.timer : undefined,
revision: cache.getMetadata().revision,
};
sendRefetch(payload);
} }
} }
@@ -215,6 +215,7 @@ export function getMetadata() {
lastEnd, lastEnd,
totalDelay, totalDelay,
totalDuration, totalDuration,
revision,
}; };
} }
@@ -243,6 +244,12 @@ export function mutateCache<T extends object>(mutation: MutatingFn<T>) {
const { newEvent, newRundown, didMutate } = mutation({ ...params, persistedRundown }); const { newEvent, newRundown, didMutate } = mutation({ ...params, persistedRundown });
// early return without calling side effects
if (!didMutate) {
isStale = false;
return { newEvent, newRundown, didMutate };
}
revision = revision + 1; revision = revision + 1;
persistedRundown = newRundown; persistedRundown = newRundown;