-
Notifications
You must be signed in to change notification settings - Fork 3k
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 #39683 from margelo/@chrispader/GetMissingOnyxMess…
…ages-deferred-updates-after-revert After revert: Defer local updates if there are missing updates and only call `GetMissingOnyxMessages` once
- Loading branch information
Showing
14 changed files
with
987 additions
and
39 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
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import type {OnyxUpdatesFromServer} from '@src/types/onyx'; | ||
|
||
type DeferredUpdatesDictionary = Record<number, OnyxUpdatesFromServer>; | ||
|
||
type DetectGapAndSplitResult = {applicableUpdates: DeferredUpdatesDictionary; updatesAfterGaps: DeferredUpdatesDictionary; latestMissingUpdateID: number | undefined}; | ||
|
||
export type {DeferredUpdatesDictionary, DetectGapAndSplitResult}; |
34 changes: 34 additions & 0 deletions
34
src/libs/actions/OnyxUpdateManager/utils/__mocks__/applyUpdates.ts
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import Onyx from 'react-native-onyx'; | ||
import type {DeferredUpdatesDictionary} from '@libs/actions/OnyxUpdateManager/types'; | ||
import ONYXKEYS from '@src/ONYXKEYS'; | ||
import createProxyForObject from '@src/utils/createProxyForObject'; | ||
|
||
let lastUpdateIDAppliedToClient = 0; | ||
Onyx.connect({ | ||
key: ONYXKEYS.ONYX_UPDATES_LAST_UPDATE_ID_APPLIED_TO_CLIENT, | ||
callback: (value) => (lastUpdateIDAppliedToClient = value ?? 0), | ||
}); | ||
|
||
type ApplyUpdatesMockValues = { | ||
onApplyUpdates: ((updates: DeferredUpdatesDictionary) => Promise<void>) | undefined; | ||
}; | ||
|
||
type ApplyUpdatesMock = { | ||
applyUpdates: jest.Mock<Promise<[]>, [updates: DeferredUpdatesDictionary]>; | ||
mockValues: ApplyUpdatesMockValues; | ||
}; | ||
|
||
const mockValues: ApplyUpdatesMockValues = { | ||
onApplyUpdates: undefined, | ||
}; | ||
const mockValuesProxy = createProxyForObject(mockValues); | ||
|
||
const applyUpdates = jest.fn((updates: DeferredUpdatesDictionary) => { | ||
const lastUpdateIdFromUpdates = Math.max(...Object.keys(updates).map(Number)); | ||
return (mockValuesProxy.onApplyUpdates === undefined ? Promise.resolve() : mockValuesProxy.onApplyUpdates(updates)).then(() => | ||
Onyx.set(ONYXKEYS.ONYX_UPDATES_LAST_UPDATE_ID_APPLIED_TO_CLIENT, Math.max(lastUpdateIDAppliedToClient, lastUpdateIdFromUpdates)), | ||
); | ||
}); | ||
|
||
export {applyUpdates, mockValuesProxy as mockValues}; | ||
export type {ApplyUpdatesMock}; |
32 changes: 32 additions & 0 deletions
32
src/libs/actions/OnyxUpdateManager/utils/__mocks__/index.ts
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import type {DeferredUpdatesDictionary, DetectGapAndSplitResult} from '@libs/actions/OnyxUpdateManager/types'; | ||
import createProxyForObject from '@src/utils/createProxyForObject'; | ||
import type * as OnyxUpdateManagerUtilsImport from '..'; | ||
import {applyUpdates} from './applyUpdates'; | ||
|
||
const UtilsImplementation: typeof OnyxUpdateManagerUtilsImport = jest.requireActual('@libs/actions/OnyxUpdateManager/utils'); | ||
|
||
type OnyxUpdateManagerUtilsMockValues = { | ||
onValidateAndApplyDeferredUpdates: ((clientLastUpdateID?: number) => Promise<void>) | undefined; | ||
}; | ||
|
||
type OnyxUpdateManagerUtilsMock = typeof UtilsImplementation & { | ||
detectGapsAndSplit: jest.Mock<Promise<DetectGapAndSplitResult>, [updates: DeferredUpdatesDictionary, clientLastUpdateID?: number]>; | ||
validateAndApplyDeferredUpdates: jest.Mock<Promise<void>, [clientLastUpdateID?: number]>; | ||
mockValues: OnyxUpdateManagerUtilsMockValues; | ||
}; | ||
|
||
const mockValues: OnyxUpdateManagerUtilsMockValues = { | ||
onValidateAndApplyDeferredUpdates: undefined, | ||
}; | ||
const mockValuesProxy = createProxyForObject(mockValues); | ||
|
||
const detectGapsAndSplit = jest.fn(UtilsImplementation.detectGapsAndSplit); | ||
|
||
const validateAndApplyDeferredUpdates = jest.fn((clientLastUpdateID?: number) => | ||
(mockValuesProxy.onValidateAndApplyDeferredUpdates === undefined ? Promise.resolve() : mockValuesProxy.onValidateAndApplyDeferredUpdates(clientLastUpdateID)).then(() => | ||
UtilsImplementation.validateAndApplyDeferredUpdates(clientLastUpdateID), | ||
), | ||
); | ||
|
||
export {applyUpdates, detectGapsAndSplit, validateAndApplyDeferredUpdates, mockValuesProxy as mockValues}; | ||
export type {OnyxUpdateManagerUtilsMock}; |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// We need to keep this in a separate file, so that we can mock this function in tests. | ||
import type {DeferredUpdatesDictionary} from '@libs/actions/OnyxUpdateManager/types'; | ||
import * as OnyxUpdates from '@userActions/OnyxUpdates'; | ||
|
||
// This function applies a list of updates to Onyx in order and resolves when all updates have been applied | ||
const applyUpdates = (updates: DeferredUpdatesDictionary) => Promise.all(Object.values(updates).map((update) => OnyxUpdates.apply(update))); | ||
|
||
// eslint-disable-next-line import/prefer-default-export | ||
export {applyUpdates}; |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import type {DeferredUpdatesDictionary} from '@libs/actions/OnyxUpdateManager/types'; | ||
import createProxyForObject from '@src/utils/createProxyForObject'; | ||
|
||
const deferredUpdatesValue = {deferredUpdates: {} as DeferredUpdatesDictionary}; | ||
|
||
const deferredUpdatesProxy = createProxyForObject(deferredUpdatesValue); | ||
|
||
export default deferredUpdatesProxy; |
Oops, something went wrong.