fix: serialization of signals containing null and undefined values (#2744)

This commit is contained in:
Luca Casonato
2024-10-31 11:22:03 +01:00
committed by GitHub
parent 22af15279d
commit 8ced083267
4 changed files with 80 additions and 12 deletions
+42 -3
View File
@@ -12,7 +12,8 @@ Deno.test("custom parse - Point", () => {
} }
const str = stringify(new Point(30, 40), { const str = stringify(new Point(30, 40), {
Point: (value) => value instanceof Point ? [value.x, value.y] : undefined, Point: (value) =>
value instanceof Point ? { value: [value.x, value.y] } : undefined,
}); });
expect(str).toEqual('[["Point",1],[2,3],30,40]'); expect(str).toEqual('[["Point",1],[2,3],30,40]');
@@ -35,18 +36,56 @@ Deno.test("custom stringify - Signals", () => {
const s = signal(2); const s = signal(2);
expect(stringify(s, { expect(stringify(s, {
Signal: (s2: unknown) => { Signal: (s2: unknown) => {
return s2 instanceof Signal ? s2.peek() : undefined; return s2 instanceof Signal ? { value: s2.peek() } : undefined;
}, },
})).toEqual( })).toEqual(
'[["Signal",1],2]', '[["Signal",1],2]',
); );
}); });
Deno.test("custom parse - Signals with null value", () => {
const res = parse<Signal>('[["Signal",-2]]', {
Signal: (value) => signal(value),
});
expect(res).toBeInstanceOf(Signal);
expect(res.peek()).toEqual(null);
});
Deno.test("custom stringify - Signals with null value", () => {
const s = signal(null);
expect(stringify(s, {
Signal: (s2: unknown) => {
return s2 instanceof Signal ? { value: s2.peek() } : undefined;
},
})).toEqual(
'[["Signal",-2]]',
);
});
Deno.test("custom parse - Signals with undefined value", () => {
const res = parse<Signal>('[["Signal",-1]]', {
Signal: (value) => signal(value),
});
expect(res).toBeInstanceOf(Signal);
expect(res.peek()).toEqual(undefined);
});
Deno.test("custom stringify - Signals with undefined value", () => {
const s = signal(undefined);
expect(stringify(s, {
Signal: (s2: unknown) => {
return s2 instanceof Signal ? { value: s2.peek() } : undefined;
},
})).toEqual(
'[["Signal",-1]]',
);
});
Deno.test("custom stringify - referenced Signals", () => { Deno.test("custom stringify - referenced Signals", () => {
const s = signal(2); const s = signal(2);
expect(stringify([s, s], { expect(stringify([s, s], {
Signal: (s2: unknown) => { Signal: (s2: unknown) => {
return s2 instanceof Signal ? s2.peek() : undefined; return s2 instanceof Signal ? { value: s2.peek() } : undefined;
}, },
})).toEqual( })).toEqual(
'[[1,1],["Signal",2],2]', '[[1,1],["Signal",2],2]',
+26 -2
View File
@@ -68,8 +68,32 @@ function unpack(
if (custom !== undefined && name in custom) { if (custom !== undefined && name in custom) {
const fn = custom[name]; const fn = custom[name];
const ref = current[1]; const ref = current[1];
unpack(arr, hydrated, ref, custom); let value;
const value = hydrated[ref]; if (ref < 0) {
switch (ref) {
case UNDEFINED:
value = undefined;
break;
case NULL:
value = null;
break;
case NAN:
value = NaN;
break;
case INFINITY_POS:
value = Infinity;
break;
case INFINITY_NEG:
value = -Infinity;
break;
case ZERO_NEG:
value = -0;
break;
}
} else {
unpack(arr, hydrated, ref, custom);
value = hydrated[ref];
}
hydrated[idx] = fn(value); hydrated[idx] = fn(value);
return; return;
} }
+7 -4
View File
@@ -8,8 +8,11 @@ import {
} from "./constants.ts"; } from "./constants.ts";
import { HOLE } from "./constants.ts"; import { HOLE } from "./constants.ts";
// deno-lint-ignore no-explicit-any export type Stringifiers = Record<
export type Stringifiers = Record<string, (value: any) => any>; string,
// deno-lint-ignore no-explicit-any
(value: any) => { value: any } | undefined
>;
/** /**
* Serializes the following: * Serializes the following:
@@ -94,8 +97,8 @@ function serializeInner(
const res = fn(value); const res = fn(value);
if (res === undefined) continue; if (res === undefined) continue;
serializeInner(out, indexes, res, custom); const innerIdx = serializeInner(out, indexes, res.value, custom);
str = `["${k}",${idx + 1}]`; str = `["${k}",${innerIdx}]`;
out[idx] = str; out[idx] = str;
return idx; return idx;
} }
+5 -3
View File
@@ -369,14 +369,16 @@ function isVNode(x: any): x is VNode {
const stringifiers: Stringifiers = { const stringifiers: Stringifiers = {
Signal: (value: unknown) => { Signal: (value: unknown) => {
return isSignal(value) ? value.peek() : undefined; return isSignal(value) ? { value: value.peek() } : undefined;
}, },
Slot: (value: unknown) => { Slot: (value: unknown) => {
if (isVNode(value) && value.type === Slot) { if (isVNode(value) && value.type === Slot) {
const props = value.props as SlotProps; const props = value.props as SlotProps;
return { return {
name: props.name, value: {
id: props.id, name: props.name,
id: props.id,
},
}; };
} }
}, },