-
Notifications
You must be signed in to change notification settings - Fork 3.9k
De-dupe ReconnectApp in the persisted requests queue #47913
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
Changes from all commits
e388597
6f278c0
615a8ba
8b505ab
be72fec
6e40d66
31bf286
7f1b772
45bf5a6
3620da6
1d9550c
d07d912
7170e14
69463bb
b9d19f0
dc6aa40
2f0e209
b6b893b
1fcd87d
b9cb098
3b21e41
919e254
1c5c49b
9ff5edd
d3bf574
2bc9f32
4b309bf
cae7b49
2792cfe
703957d
7d90d66
1e0d4ed
ccadfd6
0877308
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,7 +58,6 @@ Onyx.connect({ | |
| if (!actions) { | ||
| return; | ||
| } | ||
|
|
||
| allReportActions = actions; | ||
| }, | ||
| }); | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -5,24 +5,47 @@ import ONYXKEYS from '@src/ONYXKEYS'; | |||||
| import type {Request} from '@src/types/onyx'; | ||||||
|
|
||||||
| let persistedRequests: Request[] = []; | ||||||
| let ongoingRequest: Request | null = null; | ||||||
|
|
||||||
| Onyx.connect({ | ||||||
| key: ONYXKEYS.PERSISTED_REQUESTS, | ||||||
| callback: (val) => (persistedRequests = val ?? []), | ||||||
| callback: (val) => { | ||||||
| persistedRequests = val ?? []; | ||||||
|
|
||||||
| if (ongoingRequest && persistedRequests.length > 0) { | ||||||
|
Contributor
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. @gedu How did you ensure that this callback is called after other connection callback? |
||||||
| const nextRequestToProcess = persistedRequests.at(0); | ||||||
|
|
||||||
| // We try to remove the next request from the persistedRequests if it is the same as ongoingRequest | ||||||
| // so we don't process it twice. | ||||||
| if (isEqual(nextRequestToProcess, ongoingRequest)) { | ||||||
| persistedRequests = persistedRequests.slice(1); | ||||||
| } | ||||||
| } | ||||||
| }, | ||||||
| }); | ||||||
| Onyx.connect({ | ||||||
| key: ONYXKEYS.PERSISTED_ONGOING_REQUESTS, | ||||||
| callback: (val) => { | ||||||
| ongoingRequest = val ?? null; | ||||||
| }, | ||||||
| }); | ||||||
|
|
||||||
| /** | ||||||
| * This promise is only used by tests. DO NOT USE THIS PROMISE IN THE APPLICATION CODE | ||||||
| */ | ||||||
| function clear() { | ||||||
| ongoingRequest = null; | ||||||
|
gedu marked this conversation as resolved.
|
||||||
| Onyx.set(ONYXKEYS.PERSISTED_ONGOING_REQUESTS, null); | ||||||
| return Onyx.set(ONYXKEYS.PERSISTED_REQUESTS, []); | ||||||
| } | ||||||
|
|
||||||
| function getLength(): number { | ||||||
| return persistedRequests.length; | ||||||
| // Making it backwards compatible with the old implementation | ||||||
| return persistedRequests.length + (ongoingRequest ? 1 : 0); | ||||||
| } | ||||||
|
|
||||||
| function save(requestToPersist: Request) { | ||||||
| // If the command is not in the keepLastInstance array, add the new request as usual | ||||||
| const requests = [...persistedRequests, requestToPersist]; | ||||||
| persistedRequests = requests; | ||||||
| Onyx.set(ONYXKEYS.PERSISTED_REQUESTS, requests).then(() => { | ||||||
|
|
@@ -31,18 +54,24 @@ function save(requestToPersist: Request) { | |||||
| } | ||||||
|
|
||||||
| function remove(requestToRemove: Request) { | ||||||
| ongoingRequest = null; | ||||||
| /** | ||||||
| * We only remove the first matching request because the order of requests matters. | ||||||
| * If we were to remove all matching requests, we can end up with a final state that is different than what the user intended. | ||||||
| */ | ||||||
| const requests = [...persistedRequests]; | ||||||
| const index = requests.findIndex((persistedRequest) => isEqual(persistedRequest, requestToRemove)); | ||||||
| if (index === -1) { | ||||||
| return; | ||||||
|
|
||||||
| if (index !== -1) { | ||||||
| requests.splice(index, 1); | ||||||
| } | ||||||
| requests.splice(index, 1); | ||||||
|
|
||||||
| persistedRequests = requests; | ||||||
| Onyx.set(ONYXKEYS.PERSISTED_REQUESTS, requests).then(() => { | ||||||
|
|
||||||
| Onyx.multiSet({ | ||||||
| [ONYXKEYS.PERSISTED_REQUESTS]: persistedRequests, | ||||||
|
Contributor
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.
Suggested change
|
||||||
| [ONYXKEYS.PERSISTED_ONGOING_REQUESTS]: null, | ||||||
| }).then(() => { | ||||||
| Log.info(`[SequentialQueue] '${requestToRemove.command}' removed from the queue. Queue length is ${getLength()}`); | ||||||
| }); | ||||||
| } | ||||||
|
|
@@ -54,8 +83,52 @@ function update(oldRequestIndex: number, newRequest: Request) { | |||||
| Onyx.set(ONYXKEYS.PERSISTED_REQUESTS, requests); | ||||||
| } | ||||||
|
|
||||||
| function updateOngoingRequest(newRequest: Request) { | ||||||
| ongoingRequest = newRequest; | ||||||
|
|
||||||
| if (newRequest.persistWhenOngoing) { | ||||||
| Onyx.set(ONYXKEYS.PERSISTED_ONGOING_REQUESTS, newRequest); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| function processNextRequest(): Request | null { | ||||||
| if (ongoingRequest) { | ||||||
| Log.info(`Ongoing Request already set returning same one ${ongoingRequest.commandName}`); | ||||||
| return ongoingRequest; | ||||||
| } | ||||||
|
|
||||||
| // You must handle the case where there are no requests to process | ||||||
| if (persistedRequests.length === 0) { | ||||||
| throw new Error('No requests to process'); | ||||||
| } | ||||||
|
|
||||||
| ongoingRequest = persistedRequests.shift() ?? null; | ||||||
|
Contributor
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. Coming from #57119 checklist: After shifting persistedRequests, the request still includes the same shifted request in offline mode, like with toggle API requests. When we handle endRequestAndRemoveFromQueue it ends up removing the same shifted request. To handle this case, we need to ensure the request is unique. |
||||||
|
|
||||||
| if (ongoingRequest && ongoingRequest.persistWhenOngoing) { | ||||||
| Onyx.set(ONYXKEYS.PERSISTED_ONGOING_REQUESTS, ongoingRequest); | ||||||
| } | ||||||
|
|
||||||
| return ongoingRequest; | ||||||
| } | ||||||
|
|
||||||
| function rollbackOngoingRequest() { | ||||||
| if (!ongoingRequest) { | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| // Prepend ongoingRequest to persistedRequests | ||||||
| persistedRequests.unshift(ongoingRequest); | ||||||
|
|
||||||
| // Clear the ongoingRequest | ||||||
| ongoingRequest = null; | ||||||
| } | ||||||
|
|
||||||
| function getAll(): Request[] { | ||||||
| return persistedRequests; | ||||||
| } | ||||||
|
|
||||||
| export {clear, save, getAll, remove, update, getLength}; | ||||||
| function getOngoingRequest(): Request | null { | ||||||
| return ongoingRequest; | ||||||
| } | ||||||
|
|
||||||
| export {clear, save, getAll, remove, update, getLength, getOngoingRequest, processNextRequest, updateOngoingRequest, rollbackOngoingRequest}; | ||||||
Uh oh!
There was an error while loading. Please reload this page.