Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/libs/API/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import {readUpdateIDFrom, resolveDuplicationConflictAction, resolveEnableFeatureConflicts, resolveReconnectDuplicationConflictAction} from '@libs/actions/RequestConflictUtils';
import type {AnyRequestMatcher, EnablePolicyFeatureCommand} from '@libs/actions/RequestConflictUtils';
import Log from '@libs/Log';
import {FailureTracking, handleDeletedAccount, HandleUnusedOptimisticID, LoadTest, Logging, Pagination, Reauthentication, SaveResponseInOnyx, SupportalPermission} from '@libs/Middleware';
import {
FailureTracking,
handleDeletedAccount,
HandleUnusedOptimisticID,
LoadTest,
Logging,
Pagination,
Reauthentication,
RecordFullReconnectTime,
SaveResponseInOnyx,
SupportalPermission,
} from '@libs/Middleware';
import FraudMonitoring from '@libs/Middleware/FraudMonitoring';
import SentryServerTiming from '@libs/Middleware/SentryServerTiming';
import {push as pushToSequentialQueue, waitForIdle as waitForSequentialQueueIdle} from '@libs/Network/SequentialQueue';
Expand Down Expand Up @@ -56,8 +67,11 @@ addMiddleware(Pagination);
// SentryServerTiming - Tracks server round-trip time for configured command groups via Sentry spans.
addMiddleware(SentryServerTiming);

// SaveResponseInOnyx - Merges either the successData or failureData (or finallyData, if included in place of the former two values) into Onyx depending on if the call was successful or not. This needs to be the LAST middleware we use, don't add any
// middlewares after this, because the SequentialQueue depends on the result of this middleware to pause the queue (if needed) to bring the app to an up-to-date state.
// RecordFullReconnectTime - Records the full-reconnect time into an OpenApp/full-ReconnectApp response. Must run before SaveResponseInOnyx applies the response.
addMiddleware(RecordFullReconnectTime);

// SaveResponseInOnyx - Merges either the successData or failureData (or finallyData, if included in place of the former two values) into Onyx depending on if the call was successful or not. This must be the last middleware that applies Onyx data
// (middlewares after it, like FraudMonitoring, must not write Onyx), because the SequentialQueue depends on the result of this middleware to pause the queue (if needed) to bring the app to an up-to-date state.
addMiddleware(SaveResponseInOnyx);

// FraudMonitoring - Tags the request with the appropriate Fraud Protection event.
Expand Down
44 changes: 43 additions & 1 deletion src/libs/FullReconnectUtils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import ONYXKEYS from '@src/ONYXKEYS';
import type {AnyOnyxUpdate} from '@src/types/onyx/Request';

import Onyx from 'react-native-onyx';

import DateUtils from './DateUtils';

/**
Expand All @@ -10,6 +15,22 @@ import DateUtils from './DateUtils';
* so comparing them as strings is correct.
*/

// The cutoff the client currently holds. Consumers that only need the value (not the change event,
// which subscribeToFullReconnect owns) read it through getServerReconnectCutoff instead of opening
// their own connection. Nothing in the UI shows it, so connectWithoutView is correct here. Do not
// copy this into a component: use useOnyx there so the UI updates when the value changes.
let currentServerReconnectCutoff = '';
Onyx.connectWithoutView({
Comment thread
adhorodyski marked this conversation as resolved.
Resolved
key: ONYXKEYS.NVP_RECONNECT_APP_IF_FULL_RECONNECT_BEFORE,
callback: (value) => {
currentServerReconnectCutoff = value ?? '';
},
});

function getServerReconnectCutoff(): string {
return currentServerReconnectCutoff;
}

