Skip to content

Commit

Permalink
Merge pull request #44987 from fabioh8010/feature/onyx-connection-man…
Browse files Browse the repository at this point in the history
…ager

Bump Onyx to v2.0.65
  • Loading branch information
mountiny authored Aug 23, 2024
2 parents 3e28178 + 4e3a516 commit 1e352fd
Show file tree
Hide file tree
Showing 30 changed files with 433 additions and 429 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@
"react-native-linear-gradient": "^2.8.1",
"react-native-localize": "^2.2.6",
"react-native-modal": "^13.0.0",
"react-native-onyx": "2.0.64",
"react-native-onyx": "2.0.65",
"react-native-pager-view": "6.3.4",
"react-native-pdf": "6.7.3",
"react-native-performance": "^5.1.0",
Expand Down
4 changes: 2 additions & 2 deletions src/libs/E2E/actions/e2eLogin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default function (): Promise<boolean> {

// Subscribe to auth token, to check if we are authenticated
return new Promise((resolve, reject) => {
const connectionId = Onyx.connect({
const connection = Onyx.connect({
key: ONYXKEYS.SESSION,
callback: (session) => {
if (session?.authToken == null || session.authToken.length === 0) {
Expand All @@ -67,7 +67,7 @@ export default function (): Promise<boolean> {
}
// signal that auth was completed
resolve(neededLogin);
Onyx.disconnect(connectionId);
Onyx.disconnect(connection);
},
});
});
Expand Down
4 changes: 2 additions & 2 deletions src/libs/E2E/actions/waitForAppLoaded.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import ONYXKEYS from '@src/ONYXKEYS';
// Once we get the sidebar loaded end mark we know that the app is ready to be used:
export default function waitForAppLoaded(): Promise<void> {
return new Promise((resolve) => {
const connectionId = Onyx.connect({
const connection = Onyx.connect({
key: ONYXKEYS.IS_SIDEBAR_LOADED,
callback: (isSidebarLoaded) => {
if (!isSidebarLoaded) {
return;
}

resolve();
Onyx.disconnect(connectionId);
Onyx.disconnect(connection);
},
});
});
Expand Down
7 changes: 5 additions & 2 deletions src/libs/Network/SequentialQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,13 @@ function flush() {
});

// Ensure persistedRequests are read from storage before proceeding with the queue
const connectionID = Onyx.connect({
const connection = Onyx.connect({
key: ONYXKEYS.PERSISTED_REQUESTS,
// We exceptionally opt out of reusing the connection here to avoid extra callback calls due to
// an existing connection already made in PersistedRequests.ts.
reuseConnection: false,
callback: () => {
Onyx.disconnect(connectionID);
Onyx.disconnect(connection);
process().finally(() => {
Log.info('[SequentialQueue] Finished processing queue.');
isSequentialQueueRunning = false;
Expand Down
4 changes: 2 additions & 2 deletions src/libs/actions/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,11 @@ AppState.addEventListener('change', (nextAppState) => {
function getPolicyParamsForOpenOrReconnect(): Promise<PolicyParamsForOpenOrReconnect> {
return new Promise((resolve) => {
isReadyToOpenApp.then(() => {
const connectionID = Onyx.connect({
const connection = Onyx.connect({
key: ONYXKEYS.COLLECTION.POLICY,
waitForCollectionCallback: true,
callback: (policies) => {
Onyx.disconnect(connectionID);
Onyx.disconnect(connection);
resolve({policyIDList: getNonOptimisticPolicyIDs(policies)});
},
});
Expand Down
4 changes: 2 additions & 2 deletions src/libs/actions/Device/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ function getDeviceID(): Promise<string | null> {
return;
}

const connectionID = Onyx.connect({
const connection = Onyx.connect({
key: ONYXKEYS.DEVICE_ID,
callback: (id) => {
Onyx.disconnect(connectionID);
Onyx.disconnect(connection);
deviceID = id ?? null;
return resolve(id ?? null);
},
Expand Down
4 changes: 2 additions & 2 deletions src/libs/actions/Download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ function setDownload(sourceID: string, isDownloading: boolean): Promise<void | v
}

function clearDownloads() {
const connectionID = Onyx.connect({
const connection = Onyx.connect({
key: ONYXKEYS.COLLECTION.DOWNLOAD,
waitForCollectionCallback: true,
callback: (records) => {
Onyx.disconnect(connectionID);
Onyx.disconnect(connection);
const downloadsToDelete: Record<string, null> = {};
Object.keys(records ?? {}).forEach((recordKey) => {
downloadsToDelete[recordKey] = null;
Expand Down
25 changes: 13 additions & 12 deletions src/libs/actions/MapboxToken.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {isAfter} from 'date-fns';
import type {NativeEventSubscription} from 'react-native';
import {AppState} from 'react-native';
import type {Connection} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
import * as ActiveClientManager from '@libs/ActiveClientManager';
import * as API from '@libs/API';
Expand All @@ -17,8 +18,8 @@ Onyx.connect({
},
});

let connectionIDForToken: number | null;
let connectionIDForNetwork: number | null;
let tokenConnection: Connection | null;
let networkConnection: Connection | null;
let appStateSubscription: NativeEventSubscription | null;
let currentToken: MapboxAccessToken | undefined;
let refreshTimeoutID: NodeJS.Timeout | undefined;
Expand Down Expand Up @@ -57,13 +58,13 @@ const fetchToken = () => {
};

const init = () => {
if (connectionIDForToken) {
if (tokenConnection) {
console.debug('[MapboxToken] init() is already listening to Onyx so returning early');
return;
}

// When the token changes in Onyx, the expiration needs to be checked so a new token can be retrieved.
connectionIDForToken = Onyx.connect({
tokenConnection = Onyx.connect({
key: ONYXKEYS.MAPBOX_ACCESS_TOKEN,
callback: (token) => {
// Only the leader should be in charge of the mapbox token, or else when you have multiple tabs open, the Onyx connection fires multiple times
Expand Down Expand Up @@ -116,9 +117,9 @@ const init = () => {
});
}

if (!connectionIDForNetwork) {
if (!networkConnection) {
let network: Network | undefined;
connectionIDForNetwork = Onyx.connect({
networkConnection = Onyx.connect({
key: ONYXKEYS.NETWORK,
callback: (value) => {
// When the network reconnects, check if the token has expired. If it has, then clearing the token will
Expand All @@ -139,13 +140,13 @@ const init = () => {

const stop = () => {
console.debug('[MapboxToken] Stopping all listeners and timers');
if (connectionIDForToken) {
Onyx.disconnect(connectionIDForToken);
connectionIDForToken = null;
if (tokenConnection) {
Onyx.disconnect(tokenConnection);
tokenConnection = null;
}
if (connectionIDForNetwork) {
Onyx.disconnect(connectionIDForNetwork);
connectionIDForNetwork = null;
if (networkConnection) {
Onyx.disconnect(networkConnection);
networkConnection = null;
}
if (appStateSubscription) {
appStateSubscription.remove();
Expand Down
4 changes: 2 additions & 2 deletions src/libs/actions/TransactionEdit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ function removeBackupTransaction(transactionID: string) {
}

function restoreOriginalTransactionFromBackup(transactionID: string, isDraft: boolean) {
const connectionID = Onyx.connect({
const connection = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.TRANSACTION_BACKUP}${transactionID}`,
callback: (backupTransaction) => {
Onyx.disconnect(connectionID);
Onyx.disconnect(connection);

// Use set to completely overwrite the original transaction
Onyx.set(`${isDraft ? ONYXKEYS.COLLECTION.TRANSACTION_DRAFT : ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, backupTransaction ?? null);
Expand Down
4 changes: 2 additions & 2 deletions src/libs/actions/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -552,10 +552,10 @@ function triggerNotifications(onyxUpdates: OnyxServerUpdate[]) {

const isChannelMuted = (reportId: string) =>
new Promise((resolve) => {
const connectionId = Onyx.connect({
const connection = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.REPORT}${reportId}`,
callback: (report) => {
Onyx.disconnect(connectionId);
Onyx.disconnect(connection);

resolve(
!report?.notificationPreference ||
Expand Down
4 changes: 2 additions & 2 deletions src/libs/migrations/KeyReportActionsDraftByReportActionID.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ type ReportActionsDraftsKey = `${typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFT
*/
export default function () {
return new Promise<void | void[]>((resolve) => {
const connectionID = Onyx.connect({
const connection = Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS,
waitForCollectionCallback: true,
// eslint-disable-next-line @typescript-eslint/no-misused-promises
callback: (allReportActionsDrafts) => {
Onyx.disconnect(connectionID);
Onyx.disconnect(connection);

if (!allReportActionsDrafts) {
Log.info('[Migrate Onyx] Skipped migration KeyReportActionsDraftByReportActionID because there were no reportActionsDrafts');
Expand Down
12 changes: 6 additions & 6 deletions src/libs/migrations/NVPMigration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ export default function () {
const resolveWhenDone = after(Object.entries(migrations).length + 2, () => resolve());

for (const [oldKey, newKey] of Object.entries(migrations)) {
const connectionID = Onyx.connect({
const connection = Onyx.connect({
key: oldKey as OnyxKey,
callback: (value) => {
Onyx.disconnect(connectionID);
Onyx.disconnect(connection);
if (value === undefined) {
resolveWhenDone();
return;
Expand All @@ -43,10 +43,10 @@ export default function () {
},
});
}
const connectionIDAccount = Onyx.connect({
const accountConnection = Onyx.connect({
key: ONYXKEYS.ACCOUNT,
callback: (value: OnyxEntry<Account & {activePolicyID?: string}>) => {
Onyx.disconnect(connectionIDAccount);
Onyx.disconnect(accountConnection);
if (!value?.activePolicyID) {
resolveWhenDone();
return;
Expand All @@ -60,11 +60,11 @@ export default function () {
}).then(resolveWhenDone);
},
});
const connectionIDRecentlyUsedTags = Onyx.connect({
const recentlyUsedTagsConnection = Onyx.connect({
key: ONYXKEYS.COLLECTION.OLD_POLICY_RECENTLY_USED_TAGS,
waitForCollectionCallback: true,
callback: (value) => {
Onyx.disconnect(connectionIDRecentlyUsedTags);
Onyx.disconnect(recentlyUsedTagsConnection);
if (!value) {
resolveWhenDone();
return;
Expand Down
8 changes: 4 additions & 4 deletions src/libs/migrations/PronounsMigration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import type {PersonalDetails as TPersonalDetails} from '@src/types/onyx';

function getCurrentUserAccountIDFromOnyx(): Promise<number> {
return new Promise((resolve) => {
const connectionID = Onyx.connect({
const connection = Onyx.connect({
key: ONYXKEYS.SESSION,
callback: (val) => {
Onyx.disconnect(connectionID);
Onyx.disconnect(connection);
return resolve(val?.accountID ?? -1);
},
});
Expand All @@ -19,10 +19,10 @@ function getCurrentUserAccountIDFromOnyx(): Promise<number> {

function getCurrentUserPersonalDetailsFromOnyx(currentUserAccountID: number): Promise<NonNullable<OnyxEntry<TPersonalDetails>> | null> {
return new Promise((resolve) => {
const connectionID = Onyx.connect({
const connection = Onyx.connect({
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
callback: (val) => {
Onyx.disconnect(connectionID);
Onyx.disconnect(connection);
return resolve(val?.[currentUserAccountID] ?? null);
},
});
Expand Down
4 changes: 2 additions & 2 deletions src/libs/migrations/RemoveEmptyReportActionsDrafts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ type ReportActionsDraftsKey = `${typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFT
*/
export default function (): Promise<void> {
return new Promise<void>((resolve) => {
const connectionID = Onyx.connect({
const connection = Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS,
waitForCollectionCallback: true,
callback: (allReportActionsDrafts) => {
Onyx.disconnect(connectionID);
Onyx.disconnect(connection);

if (!allReportActionsDrafts) {
Log.info('[Migrate Onyx] Skipped migration RemoveEmptyReportActionsDrafts because there were no reportActionsDrafts');
Expand Down
4 changes: 2 additions & 2 deletions src/libs/migrations/RenameCardIsVirtual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ type OldCard = Card & {isVirtual?: boolean};
// This migration changes the property name on each card from card list from isVirtual to nameValuePairs.isVirtual
export default function () {
return new Promise<void>((resolve) => {
const connectionID = Onyx.connect({
const connection = Onyx.connect({
key: ONYXKEYS.CARD_LIST,
callback: (cardList: OnyxEntry<Record<string, OldCard>>) => {
Onyx.disconnect(connectionID);
Onyx.disconnect(connection);

if (!cardList || isEmptyObject(cardList)) {
Log.info('[Migrate Onyx] Skipped migration RenameCardIsVirtual because there are no cards linked to the account');
Expand Down
4 changes: 2 additions & 2 deletions src/libs/migrations/RenameReceiptFilename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ export default function () {
return new Promise<void>((resolve) => {
// Connect to the TRANSACTION collection key in Onyx to get all of the stored transactions.
// Go through each transaction and change the property name
const connectionID = Onyx.connect({
const connection = Onyx.connect({
key: ONYXKEYS.COLLECTION.TRANSACTION,
waitForCollectionCallback: true,
callback: (transactions: OnyxCollection<OldTransaction>) => {
Onyx.disconnect(connectionID);
Onyx.disconnect(connection);

if (!transactions || isEmptyObject(transactions)) {
Log.info('[Migrate Onyx] Skipped migration RenameReceiptFilename because there are no transactions');
Expand Down
4 changes: 2 additions & 2 deletions src/libs/migrations/TransactionBackupsToCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import type {Transaction} from '@src/types/onyx';
*/
export default function (): Promise<void> {
return new Promise<void>((resolve) => {
const connectionID = Onyx.connect({
const connection = Onyx.connect({
key: ONYXKEYS.COLLECTION.TRANSACTION,
waitForCollectionCallback: true,
callback: (transactions: OnyxCollection<Transaction>) => {
Onyx.disconnect(connectionID);
Onyx.disconnect(connection);

// Determine whether any transactions were stored
if (!transactions || Object.keys(transactions).length === 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/libs/onyxSubscribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import type {OnyxCollectionKey, OnyxKey} from '@src/ONYXKEYS';
* @return Unsubscribe callback
*/
function onyxSubscribe<TKey extends OnyxKey | `${OnyxCollectionKey}${string}`>(mapping: ConnectOptions<TKey>) {
const connectionId = Onyx.connect(mapping);
return () => Onyx.disconnect(connectionId);
const connection = Onyx.connect(mapping);
return () => Onyx.disconnect(connection);
}

export default onyxSubscribe;
4 changes: 2 additions & 2 deletions src/setup/addUtilsToWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ export default function addUtilsToWindow() {
window.Onyx.get = function (key) {
return new Promise((resolve) => {
// eslint-disable-next-line rulesdir/prefer-onyx-connect-in-libs
const connectionID = Onyx.connect({
const connection = Onyx.connect({
key,
callback: (value) => {
Onyx.disconnect(connectionID);
Onyx.disconnect(connection);
resolve(value);
},
waitForCollectionCallback: true,
Expand Down
Loading

0 comments on commit 1e352fd

Please sign in to comment.