Skip to content

Fix - When creating several rooms via whisper, only one of them appears on "Search" #641

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
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 lib/useOnyx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(
// Inside useOnyx.ts, we need to track the sourceValue separately
const sourceValueRef = useRef<NonNullable<TReturnValue> | undefined>(undefined);

// On every data fetch through this hook, we reset sourceValueRef.current to begin new batching
useEffect(() => {
sourceValueRef.current = undefined;
});

useEffect(() => {
// These conditions will ensure we can only handle dynamic collection member keys from the same collection.
if (options?.allowDynamicKey || previousKey === key) {
Expand Down Expand Up @@ -271,9 +276,9 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(
// For the other cases we will only deal with object reference checks, so just a shallow equality check is enough.
let areValuesEqual: boolean;
if (selectorRef.current) {
areValuesEqual = deepEqual(previousValueRef.current ?? undefined, newValueRef.current);
areValuesEqual = deepEqual(previousValueRef.current ?? undefined, newValueRef.current) && deepEqual(resultRef.current?.[1]?.sourceValue, sourceValueRef.current);
} else {
areValuesEqual = shallowEqual(previousValueRef.current ?? undefined, newValueRef.current);
areValuesEqual = shallowEqual(previousValueRef.current ?? undefined, newValueRef.current) && shallowEqual(resultRef.current?.[1]?.sourceValue, sourceValueRef.current);
}

// We update the cached value and the result in the following conditions:
Expand Down Expand Up @@ -326,7 +331,16 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(
shouldGetCachedValueRef.current = true;

// sourceValue is unknown type, so we need to cast it to the correct type.
sourceValueRef.current = sourceValue as NonNullable<TReturnValue>;
if (sourceValue) {
if (!sourceValueRef.current) {
sourceValueRef.current = sourceValue as NonNullable<TReturnValue>;
} else if (typeof sourceValue === 'object' && typeof sourceValueRef.current === 'object' && !Array.isArray(sourceValue) && !Array.isArray(sourceValueRef.current)) {
// Only objects can be merged together
sourceValueRef.current = {...sourceValueRef.current, ...sourceValue};
} else {
sourceValueRef.current = sourceValue as NonNullable<TReturnValue>;
}
}

// Finally, we signal that the store changed, making `getSnapshot()` be called again.
onStoreChange();
Expand Down
Loading