/**
* An empty last reconnect time means the app has never reconnected, so this returns true. An empty
* cutoff means the server has not asked for one, so this returns false.
Expand All @@ -32,4 +53,25 @@ function getLastFullReconnectTimeToRecord(serverReconnectCutoff: string): string
return now >= serverReconnectCutoff ? now : serverReconnectCutoff;
}

export {shouldTriggerFullReconnect, getLastFullReconnectTimeToRecord};
/**
* The response can deliver a newer cutoff than the one known when the request was built, and the
* held cutoff can be newer still (a Pusher update can overtake an in-flight response), so the
* recorded time satisfies whichever of the two is later. A time below either would read as stale
* and fire an extra reconnect right after the full download.
*
* The entry goes right before the delivered cutoff entry, so the reconnect subscription never sees
* a new cutoff next to an old reconnect time. That assumes Onyx broadcasts a batch's merges in
* array order: true today because both keys are cache-resident (see subscribeToFullReconnect.ts),
* and pinned by the SubscribeToFullReconnect e2e test. If it ever broke, the worst case is one
* transient extra reconnect, never a loop.
Comment on lines +62 to +66

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it ever broke, the worst case is one transient extra reconnect, never a loop.

The extra reconnect caused a visible UI issue, fixed by PR #100547.

*/
function recordFullReconnectTimeFromResponse(responseOnyxData: AnyOnyxUpdate[], knownServerReconnectCutoff: string): void {
const cutoffIndex = responseOnyxData.findIndex((update) => update.key === ONYXKEYS.NVP_RECONNECT_APP_IF_FULL_RECONNECT_BEFORE);
const deliveredCutoffValue: unknown = cutoffIndex === -1 ? undefined : responseOnyxData.at(cutoffIndex)?.value;
const deliveredCutoff = typeof deliveredCutoffValue === 'string' ? deliveredCutoffValue : '';
const cutoffToSatisfy = deliveredCutoff > knownServerReconnectCutoff ? deliveredCutoff : knownServerReconnectCutoff;
const insertionIndex = cutoffIndex === -1 ? responseOnyxData.length : cutoffIndex;
responseOnyxData.splice(insertionIndex, 0, {onyxMethod: Onyx.METHOD.MERGE, key: ONYXKEYS.LAST_FULL_RECONNECT_TIME, value: getLastFullReconnectTimeToRecord(cutoffToSatisfy)});
}

export {shouldTriggerFullReconnect, getLastFullReconnectTimeToRecord, getServerReconnectCutoff, recordFullReconnectTimeFromResponse};
26 changes: 26 additions & 0 deletions src/libs/Middleware/RecordFullReconnectTime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {isFullDownloadRequest} from '@libs/actions/RequestConflictUtils';
import {getServerReconnectCutoff, recordFullReconnectTimeFromResponse} from '@libs/FullReconnectUtils';

import CONST from '@src/CONST';
import type {AnyOnyxUpdate} from '@src/types/onyx/Request';

import type Middleware from './types';

/**
* Records LAST_FULL_RECONNECT_TIME on every successful full-download (OpenApp/full-ReconnectApp)
* response. The time is recorded from the response rather than computed when the request is
* built, because the response can itself deliver a newer reconnect cutoff — a build-time value
* can land below it and fire extra full reconnects right after downloading everything.
*/
const recordFullReconnectTime: Middleware = (requestResponse, request) =>
Comment thread
adhorodyski marked this conversation as resolved.
Resolved
requestResponse.then((response) => {
if (!isFullDownloadRequest(request) || response?.jsonCode !== CONST.JSON_CODE.SUCCESS) {
return response;
}

response.onyxData ??= [];
recordFullReconnectTimeFromResponse(response.onyxData as AnyOnyxUpdate[], getServerReconnectCutoff());
return response;
});

export default recordFullReconnectTime;
3 changes: 2 additions & 1 deletion src/libs/Middleware/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import LoadTest from './LoadTest';
import Logging from './Logging';
import {Pagination} from './Pagination';
import Reauthentication from './Reauthentication';
import RecordFullReconnectTime from './RecordFullReconnectTime';
import SaveResponseInOnyx from './SaveResponseInOnyx';
import SupportalPermission from './SupportalPermission';

export {HandleUnusedOptimisticID, LoadTest, Logging, Reauthentication, FailureTracking, SaveResponseInOnyx, Pagination, handleDeletedAccount, SupportalPermission};
export {HandleUnusedOptimisticID, LoadTest, Logging, Reauthentication, RecordFullReconnectTime, FailureTracking, SaveResponseInOnyx, Pagination, handleDeletedAccount, SupportalPermission};
35 changes: 2 additions & 33 deletions src/libs/actions/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,6 @@ Onyx.connectWithoutView({
},
});

