Skip to content
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

Save queued updates to memory only #25614

Merged
merged 10 commits into from
Sep 28, 2023
5 changes: 0 additions & 5 deletions src/ONYXKEYS.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {ValueOf} from 'type-fest';
import {OnyxUpdate} from 'react-native-onyx';
import DeepValueOf from './types/utils/DeepValueOf';
import * as OnyxTypes from './types/onyx';
import CONST from './CONST';
Expand Down Expand Up @@ -30,9 +29,6 @@ const ONYXKEYS = {
/** Note: These are Persisted Requests - not all requests in the main queue as the key name might lead one to believe */
PERSISTED_REQUESTS: 'networkRequestQueue',

/** Onyx updates from a response, or success or failure data from a request. */
QUEUED_ONYX_UPDATES: 'queuedOnyxUpdates',

/** Stores current date */
CURRENT_DATE: 'currentDate',

Expand Down Expand Up @@ -306,7 +302,6 @@ type OnyxValues = {
[ONYXKEYS.DEVICE_ID]: string;
[ONYXKEYS.IS_SIDEBAR_LOADED]: boolean;
[ONYXKEYS.PERSISTED_REQUESTS]: OnyxTypes.Request[];
[ONYXKEYS.QUEUED_ONYX_UPDATES]: OnyxUpdate[];
[ONYXKEYS.CURRENT_DATE]: string;
[ONYXKEYS.CREDENTIALS]: OnyxTypes.Credentials;
[ONYXKEYS.IOU]: OnyxTypes.IOU;
Expand Down
16 changes: 5 additions & 11 deletions src/libs/actions/QueuedOnyxUpdates.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
import Onyx, {OnyxUpdate} from 'react-native-onyx';
import ONYXKEYS from '../../ONYXKEYS';

// In this file we manage a queue of Onyx updates while the SequentialQueue is processing. There are functions to get the updates and clear the queue after saving the updates in Onyx.

let queuedOnyxUpdates: OnyxUpdate[] = [];
Onyx.connect({
key: ONYXKEYS.QUEUED_ONYX_UPDATES,
callback: (val) => (queuedOnyxUpdates = val ?? []),
});

/**
* @param updates Onyx updates to queue for later
*/
function queueOnyxUpdates(updates: OnyxUpdate[]): Promise<void> {
return Onyx.set(ONYXKEYS.QUEUED_ONYX_UPDATES, [...queuedOnyxUpdates, ...updates]);
}

function clear() {
Onyx.set(ONYXKEYS.QUEUED_ONYX_UPDATES, null);
queuedOnyxUpdates = queuedOnyxUpdates.concat(updates);
return Promise.resolve();
}

function flushQueue(): Promise<void> {
return Onyx.update(queuedOnyxUpdates).then(clear);
return Onyx.update(queuedOnyxUpdates).then(() => {
queuedOnyxUpdates = [];
});
}

export {queueOnyxUpdates, flushQueue};
Loading