From 4a520950330b36f57407c2d4a2bab1ff9540f578 Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Thu, 24 Oct 2024 08:17:58 -0700 Subject: [PATCH] Better template function fetching --- src-web/hooks/useTemplateFunctions.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src-web/hooks/useTemplateFunctions.ts b/src-web/hooks/useTemplateFunctions.ts index 1e7eb783..904efbc7 100644 --- a/src-web/hooks/useTemplateFunctions.ts +++ b/src-web/hooks/useTemplateFunctions.ts @@ -1,19 +1,24 @@ import { useQuery } from '@tanstack/react-query'; import type { GetTemplateFunctionsResponse } from '@yaakapp-internal/plugin'; +import { useState } from 'react'; import { invokeCmd } from '../lib/tauri'; import { usePluginsKey } from './usePlugins'; export function useTemplateFunctions() { const pluginsKey = usePluginsKey(); + const [numFns, setNumFns] = useState(0); const result = useQuery({ queryKey: ['template_functions', pluginsKey], - // NOTE: visibilitychange (refetchOnWindowFocus) does not work on Windows, so we'll rely on mount to - // refetch template functions for us when. This should handle the case where the plugin system isn't - // quite ready the first time this is invoked. + // Fetch periodically until functions are returned + // NOTE: visibilitychange (refetchOnWindowFocus) does not work on Windows, so we'll rely on this logic + // to refetch things until that's working again + refetchInterval: numFns > 0 ? Infinity : 500, refetchOnMount: true, queryFn: async () => { - return invokeCmd('cmd_template_functions'); + const result = await invokeCmd('cmd_template_functions'); + setNumFns(result.length); + return result; }, });