Skip to content

feat(v2): single-pass serialization #7526

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions packages/qwik/src/core/client/dom-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ import { assertTrue } from '../shared/error/assert';
import { QError, qError } from '../shared/error/error';
import { ERROR_CONTEXT, isRecoverable } from '../shared/error/error-handling';
import { getPlatform } from '../shared/platform/platform';
import { emitEvent } from '../shared/qrl/qrl-class';
import { emitEvent, type QRLInternal } from '../shared/qrl/qrl-class';
import type { QRL } from '../shared/qrl/qrl.public';
import { ChoreType } from '../shared/util-chore-type';
import { _SharedContainer } from '../shared/shared-container';
import { inflateQRL, parseQRL, wrapDeserializerProxy } from '../shared/shared-serialization';
import {
getObjectById,
inflateQRL,
parseQRL,
preprocessState,
wrapDeserializerProxy,
} from '../shared/shared-serialization';
import { QContainerValue, type HostElement, type ObjToProxyMap } from '../shared/types';
import { EMPTY_ARRAY } from '../shared/utils/flyweight';
import {
Expand Down Expand Up @@ -112,6 +118,8 @@ export class DomContainer extends _SharedContainer implements IClientContainer {
public $storeProxyMap$: ObjToProxyMap = new WeakMap();
public $qFuncs$: Array<(...args: unknown[]) => unknown>;
public $instanceHash$: string;
public $forwardRefs$: Array<number> | null = null;
public $initialQRLsIndexes$: Array<number> | null = null;
public vNodeLocate: (id: string | Element) => VNode = (id) => vnode_locate(this.rootVNode, id);

private $stateData$: unknown[];
Expand Down Expand Up @@ -159,7 +167,9 @@ export class DomContainer extends _SharedContainer implements IClientContainer {
if (qwikStates.length !== 0) {
const lastState = qwikStates[qwikStates.length - 1];
this.$rawStateData$ = JSON.parse(lastState.textContent!);
preprocessState(this.$rawStateData$, this);
this.$stateData$ = wrapDeserializerProxy(this, this.$rawStateData$) as unknown[];
this.$scheduleInitialQRLs$();
}
}

Expand Down Expand Up @@ -316,14 +326,7 @@ export class DomContainer extends _SharedContainer implements IClientContainer {
}

$getObjectById$ = (id: number | string): unknown => {
if (typeof id === 'string') {
id = parseFloat(id);
}
assertTrue(
id < this.$rawStateData$.length / 2,
`Invalid reference: ${id} >= ${this.$rawStateData$.length / 2}`
);
return this.$stateData$[id];
return getObjectById(id, this.$stateData$);
};

getSyncFn(id: number): (...args: unknown[]) => unknown {
Expand Down Expand Up @@ -371,4 +374,17 @@ export class DomContainer extends _SharedContainer implements IClientContainer {
}
this.$serverData$ = { containerAttributes };
}

private $scheduleInitialQRLs$(): void {
if (this.$initialQRLsIndexes$) {
for (const index of this.$initialQRLsIndexes$) {
this.$scheduler$(
ChoreType.QRL_RESOLVE,
null,
this.$getObjectById$(index) as QRLInternal<(...args: unknown[]) => unknown>
);
}
this.$initialQRLsIndexes$ = null;
}
}
}
2 changes: 2 additions & 0 deletions packages/qwik/src/core/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export interface ClientContainer extends Container {
rootVNode: ElementVNode;
$journal$: VNodeJournal;
renderDone: Promise<void> | null;
$forwardRefs$: Array<number> | null;
$initialQRLsIndexes$: Array<number> | null;
parseQRL<T = unknown>(qrl: string): QRL<T>;
$setRawState$(id: number, vParent: ElementVNode | VirtualVNode): void;
}
Expand Down
8 changes: 8 additions & 0 deletions packages/qwik/src/core/core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export type ClassList = string | undefined | null | false | Record<string, boole
//
// @internal (undocumented)
export interface ClientContainer extends Container {
// (undocumented)
$forwardRefs$: Array<number> | null;
// (undocumented)
$initialQRLsIndexes$: Array<number> | null;
// Warning: (ae-forgotten-export) The symbol "VNodeJournal" needs to be exported by the entry point index.d.ts
//
// (undocumented)
Expand Down Expand Up @@ -177,8 +181,12 @@ class DomContainer extends _SharedContainer implements ClientContainer {
// (undocumented)
$appendStyle$(content: string, styleId: string, host: _VirtualVNode, scoped: boolean): void;
// (undocumented)
$forwardRefs$: Array<number> | null;
// (undocumented)
$getObjectById$: (id: number | string) => unknown;
// (undocumented)
$initialQRLsIndexes$: Array<number> | null;
// (undocumented)
$instanceHash$: string;
// (undocumented)
$journal$: VNodeJournal;
Expand Down
Loading