fix: serialized compute signals re-created as normal signals (#3183)

Fixes https://github.com/denoland/fresh/issues/3182
This commit is contained in:
Marvin Hagemeister
2025-08-03 20:31:36 +02:00
committed by GitHub
parent 2e68c30d8d
commit 03f6981860
6 changed files with 421 additions and 11 deletions
+2 -1
View File
@@ -8,7 +8,7 @@ import {
type VNode,
} from "preact";
import { type CustomParser, parse } from "../../jsonify/parse.ts";
import { signal } from "@preact/signals";
import { computed, signal } from "@preact/signals";
import { DATA_FRESH_KEY, PartialMode } from "../shared_internal.tsx";
const enum RootKind {
@@ -128,6 +128,7 @@ export const ISLAND_REGISTRY = new Map<string, ComponentType>();
export const CUSTOM_PARSER: CustomParser = {
Signal: (value: unknown) => signal(value),
Computed: (value: unknown) => computed(() => value),
Slot: (value: { name: string; id: number }): SlotRef => {
return { kind: SLOT_SYMBOL, name: value.name, id: value.id };
},
+11 -1
View File
@@ -9,7 +9,7 @@ import {
options as preactOptions,
type VNode,
} from "preact";
import type { Signal } from "@preact/signals";
import type { ReadonlySignal, Signal } from "@preact/signals";
import type { Stringifiers } from "../../jsonify/stringify.ts";
import type { PageProps } from "../../render.ts";
import { Partial, type PartialProps } from "../shared.ts";
@@ -361,6 +361,13 @@ function isSignal(x: any): x is Signal {
);
}
// deno-lint-ignore no-explicit-any
function isComputedSignal(x: any): x is ReadonlySignal {
return isSignal(x) &&
(("x" in x && typeof x.x === "function") ||
"_fn" in x && typeof x._fn === "function");
}
// deno-lint-ignore no-explicit-any
function isVNode(x: any): x is VNode {
return x !== null && typeof x === "object" && "type" in x && "ref" in x &&
@@ -369,6 +376,9 @@ function isVNode(x: any): x is VNode {
}
const stringifiers: Stringifiers = {
Computed: (value: unknown) => {
return isComputedSignal(value) ? { value: value.peek() } : undefined;
},
Signal: (value: unknown) => {
return isSignal(value) ? { value: value.peek() } : undefined;
},