11import {
22 experimental_streamedQuery as streamedQuery ,
3- type QueryFunction ,
3+ createQuery ,
4+ type QueryObserverResult ,
5+ queryOptions as createQueryOptions ,
46} from '@tanstack/svelte-query' ;
57import type { Resource } from '@viamrobotics/sdk' ;
68import { toStore , fromStore } from 'svelte/store' ;
@@ -9,20 +11,25 @@ import { useQueryLogger } from '../query-logger';
911// eslint-disable-next-line @typescript-eslint/no-explicit-any
1012export type ArgumentsType < T > = T extends ( ...args : infer U ) => any ? U : never ;
1113
12- export type ResolvedReturnType < T > = T extends (
13- // eslint-disable-next-line @typescript-eslint/no-explicit-any
14- ...args : any [ ]
15- ) => Promise < infer R >
16- ? R
17- : never ;
18-
1914interface QueryOptions {
2015 // enabled defaults to true if unspecified
2116 enabled ?: boolean ;
2217 refetchMode ?: 'append' | 'reset' | 'replace' ;
2318 maxChunks ?: number ;
2419}
2520
21+ export type ResolvedReturnType < T > = T extends (
22+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
23+ ...args : any [ ]
24+ ) => infer R
25+ ? R
26+ : never ;
27+
28+ type QueryResult < T extends Resource , K extends keyof T > = QueryObserverResult <
29+ ResolvedReturnType < T [ K ] > [ ] ,
30+ Error
31+ > ;
32+
2633export const createResourceStream = < T extends Resource , K extends keyof T > (
2734 client : { current : T | undefined } ,
2835 method : K ,
@@ -32,7 +39,7 @@ export const createResourceStream = <T extends Resource, K extends keyof T>(
3239 options ?: ( ( ) => QueryOptions ) | QueryOptions ,
3340 ]
3441 | [ options ?: ( ( ) => QueryOptions ) | QueryOptions ]
35- ) : QueryFunction < ResolvedReturnType < T [ K ] > [ ] , typeof queryKey > => {
42+ ) : { current : QueryResult < T , K > } => {
3643 const debug = useQueryLogger ( ) ;
3744
3845 let [ args , options ] = additional ;
@@ -45,10 +52,10 @@ export const createResourceStream = <T extends Resource, K extends keyof T>(
4552 const _options = $derived (
4653 typeof options === 'function' ? options ( ) : options
4754 ) ;
55+
4856 const _args = $derived ( typeof args === 'function' ? args ( ) : args ) ;
4957 const name = $derived ( client . current ?. name ) ;
5058 const methodName = $derived ( String ( method ) ) ;
51-
5259 const queryKey = $derived ( [
5360 'viam-svelte-sdk' ,
5461 'partID' ,
@@ -59,40 +66,48 @@ export const createResourceStream = <T extends Resource, K extends keyof T>(
5966 ...( _args ? [ _args ] : [ ] ) ,
6067 ] ) ;
6168
62- const queryOptions = $derived ( {
63- queryKey,
64- queryFn : async ( ) => {
65- const clientFunc = client . current ?. [ method ] ;
66-
67- if ( typeof clientFunc !== 'function' ) {
68- throw new TypeError (
69- `${ String ( method ) } is not a method on the resource client.`
70- ) ;
71- }
72-
73- const logger = debug . createLogger ( ) ;
74- logger ( 'REQ' , name , methodName , _args ) ;
75-
76- try {
77- const response = ( await clientFunc ?. apply (
78- client . current ,
79- _args
80- ) ) as AsyncIterable < ResolvedReturnType < T [ K ] > > ;
81-
82- logger ( 'RES' , name , methodName , response ) ;
83- return response ;
84- } catch ( error ) {
85- logger ( 'ERR' , name , methodName , error ) ;
86- throw error ;
87- }
88- } ,
89- ..._options ,
90- } ) ;
91-
92- const queryOptionsStore = fromStore ( toStore ( ( ) => queryOptions ) ) ;
93- const query = streamedQuery < ResolvedReturnType < T [ K ] > , typeof queryKey > (
94- queryOptionsStore . current
69+ // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type, @typescript-eslint/no-explicit-any
70+ function processStream ( ) {
71+ const clientFunc = client . current ?. [ method ] ;
72+
73+ if ( typeof clientFunc !== 'function' ) {
74+ throw new TypeError (
75+ `${ String ( method ) } is not a method on the resource client.`
76+ ) ;
77+ }
78+
79+ const logger = debug . createLogger ( ) ;
80+ logger ( 'REQ' , name , methodName , _args ) ;
81+
82+ try {
83+ const response = clientFunc ?. apply (
84+ client . current ,
85+ _args
86+ ) as AsyncGenerator < ResolvedReturnType < T [ K ] > > ;
87+ console . log ( 'response' , typeof response ) ;
88+
89+ logger ( 'RES' , name , methodName , response ) ;
90+ return {
91+ async * [ Symbol . asyncIterator ] ( ) {
92+ yield * response ;
93+ } ,
94+ } ;
95+ } catch ( error ) {
96+ logger ( 'ERR' , name , methodName , error ) ;
97+ throw error ;
98+ }
99+ }
100+
101+ const queryOptions = $derived (
102+ createQueryOptions ( {
103+ queryKey,
104+ enabled : client . current !== undefined && _options ?. enabled !== false ,
105+ queryFn : streamedQuery < ResolvedReturnType < T [ K ] > > ( {
106+ queryFn : processStream ,
107+ ..._options ,
108+ } ) ,
109+ } )
95110 ) ;
96111
97- return query ;
112+ return fromStore ( createQuery ( toStore ( ( ) => queryOptions ) ) ) ;
98113} ;
0 commit comments