11import { toCamel } from './utils/to-camel'
22import { toPascal } from './utils/to-pascal'
33
4- // Cache for figma.getLocalTextStylesAsync() — called once per codegen run
5- let localTextStyleIdsCache : Promise < Set < string > > | null = null
6- let localTextStyleIdsResolved : Set < string > | null = null
7-
8- function getLocalTextStyleIds ( ) : Promise < Set < string > > {
9- if ( localTextStyleIdsCache ) return localTextStyleIdsCache
10- localTextStyleIdsCache = Promise . resolve (
11- figma . getLocalTextStylesAsync ( ) ,
12- ) . then ( ( styles ) => {
13- const set = new Set ( styles . map ( ( s ) => s . id ) )
14- localTextStyleIdsResolved = set
15- return set
16- } )
17- return localTextStyleIdsCache
18- }
19-
204// Cache for figma.getStyleByIdAsync() — keyed by style ID
215const styleByIdCache = new Map < string , Promise < BaseStyle | null > > ( )
226const styleByIdResolved = new Map < string , BaseStyle | null > ( )
@@ -35,8 +19,6 @@ function getStyleByIdCached(styleId: string): Promise<BaseStyle | null> {
3519}
3620
3721export function resetTextStyleCache ( ) : void {
38- localTextStyleIdsCache = null
39- localTextStyleIdsResolved = null
4022 styleByIdCache . clear ( )
4123 styleByIdResolved . clear ( )
4224}
@@ -63,26 +45,18 @@ export async function propsToPropsWithTypography(
6345 delete ret . w
6446 delete ret . h
6547
66- // Sync fast path: if both caches are resolved, skip await entirely
67- if ( localTextStyleIdsResolved !== null ) {
68- if ( textStyleId && localTextStyleIdsResolved . has ( textStyleId ) ) {
69- const style = styleByIdResolved . get ( textStyleId )
70- if ( style !== undefined ) {
71- if ( style ) applyTypographyStyle ( ret , style )
72- return ret
73- }
74- // Style not yet resolved — fall through to async
75- } else {
76- return ret
77- }
78- }
48+ if ( ! textStyleId ) return ret
7949
80- // Async fallback (first call or style not yet in resolved cache)
81- const localStyleIds = await getLocalTextStyleIds ( )
82- if ( textStyleId && localStyleIds . has ( textStyleId ) ) {
83- const style = await getStyleByIdCached ( textStyleId )
84- if ( style ) applyTypographyStyle ( ret , style )
50+ // Sync fast path: if style already resolved, skip await entirely
51+ const resolvedStyle = styleByIdResolved . get ( textStyleId )
52+ if ( resolvedStyle !== undefined ) {
53+ if ( resolvedStyle ) applyTypographyStyle ( ret , resolvedStyle )
54+ return ret
8555 }
56+
57+ // Async fallback: resolve style by ID (works for both local and library styles)
58+ const style = await getStyleByIdCached ( textStyleId )
59+ if ( style ) applyTypographyStyle ( ret , style )
8660 return ret
8761}
8862
0 commit comments