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

perf: cache twc function #11

Merged
merged 2 commits into from
May 4, 2024
Merged
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"esbuild": "^0.20.2",
"prettier": "^3.1.1",
"prettier-plugin-svelte": "^3.2.3",
"svelte": "5.0.0-next.115",
"svelte": "5.0.0-next.123",
"tsx": "^4.7.2",
"typescript": "^5.4.5",
"vite": "^5.2.10"
Expand Down
95 changes: 49 additions & 46 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"highlight.js": "^11.9.0",
"postcss": "^8.4.38",
"rehype-highlight": "^7.0.0",
"svelte": "5.0.0-next.115",
"svelte": "5.0.0-next.123",
"svelte-check": "^3.6.9",
"svelte-twc": "workspace:*",
"tailwindcss": "^3.4.3",
Expand Down
8 changes: 2 additions & 6 deletions src/internal/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// TwcComponent.svelte (Svelte v5.0.0-next.115)
// Note: compiler output will change before 5.0 is released!
import "svelte/internal/disclose-version";
import * as $ from "svelte/internal/client";
export function createTwcComponent(el: keyof HTMLElementTagNameMap, options: { compose: (...args: any[]) => string }) {
Expand All @@ -8,7 +6,7 @@ const cls = String.raw({ raw: typeof strings === 'string' ? [strings] : strings

var root = $.template(`<${el}><!></${el}>`);

function TwcComponent($$anchor, $$props) {
return function TwcComponent($$anchor, $$props) {
$.push($$props, true);

let props = $.rest_props($$props, ["children", "class"]);
Expand All @@ -31,7 +29,5 @@ function TwcComponent($$anchor, $$props) {

$.append($$anchor, div);
$.pop();
}

return TwcComponent;};
}};
}
4 changes: 1 addition & 3 deletions src/internal/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// TwcComponent.svelte (Svelte v5.0.0-next.115)
// Note: compiler output will change before 5.0 is released!
import * as $ from "svelte/internal/server";
export function createTwcComponent(el: keyof HTMLElementTagNameMap, options: { compose: (...args: any[]) => string }) {
return (strings: string | TemplateStringsArray, ...values: any[]) => {
Expand All @@ -16,7 +14,7 @@ return function TwcComponent($$payload, $$props) {
{ "class": options.compose(className, cls) }
],
true,
false,
true,
""
)}><!--[-->`;

Expand Down
24 changes: 14 additions & 10 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,25 +112,29 @@ export type HTMLPropsMap = {
export type Attributes<El extends keyof HTMLElementTagNameMap> = El extends keyof HTMLPropsMap
? HTMLPropsMap[El]
: HTMLAttributes<HTMLElementTagNameMap[El]>;
export type HTMLElementTagNames = keyof HTMLElementTagNameMap;
export type TwcFunction<El extends HTMLElementTagNames> = (
strings: TemplateStringsArray,
...values: any[]
) => ComponentType<SvelteComponent<Attributes<El>>>;

export type Twc = {
[El in keyof HTMLElementTagNameMap]: (
strings: TemplateStringsArray,
...values: any[]
) => ComponentType<SvelteComponent<Attributes<El>>>;
[El in HTMLElementTagNames]: TwcFunction<El>;
};
export function createCore(
createTwcComponent: (
el: keyof HTMLElementTagNameMap,
options: TwcOptions
) => (strings: TemplateStringsArray, ...values: any[]) => any
createTwcComponent: (el: HTMLElementTagNames, options: TwcOptions) => any
) {
return (options: TwcOptions) => {
const cache = new Map<HTMLElementTagNames, TwcFunction<HTMLElementTagNames>>();
return new Proxy(
{},
{
get(_, el: keyof HTMLElementTagNameMap) {
return createTwcComponent(el, options);
get(_, el: HTMLElementTagNames): TwcFunction<HTMLElementTagNames> {
const cached = cache.get(el);
if (cached) return cached;
const component = createTwcComponent(el, options);
cache.set(el, component);
return component;
}
}
) as Twc;
Expand Down