fix: duplicate references for same signals (#1503)

This commit is contained in:
Marvin Hagemeister
2023-07-24 09:37:05 +00:00
committed by GitHub
parent 38d4b90091
commit 3c0657caa8
5 changed files with 66 additions and 36 deletions
+10 -16
View File
@@ -88,6 +88,16 @@ export function serialize(data: unknown): SerializeResult {
return value;
}
// Bypass signal's `.toJSON` method because we want to serialize
// the signal itself including the signal's value and not just
// the value. This is needed because `JSON.stringify` always
// calls `.toJSON` automatically if available.
// deno-lint-ignore no-explicit-any
if (key !== null && isSignal((this as any)[key])) {
// deno-lint-ignore no-explicit-any
value = (this as any)[key];
}
// For some object types, the path in the object graph from root is not the
// same between the serialized representation, and deserialized objects. For
// these cases, we have to change the contents of the key stack to match the
@@ -155,22 +165,6 @@ export function serialize(data: unknown): SerializeResult {
parentStack.push(res);
return res;
} else {
if (key !== null) {
// Bypass signal's `.toJSON` method because we want to serialize
// the signal itself including the signal's value and not just
// the value. This is needed because `JSON.stringify` always
// calls `.toJSON` automatically if available.
// deno-lint-ignore no-explicit-any
const realValue = (this as any)[key];
if (isSignal(realValue)) {
requiresDeserializer = true;
hasSignals = true;
const res = { [KEY]: "s", v: realValue.peek() };
parentStack.push(res);
return res;
}
}
parentStack.push(value);
return value;
}