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

Cache google fonts traversal #176

Merged
merged 5 commits into from
Jun 18, 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
29 changes: 19 additions & 10 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@create-figma-plugin/ui": "^3.2",
"base64-js": "^1.5",
"classnames": "^2.5",
"lru-cache": "^10.2",
"preact": "^10.22",
"react-hook-form": "^7.51",
"romans": "^2.0",
Expand All @@ -36,7 +37,7 @@
"@changesets/changelog-github": "^0.5",
"@changesets/cli": "^2.27",
"@figma/eslint-plugin-figma-plugins": "^0.15",
"@figma/plugin-typings": "^1.93",
"@figma/plugin-typings": "^1.96",
"@trivago/prettier-plugin-sort-imports": "^4.3",
"@types/svg-path-parser": "^1.1",
"@typescript-eslint/eslint-plugin": "^7.8",
Expand Down
26 changes: 26 additions & 0 deletions plugin-src/Cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { LRUCache } from 'lru-cache';

const empty: unique symbol = Symbol('noValue');

// eslint-disable-next-line @typescript-eslint/ban-types
export class Cache<K extends {}, V extends {}> {
private cache: LRUCache<K, V | typeof empty>;

public constructor(options: LRUCache.Options<K, V | typeof empty, unknown>) {
this.cache = new LRUCache(options);
}

public get(key: K, calculate: () => V | undefined): V | undefined {
if (this.cache.has(key)) {
const cacheItem = this.cache.get(key);

return cacheItem === empty ? undefined : cacheItem;
}

const calculated = calculate();

this.cache.set(key, calculated ?? empty);

return calculated;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import slugify from 'slugify';

import { Cache } from '@plugin/Cache';
import { translateFontVariantId } from '@plugin/translators/text/font/gfonts';

import { FontId } from '@ui/lib/types/shapes/textShape';

import { items as gfonts } from './gfonts.json';
import { GoogleFont } from './googleFont';

const fontsCache = new Cache<string, GoogleFont>({ max: 30 });

export const translateGoogleFont = (fontName: FontName, fontWeight: number): FontId | undefined => {
const googleFont = getGoogleFont(fontName);

Expand All @@ -23,5 +26,7 @@ export const isGoogleFont = (fontName: FontName): boolean => {
};

const getGoogleFont = (fontName: FontName): GoogleFont | undefined => {
return gfonts.find(font => font.family === fontName.family);
return fontsCache.get(fontName.family, () =>
gfonts.find(font => font.family === fontName.family)
);
};
1 change: 1 addition & 0 deletions plugin-src/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"strict": true,
"typeRoots": ["../node_modules/@figma"],
"moduleResolution": "Node",
"skipLibCheck": true,
"resolveJsonModule": true
}
}