Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tiny-react): use symbol for node type #243

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/tiny-react/examples/server/src/entry-browser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from "@hiogawa/tiny-react";
import { tinyassert } from "@hiogawa/utils";
import { createReferenceMap } from "./integration/client-reference/runtime";
import { jsonUnescapeSymbol } from "./integration/serialization";

async function main() {
if (window.location.href.includes("__nojs")) {
Expand All @@ -25,7 +26,7 @@ async function main() {
url.searchParams.set("__serialize", "");
const res = await fetch(url);
tinyassert(res.ok);
const result: SerializeResult = await res.json();
const result: SerializeResult = jsonUnescapeSymbol(await res.text());
const newVnode = deserialize<VNode>(
result.data,
await createReferenceMap(result.referenceIds)
Expand All @@ -38,7 +39,9 @@ async function main() {
}

// hydrate with initial SNode
const initResult: SerializeResult = (globalThis as any).__serialized;
const initResult: SerializeResult = jsonUnescapeSymbol(
(globalThis as any).__serialized
);
const vnode = deserialize<VNode>(
initResult.data,
await createReferenceMap(initResult.referenceIds)
Expand Down
5 changes: 3 additions & 2 deletions packages/tiny-react/examples/server/src/entry-server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from "@hiogawa/tiny-react";
import type { ViteDevServer } from "vite";
import { createReferenceMap } from "./integration/client-reference/runtime";
import { jsonEscapeSymbol } from "./integration/serialization";
import Layout from "./routes/layout";

export async function handler(request: Request) {
Expand All @@ -15,7 +16,7 @@ export async function handler(request: Request) {

// to CSR
if (url.searchParams.has("__serialize")) {
return new Response(JSON.stringify(serialized), {
return new Response(jsonEscapeSymbol(serialized), {
headers: {
"content-type": "application/json",
},
Expand All @@ -35,7 +36,7 @@ export async function handler(request: Request) {
"<head>",
() =>
`<head><script>globalThis.__serialized = ${JSON.stringify(
serialized
jsonEscapeSymbol(serialized)
)}</script>`
);
// dev only FOUC fix
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export function jsonEscapeSymbol(v: unknown) {
return JSON.stringify(v, function (_k, v) {
// escape collision
if (typeof v === "string" && v.startsWith("!")) {
return "!" + v;
}
// symbol
if (typeof v === "symbol" && typeof v.description === "string") {
return "!s:" + v.description;
}
return v;
});
}

export function jsonUnescapeSymbol(s: string) {
return JSON.parse(s, function (_k, v) {
if (typeof v === "string" && v.startsWith("!s:")) {
return Symbol.for(v.slice(3));
}
if (typeof v === "string" && v.startsWith("!")) {
return v.slice(1);
}
return v;
});
}
26 changes: 13 additions & 13 deletions packages/tiny-react/src/helper/hyperscript.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ describe("hyperscript", () => {
"value": "hello",
},
"render": [Function],
"type": "custom",
"type": Symbol(tiny-react.custom),
},
null,
0,
{
"key": undefined,
"props": {},
"render": [Function],
"type": "custom",
"type": Symbol(tiny-react.custom),
},
undefined,
{
Expand All @@ -53,15 +53,15 @@ describe("hyperscript", () => {
"className": "text-red",
"ref": [Function],
},
"type": "tag",
"type": Symbol(tiny-react.tag),
},
{
"key": undefined,
"name": "span",
"props": {
"children": 0,
},
"type": "tag",
"type": Symbol(tiny-react.tag),
},
{
"key": undefined,
Expand All @@ -72,7 +72,7 @@ describe("hyperscript", () => {
1,
],
},
"type": "tag",
"type": Symbol(tiny-react.tag),
},
{
"key": undefined,
Expand All @@ -82,7 +82,7 @@ describe("hyperscript", () => {
0,
],
},
"type": "tag",
"type": Symbol(tiny-react.tag),
},
{
"key": undefined,
Expand All @@ -93,12 +93,12 @@ describe("hyperscript", () => {
1,
],
},
"type": "tag",
"type": Symbol(tiny-react.tag),
},
],
"className": "flex",
},
"type": "tag",
"type": Symbol(tiny-react.tag),
}
`);
});
Expand All @@ -109,7 +109,7 @@ describe("hyperscript", () => {
"key": undefined,
"props": {},
"render": [Function],
"type": "custom",
"type": Symbol(tiny-react.custom),
}
`);
expect(h(Fragment, {}, 1)).toMatchInlineSnapshot(`
Expand All @@ -119,7 +119,7 @@ describe("hyperscript", () => {
"children": 1,
},
"render": [Function],
"type": "custom",
"type": Symbol(tiny-react.custom),
}
`);
expect(h(Fragment, { children: 1 })).toMatchInlineSnapshot(`
Expand All @@ -129,7 +129,7 @@ describe("hyperscript", () => {
"children": 1,
},
"render": [Function],
"type": "custom",
"type": Symbol(tiny-react.custom),
}
`);
expect(h(Fragment, { children: [1] })).toMatchInlineSnapshot(`
Expand All @@ -141,7 +141,7 @@ describe("hyperscript", () => {
],
},
"render": [Function],
"type": "custom",
"type": Symbol(tiny-react.custom),
}
`);
expect(h(Fragment, { children: 1 }, 2)).toMatchInlineSnapshot(`
Expand All @@ -151,7 +151,7 @@ describe("hyperscript", () => {
"children": 2,
},
"render": [Function],
"type": "custom",
"type": Symbol(tiny-react.custom),
}
`);
});
Expand Down
8 changes: 4 additions & 4 deletions packages/tiny-react/src/helper/jsx-runtime.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ test("basic", () => {
"children": "yay",
"className": "hehe",
},
"type": "tag",
"type": Symbol(tiny-react.tag),
},
{
"key": undefined,
Expand All @@ -35,17 +35,17 @@ test("basic", () => {
"props": {
"children": "ment",
},
"type": "tag",
"type": Symbol(tiny-react.tag),
},
],
},
"render": [Function],
"type": "custom",
"type": Symbol(tiny-react.custom),
},
],
"id": "hi",
},
"type": "tag",
"type": Symbol(tiny-react.tag),
}
`);
});
Loading