Skip to content

Commit 43d40bd

Browse files
wusteven815mofojed
andauthored
feat: Reject promise immediately if var not found (#1718)
- Implements #1701 - `fetchVariableDefinition` now throws an `Error` when a variable is not found (instead of throwing `TimeoutError`) - `TimeoutError` still used and will be thrown on an actual timeout - Tests added to check for the correct error --------- Co-authored-by: Mike Bender <mikebender@deephaven.io>
1 parent 51ebe1b commit 43d40bd

2 files changed

Lines changed: 14 additions & 30 deletions

File tree

packages/jsapi-utils/src/ConnectionUtils.test.ts

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -56,38 +56,24 @@ it('finds the right definition if variable exists', async () => {
5656
expect(mockRemoveListener).toHaveBeenCalled();
5757
});
5858

59-
it('finds the definition in the second update if not after the first update', async () => {
59+
it('throws a TimeoutError if finding the variable timed out', async () => {
60+
const timeout = 1000;
6061
const fetchPromise = fetchVariableDefinition(
6162
connection,
62-
testDefinition2.title
63+
testDefinition2.title,
64+
timeout
6365
);
6466

6567
expect(mockSubscribeToFieldUpdates).toHaveBeenCalled();
6668
expect(mockRemoveListener).not.toHaveBeenCalled();
6769

68-
const listener = mockSubscribeToFieldUpdates.mock.calls[0][0];
69-
70-
listener({
71-
created: [testDefinition1],
72-
updated: [],
73-
removed: [],
74-
});
75-
76-
expect(mockRemoveListener).not.toHaveBeenCalled();
70+
jest.advanceTimersByTime(timeout + 2000);
7771

78-
listener({
79-
created: [testDefinition2],
80-
updated: [],
81-
removed: [],
82-
});
83-
84-
const result = await fetchPromise;
85-
86-
expect(result).toBe(testDefinition2);
72+
await expect(fetchPromise).rejects.toThrow(TimeoutError);
8773
expect(mockRemoveListener).toHaveBeenCalled();
8874
});
8975

90-
it('throws a TimeoutError if variable not found', async () => {
76+
it('throws an Error if variable not found', async () => {
9177
const fetchPromise = fetchVariableDefinition(
9278
connection,
9379
testDefinition2.title
@@ -104,11 +90,7 @@ it('throws a TimeoutError if variable not found', async () => {
10490
removed: [],
10591
});
10692

107-
expect(mockRemoveListener).not.toHaveBeenCalled();
108-
109-
jest.runOnlyPendingTimers();
110-
93+
await expect(fetchPromise).rejects.toThrow(Error);
94+
await expect(fetchPromise).rejects.not.toThrow(TimeoutError);
11195
expect(mockRemoveListener).toHaveBeenCalled();
112-
113-
await expect(fetchPromise).rejects.toThrow(TimeoutError);
11496
});

packages/jsapi-utils/src/ConnectionUtils.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export function fetchVariableDefinition(
2525

2626
const timeoutId = setTimeout(() => {
2727
removeListener?.();
28-
reject(new TimeoutError(`Variable ${name} not found`));
28+
reject(new TimeoutError(`Timeout looking for variable ${name}`));
2929
}, timeout);
3030

3131
/**
@@ -34,10 +34,12 @@ export function fetchVariableDefinition(
3434
*/
3535
function handleFieldUpdates(changes: VariableChanges): void {
3636
const definition = changes.created.find(def => def.title === name);
37+
clearTimeout(timeoutId);
38+
removeListener?.();
3739
if (definition != null) {
38-
clearTimeout(timeoutId);
39-
removeListener?.();
4040
resolve(definition);
41+
} else {
42+
reject(new Error(`Variable ${name} not found`));
4143
}
4244
}
4345

0 commit comments

Comments
 (0)