@@ -453,6 +453,7 @@ function keysChanged<TKey extends CollectionKeyBase>(
453
453
// We prepare the "cached collection" which is the entire collection + the new partial data that
454
454
// was merged in via mergeCollection().
455
455
const cachedCollection = getCachedCollection ( collectionKey ) ;
456
+ const cachedCollectionWithoutNestedNulls = utils . removeNestedNullValues ( cachedCollection ) as Record < string , unknown > ;
456
457
457
458
// Regular Onyx.connect() subscriber found.
458
459
if ( typeof subscriber . callback === 'function' ) {
@@ -464,7 +465,7 @@ function keysChanged<TKey extends CollectionKeyBase>(
464
465
// send the whole cached collection.
465
466
if ( isSubscribedToCollectionKey ) {
466
467
if ( subscriber . waitForCollectionCallback ) {
467
- subscriber . callback ( cachedCollection ) ;
468
+ subscriber . callback ( cachedCollectionWithoutNestedNulls ) ;
468
469
continue ;
469
470
}
470
471
@@ -473,7 +474,7 @@ function keysChanged<TKey extends CollectionKeyBase>(
473
474
const dataKeys = Object . keys ( partialCollection ?? { } ) ;
474
475
for ( let j = 0 ; j < dataKeys . length ; j ++ ) {
475
476
const dataKey = dataKeys [ j ] ;
476
- subscriber . callback ( cachedCollection [ dataKey ] , dataKey ) ;
477
+ subscriber . callback ( cachedCollectionWithoutNestedNulls [ dataKey ] , dataKey ) ;
477
478
}
478
479
continue ;
479
480
}
@@ -482,7 +483,7 @@ function keysChanged<TKey extends CollectionKeyBase>(
482
483
// notify them with the cached data for that key only.
483
484
if ( isSubscribedToCollectionMemberKey ) {
484
485
const subscriberCallback = subscriber . callback as DefaultConnectCallback < TKey > ;
485
- subscriberCallback ( cachedCollection [ subscriber . key ] , subscriber . key as TKey ) ;
486
+ subscriberCallback ( cachedCollectionWithoutNestedNulls [ subscriber . key ] , subscriber . key as TKey ) ;
486
487
continue ;
487
488
}
488
489
@@ -621,13 +622,16 @@ function keyChanged<TKey extends OnyxKey>(
621
622
}
622
623
if ( isCollectionKey ( subscriber . key ) && subscriber . waitForCollectionCallback ) {
623
624
const cachedCollection = getCachedCollection ( subscriber . key ) ;
624
- cachedCollection [ key ] = data ;
625
- subscriber . callback ( cachedCollection ) ;
625
+ const cachedCollectionWithoutNestedNulls = utils . removeNestedNullValues ( cachedCollection ) as Record < string , unknown > ;
626
+
627
+ cachedCollectionWithoutNestedNulls [ key ] = data ;
628
+ subscriber . callback ( cachedCollectionWithoutNestedNulls ) ;
626
629
continue ;
627
630
}
628
631
632
+ const dataWithoutNestedNulls = utils . removeNestedNullValues ( data ) ;
629
633
const subscriberCallback = subscriber . callback as DefaultConnectCallback < TKey > ;
630
- subscriberCallback ( data , key ) ;
634
+ subscriberCallback ( dataWithoutNestedNulls , key ) ;
631
635
continue ;
632
636
}
633
637
@@ -752,7 +756,8 @@ function sendDataToConnection<TKey extends OnyxKey>(mapping: Mapping<TKey>, val:
752
756
return ;
753
757
}
754
758
755
- ( mapping as DefaultConnectOptions < TKey > ) . callback ?.( val , matchedKey as TKey ) ;
759
+ const valuesWithoutNestedNulls = utils . removeNestedNullValues ( val ) ;
760
+ ( mapping as DefaultConnectOptions < TKey > ) . callback ?.( valuesWithoutNestedNulls , matchedKey as TKey ) ;
756
761
}
757
762
758
763
/**
@@ -963,11 +968,12 @@ type RemoveNullValuesOutput = {
963
968
964
969
/**
965
970
* Removes a key from storage if the value is null.
966
- * Otherwise removes all nested null values in objects and returns the object
971
+ * Otherwise removes all nested null values in objects,
972
+ * if shouldRemoveNestedNulls is true and returns the object.
967
973
*
968
974
* @returns The value without null values and a boolean "wasRemoved", which indicates if the key got removed completely
969
975
*/
970
- function removeNullValues ( key : OnyxKey , value : OnyxValue < OnyxKey > ) : RemoveNullValuesOutput {
976
+ function removeNullValues ( key : OnyxKey , value : OnyxValue < OnyxKey > , shouldRemoveNestedNulls = true ) : RemoveNullValuesOutput {
971
977
if ( value === null ) {
972
978
remove ( key ) ;
973
979
return { value, wasRemoved : true } ;
@@ -976,7 +982,7 @@ function removeNullValues(key: OnyxKey, value: OnyxValue<OnyxKey>): RemoveNullVa
976
982
// We can remove all null values in an object by merging it with itself
977
983
// utils.fastMerge recursively goes through the object and removes all null values
978
984
// Passing two identical objects as source and target to fastMerge will not change it, but only remove the null values
979
- return { value : utils . removeNestedNullValues ( value as Record < string , unknown > ) , wasRemoved : false } ;
985
+ return { value : shouldRemoveNestedNulls ? utils . removeNestedNullValues ( value as Record < string , unknown > ) : ( value as Record < string , unknown > ) , wasRemoved : false } ;
980
986
}
981
987
982
988
/**
@@ -986,28 +992,24 @@ function removeNullValues(key: OnyxKey, value: OnyxValue<OnyxKey>): RemoveNullVa
986
992
987
993
* @return an array of key - value pairs <[key, value]>
988
994
*/
989
- function prepareKeyValuePairsForStorage ( data : Record < OnyxKey , OnyxValue < OnyxKey > > ) : Array < [ OnyxKey , OnyxValue < OnyxKey > ] > {
990
- const keyValuePairs : Array < [ OnyxKey , OnyxValue < OnyxKey > ] > = [ ] ;
991
-
992
- Object . entries ( data ) . forEach ( ( [ key , value ] ) => {
993
- const { value : valueAfterRemoving , wasRemoved} = removeNullValues ( key , value ) ;
995
+ function prepareKeyValuePairsForStorage ( data : Record < OnyxKey , OnyxValue < OnyxKey > > , shouldRemoveNestedNulls : boolean ) : Array < [ OnyxKey , OnyxValue < OnyxKey > ] > {
996
+ return Object . entries ( data ) . reduce < Array < [ OnyxKey , OnyxValue < OnyxKey > ] > > ( ( pairs , [ key , value ] ) => {
997
+ const { value : valueAfterRemoving , wasRemoved} = removeNullValues ( key , value , shouldRemoveNestedNulls ) ;
994
998
995
- if ( wasRemoved ) {
996
- return ;
999
+ if ( ! wasRemoved ) {
1000
+ pairs . push ( [ key , valueAfterRemoving ] ) ;
997
1001
}
998
1002
999
- keyValuePairs . push ( [ key , valueAfterRemoving ] ) ;
1000
- } ) ;
1001
-
1002
- return keyValuePairs ;
1003
+ return pairs ;
1004
+ } , [ ] ) ;
1003
1005
}
1004
1006
1005
1007
/**
1006
1008
* Merges an array of changes with an existing value
1007
1009
*
1008
1010
* @param changes Array of changes that should be applied to the existing value
1009
1011
*/
1010
- function applyMerge ( existingValue : OnyxValue < OnyxKey > , changes : Array < OnyxValue < OnyxKey > > , shouldRemoveNullObjectValues : boolean ) : OnyxValue < OnyxKey > {
1012
+ function applyMerge ( existingValue : OnyxValue < OnyxKey > , changes : Array < OnyxValue < OnyxKey > > , shouldRemoveNestedNulls : boolean ) : OnyxValue < OnyxKey > {
1011
1013
const lastChange = changes ?. at ( - 1 ) ;
1012
1014
1013
1015
if ( Array . isArray ( lastChange ) ) {
@@ -1017,7 +1019,7 @@ function applyMerge(existingValue: OnyxValue<OnyxKey>, changes: Array<OnyxValue<
1017
1019
if ( changes . some ( ( change ) => change && typeof change === 'object' ) ) {
1018
1020
// Object values are then merged one after the other
1019
1021
return changes . reduce (
1020
- ( modifiedData , change ) => utils . fastMerge ( modifiedData as Record < OnyxKey , unknown > , change as Record < OnyxKey , unknown > , shouldRemoveNullObjectValues ) ,
1022
+ ( modifiedData , change ) => utils . fastMerge ( modifiedData as Record < OnyxKey , unknown > , change as Record < OnyxKey , unknown > , shouldRemoveNestedNulls ) ,
1021
1023
existingValue || { } ,
1022
1024
) ;
1023
1025
}
0 commit comments