@@ -5,37 +5,65 @@ import { TimeoutError } from '@deephaven/utils';
55export const FETCH_TIMEOUT = 10_000 ;
66
77/**
8- * Fetch the definition for a variable given a connection. Subscribes to field updates and triggers when the variable is found.
8+ * Fetch the definition for a variable given a connection. Waits for the next
9+ * field update event and resolves if the variable is found in the created
10+ * variables.
911 * @param connection Connection to get the variable from
1012 * @param name Name of the definition to fetch
1113 * @param timeout Timeout for the fetch
12- * @returns Promise the resolves to the variable definition if found, or rejects if there's an error or the timeout has exceeded
14+ * @returns Promise that resolves to the variable definition if found in the next field update,
15+ * or rejects if the variable is not found in that update or if the timeout is exceeded
1316 */
1417export function fetchVariableDefinition (
1518 connection : dh . IdeConnection ,
1619 name : string ,
1720 timeout = FETCH_TIMEOUT
21+ ) : Promise < dh . ide . VariableDefinition > {
22+ return fetchVariableDefinitionByPredicate (
23+ connection ,
24+ def => def . title === name ,
25+ timeout ,
26+ `Variable ${ name } not found`
27+ ) ;
28+ }
29+
30+ /**
31+ * Fetch the definition for a variable given a connection. Waits for the next
32+ * field update event and resolves if a variable matching the predicate is found
33+ * in the created variables.
34+ * @param connection Connection to get the variable from
35+ * @param predicate Predicate function to test each variable definition
36+ * @param timeout Timeout for the fetch
37+ * @param errorMessage Optional error message for timeout and not found errors
38+ * @returns Promise that resolves to the variable definition if found in the next field update,
39+ * or rejects if no matching variable is found in that update or if the timeout is exceeded
40+ */
41+ export function fetchVariableDefinitionByPredicate (
42+ connection : dh . IdeConnection ,
43+ predicate : ( definition : dh . ide . VariableDefinition ) => boolean ,
44+ timeout = FETCH_TIMEOUT ,
45+ errorMessage = 'Variable not found'
1846) : Promise < dh . ide . VariableDefinition > {
1947 return new Promise < dh . ide . VariableDefinition > ( ( resolve , reject ) => {
2048 let removeListener : ( ) => void ;
2149
2250 const timeoutId = setTimeout ( ( ) => {
2351 removeListener ?.( ) ;
24- reject ( new TimeoutError ( `Timeout looking for variable ${ name } ` ) ) ;
52+ reject ( new TimeoutError ( `Timeout: ${ errorMessage } ` ) ) ;
2553 } , timeout ) ;
2654
2755 /**
28- * Checks if the variable we're looking for is in the changes, and resolves the promise if it does
56+ * Checks if a variable matching the predicate is in the changes, and resolves the promise if it does
2957 * @param changes Variables changes that have occurred
3058 */
3159 function handleFieldUpdates ( changes : dh . ide . VariableChanges ) : void {
32- const definition = changes . created . find ( def => def . title === name ) ;
60+ const definition = changes . created . find ( predicate ) ;
3361 clearTimeout ( timeoutId ) ;
3462 removeListener ?.( ) ;
3563 if ( definition != null ) {
3664 resolve ( definition ) ;
3765 } else {
38- reject ( new Error ( `Variable ${ name } not found` ) ) ;
66+ reject ( new Error ( errorMessage ) ) ;
3967 }
4068 }
4169
0 commit comments