From 06f0dc1a06b3bd78fd01f8d7dd32bcf6920a6e09 Mon Sep 17 00:00:00 2001 From: Steven Wu Date: Mon, 8 Jan 2024 16:07:42 -0500 Subject: [PATCH 1/4] feat: Reject promise immediately if var not found --- packages/jsapi-utils/src/ConnectionUtils.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/jsapi-utils/src/ConnectionUtils.ts b/packages/jsapi-utils/src/ConnectionUtils.ts index b8b460219a..338f79a579 100644 --- a/packages/jsapi-utils/src/ConnectionUtils.ts +++ b/packages/jsapi-utils/src/ConnectionUtils.ts @@ -34,10 +34,12 @@ export function fetchVariableDefinition( */ function handleFieldUpdates(changes: VariableChanges): void { const definition = changes.created.find(def => def.title === name); + clearTimeout(timeoutId); + removeListener?.(); if (definition != null) { - clearTimeout(timeoutId); - removeListener?.(); resolve(definition); + } else { + reject(new TimeoutError(`Variable ${name} not found`)); } } From 26b2d7ca9ea3b1caa23ebc92194ff00c1304d710 Mon Sep 17 00:00:00 2001 From: Steven Wu Date: Tue, 9 Jan 2024 12:16:00 -0500 Subject: [PATCH 2/4] feat(ConnectionUtils): improve error messages --- packages/jsapi-utils/src/ConnectionUtils.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/jsapi-utils/src/ConnectionUtils.ts b/packages/jsapi-utils/src/ConnectionUtils.ts index 338f79a579..bbcccf7ace 100644 --- a/packages/jsapi-utils/src/ConnectionUtils.ts +++ b/packages/jsapi-utils/src/ConnectionUtils.ts @@ -25,7 +25,7 @@ export function fetchVariableDefinition( const timeoutId = setTimeout(() => { removeListener?.(); - reject(new TimeoutError(`Variable ${name} not found`)); + reject(new TimeoutError(`Timeout looking for Variable ${name}`)); }, timeout); /** @@ -39,7 +39,7 @@ export function fetchVariableDefinition( if (definition != null) { resolve(definition); } else { - reject(new TimeoutError(`Variable ${name} not found`)); + reject(new Error(`Variable ${name} not found`)); } } From 8fa92e24f25116b4a3c2d2467344d27d056d72bf Mon Sep 17 00:00:00 2001 From: Steven Wu Date: Tue, 9 Jan 2024 12:16:07 -0500 Subject: [PATCH 3/4] test(ConnectionUtils): update for new behaviour --- .../jsapi-utils/src/ConnectionUtils.test.ts | 36 +++++-------------- 1 file changed, 9 insertions(+), 27 deletions(-) diff --git a/packages/jsapi-utils/src/ConnectionUtils.test.ts b/packages/jsapi-utils/src/ConnectionUtils.test.ts index 08d425b847..fca74bb355 100644 --- a/packages/jsapi-utils/src/ConnectionUtils.test.ts +++ b/packages/jsapi-utils/src/ConnectionUtils.test.ts @@ -56,38 +56,24 @@ it('finds the right definition if variable exists', async () => { expect(mockRemoveListener).toHaveBeenCalled(); }); -it('finds the definition in the second update if not after the first update', async () => { +it('throws a TimeoutError if finding the variable timed out', async () => { + const timeout = 1000; const fetchPromise = fetchVariableDefinition( connection, - testDefinition2.title + testDefinition2.title, + timeout ); expect(mockSubscribeToFieldUpdates).toHaveBeenCalled(); expect(mockRemoveListener).not.toHaveBeenCalled(); - const listener = mockSubscribeToFieldUpdates.mock.calls[0][0]; - - listener({ - created: [testDefinition1], - updated: [], - removed: [], - }); - - expect(mockRemoveListener).not.toHaveBeenCalled(); + jest.advanceTimersByTime(timeout + 2000); - listener({ - created: [testDefinition2], - updated: [], - removed: [], - }); - - const result = await fetchPromise; - - expect(result).toBe(testDefinition2); + await expect(fetchPromise).rejects.toThrow(TimeoutError); expect(mockRemoveListener).toHaveBeenCalled(); }); -it('throws a TimeoutError if variable not found', async () => { +it('throws an Error if variable not found', async () => { const fetchPromise = fetchVariableDefinition( connection, testDefinition2.title @@ -104,11 +90,7 @@ it('throws a TimeoutError if variable not found', async () => { removed: [], }); - expect(mockRemoveListener).not.toHaveBeenCalled(); - - jest.runOnlyPendingTimers(); - + await expect(fetchPromise).rejects.toThrow(Error); + await expect(fetchPromise).rejects.not.toThrow(TimeoutError); expect(mockRemoveListener).toHaveBeenCalled(); - - await expect(fetchPromise).rejects.toThrow(TimeoutError); }); From 1a2626a910c82633792dafb9fd07aeee23130361 Mon Sep 17 00:00:00 2001 From: Steven Wu Date: Wed, 10 Jan 2024 09:10:43 -0500 Subject: [PATCH 4/4] fix: small grammar change Co-authored-by: Mike Bender --- packages/jsapi-utils/src/ConnectionUtils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jsapi-utils/src/ConnectionUtils.ts b/packages/jsapi-utils/src/ConnectionUtils.ts index bbcccf7ace..72e13a38ab 100644 --- a/packages/jsapi-utils/src/ConnectionUtils.ts +++ b/packages/jsapi-utils/src/ConnectionUtils.ts @@ -25,7 +25,7 @@ export function fetchVariableDefinition( const timeoutId = setTimeout(() => { removeListener?.(); - reject(new TimeoutError(`Timeout looking for Variable ${name}`)); + reject(new TimeoutError(`Timeout looking for variable ${name}`)); }, timeout); /**