From da191352d31c73e259c9d4c61705fbe3a9f80f7a Mon Sep 17 00:00:00 2001 From: Andrew Patton Date: Sun, 1 Sep 2024 17:09:32 -0700 Subject: [PATCH] =?UTF-8?q?Rename=20style=20=E2=80=9Cregistry=E2=80=9D=20?= =?UTF-8?q?=E2=86=92=20=E2=80=9Ccache=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/styling/src/useStyles.test.tsx | 32 ++++++++++++------------- packages/styling/src/useStyles.ts | 18 +++++++------- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/packages/styling/src/useStyles.test.tsx b/packages/styling/src/useStyles.test.tsx index 1ef3d81f..5d145459 100644 --- a/packages/styling/src/useStyles.test.tsx +++ b/packages/styling/src/useStyles.test.tsx @@ -4,20 +4,20 @@ import React from 'react'; import { beforeEach, describe, expect, it } from 'vitest'; import Style from './Style.js'; -import { getStyleRegistry } from './useStyles.js'; +import { getStyleCache } from './useStyles.js'; describe('@acusti/styling', () => { describe('useStyles.ts', () => { const mockStylesA = '.test-a {\n color: cyan;\n}'; const mockStylesB = '.test-b {\n padding: 10px;\n}'; - // reset styleRegistry before each test + // reset styleCache before each test beforeEach(() => { - getStyleRegistry().clear(); + getStyleCache().clear(); }); it('should cache minified styles in the registry keyed by the style string', () => { - const styleRegistry = getStyleRegistry(); + const styleCache = getStyleCache(); const { rerender } = render( @@ -26,22 +26,22 @@ describe('@acusti/styling', () => { , ); - let stylesItemA = styleRegistry.get(mockStylesA); + let stylesItemA = styleCache.get(mockStylesA); expect(stylesItemA?.referenceCount).toBe(2); expect(stylesItemA?.styles).toBe('.test-a{color:cyan}'); - expect(styleRegistry.size).toBe(1); + expect(styleCache.size).toBe(1); rerender(); expect(stylesItemA?.referenceCount).toBe(1); - expect(stylesItemA).toBe(styleRegistry.get(mockStylesA)); - expect(styleRegistry.size).toBe(1); + expect(stylesItemA).toBe(styleCache.get(mockStylesA)); + expect(styleCache.size).toBe(1); rerender(); - stylesItemA = styleRegistry.get(mockStylesA); + stylesItemA = styleCache.get(mockStylesA); expect(stylesItemA).toBe(undefined); - let stylesItemB = styleRegistry.get(mockStylesB); + let stylesItemB = styleCache.get(mockStylesB); expect(stylesItemB?.referenceCount).toBe(1); - expect(styleRegistry.size).toBe(1); + expect(styleCache.size).toBe(1); rerender( @@ -49,15 +49,15 @@ describe('@acusti/styling', () => { , ); - stylesItemA = styleRegistry.get(mockStylesA); + stylesItemA = styleCache.get(mockStylesA); expect(stylesItemA?.referenceCount).toBe(1); - expect(stylesItemA).toBe(styleRegistry.get(mockStylesA)); - stylesItemB = styleRegistry.get(mockStylesB); + expect(stylesItemA).toBe(styleCache.get(mockStylesA)); + stylesItemB = styleCache.get(mockStylesB); expect(stylesItemB?.referenceCount).toBe(1); - expect(styleRegistry.size).toBe(2); + expect(styleCache.size).toBe(2); rerender(
); - expect(styleRegistry.size).toBe(0); + expect(styleCache.size).toBe(0); }); it('should sanitize styles used as href prop if no href prop provided', () => { diff --git a/packages/styling/src/useStyles.ts b/packages/styling/src/useStyles.ts index 01a44951..6c79dd34 100644 --- a/packages/styling/src/useStyles.ts +++ b/packages/styling/src/useStyles.ts @@ -2,21 +2,21 @@ import { useEffect, useState } from 'react'; import { minifyStyles } from './minifyStyles.js'; -type StyleRegistry = Map< +type StyleCache = Map< string, { href: string; referenceCount: number; styles: string } >; -const styleRegistry: StyleRegistry = new Map(); +const styleCache: StyleCache = new Map(); -export const getStyleRegistry = () => styleRegistry; +export const getStyleCache = () => styleCache; export function useStyles(styles: string, initialHref?: string) { const [stylesItem, setStylesItem] = useState(() => { if (!styles) return { href: '', referenceCount: 0, styles: '' }; const key = initialHref ?? styles; - let item = styleRegistry.get(key); + let item = styleCache.get(key); if (item) { item.referenceCount++; @@ -27,7 +27,7 @@ export function useStyles(styles: string, initialHref?: string) { referenceCount: 1, styles: minified, }; - styleRegistry.set(key, item); + styleCache.set(key, item); } return item; @@ -38,19 +38,19 @@ export function useStyles(styles: string, initialHref?: string) { const key = initialHref ?? styles; - if (!styleRegistry.get(key)) { + if (!styleCache.get(key)) { const minified = minifyStyles(styles); const item = { href: sanitizeHref(initialHref ?? minified), referenceCount: 1, styles: minified, }; - styleRegistry.set(key, item); + styleCache.set(key, item); setStylesItem(item); } return () => { - const existingItem = styleRegistry.get(styles); + const existingItem = styleCache.get(styles); if (existingItem) { existingItem.referenceCount--; if (!existingItem.referenceCount) { @@ -58,7 +58,7 @@ export function useStyles(styles: string, initialHref?: string) { // and add another referenceCount check // to deal with instance where existing