Skip to content

Commit beb5f5a

Browse files
committed
fix: flaky signal e2e test
1 parent 5086648 commit beb5f5a

File tree

4 files changed

+45
-38
lines changed

4 files changed

+45
-38
lines changed

packages/qwik/src/core/shared/shared-serialization.ts

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ export const createSerializationContext = (
712712
} as StreamWriter;
713713
}
714714
const seenObjsMap = new Map<unknown, SeenRef>();
715-
const pathMap = new Map<unknown, string | number>();
715+
const rootsPathMap = new Map<unknown, string | number>();
716716
const syncFnMap = new Map<string, number>();
717717
const syncFns: string[] = [];
718718
const roots: unknown[] = [];
@@ -723,14 +723,13 @@ export const createSerializationContext = (
723723
};
724724

725725
const $addRootPath$ = (obj: unknown) => {
726-
const rootPath = pathMap.get(obj);
726+
const rootPath = rootsPathMap.get(obj);
727727
if (rootPath) {
728728
return rootPath;
729729
}
730730
const seen = seenObjsMap.get(obj);
731731
if (!seen) {
732-
// TODO:
733-
throw qError(QError.serializeErrorMissingRootId);
732+
throw qError(QError.serializeErrorMissingRootId, [obj]);
734733
}
735734
const path = [];
736735
let current: typeof seen | undefined = seen;
@@ -745,7 +744,7 @@ export const createSerializationContext = (
745744
}
746745

747746
const pathStr = path.length > 1 ? path.join(' ') : path.length ? path[0] : seen.$index$;
748-
pathMap.set(obj, pathStr);
747+
rootsPathMap.set(obj, pathStr);
749748
return pathStr;
750749
};
751750

@@ -818,7 +817,7 @@ export const createSerializationContext = (
818817
$getProp$: getProp,
819818
$setProp$: setProp,
820819
$prepVNodeData$: prepVNodeData,
821-
$pathMap$: pathMap,
820+
$pathMap$: rootsPathMap,
822821
};
823822
};
824823

@@ -1296,7 +1295,7 @@ async function serialize(serializationContext: SerializationContext): Promise<vo
12961295

12971296
function $resolvePromise$(
12981297
promise: Promise<unknown>,
1299-
$addRoot$: (obj: unknown) => string | number,
1298+
$addRoot$: (obj: unknown) => number,
13001299
classCreator: (resolved: boolean, resolvedValue: unknown) => PromiseResult
13011300
) {
13021301
const forwardRefId = forwardRefsId++;
@@ -1315,44 +1314,50 @@ async function serialize(serializationContext: SerializationContext): Promise<vo
13151314
return forwardRefId;
13161315
}
13171316

1318-
$writer$.write('[');
1317+
const outputRoots = async () => {
1318+
$writer$.write('[');
13191319

1320-
let lastRootsLength = 0;
1321-
let rootsLength = serializationContext.$roots$.length;
1322-
while (lastRootsLength < rootsLength || promises.size) {
1323-
if (lastRootsLength !== 0) {
1324-
$writer$.write(',');
1325-
}
1326-
for (let i = lastRootsLength; i < rootsLength; i++) {
1327-
const root = serializationContext.$roots$[i];
1328-
writeValue(root);
1329-
const isLast = i === rootsLength - 1;
1330-
if (!isLast) {
1320+
let lastRootsLength = 0;
1321+
let rootsLength = serializationContext.$roots$.length;
1322+
while (lastRootsLength < rootsLength || promises.size) {
1323+
if (lastRootsLength !== 0) {
13311324
$writer$.write(',');
13321325
}
1333-
}
13341326

1335-
if (promises.size) {
1336-
try {
1337-
await Promise.race(promises);
1338-
} catch {
1339-
// ignore rejections, they will be serialized as rejected promises
1327+
let separator = false;
1328+
for (let i = lastRootsLength; i < rootsLength; i++) {
1329+
if (separator) {
1330+
$writer$.write(',');
1331+
} else {
1332+
separator = true;
1333+
}
1334+
writeValue(serializationContext.$roots$[i]);
1335+
}
1336+
1337+
if (promises.size) {
1338+
try {
1339+
await Promise.race(promises);
1340+
} catch {
1341+
// ignore rejections, they will be serialized as rejected promises
1342+
}
13401343
}
1344+
1345+
lastRootsLength = rootsLength;
1346+
rootsLength = serializationContext.$roots$.length;
13411347
}
13421348

1343-
lastRootsLength = rootsLength;
1344-
rootsLength = serializationContext.$roots$.length;
1345-
}
1349+
if (forwardRefs.length) {
1350+
$writer$.write(',');
1351+
$writer$.write(TypeIds.ForwardRefs + ',');
1352+
outputArray(forwardRefs, (value) => {
1353+
$writer$.write(String(value));
1354+
});
1355+
}
13461356

1347-
if (forwardRefs.length) {
1348-
$writer$.write(',');
1349-
$writer$.write(TypeIds.ForwardRefs + ',');
1350-
outputArray(forwardRefs, (value) => {
1351-
$writer$.write(String(value));
1352-
});
1353-
}
1357+
$writer$.write(']');
1358+
};
13541359

1355-
$writer$.write(']');
1360+
await outputRoots();
13561361
}
13571362

13581363
function $getCustomSerializerPromise$<T, S>(signal: SerializerSignalImpl<T, S>, value: any) {

packages/qwik/src/core/shared/shared-serialization.unit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ describe('shared-serialization', () => {
447447
Constant null
448448
]
449449
2 RootRef "1 1 0 0"
450-
(96 chars)"
450+
(88 chars)"
451451
`);
452452
});
453453
it(title(TypeIds.ComputedSignal), async () => {

packages/qwik/src/core/ssr/ssr-types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export interface SSRContainer extends Container {
9090
textNode(text: string): void;
9191
htmlNode(rawHtml: string): void;
9292
commentNode(text: string): void;
93-
addRoot(obj: any): string | number | undefined;
93+
addRoot(obj: any): number | undefined;
9494
getLastNode(): ISsrNode;
9595
addUnclaimedProjection(frame: ISsrComponentFrame, name: string, children: JSXChildren): void;
9696
isStatic(): boolean;

starters/e2e/e2e.signals.e2e.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,8 @@ test.describe("signals", () => {
464464
await expect(resultC).toHaveText("0:0");
465465
await expect(resultTotal).toHaveText("0:0");
466466

467+
await page.waitForLoadState("networkidle");
468+
467469
await buttonA.click();
468470
await expect(resultA).toHaveText("1:1");
469471
await expect(resultB).toHaveText("0:0");

0 commit comments

Comments
 (0)