Skip to content

Commit

Permalink
Better template function fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
gschier committed Oct 24, 2024
1 parent 55b12d7 commit 4a52095
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src-web/hooks/useTemplateFunctions.ts
Original file line number Diff line number Diff line change
@@ -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<number>(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<GetTemplateFunctionsResponse[]>('cmd_template_functions');
const result = await invokeCmd<GetTemplateFunctionsResponse[]>('cmd_template_functions');
setNumFns(result.length);
return result;
},
});

Expand Down

0 comments on commit 4a52095

Please sign in to comment.