Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 10 additions & 36 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,6 @@
import { toCamel } from './utils/to-camel'
import { toPascal } from './utils/to-pascal'

// Cache for figma.getLocalTextStylesAsync() — called once per codegen run
let localTextStyleIdsCache: Promise<Set<string>> | null = null
let localTextStyleIdsResolved: Set<string> | null = null

function getLocalTextStyleIds(): Promise<Set<string>> {
if (localTextStyleIdsCache) return localTextStyleIdsCache
localTextStyleIdsCache = Promise.resolve(
figma.getLocalTextStylesAsync(),
).then((styles) => {
const set = new Set(styles.map((s) => s.id))
localTextStyleIdsResolved = set
return set
})
return localTextStyleIdsCache
}

// Cache for figma.getStyleByIdAsync() — keyed by style ID
const styleByIdCache = new Map<string, Promise<BaseStyle | null>>()
const styleByIdResolved = new Map<string, BaseStyle | null>()
Expand All @@ -35,8 +19,6 @@ function getStyleByIdCached(styleId: string): Promise<BaseStyle | null> {
}

export function resetTextStyleCache(): void {
localTextStyleIdsCache = null
localTextStyleIdsResolved = null
styleByIdCache.clear()
styleByIdResolved.clear()
}
Expand All @@ -63,26 +45,18 @@ export async function propsToPropsWithTypography(
delete ret.w
delete ret.h

// Sync fast path: if both caches are resolved, skip await entirely
if (localTextStyleIdsResolved !== null) {
if (textStyleId && localTextStyleIdsResolved.has(textStyleId)) {
const style = styleByIdResolved.get(textStyleId)
if (style !== undefined) {
if (style) applyTypographyStyle(ret, style)
return ret
}
// Style not yet resolved — fall through to async
} else {
return ret
}
}
if (!textStyleId) return ret

// Async fallback (first call or style not yet in resolved cache)
const localStyleIds = await getLocalTextStyleIds()
if (textStyleId && localStyleIds.has(textStyleId)) {
const style = await getStyleByIdCached(textStyleId)
if (style) applyTypographyStyle(ret, style)
// Sync fast path: if style already resolved, skip await entirely
const resolvedStyle = styleByIdResolved.get(textStyleId)
if (resolvedStyle !== undefined) {
if (resolvedStyle) applyTypographyStyle(ret, resolvedStyle)
return ret
}

// Async fallback: resolve style by ID (works for both local and library styles)
const style = await getStyleByIdCached(textStyleId)
if (style) applyTypographyStyle(ret, style)
return ret
}

Expand Down