-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(auth): debounce refreshAuthTokens (#12845)
* chore: add debounce callback helper * chore: add unit tests * chore: debounce fetchAuthSession * chore: fix unit test * chore: fix bundle size limits * chore: update debounce logic * chore: update dedup logic * chore: debounce refreshAuthTokens * chore: fix bundle size * chore: address feedback * chore: fix unit test * chore: address feedback * chore: update yarn.lock * chore: address feedbak
- Loading branch information
Showing
8 changed files
with
105 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { deDupeAsyncFunction } from '../../src/utils/deDupeAsyncFunction'; | ||
|
||
describe('dedupeAsyncFunction()', () => { | ||
const numberOfConcurrentCalls = 10; | ||
const mockServiceFunction = jest.fn(); | ||
const mockReturnValue = { id: 1 }; | ||
|
||
beforeEach(() => { | ||
mockServiceFunction.mockImplementation(async () => mockReturnValue); | ||
}); | ||
afterEach(() => { | ||
mockServiceFunction.mockClear(); | ||
}); | ||
|
||
it('should invoke the mockServiceFunction', async () => { | ||
const deDupedFunction = deDupeAsyncFunction(mockServiceFunction); | ||
|
||
deDupedFunction(); | ||
expect(mockServiceFunction).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should invoke the mockServiceFunction one time during concurrent sync calls', () => { | ||
const deDupedFunction = deDupeAsyncFunction(mockServiceFunction); | ||
for (let i = 0; i < numberOfConcurrentCalls; i++) { | ||
deDupedFunction(); | ||
} | ||
expect(mockServiceFunction).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should return a value once the mockServiceFunction is resolved', async () => { | ||
const deDupedFunction = deDupeAsyncFunction(mockServiceFunction); | ||
expect(await deDupedFunction()).toEqual(mockReturnValue); | ||
expect(mockServiceFunction).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should allow to invoke the mockServiceFunction again after the promise has being resolved', async () => { | ||
const deDupedFunction = deDupeAsyncFunction(mockServiceFunction); | ||
for (let i = 0; i < numberOfConcurrentCalls; i++) { | ||
expect(deDupedFunction()).toBeInstanceOf(Promise); | ||
} | ||
|
||
// resolves the promise | ||
expect(await deDupedFunction()).toEqual(mockReturnValue); | ||
|
||
// should allow to call the mockServiceFunction again | ||
deDupedFunction(); | ||
|
||
expect(mockServiceFunction).toHaveBeenCalledTimes(2); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// this will make the tsc-compliance-test to pass | ||
type Awaited<T> = T extends null | undefined | ||
? T // special case for `null | undefined` when not in `--strictNullChecks` mode | ||
: T extends object & { then(onfulfilled: infer F, ...args: infer _): any } // `await` only unwraps object types with a callable `then`. Non-object types are not unwrapped | ||
? F extends (value: infer V, ...args: infer _) => any // if the argument to `then` is callable, extracts the first argument | ||
? Awaited<V> // recursively unwrap the value | ||
: never // the argument to `then` was not callable | ||
: T; // | ||
/** | ||
* returns in-flight promise if there is one | ||
* | ||
* @param asyncFunction - asyncFunction to be deduped. | ||
* @returns - the return type of the callback | ||
*/ | ||
export const deDupeAsyncFunction = <A extends any[], R>( | ||
asyncFunction: (...args: A) => Promise<R> | ||
) => { | ||
let inflightPromise: Promise<Awaited<R>> | undefined; | ||
return async (...args: A): Promise<Awaited<R>> => { | ||
if (inflightPromise) return inflightPromise; | ||
|
||
inflightPromise = new Promise(async (resolve, reject) => { | ||
try { | ||
const result = await asyncFunction(...args); | ||
resolve(result); | ||
} catch (error) { | ||
reject(error); | ||
} finally { | ||
inflightPromise = undefined; | ||
} | ||
}); | ||
|
||
return inflightPromise; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters