Skip to content

Commit c71c1db

Browse files
committed
Improve comments
1 parent f4a643b commit c71c1db

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

lib/OnyxConnectionManager.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,17 @@ class OnyxConnectionManager {
7474
private lastCallbackID: number;
7575

7676
/**
77-
* Stores the last generated session ID for the connection manager.
77+
* Stores the last generated session ID for the connection manager. The current session ID
78+
* is appended to the connection IDs and it's used to create new different connections for the same key
79+
* when `refreshSessionID()` is called.
80+
*
81+
* When calling `Onyx.clear()` after a logout operation some connections might remain active as they
82+
* aren't tied to the React's lifecycle e.g. `Onyx.connect()` usage, causing infinite loading state issues to new `useOnyx()` subscribers
83+
* that are connecting to the same key as we didn't populate the cache again because we are still reusing such connections.
84+
*
85+
* To elimitate this problem, the session ID must be refreshed during the `Onyx.clear()` call (by using `refreshSessionID()`)
86+
* in order to create fresh connections when new subscribers connect to the same keys again, allowing them
87+
* to use the cache system correctly and avoid the mentioned issues in `useOnyx()`.
7888
*/
7989
private sessionID: string;
8090

@@ -97,7 +107,7 @@ class OnyxConnectionManager {
97107
const {key, initWithStoredValues, reuseConnection, waitForCollectionCallback} = connectOptions;
98108

99109
// The current session ID is appended to the connection ID so we can have different connections
100-
// after an Onyx.clear() operation.
110+
// after an `Onyx.clear()` operation.
101111
let suffix = `,sessionID=${this.sessionID}`;
102112

103113
// We will generate a unique ID in any of the following situations:
@@ -239,9 +249,7 @@ class OnyxConnectionManager {
239249
}
240250

241251
/**
242-
* Refreshes the connection manager's session ID. Used in Onyx.clear() in order to
243-
* create new connections after Onyx is cleared, instead of reusing existing connections
244-
* that can produce unexpected behavior in Onyx subscribers.
252+
* Refreshes the connection manager's session ID.
245253
*/
246254
refreshSessionID(): void {
247255
this.sessionID = Str.guid();

lib/useOnyx.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,14 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(key: TKey
236236
areValuesEqual = shallowEqual(previousValueRef.current ?? undefined, newValueRef.current);
237237
}
238238

239-
// If the previously cached value is different from the new value, we update both cached value
240-
// and the result to be returned by the hook.
241-
// If the cache was set for the first time or we have a pending Onyx.clear() task, we also update the cached value and the result.
242-
const isCacheSetFirstTime = previousValueRef.current === null && (hasCacheForKey || OnyxCache.hasPendingTask(TASK.CLEAR));
243-
if (isCacheSetFirstTime || !areValuesEqual) {
239+
// We updated the cached value and the result in the following conditions:
240+
// We will update the cached value and the result in any of the following situations:
241+
// - The previously cached value is different from the new value.
242+
// - The previously cached value is `null` (not set from cache yet) and we have cache for this key
243+
// OR we have a pending `Onyx.clear()` task (if `Onyx.clear()` is running cache might not be available anymore
244+
// so we update the cached value/result right away in order to prevent infinite loading state issues).
245+
const shouldUpdateResult = !areValuesEqual || (previousValueRef.current === null && (hasCacheForKey || OnyxCache.hasPendingTask(TASK.CLEAR)));
246+
if (shouldUpdateResult) {
244247
previousValueRef.current = newValueRef.current;
245248

246249
// If the new value is `null` we default it to `undefined` to ensure the consumer gets a consistent result from the hook.

0 commit comments

Comments
 (0)