11import { createContext , useContext , useEffect , useState } from 'react' ;
22import type { dh } from '@deephaven/jsapi-types' ;
3+ import { ObjectFetcherContext } from './useObjectFetcher' ;
34
45/** Function for unsubscribing from a given subscription */
56export type UnsubscribeFunction = ( ) => void ;
@@ -23,6 +24,10 @@ export type ObjectFetchUpdate<T = unknown> = {
2324 error : unknown | null ;
2425} ;
2526
27+ export type ObjectFetchUpdateCallback < T = unknown > = (
28+ update : ObjectFetchUpdate < T >
29+ ) => void ;
30+
2631/** ObjectFetchManager for managing a subscription to an object using a VariableDescriptor */
2732export type ObjectFetchManager = {
2833 /**
@@ -33,7 +38,7 @@ export type ObjectFetchManager = {
3338 */
3439 subscribe : < T = unknown > (
3540 descriptor : dh . ide . VariableDescriptor ,
36- onUpdate : ( update : ObjectFetchUpdate < T > ) => void
41+ onUpdate : ObjectFetchUpdateCallback < T >
3742 ) => UnsubscribeFunction ;
3843} ;
3944
@@ -58,14 +63,22 @@ export function useObjectFetch<T = unknown>(
5863 } )
5964 ) ;
6065
66+ const objectFetcher = useContext ( ObjectFetcherContext ) ;
6167 const objectFetchManager = useContext ( ObjectFetchManagerContext ) ;
6268
6369 useEffect ( ( ) => {
6470 if ( objectFetchManager == null ) {
65- setCurrentUpdate ( {
66- fetch : null ,
67- error : new Error ( 'No ObjectFetchManager available in context' ) ,
68- } ) ;
71+ if ( objectFetcher == null ) {
72+ setCurrentUpdate ( {
73+ fetch : null ,
74+ error : new Error ( 'No ObjectFetchManager available in context' ) ,
75+ } ) ;
76+ } else {
77+ setCurrentUpdate ( {
78+ fetch : ( ) => objectFetcher ( descriptor ) ,
79+ error : null ,
80+ } ) ;
81+ }
6982 return ;
7083 }
7184 // Signal that we're still loading
@@ -74,7 +87,7 @@ export function useObjectFetch<T = unknown>(
7487 error : null ,
7588 } ) ;
7689 return objectFetchManager . subscribe ( descriptor , setCurrentUpdate ) ;
77- } , [ descriptor , objectFetchManager ] ) ;
90+ } , [ descriptor , objectFetcher , objectFetchManager ] ) ;
7891
7992 return currentUpdate ;
8093}
0 commit comments