// The server's cutoff for a full reconnect (see subscribeToFullReconnect). We read it only when
// building reconnectApp's data, never in a component, so connectWithoutView is correct here. Do not
// copy this into a component: use useOnyx there so the UI updates when the value changes.
let serverReconnectCutoff = '';
Onyx.connectWithoutView({
key: ONYXKEYS.NVP_RECONNECT_APP_IF_FULL_RECONNECT_BEFORE,
callback: (value) => {
serverReconnectCutoff = value ?? '';
},
});

// allReports and allPolicies are used in the "ForOpenOrReconnect" functions and are not directly associated with the View,
// so retrieving them using Onyx.connectWithoutView is correct.
let allReports: OnyxCollection<OnyxTypes.Report>;
Expand Down Expand Up @@ -312,12 +301,7 @@ function getPolicyParamsForOpenOrReconnect(): PolicyParamsForOpenOrReconnect {
return {policyIDList: getNonOptimisticPolicyIDs(allPolicies)};
}

type OnyxDataForOpenOrReconnectKeys =
| typeof ONYXKEYS.COLLECTION.REPORT
| typeof ONYXKEYS.IS_LOADING_REPORT_DATA
| typeof ONYXKEYS.HAS_LOADED_APP
| typeof ONYXKEYS.IS_LOADING_APP
| typeof ONYXKEYS.LAST_FULL_RECONNECT_TIME;
type OnyxDataForOpenOrReconnectKeys = typeof ONYXKEYS.COLLECTION.REPORT | typeof ONYXKEYS.IS_LOADING_REPORT_DATA | typeof ONYXKEYS.HAS_LOADED_APP | typeof ONYXKEYS.IS_LOADING_APP;

