From 884ab0b67bc19f04e755795422b8a27f68999122 Mon Sep 17 00:00:00 2001 From: Carlos Valente <34649812+cpvalente@users.noreply.github.com> Date: Fri, 17 Nov 2023 16:18:02 +0100 Subject: [PATCH] refactor: rundown (#597) * refactor: add revision number to rundown * chore: migrate query --- apps/client/package.json | 6 +- apps/client/src/common/api/apiConstants.ts | 3 +- apps/client/src/common/api/apiUtils.ts | 16 +-- apps/client/src/common/api/eventsApi.ts | 12 +- apps/client/src/common/context/AppContext.tsx | 2 +- .../src/common/hooks-query/useOscSettings.ts | 8 +- .../src/common/hooks-query/useRundown.ts | 26 ++-- .../client/src/common/hooks/useEventAction.ts | 119 +++++++++++------- apps/client/src/common/queryClient.ts | 2 +- .../modals/quick-start/QuickStart.tsx | 6 +- .../modals/settings-modal/AliasesForm.tsx | 2 +- .../settings-modal/CuesheetSettingsForm.tsx | 2 +- .../settings-modal/ViewSettingsForm.tsx | 2 +- .../modals/upload-modal/UploadModal.tsx | 6 +- .../client/src/features/operator/Operator.tsx | 2 +- .../src/features/rundown/RundownEntry.tsx | 7 +- .../src/controllers/rundownController.ts | 14 ++- apps/server/src/routes/rundownRouter.ts | 4 + .../rundown-service/delayedRundown.utils.ts | 39 +++++- .../BackendResponse.type.ts | 6 + packages/types/src/index.ts | 1 + pnpm-lock.yaml | 85 +++++-------- 22 files changed, 225 insertions(+), 145 deletions(-) create mode 100644 packages/types/src/api/rundown-controller/BackendResponse.type.ts diff --git a/apps/client/package.json b/apps/client/package.json index 6238b4a5a..653c5ee96 100644 --- a/apps/client/package.json +++ b/apps/client/package.json @@ -12,8 +12,8 @@ "@react-icons/all-files": "^4.1.0", "@sentry/react": "^7.46.0", "@sentry/tracing": "^7.46.0", - "@tanstack/react-query": "^4.28.0", - "@tanstack/react-query-devtools": "^4.29.0", + "@tanstack/react-query": "^5.8.4", + "@tanstack/react-query-devtools": "^5.8.4", "@tanstack/react-table": "^8.9.2", "autosize": "^6.0.1", "axios": "^1.2.0", @@ -59,7 +59,7 @@ }, "devDependencies": { "@sentry/vite-plugin": "^0.4.0", - "@tanstack/eslint-plugin-query": "^4.26.2", + "@tanstack/eslint-plugin-query": "^5.8.4", "@testing-library/jest-dom": "^5.16.5", "@testing-library/react": "^13.1.1", "@testing-library/user-event": "^14.1.1", diff --git a/apps/client/src/common/api/apiConstants.ts b/apps/client/src/common/api/apiConstants.ts index f3a19e46f..9959d7275 100644 --- a/apps/client/src/common/api/apiConstants.ts +++ b/apps/client/src/common/api/apiConstants.ts @@ -2,8 +2,7 @@ export const PROJECT_DATA = ['project']; export const ALIASES = ['aliases']; export const USERFIELDS = ['userFields']; -export const RUNDOWN_TABLE_KEY = 'rundown'; -export const RUNDOWN_TABLE = [RUNDOWN_TABLE_KEY]; +export const RUNDOWN = ['rundown']; export const APP_INFO = ['appinfo']; export const OSC_SETTINGS = ['oscSettings']; export const APP_SETTINGS = ['appSettings']; diff --git a/apps/client/src/common/api/apiUtils.ts b/apps/client/src/common/api/apiUtils.ts index 4388980df..29f919212 100644 --- a/apps/client/src/common/api/apiUtils.ts +++ b/apps/client/src/common/api/apiUtils.ts @@ -42,12 +42,12 @@ export function logAxiosError(prepend: string, error: unknown) { * Utility function invalidates react-query caches */ export async function invalidateAllCaches() { - await ontimeQueryClient.invalidateQueries(['project']); - await ontimeQueryClient.invalidateQueries(['aliases']); - await ontimeQueryClient.invalidateQueries(['userFields']); - await ontimeQueryClient.invalidateQueries(['rundown']); - await ontimeQueryClient.invalidateQueries(['appinfo']); - await ontimeQueryClient.invalidateQueries(['oscSettings']); - await ontimeQueryClient.invalidateQueries(['appSettings']); - await ontimeQueryClient.invalidateQueries(['viewSettings']); + await ontimeQueryClient.invalidateQueries({ queryKey: ['project'] }); + await ontimeQueryClient.invalidateQueries({ queryKey: ['aliases'] }); + await ontimeQueryClient.invalidateQueries({ queryKey: ['userFields'] }); + await ontimeQueryClient.invalidateQueries({ queryKey: ['rundown'] }); + await ontimeQueryClient.invalidateQueries({ queryKey: ['appinfo'] }); + await ontimeQueryClient.invalidateQueries({ queryKey: ['oscSettings'] }); + await ontimeQueryClient.invalidateQueries({ queryKey: ['appSettings'] }); + await ontimeQueryClient.invalidateQueries({ queryKey: ['viewSettings'] }); } diff --git a/apps/client/src/common/api/eventsApi.ts b/apps/client/src/common/api/eventsApi.ts index bb574e340..8196853e2 100644 --- a/apps/client/src/common/api/eventsApi.ts +++ b/apps/client/src/common/api/eventsApi.ts @@ -1,5 +1,5 @@ import axios from 'axios'; -import { OntimeRundown, OntimeRundownEntry } from 'ontime-types'; +import { GetRundownCached, OntimeRundown, OntimeRundownEntry } from 'ontime-types'; import { rundownURL } from './apiConstants'; @@ -7,6 +7,16 @@ import { rundownURL } from './apiConstants'; * @description HTTP request to fetch all events * @return {Promise} */ +export async function fetchCachedRundown(): Promise { + const res = await axios.get(`${rundownURL}/cached`); + return res.data; +} + +/** + * @deprecated use fetchCachedRundown instead + * @description HTTP request to fetch all events + * @return {Promise} + */ export async function fetchRundown(): Promise { const res = await axios.get(rundownURL); return res.data; diff --git a/apps/client/src/common/context/AppContext.tsx b/apps/client/src/common/context/AppContext.tsx index 373a86dfb..8a9e082c6 100644 --- a/apps/client/src/common/context/AppContext.tsx +++ b/apps/client/src/common/context/AppContext.tsx @@ -25,7 +25,7 @@ export const AppContextProvider = ({ children }: PropsWithChildren) => { const [operatorAuth, setOperatorAuth] = useState(true); useEffect(() => { - if (status === 'loading') return; + if (status === 'pending') return; if (!data) return; const previousEditor = sessionStorage.getItem(storageKeys.editor); diff --git a/apps/client/src/common/hooks-query/useOscSettings.ts b/apps/client/src/common/hooks-query/useOscSettings.ts index 5b55556e2..67becece2 100644 --- a/apps/client/src/common/hooks-query/useOscSettings.ts +++ b/apps/client/src/common/hooks-query/useOscSettings.ts @@ -24,20 +24,20 @@ export default function useOscSettings() { } export function useOscSettingsMutation() { - const { isLoading, mutateAsync } = useMutation({ + const { isPending, mutateAsync } = useMutation({ mutationFn: postOSC, onError: (error) => logAxiosError('Error saving OSC settings', error), onSuccess: (res) => ontimeQueryClient.setQueryData(OSC_SETTINGS, res.data), onSettled: () => ontimeQueryClient.invalidateQueries({ queryKey: OSC_SETTINGS }), }); - return { isLoading, mutateAsync }; + return { isPending, mutateAsync }; } export function usePostOscSubscriptions() { - const { isLoading, mutateAsync } = useMutation({ + const { isPending, mutateAsync } = useMutation({ mutationFn: postOscSubscriptions, onError: (error) => logAxiosError('Error saving OSC settings', error), onSettled: () => ontimeQueryClient.invalidateQueries({ queryKey: OSC_SETTINGS }), }); - return { isLoading, mutateAsync }; + return { isPending, mutateAsync }; } diff --git a/apps/client/src/common/hooks-query/useRundown.ts b/apps/client/src/common/hooks-query/useRundown.ts index fbdf74a05..cd3a02253 100644 --- a/apps/client/src/common/hooks-query/useRundown.ts +++ b/apps/client/src/common/hooks-query/useRundown.ts @@ -1,19 +1,29 @@ import { useQuery } from '@tanstack/react-query'; +import { GetRundownCached } from 'ontime-types'; import { queryRefetchInterval } from '../../ontimeConfig'; -import { RUNDOWN_TABLE } from '../api/apiConstants'; -import { fetchRundown } from '../api/eventsApi'; +import { RUNDOWN } from '../api/apiConstants'; +import { fetchCachedRundown } from '../api/eventsApi'; +const cachedRundownPlaceholder = { rundown: [], revision: -1 }; + +// TODO: can we leverage structural sharing to see if data has changed? export default function useRundown() { - const { data, status, isError, refetch } = useQuery({ - queryKey: RUNDOWN_TABLE, - queryFn: fetchRundown, - placeholderData: [], + return useQuery({ + queryKey: RUNDOWN, + queryFn: fetchCachedRundown, + placeholderData: cachedRundownPlaceholder, retry: 5, + select: (data) => data.rundown, retryDelay: (attempt) => attempt * 2500, refetchInterval: queryRefetchInterval, networkMode: 'always', + // structuralSharing: (oldData: GetRundownCached | undefined, newData: GetRundownCached) => { + // if (oldData === undefined) { + // cachedRundownPlaceholder; + // } + // const hasDataChanged = oldData?.revision === newData.revision; + // return hasDataChanged ? oldData : newData; + // }, }); - - return { data, status, isError, refetch }; } diff --git a/apps/client/src/common/hooks/useEventAction.ts b/apps/client/src/common/hooks/useEventAction.ts index 7bd9ff7c4..cc54112a7 100644 --- a/apps/client/src/common/hooks/useEventAction.ts +++ b/apps/client/src/common/hooks/useEventAction.ts @@ -1,9 +1,9 @@ import { useCallback } from 'react'; import { useMutation, useQueryClient } from '@tanstack/react-query'; -import { isOntimeEvent, OntimeRundown, OntimeRundownEntry } from 'ontime-types'; +import { GetRundownCached, isOntimeEvent, OntimeRundownEntry } from 'ontime-types'; import { getCueCandidate, swapOntimeEvents } from 'ontime-utils'; -import { RUNDOWN_TABLE, RUNDOWN_TABLE_KEY } from '../api/apiConstants'; +import { RUNDOWN } from '../api/apiConstants'; import { logAxiosError } from '../api/apiUtils'; import { ReorderEntry, @@ -36,7 +36,7 @@ export const useEventAction = () => { // Fetch anyway, just to be sure mutationFn: requestPostEvent, onSettled: () => { - queryClient.invalidateQueries(RUNDOWN_TABLE); + queryClient.invalidateQueries({ queryKey: RUNDOWN }); }, networkMode: 'always', }); @@ -67,8 +67,10 @@ export const useEventAction = () => { after: options?.after, }; + const rundown = queryClient.getQueryData(RUNDOWN)?.rundown ?? []; + if (newEvent?.cue === undefined) { - newEvent.cue = getCueCandidate(queryClient.getQueryData(RUNDOWN_TABLE) || [], options?.after); + newEvent.cue = getCueCandidate(rundown, options?.after); } // hard coding duration value to be as expected for now @@ -78,7 +80,6 @@ export const useEventAction = () => { } if (applicationOptions.startTimeIsLastEnd && applicationOptions?.lastEventId) { - const rundown = queryClient.getQueryData(RUNDOWN_TABLE) as OntimeRundown; const previousEvent = rundown.find((event) => event.id === applicationOptions.lastEventId); if (previousEvent !== undefined && previousEvent.type === 'event') { newEvent.timeStart = previousEvent.timeEnd; @@ -115,25 +116,35 @@ export const useEventAction = () => { // we optimistically update here onMutate: async (newEvent) => { // cancel ongoing queries - await queryClient.cancelQueries([RUNDOWN_TABLE_KEY, newEvent.id]); + await queryClient.cancelQueries({ queryKey: RUNDOWN }); // Snapshot the previous value - const previousEvent = queryClient.getQueryData([RUNDOWN_TABLE_KEY, newEvent.id]); - // optimistically update object - queryClient.setQueryData([RUNDOWN_TABLE_KEY, newEvent.id], newEvent); + const previousData = queryClient.getQueryData(RUNDOWN); + + if (previousData) { + // optimistically update object + const optimisticRundown = [...previousData.rundown]; + const index = optimisticRundown.findIndex((event) => event.id === newEvent.id); + if (index > -1) { + // @ts-expect-error -- we expect the event types to match + optimisticRundown[index] = { ...optimisticRundown[index], ...newEvent }; + + queryClient.setQueryData(RUNDOWN, { rundown: optimisticRundown, revision: -1 }); + } + } // Return a context with the previous and new events - return { previousEvent, newEvent }; + return { previousData, newEvent }; }, // Mutation fails, rollback undoes optimist update onError: (_error, _newEvent, context) => { - queryClient.setQueryData([RUNDOWN_TABLE_KEY, context?.newEvent.id], context?.previousEvent); + queryClient.setQueryData(RUNDOWN, context?.previousData); }, // Mutation finished, failed or successful // Fetch anyway, just to be sure onSettled: async () => { - await queryClient.invalidateQueries([RUNDOWN_TABLE_KEY]); + await queryClient.invalidateQueries({ queryKey: RUNDOWN }); }, networkMode: 'always', }); @@ -161,28 +172,37 @@ export const useEventAction = () => { // we optimistically update here onMutate: async (eventId) => { // cancel ongoing queries - await queryClient.cancelQueries([RUNDOWN_TABLE_KEY, eventId]); + await queryClient.cancelQueries({ queryKey: RUNDOWN }); // Snapshot the previous value - const previousEvents = queryClient.getQueryData(RUNDOWN_TABLE); + const previousData = queryClient.getQueryData(RUNDOWN); - const filtered = [...(previousEvents as OntimeRundown)].filter((e) => e.id !== eventId); + if (previousData) { + // optimistically update object + const optimisticRundown = [...previousData.rundown]; + const index = optimisticRundown.findIndex((event) => event.id === eventId); + if (index > -1) { + optimisticRundown.splice(index, 1); - // optimistically update object - queryClient.setQueryData(RUNDOWN_TABLE, filtered); + queryClient.setQueryData(RUNDOWN, { + rundown: optimisticRundown, + revision: -1, + }); + } + } // Return a context with the previous and new events - return { previousEvents }; + return { previousData }; }, // Mutation fails, rollback undoes optimist update onError: (_error, _eventId, context) => { - queryClient.setQueryData(RUNDOWN_TABLE, context?.previousEvents); + queryClient.setQueryData(RUNDOWN, context?.previousData); }, // Mutation finished, failed or successful // Fetch anyway, just to be sure onSettled: () => { - queryClient.invalidateQueries(RUNDOWN_TABLE); + queryClient.invalidateQueries({ queryKey: RUNDOWN }); }, networkMode: 'always', }); @@ -210,26 +230,26 @@ export const useEventAction = () => { // we optimistically update here onMutate: async () => { // cancel ongoing queries - await queryClient.cancelQueries(RUNDOWN_TABLE, { exact: true }); + await queryClient.cancelQueries({ queryKey: RUNDOWN }); // Snapshot the previous value - const previousEvents = queryClient.getQueryData(RUNDOWN_TABLE); + const previousData = queryClient.getQueryData(RUNDOWN); // optimistically update object - queryClient.setQueryData(RUNDOWN_TABLE, []); + queryClient.setQueryData(RUNDOWN, { rundown: [], revision: -1 }); // Return a context with the previous and new events - return { previousEvents }; + return { previousData }; }, // Mutation fails, rollback undos optimist update onError: (_error, _eventId, context) => { - queryClient.setQueryData(RUNDOWN_TABLE, context?.previousEvents); + queryClient.setQueryData(RUNDOWN, context?.previousData); }, // Mutation finished, failed or successful // Fetch anyway, just to be sure onSettled: () => { - queryClient.invalidateQueries(RUNDOWN_TABLE); + queryClient.invalidateQueries({ queryKey: RUNDOWN }); }, networkMode: 'always', }); @@ -253,7 +273,7 @@ export const useEventAction = () => { mutationFn: requestApplyDelay, // Mutation finished, failed or successful onSettled: () => { - queryClient.invalidateQueries(RUNDOWN_TABLE); + queryClient.invalidateQueries({ queryKey: RUNDOWN }); }, networkMode: 'always', }); @@ -281,30 +301,32 @@ export const useEventAction = () => { // we optimistically update here onMutate: async (data) => { // cancel ongoing queries - await queryClient.cancelQueries(RUNDOWN_TABLE, { exact: true }); + await queryClient.cancelQueries({ queryKey: RUNDOWN }); // Snapshot the previous value - const previousEvents = queryClient.getQueryData(RUNDOWN_TABLE); + const previousData = queryClient.getQueryData(RUNDOWN); - const e = [...(previousEvents as OntimeRundown)]; - const [reorderedItem] = e.splice(data.from, 1); - e.splice(data.to, 0, reorderedItem); + if (previousData) { + // optimistically update object + const optimisticRundown = [...previousData.rundown]; + const [reorderedItem] = optimisticRundown.splice(data.from, 1); + optimisticRundown.splice(data.to, 0, reorderedItem); - // optimistically update object - queryClient.setQueryData(RUNDOWN_TABLE, e); + queryClient.setQueryData(RUNDOWN, { rundown: optimisticRundown, revision: -1 }); + } // Return a context with the previous and new events - return { previousEvents }; + return { previousData }; }, // Mutation fails, rollback undoes optimist update onError: (_error, _eventId, context) => { - queryClient.setQueryData(RUNDOWN_TABLE, context?.previousEvents); + queryClient.setQueryData(RUNDOWN, context?.previousData); }, // Mutation finished, failed or successful // Fetch anyway, just to be sure onSettled: () => { - queryClient.invalidateQueries(RUNDOWN_TABLE); + queryClient.invalidateQueries({ queryKey: RUNDOWN }); }, networkMode: 'always', }); @@ -337,31 +359,32 @@ export const useEventAction = () => { // we optimistically update here onMutate: async ({ from, to }) => { // cancel ongoing queries - await queryClient.cancelQueries(RUNDOWN_TABLE, { exact: true }); + await queryClient.cancelQueries({ queryKey: RUNDOWN }); // Snapshot the previous value - const rundown = queryClient.getQueryData(RUNDOWN_TABLE) as OntimeRundown; + const previousData = queryClient.getQueryData(RUNDOWN); + if (previousData) { + // optimistically update object + const fromEventIndex = previousData.rundown.findIndex((event) => event.id === from); + const toEventIndex = previousData.rundown.findIndex((event) => event.id === to); - const fromEventIndex = rundown.findIndex((event) => event.id === from); - const toEventIndex = rundown.findIndex((event) => event.id === to); + const optimisticRundown = swapOntimeEvents(previousData.rundown, fromEventIndex, toEventIndex); - const previousEvents = swapOntimeEvents(rundown, fromEventIndex, toEventIndex); - - // optimistically update object - queryClient.setQueryData(RUNDOWN_TABLE, previousEvents); + queryClient.setQueryData(RUNDOWN, { rundown: optimisticRundown, revision: -1 }); + } // Return a context with the previous events - return { previousEvents }; + return { previousData }; }, // Mutation fails, rollback undoes optimist update onError: (_error, _eventId, context) => { - queryClient.setQueryData(RUNDOWN_TABLE, context?.previousEvents); + queryClient.setQueryData(RUNDOWN, context?.previousData); }, // Mutation finished, failed or successful // Fetch anyway, just to be sure onSettled: () => { - queryClient.invalidateQueries(RUNDOWN_TABLE); + queryClient.invalidateQueries({ queryKey: RUNDOWN }); }, networkMode: 'always', }); diff --git a/apps/client/src/common/queryClient.ts b/apps/client/src/common/queryClient.ts index 368c6582b..7da808d11 100644 --- a/apps/client/src/common/queryClient.ts +++ b/apps/client/src/common/queryClient.ts @@ -3,7 +3,7 @@ import { QueryClient } from '@tanstack/react-query'; export const ontimeQueryClient = new QueryClient({ defaultOptions: { queries: { - cacheTime: 1000 * 60 * 10, // 10 min + gcTime: 1000 * 60 * 10, // 10 min }, }, }); diff --git a/apps/client/src/features/modals/quick-start/QuickStart.tsx b/apps/client/src/features/modals/quick-start/QuickStart.tsx index 5fbc675c6..8d8319b1d 100644 --- a/apps/client/src/features/modals/quick-start/QuickStart.tsx +++ b/apps/client/src/features/modals/quick-start/QuickStart.tsx @@ -18,7 +18,7 @@ import { } from '@chakra-ui/react'; import type { ProjectData } from 'ontime-types'; -import { PROJECT_DATA, RUNDOWN_TABLE } from '../../../common/api/apiConstants'; +import { PROJECT_DATA, RUNDOWN } from '../../../common/api/apiConstants'; import { postNew } from '../../../common/api/ontimeApi'; import useProjectData from '../../../common/hooks-query/useProjectData'; import { projectDataPlaceholder } from '../../../common/models/ProjectData'; @@ -52,8 +52,8 @@ export default function QuickStart({ onClose, isOpen }: QuickStartProps) { const onSubmit = async (data: Partial) => { try { await postNew(data); - await ontimeQueryClient.invalidateQueries(PROJECT_DATA); - await ontimeQueryClient.invalidateQueries(RUNDOWN_TABLE); + await ontimeQueryClient.invalidateQueries({ queryKey: PROJECT_DATA }); + await ontimeQueryClient.invalidateQueries({ queryKey: RUNDOWN }); onClose(); } catch (_) { diff --git a/apps/client/src/features/modals/settings-modal/AliasesForm.tsx b/apps/client/src/features/modals/settings-modal/AliasesForm.tsx index 49dbfdf7f..b9a155cf2 100644 --- a/apps/client/src/features/modals/settings-modal/AliasesForm.tsx +++ b/apps/client/src/features/modals/settings-modal/AliasesForm.tsx @@ -78,7 +78,7 @@ export default function AliasesForm() { }); }; - const disableInputs = status === 'loading'; + const disableInputs = status === 'pending'; const hasTooManyOptions = fields.length >= 20; if (isFetching) { diff --git a/apps/client/src/features/modals/settings-modal/CuesheetSettingsForm.tsx b/apps/client/src/features/modals/settings-modal/CuesheetSettingsForm.tsx index 9d997b0cd..42c9f9634 100644 --- a/apps/client/src/features/modals/settings-modal/CuesheetSettingsForm.tsx +++ b/apps/client/src/features/modals/settings-modal/CuesheetSettingsForm.tsx @@ -51,7 +51,7 @@ export default function CuesheetSettingsForm() { reset(data); }; - const disableInputs = status === 'loading'; + const disableInputs = status === 'pending'; if (isFetching) { return ; diff --git a/apps/client/src/features/modals/settings-modal/ViewSettingsForm.tsx b/apps/client/src/features/modals/settings-modal/ViewSettingsForm.tsx index ed1008ad0..bdce1ce4e 100644 --- a/apps/client/src/features/modals/settings-modal/ViewSettingsForm.tsx +++ b/apps/client/src/features/modals/settings-modal/ViewSettingsForm.tsx @@ -79,7 +79,7 @@ export default function ViewSettingsForm() { return null; } - const disableInputs = status === 'loading'; + const disableInputs = status === 'pending'; if (isFetching || isFetchingInfo) { return ; diff --git a/apps/client/src/features/modals/upload-modal/UploadModal.tsx b/apps/client/src/features/modals/upload-modal/UploadModal.tsx index b8ee198d6..e2821340c 100644 --- a/apps/client/src/features/modals/upload-modal/UploadModal.tsx +++ b/apps/client/src/features/modals/upload-modal/UploadModal.tsx @@ -13,7 +13,7 @@ import { useQueryClient } from '@tanstack/react-query'; import { OntimeRundown, ProjectData, UserFields } from 'ontime-types'; import { defaultExcelImportMap, ExcelImportMap } from 'ontime-utils'; -import { PROJECT_DATA, RUNDOWN_TABLE, USERFIELDS } from '../../../common/api/apiConstants'; +import { PROJECT_DATA, RUNDOWN, USERFIELDS } from '../../../common/api/apiConstants'; import { invalidateAllCaches, maybeAxiosError } from '../../../common/api/apiUtils'; import { patchData, @@ -155,11 +155,11 @@ export default function UploadModal({ onClose, isOpen }: UploadModalProps) { setSubmitting(true); try { await patchData({ rundown, userFields, project }); - queryClient.setQueryData(RUNDOWN_TABLE, rundown); + queryClient.setQueryData(RUNDOWN, { rundown, revision: -1 }); queryClient.setQueryData(USERFIELDS, userFields); queryClient.setQueryData(PROJECT_DATA, project); await queryClient.invalidateQueries({ - queryKey: [...RUNDOWN_TABLE, ...USERFIELDS, ...PROJECT_DATA], + queryKey: [...RUNDOWN, ...USERFIELDS, ...PROJECT_DATA], }); doClose = true; } catch (error) { diff --git a/apps/client/src/features/operator/Operator.tsx b/apps/client/src/features/operator/Operator.tsx index b6dc72c5c..b43b9ed9a 100644 --- a/apps/client/src/features/operator/Operator.tsx +++ b/apps/client/src/features/operator/Operator.tsx @@ -79,7 +79,7 @@ export default function Operator() { const debouncedHandleScroll = debounce(handleUserScroll, 1000); const missingData = !data || !userFields || !projectData; - const isLoading = status === 'loading' || userFieldsStatus === 'loading' || projectDataStatus === 'loading'; + const isLoading = status === 'pending' || userFieldsStatus === 'pending' || projectDataStatus === 'pending'; if (missingData || isLoading) { return ; diff --git a/apps/client/src/features/rundown/RundownEntry.tsx b/apps/client/src/features/rundown/RundownEntry.tsx index 5041bca8b..41d917404 100644 --- a/apps/client/src/features/rundown/RundownEntry.tsx +++ b/apps/client/src/features/rundown/RundownEntry.tsx @@ -1,8 +1,8 @@ import { useCallback } from 'react'; -import { OntimeEvent, OntimeRundownEntry, Playback, SupportedEvent } from 'ontime-types'; +import { GetRundownCached, OntimeEvent, OntimeRundownEntry, Playback, SupportedEvent } from 'ontime-types'; import { calculateDuration, getCueCandidate } from 'ontime-utils'; -import { RUNDOWN_TABLE } from '../../common/api/apiConstants'; +import { RUNDOWN } from '../../common/api/apiConstants'; import { useEventAction } from '../../common/hooks/useEventAction'; import { ontimeQueryClient } from '../../common/queryClient'; import { useAppMode } from '../../common/stores/appModeStore'; @@ -100,7 +100,8 @@ export default function RundownEntry(props: RundownEntryProps) { } case 'clone': { const newEvent = cloneEvent(data as OntimeEvent, data.id); - newEvent.cue = getCueCandidate(ontimeQueryClient.getQueryData(RUNDOWN_TABLE) || [], data.id); + const rundown = ontimeQueryClient.getQueryData(RUNDOWN)?.rundown ?? [] + newEvent.cue = getCueCandidate(rundown, data.id); addEvent(newEvent); break; } diff --git a/apps/server/src/controllers/rundownController.ts b/apps/server/src/controllers/rundownController.ts index e0e55e357..b5d1bba5b 100644 --- a/apps/server/src/controllers/rundownController.ts +++ b/apps/server/src/controllers/rundownController.ts @@ -1,3 +1,7 @@ +import { GetRundownCached } from 'ontime-types'; + +import { Request, Response, RequestHandler } from 'express'; + import { failEmptyObjects } from '../utils/routerUtils.js'; import { addEvent, @@ -8,8 +12,7 @@ import { reorderEvent, swapEvents, } from '../services/rundown-service/RundownService.js'; -import { getDelayedRundown } from '../services/rundown-service/delayedRundown.utils.js'; -import { RequestHandler } from 'express'; +import { getDelayedRundown, getRundownCache } from '../services/rundown-service/delayedRundown.utils.js'; // Create controller for GET request to '/events' // Returns - @@ -18,6 +21,13 @@ export const rundownGetAll: RequestHandler = async (_req, res) => { res.json(delayedRundown); }; +// Create controller for GET request to '/events/cached' +// Returns - +export const rundownGetCached: RequestHandler = async (_req: Request, res: Response) => { + const cachedRundown = getRundownCache(); + res.json(cachedRundown); +}; + // Create controller for POST request to '/events/' // Returns - export const rundownPost: RequestHandler = async (req, res) => { diff --git a/apps/server/src/routes/rundownRouter.ts b/apps/server/src/routes/rundownRouter.ts index 63de25641..86b134b88 100644 --- a/apps/server/src/routes/rundownRouter.ts +++ b/apps/server/src/routes/rundownRouter.ts @@ -4,6 +4,7 @@ import { rundownApplyDelay, rundownDelete, rundownGetAll, + rundownGetCached, rundownPost, rundownPut, rundownReorder, @@ -19,6 +20,9 @@ import { export const router = express.Router(); +// create route between controller and '/events/cached' endpoint +router.get('/cached', rundownGetCached); + // create route between controller and '/events/' endpoint router.get('/', rundownGetAll); diff --git a/apps/server/src/services/rundown-service/delayedRundown.utils.ts b/apps/server/src/services/rundown-service/delayedRundown.utils.ts index 9b6387471..64ef7f877 100644 --- a/apps/server/src/services/rundown-service/delayedRundown.utils.ts +++ b/apps/server/src/services/rundown-service/delayedRundown.utils.ts @@ -1,4 +1,5 @@ import { + GetRundownCached, isOntimeBlock, isOntimeDelay, isOntimeEvent, @@ -16,6 +17,11 @@ import { isProduction } from '../../setup.js'; import { deleteAtIndex, insertAtIndex, reorderArray } from '../../utils/arrayUtils.js'; import { _applyDelay } from '../delayUtils.js'; +/** + * Keep incremental revision number of rundown for runtime + */ +let rundownRevision = 0; + /** * Key of rundown in cache */ @@ -38,7 +44,25 @@ export function invalidateFromError(errorMessage = 'Found mismatch between store * Returns rundown with calculated delays * Ensures request goes through the caching layer */ -export function getDelayedRundown(): OntimeRundown { +export function getRundownCache(): GetRundownCached { + function calculateRundown() { + const rundown = DataProvider.getRundown(); + return calculateRuntimeDelays(rundown); + } + + const cached = getCached(delayedRundownCacheKey, calculateRundown); + + return { + rundown: cached, + revision: rundownRevision, + }; +} + +/** + * Returns rundown with calculated delays + * Ensures request goes through the caching layer + */ +export function getDelayedRundown() { function calculateRundown() { const rundown = DataProvider.getRundown(); return calculateRuntimeDelays(rundown); @@ -72,6 +96,8 @@ export async function cachedAdd(eventIndex: number, event: OntimeEvent | OntimeD runtimeCacheStore.setCached(delayedRundownCacheKey, newDelayedRundown); // we need to delay updating this to ensure add operation happens on same dataset await DataProvider.setRundown(newRundown); + + rundownRevision++; } /** @@ -113,6 +139,8 @@ export async function cachedEdit( // we need to delay updating this to ensure edit operation happens on same dataset await DataProvider.setRundown(updatedRundown); + rundownRevision++; + return newEvent; } @@ -147,6 +175,8 @@ export async function cachedDelete(eventId: string) { } // we need to delay updating this to ensure edit operation happens on same dataset await DataProvider.setRundown(updatedRundown); + + rundownRevision++; } /** @@ -178,12 +208,15 @@ export async function cachedReorder(eventId: string, from: number, to: number) { // we need to delay updating this to ensure edit operation happens on same dataset await DataProvider.setRundown(updatedRundown); + rundownRevision++; + return reorderedEvent; } export async function cachedClear() { await DataProvider.clearRundown(); runtimeCacheStore.setCached(delayedRundownCacheKey, []); + rundownRevision++; } /** @@ -211,6 +244,8 @@ export async function cachedSwap(fromEventId: string, toEventId: string) { } await DataProvider.setRundown(rundownToUpdate); + + rundownRevision++; } export async function cachedApplyDelay(eventId: string) { @@ -224,6 +259,8 @@ export async function cachedApplyDelay(eventId: string) { // update runtimeCacheStore.setCached(delayedRundownCacheKey, cachedRundown); await DataProvider.setRundown(persistedRundown); + + rundownRevision++; } /** diff --git a/packages/types/src/api/rundown-controller/BackendResponse.type.ts b/packages/types/src/api/rundown-controller/BackendResponse.type.ts new file mode 100644 index 000000000..7b5763fb6 --- /dev/null +++ b/packages/types/src/api/rundown-controller/BackendResponse.type.ts @@ -0,0 +1,6 @@ +import { OntimeRundown } from '../../definitions/core/Rundown.type.js'; + +export interface GetRundownCached { + rundown: OntimeRundown; + revision: number; +} diff --git a/packages/types/src/index.ts b/packages/types/src/index.ts index 20f74c16b..e2afa6fe6 100644 --- a/packages/types/src/index.ts +++ b/packages/types/src/index.ts @@ -35,6 +35,7 @@ export type { OSCSettings, OscSubscription, OscSubscriptionOptions } from './def // SERVER RESPONSES export type { NetworkInterface, GetInfo } from './api/ontime-controller/BackendResponse.type.js'; +export type { GetRundownCached } from './api/rundown-controller/BackendResponse.type.js'; // SERVER RUNTIME export { type Log, LogLevel, type LogMessage, LogOrigin } from './definitions/runtime/Logger.type.js'; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 93650ef4a..a0405d819 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -78,11 +78,11 @@ importers: specifier: ^7.46.0 version: 7.46.0 '@tanstack/react-query': - specifier: ^4.28.0 - version: 4.28.0(react-dom@18.2.0)(react@18.2.0) + specifier: ^5.8.4 + version: 5.8.4(react-dom@18.2.0)(react@18.2.0) '@tanstack/react-query-devtools': - specifier: ^4.29.0 - version: 4.29.0(@tanstack/react-query@4.28.0)(react-dom@18.2.0)(react@18.2.0) + specifier: ^5.8.4 + version: 5.8.4(@tanstack/react-query@5.8.4)(react-dom@18.2.0)(react@18.2.0) '@tanstack/react-table': specifier: ^8.9.2 version: 8.9.2(react-dom@18.2.0)(react@18.2.0) @@ -139,8 +139,8 @@ importers: specifier: ^0.4.0 version: 0.4.0 '@tanstack/eslint-plugin-query': - specifier: ^4.26.2 - version: 4.26.2 + specifier: ^5.8.4 + version: 5.8.4(eslint@8.53.0)(typescript@5.2.2) '@testing-library/jest-dom': specifier: ^5.16.5 version: 5.16.5 @@ -2704,41 +2704,44 @@ packages: defer-to-connect: 2.0.1 dev: true - /@tanstack/eslint-plugin-query@4.26.2: - resolution: {integrity: sha512-ugAvl6Is+bUMLt9BlAnXK6Wi7UnGV+4RwJ2W1ToFoucPvUb2Uf+ADU38JkHaNsI/TFgE3+kePhKh0zzDBhkw0Q==} + /@tanstack/eslint-plugin-query@5.8.4(eslint@8.53.0)(typescript@5.2.2): + resolution: {integrity: sha512-KVgcMc+Bn1qbwkxYVWQoiVSNEIN4IAiLj3cUH/SAHT8m8E59Y97o8ON1syp0Rcw094ItG8pEVZFyQuOaH6PDgQ==} + peerDependencies: + eslint: ^8.0.0 + dependencies: + '@typescript-eslint/utils': 5.62.0(eslint@8.53.0)(typescript@5.2.2) + eslint: 8.53.0 + transitivePeerDependencies: + - supports-color + - typescript dev: true - /@tanstack/match-sorter-utils@8.7.6: - resolution: {integrity: sha512-2AMpRiA6QivHOUiBpQAVxjiHAA68Ei23ZUMNaRJrN6omWiSFLoYrxGcT6BXtuzp0Jw4h6HZCmGGIM/gbwebO2A==} - engines: {node: '>=12'} - dependencies: - remove-accents: 0.4.2 + /@tanstack/query-core@5.8.3: + resolution: {integrity: sha512-SWFMFtcHfttLYif6pevnnMYnBvxKf3C+MHMH7bevyYfpXpTMsLB9O6nNGBdWSoPwnZRXFNyNeVZOw25Wmdasow==} dev: false - /@tanstack/query-core@4.27.0: - resolution: {integrity: sha512-sm+QncWaPmM73IPwFlmWSKPqjdTXZeFf/7aEmWh00z7yl2FjqophPt0dE1EHW9P1giMC5rMviv7OUbSDmWzXXA==} + /@tanstack/query-devtools@5.8.4: + resolution: {integrity: sha512-F1dRbITNt9tMUoM9WCH8WQ2c54116hv52m/PKK8ZiN/pO2wGVzTZtKuLanF8pFpwmNchjIixcMw/a57HY5ivcw==} dev: false - /@tanstack/react-query-devtools@4.29.0(@tanstack/react-query@4.28.0)(react-dom@18.2.0)(react@18.2.0): - resolution: {integrity: sha512-bzotqin4Wa/GlPgJ2dI7eggQcbMDLIOwEClHGrkyie76DbT8vEEmEV9Kbh6kriKVSqCLpa9ZrgG/f8/Bx1zIwA==} + /@tanstack/react-query-devtools@5.8.4(@tanstack/react-query@5.8.4)(react-dom@18.2.0)(react@18.2.0): + resolution: {integrity: sha512-mffs51FJqXU/5rwhbwv393DccL6et7uK2pRLwOcmMrWbPyW8vpxr9oidaghHX4cdVeP/7u5owW9yMpBhBAJfcQ==} peerDependencies: - '@tanstack/react-query': 4.28.0 - react: ^16.8.0 || ^17.0.0 || ^18.0.0 - react-dom: ^16.8.0 || ^17.0.0 || ^18.0.0 + '@tanstack/react-query': ^5.8.4 + react: ^18.0.0 + react-dom: ^18.0.0 dependencies: - '@tanstack/match-sorter-utils': 8.7.6 - '@tanstack/react-query': 4.28.0(react-dom@18.2.0)(react@18.2.0) + '@tanstack/query-devtools': 5.8.4 + '@tanstack/react-query': 5.8.4(react-dom@18.2.0)(react@18.2.0) react: 18.2.0 react-dom: 18.2.0(react@18.2.0) - superjson: 1.12.1 - use-sync-external-store: 1.2.0(react@18.2.0) dev: false - /@tanstack/react-query@4.28.0(react-dom@18.2.0)(react@18.2.0): - resolution: {integrity: sha512-8cGBV5300RHlvYdS4ea+G1JcZIt5CIuprXYFnsWggkmGoC0b5JaqG0fIX3qwDL9PTNkKvG76NGThIWbpXivMrQ==} + /@tanstack/react-query@5.8.4(react-dom@18.2.0)(react@18.2.0): + resolution: {integrity: sha512-CD+AkXzg8J72JrE6ocmuBEJfGzEzu/bzkD6sFXFDDB5yji9N20JofXZlN6n0+CaPJuIi+e4YLCbGsyPFKkfNQA==} peerDependencies: - react: ^16.8.0 || ^17.0.0 || ^18.0.0 - react-dom: ^16.8.0 || ^17.0.0 || ^18.0.0 + react: ^18.0.0 + react-dom: ^18.0.0 react-native: '*' peerDependenciesMeta: react-dom: @@ -2746,10 +2749,9 @@ packages: react-native: optional: true dependencies: - '@tanstack/query-core': 4.27.0 + '@tanstack/query-core': 5.8.3 react: 18.2.0 react-dom: 18.2.0(react@18.2.0) - use-sync-external-store: 1.2.0(react@18.2.0) dev: false /@tanstack/react-table@8.9.2(react-dom@18.2.0)(react@18.2.0): @@ -4243,13 +4245,6 @@ packages: engines: {node: '>= 0.6'} dev: false - /copy-anything@3.0.3: - resolution: {integrity: sha512-fpW2W/BqEzqPp29QS+MwwfisHCQZtiduTe/m8idFo0xbti9fIZ2WVhAsCv4ggFVH3AgCkVdpoOCtQC6gBrdhjw==} - engines: {node: '>=12.13'} - dependencies: - is-what: 4.1.8 - dev: false - /copy-to-clipboard@3.3.3: resolution: {integrity: sha512-2KV8NhB5JqC3ky0r9PMCAZKbUHSwtEo4CwCs0KXgruG43gX5PMqDEBbVU4OUzw2MuAWUfsuFmWvEKG5QRfSnJA==} dependencies: @@ -6131,11 +6126,6 @@ packages: get-intrinsic: 1.1.3 dev: true - /is-what@4.1.8: - resolution: {integrity: sha512-yq8gMao5upkPoGEU9LsB2P+K3Kt8Q3fQFCGyNCWOAnJAMzEXVV9drYb0TXr42TTliLLhKIBvulgAXgtLLnwzGA==} - engines: {node: '>=12.13'} - dev: false - /is-wsl@2.2.0: resolution: {integrity: sha512-fKzAra0rGJUUBwGBgNkHZuToZcn+TtXHpeCgmkMJMMYx1sQDYaCSyjJBSCa2nH1DGm7s3n1oBnohoVTBaN7Lww==} engines: {node: '>=8'} @@ -7488,10 +7478,6 @@ packages: functions-have-names: 1.2.3 dev: true - /remove-accents@0.4.2: - resolution: {integrity: sha512-7pXIJqJOq5tFgG1A2Zxti3Ht8jJF337m4sowbuHsW30ZnkQFnDzy9qBNhgzX8ZLW4+UBcXiiR7SwR6pokHsxiA==} - dev: false - /require-directory@2.1.1: resolution: {integrity: sha512-fGxEI7+wsG9xrvdjsrlmL22OMTTiHRwAMroiEeMgq8gzoLC/PQr7RsRDSTLUg/bZAZtF+TVIkHc6/4RIKrui+Q==} engines: {node: '>=0.10.0'} @@ -8001,13 +7987,6 @@ packages: - supports-color dev: true - /superjson@1.12.1: - resolution: {integrity: sha512-HMTj43zvwW5bD+JCZCvFf4DkZQCmiLTen4C+W1Xogj0SPOpnhxsriogM04QmBVGH5b3kcIIOr6FqQ/aoIDx7TQ==} - engines: {node: '>=10'} - dependencies: - copy-anything: 3.0.3 - dev: false - /supports-color@5.5.0: resolution: {integrity: sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow==} engines: {node: '>=4'}