@@ -67,6 +67,10 @@ import { SerializerSignalImpl } from '../reactive-primitives/impl/serializer-sig
67
67
import { AsyncComputedSignalImpl } from '../reactive-primitives/impl/async-computed-signal-impl' ;
68
68
import { isObject } from './utils/types' ;
69
69
70
+ /** Arrays/Objects are special-cased so their identifiers is a single digit. */
71
+ const needsInflation = ( typeId : TypeIds ) =>
72
+ typeId >= TypeIds . Error || typeId === TypeIds . Array || typeId === TypeIds . Object ;
73
+
70
74
const deserializedProxyMap = new WeakMap < object , unknown [ ] > ( ) ;
71
75
72
76
type DeserializerProxy < T extends object = object > = T & { [ SERIALIZER_PROXY_UNWRAP ] : object } ;
@@ -122,7 +126,7 @@ class DeserializationHandler implements ProxyHandler<object> {
122
126
const idx = i * 2 ;
123
127
const typeId = this . $data$ [ idx ] as number ;
124
128
const value = this . $data$ [ idx + 1 ] ;
125
- if ( typeId === undefined ) {
129
+ if ( typeId === TypeIds . Plain ) {
126
130
// The value is already cached
127
131
return value ;
128
132
}
@@ -131,14 +135,11 @@ class DeserializationHandler implements ProxyHandler<object> {
131
135
const propValue = allocate ( container , typeId , value ) ;
132
136
133
137
Reflect . set ( target , property , propValue ) ;
134
- this . $data$ [ idx ] = undefined ;
138
+ this . $data$ [ idx ] = TypeIds . Plain ;
135
139
this . $data$ [ idx + 1 ] = propValue ;
136
140
137
- /**
138
- * We stored the reference, so now we can inflate, allowing cycles. Arrays are special-cased so
139
- * their identifiers is a single digit.
140
- */
141
- if ( typeId === TypeIds . Array || typeId >= TypeIds . Error ) {
141
+ /** We stored the reference, so now we can inflate, allowing cycles */
142
+ if ( needsInflation ( typeId ) ) {
142
143
inflate ( container , propValue , typeId , value ) ;
143
144
}
144
145
@@ -162,7 +163,7 @@ class DeserializationHandler implements ProxyHandler<object> {
162
163
return out ;
163
164
}
164
165
const idx = i * 2 ;
165
- this . $data$ [ idx ] = undefined ;
166
+ this . $data$ [ idx ] = TypeIds . Plain ;
166
167
this . $data$ [ idx + 1 ] = value ;
167
168
return true ;
168
169
}
@@ -191,7 +192,7 @@ const inflate = (
191
192
typeId : TypeIds ,
192
193
data : unknown
193
194
) : void => {
194
- if ( typeId === undefined ) {
195
+ if ( typeId === TypeIds . Plain ) {
195
196
// Already processed
196
197
return ;
197
198
}
@@ -458,7 +459,7 @@ const _constantNames = [
458
459
] as const ;
459
460
460
461
const allocate = ( container : DeserializeContainer , typeId : number , value : unknown ) : any => {
461
- if ( typeId === undefined ) {
462
+ if ( typeId === TypeIds . Plain ) {
462
463
return value ;
463
464
}
464
465
switch ( typeId ) {
@@ -478,8 +479,6 @@ const allocate = (container: DeserializeContainer, typeId: number, value: unknow
478
479
return value ;
479
480
case TypeIds . Constant :
480
481
return _constants [ value as Constants ] ;
481
- case TypeIds . Number :
482
- return value as number ;
483
482
case TypeIds . Array :
484
483
// Wrap while inflating so we can handle cyclic references
485
484
return wrapDeserializerProxy ( container as any , value as any [ ] ) ;
@@ -541,6 +540,8 @@ const allocate = (container: DeserializeContainer, typeId: number, value: unknow
541
540
( value as any [ ] ) [ 0 ] as TypeIds ,
542
541
( value as any [ ] ) [ 1 ]
543
542
) ;
543
+ ( value as any [ ] ) [ 0 ] = TypeIds . Plain ;
544
+ ( value as any [ ] ) [ 1 ] = storeValue ;
544
545
return getOrCreateStore ( storeValue , StoreFlags . NONE , container as DomContainer ) ;
545
546
case TypeIds . URLSearchParams :
546
547
return new URLSearchParams ( value as string ) ;
@@ -554,8 +555,6 @@ const allocate = (container: DeserializeContainer, typeId: number, value: unknow
554
555
return new Set ( ) ;
555
556
case TypeIds . Map :
556
557
return new Map ( ) ;
557
- case TypeIds . String :
558
- return value as string ;
559
558
case TypeIds . Promise :
560
559
let resolve ! : ( value : any ) => void ;
561
560
let reject ! : ( error : any ) => void ;
@@ -1096,7 +1095,7 @@ async function serialize(serializationContext: SerializationContext): Promise<vo
1096
1095
} else if ( value === Number . MIN_SAFE_INTEGER ) {
1097
1096
output ( TypeIds . Constant , Constants . MinSafeInt ) ;
1098
1097
} else {
1099
- output ( TypeIds . Number , value ) ;
1098
+ output ( TypeIds . Plain , value ) ;
1100
1099
}
1101
1100
} else if ( typeof value === 'object' ) {
1102
1101
if ( value === EMPTY_ARRAY ) {
@@ -1118,7 +1117,7 @@ async function serialize(serializationContext: SerializationContext): Promise<vo
1118
1117
output ( TypeIds . Constant , Constants . EmptyString ) ;
1119
1118
} else {
1120
1119
if ( ! outputAsRootRef ( value ) ) {
1121
- output ( TypeIds . String , value ) ;
1120
+ output ( TypeIds . Plain , value ) ;
1122
1121
}
1123
1122
}
1124
1123
} else if ( typeof value === 'undefined' ) {
@@ -1640,11 +1639,11 @@ export function _deserialize(rawStateData: string | null, element?: unknown): un
1640
1639
}
1641
1640
1642
1641
function deserializeData ( container : DeserializeContainer , typeId : number , value : unknown ) {
1643
- if ( typeId === undefined ) {
1642
+ if ( typeId === TypeIds . Plain ) {
1644
1643
return value ;
1645
1644
}
1646
1645
const propValue = allocate ( container , typeId , value ) ;
1647
- if ( typeId >= TypeIds . Error ) {
1646
+ if ( needsInflation ( typeId ) ) {
1648
1647
inflate ( container , propValue , typeId , value ) ;
1649
1648
}
1650
1649
return propValue ;
@@ -1925,24 +1924,24 @@ export const canSerialize = (value: any, seen: WeakSet<any> = new WeakSet()): bo
1925
1924
const QRL_RUNTIME_CHUNK = 'mock-chunk' ;
1926
1925
1927
1926
export const enum TypeIds {
1927
+ Plain ,
1928
1928
RootRef ,
1929
1929
ForwardRef ,
1930
- ForwardRefs ,
1931
1930
/** Undefined, null, true, false, NaN, +Inf, -Inf, Slot, Fragment */
1932
1931
Constant ,
1933
- Number ,
1934
- String ,
1935
1932
Array ,
1933
+ Object ,
1936
1934
URL ,
1937
1935
Date ,
1938
1936
Regex ,
1939
1937
VNode ,
1938
+ /// ^ single-digit types ^
1940
1939
RefVNode ,
1941
1940
BigInt ,
1942
1941
URLSearchParams ,
1943
- /// All values below need inflation because they may have reference cycles
1942
+ ForwardRefs ,
1943
+ /// All types below will be inflate()d
1944
1944
Error ,
1945
- Object ,
1946
1945
Promise ,
1947
1946
Set ,
1948
1947
Map ,
@@ -1964,22 +1963,21 @@ export const enum TypeIds {
1964
1963
SubscriptionData ,
1965
1964
}
1966
1965
export const _typeIdNames = [
1966
+ 'Plain' ,
1967
1967
'RootRef' ,
1968
1968
'ForwardRef' ,
1969
- 'ForwardRefs' ,
1970
1969
'Constant' ,
1971
- 'Number' ,
1972
- 'String' ,
1973
1970
'Array' ,
1971
+ 'Object' ,
1974
1972
'URL' ,
1975
1973
'Date' ,
1976
1974
'Regex' ,
1977
1975
'VNode' ,
1978
1976
'RefVNode' ,
1979
1977
'BigInt' ,
1980
1978
'URLSearchParams' ,
1979
+ 'ForwardRefs' ,
1981
1980
'Error' ,
1982
- 'Object' ,
1983
1981
'Promise' ,
1984
1982
'Set' ,
1985
1983
'Map' ,
@@ -2070,11 +2068,15 @@ export const dumpState = (
2070
2068
}
2071
2069
const key = state [ i ] ;
2072
2070
let value = state [ ++ i ] ;
2073
- if ( key === undefined ) {
2074
- hasRaw = true ;
2075
- out . push (
2076
- `${ RED } [raw${ isObject ( value ) ? ` ${ value . constructor . name } ` : '' } ]${ RESET } ${ printRaw ( value , `${ prefix } ` ) } `
2077
- ) ;
2071
+ if ( key === TypeIds . Plain ) {
2072
+ const isRaw = typeof value !== 'number' && typeof value !== 'string' ;
2073
+ if ( isRaw ) {
2074
+ hasRaw = true ;
2075
+ }
2076
+ const type = isRaw
2077
+ ? `[raw${ isObject ( value ) ? ` ${ value . constructor . name } ` : '' } ]`
2078
+ : typeIdToName ( key as TypeIds ) ;
2079
+ out . push ( `${ RED } ${ type } ${ RESET } ${ printRaw ( value , `${ prefix } ` ) } ` ) ;
2078
2080
} else {
2079
2081
if ( key === TypeIds . Constant ) {
2080
2082
value = constantToName ( value as Constants ) ;
0 commit comments