-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore(auth): debounce refreshAuthTokens #12845
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4b27260
chore: add debounce callback helper
israx 751b15b
chore: add unit tests
israx 80c2b60
chore: debounce fetchAuthSession
israx 4ab0de2
chore: fix unit test
israx b0d982a
chore: fix bundle size limits
israx 3419b8f
chore: update debounce logic
israx 4bbeba0
chore: update dedup logic
israx e3df8aa
chore: debounce refreshAuthTokens
israx 0db3893
chore: fix bundle size
israx a717e20
chore: address feedback
israx b46967d
chore: fix unit test
israx 4ce72c3
chore: address feedback
israx 1444ffd
chore: update yarn.lock
israx a976d76
chore: address feedbak
israx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😬