Skip to content

Commit

Permalink
perf: improve performance of computing dock configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
camc314 committed Feb 19, 2024
1 parent 1231b4f commit fdd41ec
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 109 deletions.
1 change: 1 addition & 0 deletions how-to/workspace-platform-starter/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## v17.2.0

- Improved performance of switching schemes
- Improved performance of computing dock configuration, especially on theme changes.

## v17.0.0

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isStringValue } from "./utils";

const IMAGE_CACHE: { [key: string]: string } = {};
const IMAGE_CACHE = new Map<string, { dataUri: string | undefined }>();

/**
* Load an image to a data url containing base64 image data.
Expand All @@ -18,8 +18,9 @@ export async function imageUrlToDataUrl(

const key = `${url}_${dimensions}`;

if (IMAGE_CACHE[key]) {
return IMAGE_CACHE[key];
const cachedValue = IMAGE_CACHE.get(key);
if (cachedValue) {
return cachedValue.dataUri;
}

return new Promise<string | undefined>((resolve) => {
Expand All @@ -41,17 +42,19 @@ export async function imageUrlToDataUrl(
if (ctx) {
ctx.drawImage(img, 0, 0, dimensions, dimensions);
dataUri = canvas.toDataURL("image/png", 1);
IMAGE_CACHE[key] = dataUri;
IMAGE_CACHE.set(key, { dataUri });
}
} catch {}
resolve(dataUri);
});
img.addEventListener("error", () => {
IMAGE_CACHE.set(key, { dataUri: undefined });
// eslint-disable-next-line unicorn/no-useless-undefined
resolve(undefined);
});
img.src = url;
} catch {
IMAGE_CACHE.set(key, { dataUri: undefined });
// eslint-disable-next-line unicorn/no-useless-undefined
resolve(undefined);
}
Expand Down
Loading

0 comments on commit fdd41ec

Please sign in to comment.