-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Add cancel function when you select an option in search and navigate away from the optionList #42471
Merged
mountiny
merged 8 commits into
Expensify:main
from
callstack-internal:feat/Add-cancel-function-when-you-select-an-option-in-search
May 30, 2024
Merged
Add cancel function when you select an option in search and navigate away from the optionList #42471
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c8a404f
feat: add specific api abort controller
rinej e61b980
refactor: use read_commands const
rinej e0b0ec6
fix: fix typescript
rinej c0ce318
feat: add hook for canceling search request
rinej aba3b93
refactor: adjust types
rinej 56989c9
feat: add cancel function to all right modal panels
rinej e7d0ceb
Merge branch 'main' into feat/Add-cancel-function-when-you-select-an-…
rinej fd3a6eb
Merge branch 'main' into feat/Add-cancel-function-when-you-select-an-…
rinej 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
type SearchForReportsParams = { | ||
searchInput: string; | ||
canCancel?: boolean; | ||
}; | ||
|
||
export default SearchForReportsParams; |
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 |
---|---|---|
|
@@ -14,6 +14,8 @@ import HttpsError from './Errors/HttpsError'; | |
let shouldFailAllRequests = false; | ||
let shouldForceOffline = false; | ||
|
||
type AbortCommand = 'All' | 'SearchForReports'; | ||
|
||
Onyx.connect({ | ||
key: ONYXKEYS.NETWORK, | ||
callback: (network) => { | ||
|
@@ -26,7 +28,9 @@ Onyx.connect({ | |
}); | ||
|
||
// We use the AbortController API to terminate pending request in `cancelPendingRequests` | ||
let cancellationController = new AbortController(); | ||
const abortControllerMap = new Map<AbortCommand, AbortController>(); | ||
abortControllerMap.set('All', new AbortController()); | ||
abortControllerMap.set('SearchForReports', new AbortController()); | ||
|
||
// Some existing old commands (6+ years) exempted from the auth writes count check | ||
const exemptedCommandsWithAuthWrites: string[] = ['SetWorkspaceAutoReportingFrequency']; | ||
|
@@ -45,11 +49,11 @@ const APICommandRegex = /\/api\/([^&?]+)\??.*/; | |
* Send an HTTP request, and attempt to resolve the json response. | ||
* If there is a network error, we'll set the application offline. | ||
*/ | ||
function processHTTPRequest(url: string, method: RequestType = 'get', body: FormData | null = null, canCancel = true): Promise<Response> { | ||
function processHTTPRequest(url: string, method: RequestType = 'get', body: FormData | null = null, abortSignal: AbortSignal | undefined = undefined): Promise<Response> { | ||
const startTime = new Date().valueOf(); | ||
return fetch(url, { | ||
// We hook requests to the same Controller signal, so we can cancel them all at once | ||
signal: canCancel ? cancellationController.signal : undefined, | ||
signal: abortSignal, | ||
method, | ||
body, | ||
}) | ||
|
@@ -159,15 +163,19 @@ function xhr(command: string, data: Record<string, unknown>, type: RequestType = | |
}); | ||
|
||
const url = ApiUtils.getCommandURL({shouldUseSecure, command}); | ||
return processHTTPRequest(url, type, formData, Boolean(data.canCancel)); | ||
|
||
const abortSignalController = data.canCancel ? abortControllerMap.get(command as AbortCommand) ?? abortControllerMap.get('All') : undefined; | ||
return processHTTPRequest(url, type, formData, abortSignalController?.signal); | ||
} | ||
|
||
function cancelPendingRequests() { | ||
cancellationController.abort(); | ||
function cancelPendingRequests(command: AbortCommand = 'All') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably would be good to add a new const in I would use the READ_COMMANDS object |
||
const controller = abortControllerMap.get(command) ?? abortControllerMap.get('All'); | ||
|
||
controller?.abort(); | ||
|
||
// We create a new instance because once `abort()` is called any future requests using the same controller would | ||
// automatically get rejected: https://dom.spec.whatwg.org/#abortcontroller-api-integration | ||
cancellationController = new AbortController(); | ||
abortControllerMap.set(command, new AbortController()); | ||
} | ||
|
||
export default { | ||
|
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 |
---|---|---|
|
@@ -12,6 +12,7 @@ import useDebouncedState from '@hooks/useDebouncedState'; | |
import useDismissedReferralBanners from '@hooks/useDismissedReferralBanners'; | ||
import useLocalize from '@hooks/useLocalize'; | ||
import useNetwork from '@hooks/useNetwork'; | ||
import HttpUtils from '@libs/HttpUtils'; | ||
import type {MaybePhraseKey} from '@libs/Localize'; | ||
import Navigation from '@libs/Navigation/Navigation'; | ||
import type {RootStackParamList} from '@libs/Navigation/types'; | ||
|
@@ -143,6 +144,8 @@ function ChatFinderPage({betas, isSearchingForReports, navigation}: ChatFinderPa | |
return; | ||
} | ||
|
||
HttpUtils.cancelPendingRequests('SearchForReports'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would use the |
||
|
||
if (option.reportID) { | ||
setSearchValue(''); | ||
Navigation.dismissModal(option.reportID); | ||
|
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.
I think you can infer this based on
API commands
(typeof consts) OR 'All'?