Skip to content

Commit

Permalink
Merge pull request #25614 from Expensify/marcaaron-memoryOnlyQueuedUp…
Browse files Browse the repository at this point in the history
…dates

Save queued updates to memory only
  • Loading branch information
tgolen authored Sep 28, 2023
2 parents bb90e97 + 02b5eef commit dd7820d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 16 deletions.
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 @@ -307,7 +303,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};

0 comments on commit dd7820d

Please sign in to comment.