Skip to content

Commit

Permalink
perf: "cache" blurhashes in dictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
ferferga committed Jan 25, 2021
1 parent 518e103 commit 61c21fc
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions components/Layout/Images/BlurhashCanvas.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@
import Vue from 'vue';
import getPixels from '~/plugins/workers/blurhash.worker';
/**
* HACK: The cleanest way to do this would be to use an store. However, that required a deep watcher and, overall,
* I felt that it took longer to load the blurhashes using that approach than this.
*
* Blurhashes pixels *shouldn't* be required by any other component, so it might not make sense at all
* to move to the store approach and we might be fine with this one.
*/
const cache: { [hash: string]: Uint8ClampedArray } = {};
export default Vue.extend({
props: {
hash: {
Expand All @@ -41,19 +50,23 @@ export default Vue.extend({
};
},
watch: {
hash(): void {
this.$nextTick(() => {
this.loading = true;
this.getPixels();
});
hash: {
immediate: true,
handler(): void {
this.$nextTick(() => {
this.loading = true;
if (cache[this.hash]) {
this.pixels = cache[this.hash];
} else {
this.getPixels();
}
});
}
},
pixels(): void {
this.draw();
}
},
mounted() {
this.getPixels();
},
methods: {
draw(): void {
const ctx = (this.$refs.canvas as HTMLCanvasElement).getContext('2d');
Expand All @@ -73,6 +86,7 @@ export default Vue.extend({
this.height,
this.punch
);
cache[this.hash] = this.pixels;
} catch {
this.pixels = undefined;
this.$emit('error');
Expand Down

0 comments on commit 61c21fc

Please sign in to comment.