-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25614 from Expensify/marcaaron-memoryOnlyQueuedUp…
…dates Save queued updates to memory only
- Loading branch information
Showing
2 changed files
with
5 additions
and
16 deletions.
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
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}; |