-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Add deferred updates queue functions to OnyxUpdateManager
to manually apply updates (e.g. from push notifications)
#42044
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 10 commits
23b0714
ddc09c1
b3de363
c385463
91032c8
4a3b39a
e11b521
d159c01
f907326
c91b5c0
c57a5e9
340125e
0455e0d
6f7436a
da8b8ba
a52e2ca
e427034
4379a0a
9df344c
17c5d0b
15600b0
ccedd1c
342873e
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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import Onyx from 'react-native-onyx'; | ||
import type {DeferredUpdatesDictionary} from '@libs/actions/OnyxUpdateManager/types'; | ||
import * as SequentialQueue from '@libs/Network/SequentialQueue'; | ||
import ONYXKEYS from '@src/ONYXKEYS'; | ||
import type {OnyxUpdatesFromServer} from '@src/types/onyx'; | ||
import createProxyForObject from '@src/utils/createProxyForObject'; | ||
// eslint-disable-next-line import/no-cycle | ||
import * as OnyxUpdateManagerUtils from '.'; | ||
import GetMissingOnyxUpdatesPromiseProxy from './GetMissingOnyxUpdatesPromise'; | ||
|
||
let deferredUpdates: DeferredUpdatesDictionary = {}; | ||
|
||
/** | ||
* Manually processes and applies the updates from the deferred updates queue. (used e.g. for push notifications) | ||
*/ | ||
function processDeferredUpdates() { | ||
chrispader marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (GetMissingOnyxUpdatesPromiseProxy.GetMissingOnyxUpdatesPromise) { | ||
GetMissingOnyxUpdatesPromiseProxy.GetMissingOnyxUpdatesPromise.finally(() => OnyxUpdateManagerUtils.validateAndApplyDeferredUpdates); | ||
} | ||
|
||
GetMissingOnyxUpdatesPromiseProxy.GetMissingOnyxUpdatesPromise = OnyxUpdateManagerUtils.validateAndApplyDeferredUpdates(); | ||
} | ||
|
||
/** | ||
* Allows adding onyx updates to the deferred updates queue manually. | ||
* By default, this will automatically process the updates. Setting "shouldProcessUpdates" to false will prevent this. | ||
* @param updates The updates that should be applied (e.g. updates from push notifications) | ||
* @param shouldProcessUpdates Whether the updates should be processed immediately | ||
* @returns | ||
*/ | ||
function enqueueDeferredUpdates(updates: OnyxUpdatesFromServer[], shouldProcessUpdates = true) { | ||
chrispader marked this conversation as resolved.
Show resolved
Hide resolved
|
||
SequentialQueue.pause(); | ||
|
||
updates.forEach((update) => { | ||
const lastUpdateID = Number(update.lastUpdateID); | ||
if (deferredUpdates[lastUpdateID]) { | ||
return; | ||
} | ||
|
||
deferredUpdates[lastUpdateID] = update; | ||
}); | ||
|
||
if (!shouldProcessUpdates) { | ||
return; | ||
} | ||
|
||
processDeferredUpdates(); | ||
} | ||
|
||
/** | ||
* Clears the deferred updates queue and unpauses the SequentialQueue | ||
* @param shouldUnpauseSequentialQueue Whether the SequentialQueue should be unpaused after clearing the deferred updates | ||
*/ | ||
function clearDeferredUpdates(shouldUnpauseSequentialQueue = true) { | ||
chrispader marked this conversation as resolved.
Show resolved
Hide resolved
|
||
GetMissingOnyxUpdatesPromiseProxy.GetMissingOnyxUpdatesPromise = undefined; | ||
deferredUpdates = {}; | ||
|
||
if (!shouldUnpauseSequentialQueue) { | ||
return; | ||
} | ||
|
||
Onyx.set(ONYXKEYS.ONYX_UPDATES_FROM_SERVER, null); | ||
SequentialQueue.unpause(); | ||
} | ||
|
||
const DeferredUpdates = {deferredUpdates, enqueueDeferredUpdates, clearDeferredUpdates, processDeferredUpdates}; | ||
|
||
export default createProxyForObject(DeferredUpdates); | ||
chrispader marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import type {Response} from '@src/types/onyx'; | ||
import createProxyForObject from '@src/utils/createProxyForObject'; | ||
|
||
const GetMissingOnyxUpdatesPromiseValue = {GetMissingOnyxUpdatesPromise: undefined as Promise<Response | Response[] | void> | undefined}; | ||
|
||
export default createProxyForObject(GetMissingOnyxUpdatesPromiseValue); | ||
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. As you might guess I'm not a big fan of this proxy object pattern 😄. These feel more or less like global variables and are prone to error in the same way since their values can be changed anywhere, at anytime and in any way. It feels like we're pretty much creating a lock with this object so it would be nice if we could abstract it like one. However, I'm not sure exactly how it would look. Cleaning this up is NAB but if you know an easy way to write it I'm curious. |
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.