Skip to content

Commit a14b9ff

Browse files
authored
Merge pull request #7526 from QwikDev/v2-forward-ref
feat(v2): single-pass serialization
2 parents 9f12547 + beb5f5a commit a14b9ff

File tree

11 files changed

+829
-514
lines changed

11 files changed

+829
-514
lines changed

packages/qwik/src/core/client/dom-container.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@ import { assertTrue } from '../shared/error/assert';
44
import { QError, qError } from '../shared/error/error';
55
import { ERROR_CONTEXT, isRecoverable } from '../shared/error/error-handling';
66
import { getPlatform } from '../shared/platform/platform';
7-
import { emitEvent } from '../shared/qrl/qrl-class';
7+
import { emitEvent, type QRLInternal } from '../shared/qrl/qrl-class';
88
import type { QRL } from '../shared/qrl/qrl.public';
99
import { ChoreType } from '../shared/util-chore-type';
1010
import { _SharedContainer } from '../shared/shared-container';
11-
import { inflateQRL, parseQRL, wrapDeserializerProxy } from '../shared/shared-serialization';
11+
import {
12+
getObjectById,
13+
inflateQRL,
14+
parseQRL,
15+
preprocessState,
16+
wrapDeserializerProxy,
17+
} from '../shared/shared-serialization';
1218
import { QContainerValue, type HostElement, type ObjToProxyMap } from '../shared/types';
1319
import { EMPTY_ARRAY } from '../shared/utils/flyweight';
1420
import {
@@ -112,6 +118,8 @@ export class DomContainer extends _SharedContainer implements IClientContainer {
112118
public $storeProxyMap$: ObjToProxyMap = new WeakMap();
113119
public $qFuncs$: Array<(...args: unknown[]) => unknown>;
114120
public $instanceHash$: string;
121+
public $forwardRefs$: Array<number> | null = null;
122+
public $initialQRLsIndexes$: Array<number> | null = null;
115123
public vNodeLocate: (id: string | Element) => VNode = (id) => vnode_locate(this.rootVNode, id);
116124

117125
private $stateData$: unknown[];
@@ -159,7 +167,9 @@ export class DomContainer extends _SharedContainer implements IClientContainer {
159167
if (qwikStates.length !== 0) {
160168
const lastState = qwikStates[qwikStates.length - 1];
161169
this.$rawStateData$ = JSON.parse(lastState.textContent!);
170+
preprocessState(this.$rawStateData$, this);
162171
this.$stateData$ = wrapDeserializerProxy(this, this.$rawStateData$) as unknown[];
172+
this.$scheduleInitialQRLs$();
163173
}
164174
}
165175

@@ -316,14 +326,7 @@ export class DomContainer extends _SharedContainer implements IClientContainer {
316326
}
317327

318328
$getObjectById$ = (id: number | string): unknown => {
319-
if (typeof id === 'string') {
320-
id = parseFloat(id);
321-
}
322-
assertTrue(
323-
id < this.$rawStateData$.length / 2,
324-
`Invalid reference: ${id} >= ${this.$rawStateData$.length / 2}`
325-
);
326-
return this.$stateData$[id];
329+
return getObjectById(id, this.$stateData$);
327330
};
328331

329332
getSyncFn(id: number): (...args: unknown[]) => unknown {
@@ -371,4 +374,17 @@ export class DomContainer extends _SharedContainer implements IClientContainer {
371374
}
372375
this.$serverData$ = { containerAttributes };
373376
}
377+
378+
private $scheduleInitialQRLs$(): void {
379+
if (this.$initialQRLsIndexes$) {
380+
for (const index of this.$initialQRLsIndexes$) {
381+
this.$scheduler$(
382+
ChoreType.QRL_RESOLVE,
383+
null,
384+
this.$getObjectById$(index) as QRLInternal<(...args: unknown[]) => unknown>
385+
);
386+
}
387+
this.$initialQRLsIndexes$ = null;
388+
}
389+
}
374390
}

packages/qwik/src/core/client/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ export interface ClientContainer extends Container {
1818
rootVNode: ElementVNode;
1919
$journal$: VNodeJournal;
2020
renderDone: Promise<void> | null;
21+
$forwardRefs$: Array<number> | null;
22+
$initialQRLsIndexes$: Array<number> | null;
2123
parseQRL<T = unknown>(qrl: string): QRL<T>;
2224
$setRawState$(id: number, vParent: ElementVNode | VirtualVNode): void;
2325
}

packages/qwik/src/core/core.api.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ export type ClassList = string | undefined | null | false | Record<string, boole
2424
//
2525
// @internal (undocumented)
2626
export interface ClientContainer extends Container {
27+
// (undocumented)
28+
$forwardRefs$: Array<number> | null;
29+
// (undocumented)
30+
$initialQRLsIndexes$: Array<number> | null;
2731
// Warning: (ae-forgotten-export) The symbol "VNodeJournal" needs to be exported by the entry point index.d.ts
2832
//
2933
// (undocumented)
@@ -177,8 +181,12 @@ class DomContainer extends _SharedContainer implements ClientContainer {
177181
// (undocumented)
178182
$appendStyle$(content: string, styleId: string, host: _VirtualVNode, scoped: boolean): void;
179183
// (undocumented)
184+
$forwardRefs$: Array<number> | null;
185+
// (undocumented)
180186
$getObjectById$: (id: number | string) => unknown;
181187
// (undocumented)
188+
$initialQRLsIndexes$: Array<number> | null;
189+
// (undocumented)
182190
$instanceHash$: string;
183191
// (undocumented)
184192
$journal$: VNodeJournal;

0 commit comments

Comments
 (0)