Skip to content

Commit f0bc1a1

Browse files
committed
Replace lodash memoize with memoize package
1 parent 298df06 commit f0bc1a1

4 files changed

Lines changed: 30 additions & 34 deletions

File tree

packages/layerchart/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
"d3-tile": "^1.0.0",
111111
"d3-time": "^3.1.0",
112112
"lodash-es": "^4.17.21",
113+
"memoize": "^10.1.0",
113114
"runed": "^0.28.0"
114115
},
115116
"peerDependencies": {

packages/layerchart/src/lib/utils/canvas.ts

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { cls } from '@layerstack/tailwind';
2-
import { memoize } from 'lodash-es';
31
import type { ClassValue } from 'svelte/elements';
2+
import memoize from 'memoize';
3+
import { cls } from '@layerstack/tailwind';
44
import type { PatternShape } from '$lib/components/Pattern.svelte';
55

66
export const DEFAULT_FILL = 'rgb(0, 0, 0)';
@@ -332,20 +332,9 @@ export function _createLinearGradient(
332332
}
333333

334334
/** Create linear gradient and memoize result to fix reactivity */
335-
export const createLinearGradient = memoize(
336-
_createLinearGradient,
337-
(
338-
ctx: CanvasRenderingContext2D,
339-
x0: number,
340-
y0: number,
341-
x1: number,
342-
y1: number,
343-
stops: { offset: number; color: string }[]
344-
) => {
345-
const key = JSON.stringify({ x0, y0, x1, y1, stops });
346-
return key;
347-
}
348-
);
335+
export const createLinearGradient = memoize(_createLinearGradient, {
336+
cacheKey: (args) => JSON.stringify(args.slice(1)), // Ignore `ctx` argument
337+
});
349338

350339
export function _createPattern(
351340
ctx: CanvasRenderingContext2D,
@@ -395,16 +384,6 @@ export function _createPattern(
395384
}
396385

397386
/** Create pattern and memoize result to fix reactivity */
398-
export const createPattern = memoize(
399-
_createPattern,
400-
(
401-
ctx: CanvasRenderingContext2D,
402-
width: number,
403-
height: number,
404-
shapes: PatternShape[],
405-
background?: string
406-
) => {
407-
const key = JSON.stringify({ width, height, shapes, background });
408-
return key;
409-
}
410-
);
387+
export const createPattern = memoize(_createPattern, {
388+
cacheKey: (args) => JSON.stringify(args.slice(1)), // Ignore `ctx` argument
389+
});

packages/layerchart/src/lib/utils/string.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { memoize } from 'lodash-es';
1+
import memoize from 'memoize';
22

33
const MEASUREMENT_ELEMENT_ID = '__text_measurement_id';
44

@@ -29,10 +29,9 @@ function _getStringWidth(str: string, style?: CSSStyleDeclaration) {
2929
}
3030
}
3131

32-
export const getStringWidth = memoize(
33-
_getStringWidth,
34-
(str, style) => `${str}_${JSON.stringify(style)}`
35-
);
32+
export const getStringWidth = memoize(_getStringWidth, {
33+
cacheKey: ([str, style]) => `${str}_${JSON.stringify(style)}`,
34+
});
3635

3736
export type RasterizeTextOptions = {
3837
fontSize?: string;

pnpm-lock.yaml

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)