Skip to content
Draft
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
20 changes: 20 additions & 0 deletions packages/core/src/refkey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,26 @@ function getObjectKey(value: WeakKey) {
return key;
}

const JsonSym: unique symbol = Symbol();
export type Json = { value: unknown; [JsonSym]: true };

export function json(value: unknown): Json {
const json: Json = {
value,
[JsonSym]: true,
};

markRaw(json);

return json;
}

export function isJson(value: unknown): value is Json {
return (
typeof value === "object" && value !== null && Object.hasOwn(value, JsonSym)
);
}

const RefkeySym: unique symbol = Symbol();

export type Refkey = { key: string; [RefkeySym]: true };
Expand Down
23 changes: 22 additions & 1 deletion packages/core/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
root,
untrack,
} from "./reactivity.js";
import { isRefkey } from "./refkey.js";
import { isJson, isRefkey, Json } from "./refkey.js";
import {
Child,
Children,
Expand Down Expand Up @@ -482,6 +482,24 @@ function appendChild(node: RenderedTextTree, rawChild: Child) {
cache.set(child, newNodes);
return newNodes;
});
} else if (isJson(child)) {
node.push(
JSON.stringify(
child.value,
(key, value) => {
if (typeof value === "function") {
const componentRoot: RenderedTextTree = [];
pushStack(value.component, value.props);
renderWorker(componentRoot, untrack(value));
popStack();
return JSON.parse(componentRoot.toString());
} else {
return value;
}
},
2,
),
);
} else {
throw new Error("Unexpected child type");
}
Expand All @@ -493,6 +511,7 @@ type NormalizedChild =
| string
| (() => Child | Children)
| CustomContext
| Json
| IntrinsicElement;

function normalizeChild(child: Child): NormalizedChildren {
Expand Down Expand Up @@ -521,6 +540,8 @@ function normalizeChild(child: Child): NormalizedChildren {
return child;
} else if (isIntrinsicElement(child)) {
return child;
} else if (isJson(child)) {
return child;
} else {
return String(child);
}
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/runtime/component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Ref } from "@vue/reactivity";
import { CustomContext } from "../reactivity.js";
import { Refkey } from "../refkey.js";
import { Json, Refkey } from "../refkey.js";
import { IntrinsicElement } from "./intrinsic.js";

export type Child =
Expand All @@ -13,6 +13,7 @@ export type Child =
| (() => Children)
| Ref
| Refkey
| Json
| CustomContext
| IntrinsicElement;

Expand Down
28 changes: 28 additions & 0 deletions packages/core/test/test.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Children, json } from "@alloy-js/core";
import { expect, it } from "vitest";

const TestingJsonObject = (): Children => {
return json({
id: "test-id",
name: "Test Name",
nested: <Bar />,
});
};

const Bar = (): Children => {
return json({
bar: "Foo",
});
};

it("works", () => {
expect(TestingJsonObject()).toRenderTo(`
{
"id": "test-id",
"name": "Test Name",
"nested": {
"bar": "Foo"
}
}
`);
});
1 change: 1 addition & 0 deletions packages/core/test/vitest.setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "@alloy-js/core/testing";
3 changes: 3 additions & 0 deletions packages/core/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ export default defineConfig({
jsx: "preserve",
sourcemap: "both",
},
test: {
setupFiles: ["./test/vitest.setup.ts"],
},
plugins: [alloyPlugin()],
});
Loading