@@ -933,20 +933,21 @@ async function serialize(serializationContext: SerializationContext): Promise<vo
933
933
serializationContext . $addRoot$ ( qrl , null ) ;
934
934
} ;
935
935
936
- const outputRootRef = ( value : unknown , elseCallback : ( ) => void ) => {
936
+ const outputRootRef = ( value : unknown , rootDepth = 0 ) => {
937
937
const seen = $wasSeen$ ( value ) ;
938
938
const rootRefPath = $pathMap$ . get ( value ) ;
939
- if ( isRootObject ( ) && seen && seen . $parent$ !== null && rootRefPath ) {
939
+ if ( rootDepth === depth && seen && seen . $parent$ !== null && rootRefPath ) {
940
940
output ( TypeIds . RootRef , rootRefPath ) ;
941
- } else if ( depth > 0 && seen && seen . $rootIndex$ !== - 1 ) {
941
+ return true ;
942
+ } else if ( depth > rootDepth && seen && seen . $rootIndex$ !== - 1 ) {
942
943
output ( TypeIds . RootRef , seen . $rootIndex$ ) ;
943
- } else {
944
- elseCallback ( ) ;
944
+ return true ;
945
945
}
946
+ return false ;
946
947
} ;
947
948
948
949
const writeValue = ( value : unknown ) => {
949
- if ( fastSkipSerialize ( value as object ) ) {
950
+ if ( fastSkipSerialize ( value as object | Function ) ) {
950
951
output ( TypeIds . Constant , Constants . Undefined ) ;
951
952
} else if ( typeof value === 'bigint' ) {
952
953
output ( TypeIds . BigInt , value . toString ( ) ) ;
@@ -958,7 +959,7 @@ async function serialize(serializationContext: SerializationContext): Promise<vo
958
959
} else if ( value === Fragment ) {
959
960
output ( TypeIds . Constant , Constants . Fragment ) ;
960
961
} else if ( isQrl ( value ) ) {
961
- outputRootRef ( value , ( ) => {
962
+ if ( ! outputRootRef ( value ) ) {
962
963
const qrl = qrlToString ( serializationContext , value ) ;
963
964
const type = preloadQrls . has ( value ) ? TypeIds . PreloadQRL : TypeIds . QRL ;
964
965
if ( isRootObject ( ) ) {
@@ -967,7 +968,7 @@ async function serialize(serializationContext: SerializationContext): Promise<vo
967
968
const id = serializationContext . $addRoot$ ( qrl ) ;
968
969
output ( type , id ) ;
969
970
}
970
- } ) ;
971
+ }
971
972
} else if ( isQwikComponent ( value ) ) {
972
973
const [ qrl ] : [ QRLInternal ] = ( value as any ) [ SERIALIZABLE_STATE ] ;
973
974
serializationContext . $renderSymbols$ . add ( qrl . $symbol$ ) ;
@@ -1011,9 +1012,9 @@ async function serialize(serializationContext: SerializationContext): Promise<vo
1011
1012
if ( value . length === 0 ) {
1012
1013
output ( TypeIds . Constant , Constants . EmptyString ) ;
1013
1014
} else {
1014
- outputRootRef ( value , ( ) => {
1015
+ if ( ! outputRootRef ( value ) ) {
1015
1016
output ( TypeIds . String , value ) ;
1016
- } ) ;
1017
+ }
1017
1018
}
1018
1019
} else if ( typeof value === 'undefined' ) {
1019
1020
output ( TypeIds . Constant , Constants . Undefined ) ;
@@ -1031,28 +1032,15 @@ async function serialize(serializationContext: SerializationContext): Promise<vo
1031
1032
* The object writer outputs an array object (without type prefix) and this increases the depth
1032
1033
* for the objects within (depth 1).
1033
1034
*/
1034
- const isRootObject = depth === 1 ;
1035
1035
// Objects are the only way to create circular dependencies.
1036
1036
// So the first thing to to is to see if we have a circular dependency.
1037
1037
// (NOTE: For root objects we need to serialize them regardless if we have seen
1038
1038
// them before, otherwise the root object reference will point to itself.)
1039
1039
// Also note that depth will be 1 for objects in root
1040
- if ( isRootObject ) {
1041
- const seen = $wasSeen$ ( value ) ;
1042
- const rootPath = $pathMap$ . get ( value ) ;
1043
- if ( rootPath && seen && seen . $parent$ !== null ) {
1044
- output ( TypeIds . RootRef , rootPath ) ;
1045
- return ;
1046
- }
1047
- } else if ( depth > 1 ) {
1048
- const seen = $wasSeen$ ( value ) ;
1049
- if ( seen && seen . $rootIndex$ !== - 1 ) {
1050
- // We have seen this object before, so we can serialize it as a reference.
1051
- // Otherwise serialize as normal
1052
- output ( TypeIds . RootRef , seen . $rootIndex$ ) ;
1053
- return ;
1054
- }
1040
+ if ( outputRootRef ( value , 1 ) ) {
1041
+ return ;
1055
1042
}
1043
+
1056
1044
if ( isPropsProxy ( value ) ) {
1057
1045
const varProps = value [ _VAR_PROPS ] ;
1058
1046
const constProps = value [ _CONST_PROPS ] ;
0 commit comments