|
| 1 | +import Onyx from 'react-native-onyx'; |
| 2 | +import type {DeferredUpdatesDictionary} from '@libs/actions/OnyxUpdateManager/types'; |
| 3 | +import * as SequentialQueue from '@libs/Network/SequentialQueue'; |
| 4 | +import ONYXKEYS from '@src/ONYXKEYS'; |
| 5 | +import type {OnyxUpdatesFromServer, Response} from '@src/types/onyx'; |
| 6 | +import {isValidOnyxUpdateFromServer} from '@src/types/onyx/OnyxUpdatesFromServer'; |
| 7 | +// eslint-disable-next-line import/no-cycle |
| 8 | +import * as OnyxUpdateManagerUtils from '.'; |
| 9 | + |
| 10 | +let missingOnyxUpdatesQueryPromise: Promise<Response | Response[] | void> | undefined; |
| 11 | +let deferredUpdates: DeferredUpdatesDictionary = {}; |
| 12 | + |
| 13 | +/** |
| 14 | + * Returns the promise that fetches the missing onyx updates |
| 15 | + * @returns the promise |
| 16 | + */ |
| 17 | +function getMissingOnyxUpdatesQueryPromise() { |
| 18 | + return missingOnyxUpdatesQueryPromise; |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * Sets the promise that fetches the missing onyx updates |
| 23 | + */ |
| 24 | +function setMissingOnyxUpdatesQueryPromise(promise: Promise<Response | Response[] | void>) { |
| 25 | + missingOnyxUpdatesQueryPromise = promise; |
| 26 | +} |
| 27 | + |
| 28 | +type GetDeferredOnyxUpdatesOptiosn = { |
| 29 | + minUpdateID?: number; |
| 30 | +}; |
| 31 | + |
| 32 | +/** |
| 33 | + * Returns the deferred updates that are currently in the queue |
| 34 | + * @param minUpdateID An optional minimum update ID to filter the deferred updates by |
| 35 | + * @returns |
| 36 | + */ |
| 37 | +function getUpdates(options?: GetDeferredOnyxUpdatesOptiosn) { |
| 38 | + if (options?.minUpdateID == null) { |
| 39 | + return deferredUpdates; |
| 40 | + } |
| 41 | + |
| 42 | + return Object.entries(deferredUpdates).reduce<DeferredUpdatesDictionary>( |
| 43 | + (accUpdates, [lastUpdateID, update]) => ({ |
| 44 | + ...accUpdates, |
| 45 | + ...(Number(lastUpdateID) > (options.minUpdateID ?? 0) ? {[Number(lastUpdateID)]: update} : {}), |
| 46 | + }), |
| 47 | + {}, |
| 48 | + ); |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Returns a boolean indicating whether the deferred updates queue is empty |
| 53 | + * @returns a boolean indicating whether the deferred updates queue is empty |
| 54 | + */ |
| 55 | +function isEmpty() { |
| 56 | + return Object.keys(deferredUpdates).length === 0; |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Manually processes and applies the updates from the deferred updates queue. (used e.g. for push notifications) |
| 61 | + */ |
| 62 | +function process() { |
| 63 | + if (missingOnyxUpdatesQueryPromise) { |
| 64 | + missingOnyxUpdatesQueryPromise.finally(() => OnyxUpdateManagerUtils.validateAndApplyDeferredUpdates); |
| 65 | + } |
| 66 | + |
| 67 | + missingOnyxUpdatesQueryPromise = OnyxUpdateManagerUtils.validateAndApplyDeferredUpdates(); |
| 68 | +} |
| 69 | + |
| 70 | +type EnqueueDeferredOnyxUpdatesOptions = { |
| 71 | + shouldPauseSequentialQueue?: boolean; |
| 72 | +}; |
| 73 | + |
| 74 | +/** |
| 75 | + * Allows adding onyx updates to the deferred updates queue manually. |
| 76 | + * @param updates The updates that should be applied (e.g. updates from push notifications) |
| 77 | + * @param options additional flags to change the behaviour of this function |
| 78 | + */ |
| 79 | +function enqueue(updates: OnyxUpdatesFromServer | DeferredUpdatesDictionary, options?: EnqueueDeferredOnyxUpdatesOptions) { |
| 80 | + if (options?.shouldPauseSequentialQueue ?? true) { |
| 81 | + SequentialQueue.pause(); |
| 82 | + } |
| 83 | + |
| 84 | + // We check here if the "updates" param is a single update. |
| 85 | + // If so, we only need to insert one update into the deferred updates queue. |
| 86 | + if (isValidOnyxUpdateFromServer(updates)) { |
| 87 | + const lastUpdateID = Number(updates.lastUpdateID); |
| 88 | + deferredUpdates[lastUpdateID] = updates; |
| 89 | + } else { |
| 90 | + // If the "updates" param is an object, we need to insert multiple updates into the deferred updates queue. |
| 91 | + Object.entries(updates).forEach(([lastUpdateIDString, update]) => { |
| 92 | + const lastUpdateID = Number(lastUpdateIDString); |
| 93 | + if (deferredUpdates[lastUpdateID]) { |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + deferredUpdates[lastUpdateID] = update; |
| 98 | + }); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * Adds updates to the deferred updates queue and processes them immediately |
| 104 | + * @param updates The updates that should be applied (e.g. updates from push notifications) |
| 105 | + */ |
| 106 | +function enqueueAndProcess(updates: OnyxUpdatesFromServer | DeferredUpdatesDictionary, options?: EnqueueDeferredOnyxUpdatesOptions) { |
| 107 | + enqueue(updates, options); |
| 108 | + process(); |
| 109 | +} |
| 110 | + |
| 111 | +type ClearDeferredOnyxUpdatesOptions = { |
| 112 | + shouldResetGetMissingOnyxUpdatesPromise?: boolean; |
| 113 | + shouldUnpauseSequentialQueue?: boolean; |
| 114 | +}; |
| 115 | + |
| 116 | +/** |
| 117 | + * Clears the deferred updates queue and unpauses the SequentialQueue |
| 118 | + * @param options additional flags to change the behaviour of this function |
| 119 | + */ |
| 120 | +function clear(options?: ClearDeferredOnyxUpdatesOptions) { |
| 121 | + deferredUpdates = {}; |
| 122 | + |
| 123 | + if (options?.shouldResetGetMissingOnyxUpdatesPromise ?? true) { |
| 124 | + missingOnyxUpdatesQueryPromise = undefined; |
| 125 | + } |
| 126 | + |
| 127 | + if (options?.shouldUnpauseSequentialQueue ?? true) { |
| 128 | + Onyx.set(ONYXKEYS.ONYX_UPDATES_FROM_SERVER, null); |
| 129 | + SequentialQueue.unpause(); |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +export {getMissingOnyxUpdatesQueryPromise, setMissingOnyxUpdatesQueryPromise, getUpdates, isEmpty, process, enqueue, enqueueAndProcess, clear}; |
0 commit comments