mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-25 19:01:14 +01:00
Hacky server streaming done
This commit is contained in:
@@ -1,13 +1,30 @@
|
||||
import { useMutation, useQuery } from '@tanstack/react-query';
|
||||
import { invoke } from '@tauri-apps/api';
|
||||
import { useState } from 'react';
|
||||
import { useListenToTauriEvent } from './useListenToTauriEvent';
|
||||
|
||||
interface ReflectResponseService {
|
||||
name: string;
|
||||
methods: { name: string; schema: string; serverStreaming: boolean; clientStreaming: boolean }[];
|
||||
}
|
||||
|
||||
interface Message {
|
||||
message: string;
|
||||
time: Date;
|
||||
}
|
||||
|
||||
export function useGrpc(url: string | null) {
|
||||
const callUnary = useMutation<
|
||||
string,
|
||||
unknown,
|
||||
{ service: string; method: string; message: string }
|
||||
>({
|
||||
mutationKey: ['grpc_call_reflect', url],
|
||||
const [messages, setMessages] = useState<Message[]>([]);
|
||||
useListenToTauriEvent<string>(
|
||||
'grpc_message',
|
||||
(event) => {
|
||||
console.log('GOT MESSAGE', event);
|
||||
setMessages((prev) => [...prev, { message: event.payload, time: new Date() }]);
|
||||
},
|
||||
[],
|
||||
);
|
||||
const unary = useMutation<string, string, { service: string; method: string; message: string }>({
|
||||
mutationKey: ['grpc_unary', url],
|
||||
mutationFn: async ({ service, method, message }) => {
|
||||
if (url === null) throw new Error('No URL provided');
|
||||
return (await invoke('grpc_call_unary', {
|
||||
@@ -19,17 +36,36 @@ export function useGrpc(url: string | null) {
|
||||
},
|
||||
});
|
||||
|
||||
const reflect = useQuery<string | null>({
|
||||
const serverStreaming = useMutation<
|
||||
string,
|
||||
string,
|
||||
{ service: string; method: string; message: string }
|
||||
>({
|
||||
mutationKey: ['grpc_server_streaming', url],
|
||||
mutationFn: async ({ service, method, message }) => {
|
||||
if (url === null) throw new Error('No URL provided');
|
||||
return (await invoke('grpc_server_streaming', {
|
||||
endpoint: url,
|
||||
service,
|
||||
method,
|
||||
message,
|
||||
})) as string;
|
||||
},
|
||||
});
|
||||
|
||||
const reflect = useQuery<ReflectResponseService[]>({
|
||||
queryKey: ['grpc_reflect', url ?? ''],
|
||||
queryFn: async () => {
|
||||
if (url === null) return null;
|
||||
if (url === null) return [];
|
||||
console.log('GETTING SCHEMA', url);
|
||||
return (await invoke('grpc_reflect', { endpoint: url })) as string;
|
||||
return (await invoke('grpc_reflect', { endpoint: url })) as ReflectResponseService[];
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
callUnary,
|
||||
unary,
|
||||
serverStreaming,
|
||||
schema: reflect.data,
|
||||
messages,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user