/**
* Returns the Onyx data that is used for both the OpenApp and ReconnectApp API commands.
Expand All @@ -339,13 +323,7 @@ function getOnyxDataForOpenOrReconnect(
}
Log.info(`[App] isLoadingReportData set to true`, false, {command: commandName});

const result: OnyxData<
| typeof ONYXKEYS.IS_LOADING_REPORT_DATA
| typeof ONYXKEYS.HAS_LOADED_APP
| typeof ONYXKEYS.IS_LOADING_APP
| typeof ONYXKEYS.COLLECTION.REPORT
| typeof ONYXKEYS.LAST_FULL_RECONNECT_TIME
> = {
const result: OnyxData<OnyxDataForOpenOrReconnectKeys> = {
optimisticData: [
{
onyxMethod: Onyx.METHOD.MERGE,
Expand Down Expand Up @@ -393,15 +371,6 @@ function getOnyxDataForOpenOrReconnect(
});
}

if (isOpenApp || isFullReconnect) {
// Record this reconnect so subscribeToFullReconnect stops asking for another one.
result.successData?.push({
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.LAST_FULL_RECONNECT_TIME,
value: getLastFullReconnectTimeToRecord(serverReconnectCutoff),
});
}

if (shouldKeepPublicRooms) {
const publicReports = Object.values(allReports ?? {}).filter((report) => isPublicRoom(report) && isValidReport(report));
if (publicReports) {
Expand Down
5 changes: 5 additions & 0 deletions src/libs/actions/RequestConflictUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ function reconnectCoverageFrom(request: AnyRequest): number {
return typeof updateIDFrom === 'number' ? updateIDFrom : 0;
}

function isFullDownloadRequest(request: AnyRequest): boolean {
return isReconnectFamilyRequest(request) && reconnectCoverageFrom(request) === 0;
}

/**
* Coverage-based duplicate resolver for an incoming ReconnectApp. See the Conflict Resolution section of
* contributingGuides/SEQUENTIAL_QUEUE.md for how coverage and the ongoing-request check decide the outcome.
Expand Down Expand Up @@ -353,6 +357,7 @@ export {
resolveOpenReportDuplicationConflictAction,
resolveReconnectDuplicationConflictAction,
readUpdateIDFrom,
isFullDownloadRequest,
resolveCommentDeletionConflicts,
resolveEditCommentWithNewAddCommentRequest,
createUpdateCommentMatcher,
Expand Down
76 changes: 75 additions & 1 deletion tests/unit/FullReconnectUtilsTest.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
import DateUtils from '@libs/DateUtils';
import {getLastFullReconnectTimeToRecord, shouldTriggerFullReconnect} from '@libs/FullReconnectUtils';
import {getLastFullReconnectTimeToRecord, recordFullReconnectTimeFromResponse, shouldTriggerFullReconnect} from '@libs/FullReconnectUtils';

import ONYXKEYS from '@src/ONYXKEYS';
import type {AnyOnyxUpdate} from '@src/types/onyx/Request';

import Onyx from 'react-native-onyx';

const CLIENT_NOW = '2026-06-12 10:00:00.000';
const BEFORE_CLIENT_NOW = '2026-06-12 09:59:00.000';
const AFTER_CLIENT_NOW = '2026-06-12 10:05:00.000';

function cutoffEntry(cutoff: string): AnyOnyxUpdate {
return {onyxMethod: Onyx.METHOD.MERGE, key: ONYXKEYS.NVP_RECONNECT_APP_IF_FULL_RECONNECT_BEFORE, value: cutoff};
}

function recordedTimeEntry(time: string): AnyOnyxUpdate {
return {onyxMethod: Onyx.METHOD.MERGE, key: ONYXKEYS.LAST_FULL_RECONNECT_TIME, value: time};
}

function someOtherEntry(): AnyOnyxUpdate {
return {onyxMethod: Onyx.METHOD.MERGE, key: ONYXKEYS.HAS_LOADED_APP, value: true};
}

describe('FullReconnectUtils', () => {
describe('shouldTriggerFullReconnect', () => {
test.each([
Expand Down Expand Up @@ -43,4 +60,61 @@ describe('FullReconnectUtils', () => {
}
});
});

describe('recordFullReconnectTimeFromResponse', () => {
beforeEach(() => {
jest.spyOn(DateUtils, 'getDBTime').mockReturnValue(CLIENT_NOW);
});

afterEach(() => {
jest.restoreAllMocks();
});

it('records the reconnect time right before the delivered cutoff', () => {
const responseOnyxData = [someOtherEntry(), cutoffEntry(AFTER_CLIENT_NOW)];

recordFullReconnectTimeFromResponse(responseOnyxData, '');

expect(responseOnyxData).toEqual([someOtherEntry(), recordedTimeEntry(AFTER_CLIENT_NOW), cutoffEntry(AFTER_CLIENT_NOW)]);
});

it('records client-now when the response delivers no cutoff and none is held', () => {
const responseOnyxData = [someOtherEntry()];

recordFullReconnectTimeFromResponse(responseOnyxData, '');

expect(responseOnyxData).toEqual([someOtherEntry(), recordedTimeEntry(CLIENT_NOW)]);
});

it('records the held cutoff when the response delivers no cutoff, so an already-held cutoff never reads as stale again', () => {
const responseOnyxData = [someOtherEntry()];

recordFullReconnectTimeFromResponse(responseOnyxData, AFTER_CLIENT_NOW);

expect(responseOnyxData).toEqual([someOtherEntry(), recordedTimeEntry(AFTER_CLIENT_NOW)]);
});

it('records the held cutoff when the response delivers an older one, so a Pusher update that overtook the response never reads as stale again', () => {
const responseOnyxData = [cutoffEntry(BEFORE_CLIENT_NOW)];

recordFullReconnectTimeFromResponse(responseOnyxData, AFTER_CLIENT_NOW);

expect(responseOnyxData).toEqual([recordedTimeEntry(AFTER_CLIENT_NOW), cutoffEntry(BEFORE_CLIENT_NOW)]);
});

it('never records a time that would ask for another reconnect at the delivered or the held cutoff', () => {
for (const deliveredCutoff of ['', BEFORE_CLIENT_NOW, CLIENT_NOW, AFTER_CLIENT_NOW]) {
for (const heldCutoff of ['', BEFORE_CLIENT_NOW, CLIENT_NOW, AFTER_CLIENT_NOW]) {
const responseOnyxData = deliveredCutoff === '' ? [] : [cutoffEntry(deliveredCutoff)];

recordFullReconnectTimeFromResponse(responseOnyxData, heldCutoff);

const recordedTime: unknown = responseOnyxData.at(0)?.value;
const recorded = typeof recordedTime === 'string' ? recordedTime : '';
expect(shouldTriggerFullReconnect(recorded, deliveredCutoff)).toBe(false);
expect(shouldTriggerFullReconnect(recorded, heldCutoff)).toBe(false);
}
}
});
});
});
92 changes: 92 additions & 0 deletions tests/unit/RecordFullReconnectTimeMiddlewareTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import DateUtils from '@libs/DateUtils';
import recordFullReconnectTime from '@libs/Middleware/RecordFullReconnectTime';

import ONYXKEYS from '@src/ONYXKEYS';
import type Request from '@src/types/onyx/Request';
import type {AnyOnyxUpdate} from '@src/types/onyx/Request';
import type Response from '@src/types/onyx/Response';

import type {OnyxKey} from 'react-native-onyx';

import Onyx from 'react-native-onyx';

const CLIENT_NOW = '2026-06-12 10:00:00.000';
const DELIVERED_CUTOFF = '2026-06-12 10:05:00.000';

function cutoffEntry(cutoff: string): AnyOnyxUpdate {
return {onyxMethod: Onyx.METHOD.MERGE, key: ONYXKEYS.NVP_RECONNECT_APP_IF_FULL_RECONNECT_BEFORE, value: cutoff};
}

function recordedTimeEntry(time: string): AnyOnyxUpdate {
return {onyxMethod: Onyx.METHOD.MERGE, key: ONYXKEYS.LAST_FULL_RECONNECT_TIME, value: time};
}

function buildRequest(command: string, data: Record<string, unknown> = {}): Request<OnyxKey> {
return {command, data};
}

function buildResponse(jsonCode: number): Response<OnyxKey> {
return {jsonCode, onyxData: [cutoffEntry(DELIVERED_CUTOFF)]};
}

describe('RecordFullReconnectTime middleware', () => {
beforeEach(() => {
jest.spyOn(DateUtils, 'getDBTime').mockReturnValue(CLIENT_NOW);
});

afterEach(() => {
jest.restoreAllMocks();
});

test.each(['OpenApp', 'ReconnectApp'])('records the reconnect time from the cutoff a %s response delivers', async (command) => {
const request = buildRequest(command);
const response = buildResponse(200);

await recordFullReconnectTime(Promise.resolve(response), request, false);

expect(response.onyxData).toEqual([recordedTimeEntry(DELIVERED_CUTOFF), cutoffEntry(DELIVERED_CUTOFF)]);
});

it('records nothing for a partial ReconnectApp, one that fetches from an update ID', async () => {
const request = buildRequest('ReconnectApp', {updateIDFrom: 123});
const response = buildResponse(200);

await recordFullReconnectTime(Promise.resolve(response), request, false);

expect(response.onyxData).toEqual([cutoffEntry(DELIVERED_CUTOFF)]);
});

it('records client-now when a successful response carries no onyxData at all', async () => {
const request = buildRequest('OpenApp');
const response: Response<OnyxKey> = {jsonCode: 200};

await recordFullReconnectTime(Promise.resolve(response), request, false);

expect(response.onyxData).toEqual([recordedTimeEntry(CLIENT_NOW)]);
});

test.each(['OpenReport', 'GetMissingOnyxMessages'])('leaves a %s response untouched', async (command) => {
const request = buildRequest(command, {updateIDFrom: 5, updateIDTo: 9});
const response = buildResponse(200);

await recordFullReconnectTime(Promise.resolve(response), request, false);

expect(response.onyxData).toEqual([cutoffEntry(DELIVERED_CUTOFF)]);
});

it('leaves failed responses untouched', async () => {
const request = buildRequest('OpenApp');
const response = buildResponse(407);

await recordFullReconnectTime(Promise.resolve(response), request, false);

expect(response.onyxData).toEqual([cutoffEntry(DELIVERED_CUTOFF)]);
});

it('passes the response through unchanged as the middleware result', async () => {
const request = buildRequest('OpenApp');
const response = buildResponse(200);

await expect(recordFullReconnectTime(Promise.resolve(response), request, false)).resolves.toBe(response);
});
});
Loading
Loading