Skip to content

Commit

Permalink
Introduced StringSerializer for strings that start with the prefixes …
Browse files Browse the repository at this point in the history
…of serializers.
  • Loading branch information
genki committed Sep 19, 2023
1 parent c42dc77 commit 5d85187
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 25 deletions.
6 changes: 1 addition & 5 deletions packages/qwik/src/core/container/pause.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
import { isArray, isObject, isSerializableObject } from '../util/types';
import { directGetAttribute, directSetAttribute } from '../render/fast-calls';
import { isNotNullable, isPromise } from '../util/promises';
import { collectDeps, serializeValue, UNDEFINED_PREFIX, NOSERIALIZE_PREFIX } from './serializers';
import { collectDeps, serializeValue, UNDEFINED_PREFIX } from './serializers';
import {
type ContainerState,
FILTER_REJECT,
Expand Down Expand Up @@ -138,8 +138,6 @@ export const _serializeData = async (data: any, pureQRL?: boolean) => {
break;
}
return obj;
case 'string':
return NOSERIALIZE_PREFIX + obj;
case 'boolean':
return obj;
}
Expand Down Expand Up @@ -482,8 +480,6 @@ export const _pauseFromContexts = async (
break;
}
return obj;
case 'string':
return NOSERIALIZE_PREFIX + obj;
case 'boolean':
return obj;
}
Expand Down
22 changes: 3 additions & 19 deletions packages/qwik/src/core/container/resume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@ import { emitEvent } from '../util/event';

import { isArray, isSerializableObject, isString } from '../util/types';
import { directGetAttribute, directSetAttribute } from '../render/fast-calls';
import {
createParser,
OBJECT_TRANSFORMS,
type Parser,
UNDEFINED_PREFIX,
NOSERIALIZE_PREFIX,
} from './serializers';
import { createParser, OBJECT_TRANSFORMS, type Parser, UNDEFINED_PREFIX } from './serializers';
import {
type ContainerState,
_getContainerState,
Expand Down Expand Up @@ -82,12 +76,7 @@ export const _deserializeData = (data: string, element?: unknown) => {
for (let i = 0; i < _objs.length; i++) {
const value = _objs[i];
if (isString(value)) {
_objs[i] =
value === UNDEFINED_PREFIX
? undefined
: value.startsWith(NOSERIALIZE_PREFIX)
? value.slice(1)
: parser.prepare(value);
_objs[i] = value === UNDEFINED_PREFIX ? undefined : parser.prepare(value);
}
}

Expand Down Expand Up @@ -239,12 +228,7 @@ export const resumeContainer = (containerEl: Element) => {
assertTrue(objs.length > index, 'resume: index is out of bounds', id);
let value = objs[index];
if (isString(value)) {
value =
value === UNDEFINED_PREFIX
? undefined
: value.startsWith(NOSERIALIZE_PREFIX)
? value.slice(1)
: parser.prepare(value);
value = value === UNDEFINED_PREFIX ? undefined : parser.prepare(value);
}
let obj = value;
for (let i = id.length - 1; i >= 0; i--) {
Expand Down
15 changes: 14 additions & 1 deletion packages/qwik/src/core/container/serializers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ import { Slot } from '../render/jsx/slot.public';
\': single quote (U+0027 APOSTROPHE)
\\: backslash (U+005C REVERSE SOLIDUS)
*/
export const NOSERIALIZE_PREFIX = '\u0000';
export const UNDEFINED_PREFIX = '\u0001';

export interface Serializer<T> {
Expand Down Expand Up @@ -486,7 +485,16 @@ const MapSerializer: Serializer<Map<any, any>> = {
},
};

const StringSerializer: Serializer<string> = {
$prefix$: '\u001b',
$test$: (v) => typeof v === 'string' && prefixes.has(v[0]),
$serialize$: (v) => v,
$prepare$: (data) => data,
$fill$: undefined,
};

const serializers: Serializer<any>[] = [
StringSerializer, /////////// \u001b
QRLSerializer, ////////////// \u0002
SignalSerializer, /////////// \u0012
SignalWrapperSerializer, //// \u0013
Expand All @@ -508,6 +516,8 @@ const serializers: Serializer<any>[] = [
DocumentSerializer, ///////// \u000F
];

const prefixes = new Set([UNDEFINED_PREFIX, ...serializers.map((a) => a.$prefix$)]);

const collectorSerializers = /*#__PURE__*/ serializers.filter((a) => a.$collect$);

export const canSerialize = (obj: any): boolean => {
Expand Down Expand Up @@ -544,6 +554,9 @@ export const serializeValue = (
return value;
}
}
if (typeof obj === 'string') {
return obj;
}
return undefined;
};

Expand Down

0 comments on commit 5d85187

Please sign in to comment.