Skip to content

Commit fe3b110

Browse files
committed
[frontend] memoize icons
1 parent 3e64406 commit fe3b110

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

frontend/src/memoize.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export function memoize<T extends HTMLElement>(
2+
fn: () => T,
3+
key: any,
4+
cache: Record<string, T>
5+
): T {
6+
if (cache[key]) {
7+
return cache[key];
8+
}
9+
const element = fn();
10+
cache[key] = element;
11+
return element;
12+
}

frontend/src/tabs.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import {
88
} from "dreamland/core";
99
import { Icon } from "./ui/Icon";
1010
import { IconButton } from "./Omnibox";
11+
import { memoize } from "./memoize";
12+
13+
let cache = {};
1114

1215
export const DragTab: Component<{
1316
active: boolean;
@@ -43,7 +46,13 @@ export const DragTab: Component<{
4346
}}
4447
>
4548
<div class={use(this.active).map((x) => `main ${x ? "active" : ""}`)}>
46-
<img src={use(this.icon)} />
49+
{memoize(
50+
() => (
51+
<img src={use(this.icon)} />
52+
),
53+
this.icon,
54+
cache
55+
)}
4756
<span>{use(this.title)}</span>
4857
<button
4958
class="close"

0 commit comments

Comments
 (0)