@@ -224,6 +224,7 @@ const inflate = (
224
224
}
225
225
break ;
226
226
case TypeIds . QRL :
227
+ case TypeIds . PreloadQRL :
227
228
inflateQRL ( container , target ) ;
228
229
break ;
229
230
case TypeIds . Task :
@@ -462,6 +463,7 @@ const allocate = (container: DeserializeContainer, typeId: number, value: unknow
462
463
case TypeIds . Object :
463
464
return { } ;
464
465
case TypeIds . QRL :
466
+ case TypeIds . PreloadQRL :
465
467
const qrl =
466
468
typeof value === 'number'
467
469
? // root reference
@@ -859,30 +861,15 @@ const discoverValuesForVNodeData = (vnodeData: VNodeData, callback: (value: unkn
859
861
860
862
class PromiseResult {
861
863
constructor (
862
- public $resolved$ : boolean ,
863
- public $value$ : unknown
864
- ) { }
865
- }
866
-
867
- class ResourceResult extends PromiseResult {
868
- constructor (
864
+ public $type$ : number ,
869
865
public $resolved$ : boolean ,
870
866
public $value$ : unknown ,
871
- public $effects$ : Map < string | symbol , Set < EffectSubscription > > | null
872
- ) {
873
- super ( $resolved$ , $value$ ) ;
874
- }
875
- }
876
-
877
- class SerializerResult extends PromiseResult {
878
- constructor (
879
- public $resolved$ : boolean ,
880
- public $value$ : unknown ,
881
- public $effects$ : null | Set < EffectSubscription > ,
882
- public $qrl$ : QRLInternal | null
883
- ) {
884
- super ( $resolved$ , $value$ ) ;
885
- }
867
+ public $effects$ :
868
+ | Map < string | symbol , Set < EffectSubscription > >
869
+ | Set < EffectSubscription >
870
+ | null = null ,
871
+ public $qrl$ : QRLInternal | null = null
872
+ ) { }
886
873
}
887
874
/**
888
875
* Format:
@@ -900,7 +887,9 @@ async function serialize(serializationContext: SerializationContext): Promise<vo
900
887
const forwardRefs : number [ ] = [ ] ;
901
888
let forwardRefsId = 0 ;
902
889
const promises : Set < Promise < unknown > > = new Set ( ) ;
890
+ const preloadQrls = new Set < QRLInternal > ( ) ;
903
891
let parent : unknown = null ;
892
+ const isRootObject = ( ) => depth === 0 ;
904
893
905
894
const outputArray = ( value : unknown [ ] , writeFn : ( value : unknown , idx : number ) => void ) => {
906
895
$writer$ . write ( '[' ) ;
@@ -941,6 +930,23 @@ async function serialize(serializationContext: SerializationContext): Promise<vo
941
930
}
942
931
} ;
943
932
933
+ const addPreloadQrl = ( qrl : QRLInternal ) => {
934
+ preloadQrls . add ( qrl ) ;
935
+ serializationContext . $addRoot$ ( qrl , null ) ;
936
+ } ;
937
+
938
+ const outputRootRef = ( value : unknown , elseCallback : ( ) => void ) => {
939
+ const seen = $wasSeen$ ( value ) ;
940
+ const rootRefPath = $pathMap$ . get ( value ) ;
941
+ if ( isRootObject ( ) && seen && seen . $parent$ !== null && rootRefPath ) {
942
+ output ( TypeIds . RootRef , rootRefPath ) ;
943
+ } else if ( depth > 0 && seen && seen . $rootIndex$ !== - 1 ) {
944
+ output ( TypeIds . RootRef , seen . $rootIndex$ ) ;
945
+ } else {
946
+ elseCallback ( ) ;
947
+ }
948
+ } ;
949
+
944
950
const writeValue = ( value : unknown ) => {
945
951
if ( fastSkipSerialize ( value as object ) ) {
946
952
output ( TypeIds . Constant , Constants . Undefined ) ;
@@ -954,29 +960,16 @@ async function serialize(serializationContext: SerializationContext): Promise<vo
954
960
} else if ( value === Fragment ) {
955
961
output ( TypeIds . Constant , Constants . Fragment ) ;
956
962
} else if ( isQrl ( value ) ) {
957
- // TODO: this is the same logic as for strings
958
- const seen = $wasSeen$ ( value ) ;
959
- const rootRefPath = $pathMap$ . get ( value ) ;
960
- const isRootObject = depth === 0 ;
961
- if (
962
- isRootObject &&
963
- seen &&
964
- seen . $parent$ !== null &&
965
- rootRefPath
966
- // && rootRefPathIsShorterThanObject
967
- ) {
968
- output ( TypeIds . RootRef , rootRefPath ) ;
969
- } else if ( depth > 0 && seen && seen . $rootIndex$ !== - 1 ) {
970
- output ( TypeIds . RootRef , seen . $rootIndex$ ) ;
971
- } else {
963
+ outputRootRef ( value , ( ) => {
972
964
const qrl = qrlToString ( serializationContext , value ) ;
973
- if ( ! isRootObject ) {
974
- const id = serializationContext . $addRoot$ ( qrl ) ;
975
- output ( TypeIds . QRL , id ) ;
965
+ const type = preloadQrls . has ( value ) ? TypeIds . PreloadQRL : TypeIds . QRL ;
966
+ if ( isRootObject ( ) ) {
967
+ output ( type , qrl ) ;
976
968
} else {
977
- output ( TypeIds . QRL , qrl ) ;
969
+ const id = serializationContext . $addRoot$ ( qrl ) ;
970
+ output ( type , id ) ;
978
971
}
979
- }
972
+ } ) ;
980
973
} else if ( isQwikComponent ( value ) ) {
981
974
const [ qrl ] : [ QRLInternal ] = ( value as any ) [ SERIALIZABLE_STATE ] ;
982
975
serializationContext . $renderSymbols$ . add ( qrl . $symbol$ ) ;
@@ -1022,27 +1015,9 @@ async function serialize(serializationContext: SerializationContext): Promise<vo
1022
1015
if ( value . length === 0 ) {
1023
1016
output ( TypeIds . Constant , Constants . EmptyString ) ;
1024
1017
} else {
1025
- // const rootRefPathIsShorterThanObject =
1026
- // typeof rootRefPath === 'string'
1027
- // ? // check if calculated root ref path is shorter than the object
1028
- // rootRefPath.length <= value.length
1029
- // : true;
1030
- const seen = $wasSeen$ ( value ) ;
1031
- const rootRefPath = $pathMap$ . get ( value ) ;
1032
- const isRootObject = depth === 0 ;
1033
- if (
1034
- isRootObject &&
1035
- seen &&
1036
- seen . $parent$ !== null &&
1037
- rootRefPath
1038
- // && rootRefPathIsShorterThanObject
1039
- ) {
1040
- output ( TypeIds . RootRef , rootRefPath ) ;
1041
- } else if ( depth > 0 && seen && seen . $rootIndex$ !== - 1 ) {
1042
- output ( TypeIds . RootRef , seen . $rootIndex$ ) ;
1043
- } else {
1018
+ outputRootRef ( value , ( ) => {
1044
1019
output ( TypeIds . String , value ) ;
1045
- }
1020
+ } ) ;
1046
1021
}
1047
1022
} else if ( typeof value === 'undefined' ) {
1048
1023
output ( TypeIds . Constant , Constants . Undefined ) ;
@@ -1099,7 +1074,12 @@ async function serialize(serializationContext: SerializationContext): Promise<vo
1099
1074
serializationContext . $resources$ . add ( value ) ;
1100
1075
// TODO the effects include the resource return which has duplicate data
1101
1076
const forwardRefId = $resolvePromise$ ( value . value , $addRoot$ , ( resolved , resolvedValue ) => {
1102
- return new ResourceResult ( resolved , resolvedValue , getStoreHandler ( value ) ! . $effects$ ) ;
1077
+ return new PromiseResult (
1078
+ TypeIds . Resource ,
1079
+ resolved ,
1080
+ resolvedValue ,
1081
+ getStoreHandler ( value ) ! . $effects$
1082
+ ) ;
1103
1083
} ) ;
1104
1084
output ( TypeIds . ForwardRef , forwardRefId ) ;
1105
1085
} else {
@@ -1128,7 +1108,7 @@ async function serialize(serializationContext: SerializationContext): Promise<vo
1128
1108
const result = value [ SerializerSymbol ] ( value ) ;
1129
1109
if ( isPromise ( result ) ) {
1130
1110
const forwardRef = $resolvePromise$ ( result , $addRoot$ , ( resolved , resolvedValue ) => {
1131
- return new SerializerResult ( resolved , resolvedValue , null , null ) ;
1111
+ return new PromiseResult ( TypeIds . SerializerSignal , resolved , resolvedValue , null , null ) ;
1132
1112
} ) ;
1133
1113
output ( TypeIds . ForwardRef , forwardRef ) ;
1134
1114
} else {
@@ -1157,11 +1137,13 @@ async function serialize(serializationContext: SerializationContext): Promise<vo
1157
1137
output ( TypeIds . RefVNode , value . $ssrNode$ . id ) ;
1158
1138
} else if ( value instanceof SignalImpl ) {
1159
1139
if ( value instanceof SerializerSignalImpl ) {
1140
+ addPreloadQrl ( value . $computeQrl$ ) ;
1160
1141
const forwardRefId = $resolvePromise$ (
1161
1142
$getCustomSerializerPromise$ ( value , value . $untrackedValue$ ) ,
1162
1143
$addRoot$ ,
1163
1144
( resolved , resolvedValue ) => {
1164
- return new SerializerResult (
1145
+ return new PromiseResult (
1146
+ TypeIds . SerializerSignal ,
1165
1147
resolved ,
1166
1148
resolvedValue ,
1167
1149
value . $effects$ ,
@@ -1191,7 +1173,7 @@ async function serialize(serializationContext: SerializationContext): Promise<vo
1191
1173
...( value . $effects$ || [ ] ) ,
1192
1174
] ) ;
1193
1175
} else if ( value instanceof ComputedSignalImpl ) {
1194
- serializationContext . $addRoot$ ( value . $computeQrl$ , null ) ;
1176
+ addPreloadQrl ( value . $computeQrl$ ) ;
1195
1177
const out : [ QRLInternal , Set < EffectSubscription > | null , unknown ?] = [
1196
1178
value . $computeQrl$ ,
1197
1179
// TODO check if we can use domVRef for effects
@@ -1281,13 +1263,13 @@ async function serialize(serializationContext: SerializationContext): Promise<vo
1281
1263
output ( TypeIds . Task , out ) ;
1282
1264
} else if ( isPromise ( value ) ) {
1283
1265
const forwardRefId = $resolvePromise$ ( value , $addRoot$ , ( resolved , resolvedValue ) => {
1284
- return new PromiseResult ( resolved , resolvedValue ) ;
1266
+ return new PromiseResult ( TypeIds . Promise , resolved , resolvedValue ) ;
1285
1267
} ) ;
1286
1268
output ( TypeIds . ForwardRef , forwardRefId ) ;
1287
1269
} else if ( value instanceof PromiseResult ) {
1288
- if ( value instanceof ResourceResult ) {
1270
+ if ( value . $type$ === TypeIds . Resource ) {
1289
1271
output ( TypeIds . Resource , [ value . $resolved$ , value . $value$ , value . $effects$ ] ) ;
1290
- } else if ( value instanceof SerializerResult ) {
1272
+ } else if ( value . $type$ === TypeIds . SerializerSignal ) {
1291
1273
if ( value . $qrl$ ) {
1292
1274
output ( TypeIds . SerializerSignal , [ value . $qrl$ , value . $effects$ , value . $value$ ] ) ;
1293
1275
} else if ( value . $resolved$ ) {
@@ -1634,8 +1616,8 @@ export function preprocessState(data: unknown[], container: DeserializeContainer
1634
1616
return type === TypeIds . ForwardRefs ;
1635
1617
} ;
1636
1618
1637
- const isQrlType = ( type : TypeIds ) => {
1638
- return type === TypeIds . QRL ;
1619
+ const isPreloadQrlType = ( type : TypeIds ) => {
1620
+ return type === TypeIds . PreloadQRL ;
1639
1621
} ;
1640
1622
1641
1623
const processRootRef = ( index : number ) => {
@@ -1676,7 +1658,7 @@ export function preprocessState(data: unknown[], container: DeserializeContainer
1676
1658
processRootRef ( i ) ;
1677
1659
} else if ( isForwardRefsMap ( data [ i ] as TypeIds ) ) {
1678
1660
container . $forwardRefs$ = data [ i + 1 ] as number [ ] ;
1679
- } else if ( isQrlType ( data [ i ] as TypeIds ) ) {
1661
+ } else if ( isPreloadQrlType ( data [ i ] as TypeIds ) ) {
1680
1662
container . $initialQRLsIndexes$ ||= [ ] ;
1681
1663
container . $initialQRLsIndexes$ . push ( i / 2 ) ;
1682
1664
}
@@ -1841,6 +1823,7 @@ export const enum TypeIds {
1841
1823
Map ,
1842
1824
Uint8Array ,
1843
1825
QRL ,
1826
+ PreloadQRL ,
1844
1827
Task ,
1845
1828
Resource ,
1846
1829
Component ,
@@ -1877,6 +1860,7 @@ export const _typeIdNames = [
1877
1860
'Map' ,
1878
1861
'Uint8Array' ,
1879
1862
'QRL' ,
1863
+ 'PreloadQRL' ,
1880
1864
'Task' ,
1881
1865
'Resource' ,
1882
1866
'Component' ,
0 commit comments