Skip to content

Commit

Permalink
perf(pdblib): use a cache for decoration data
Browse files Browse the repository at this point in the history
  • Loading branch information
cyyynthia committed Apr 16, 2024
1 parent b5f61e2 commit c74ad53
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
28 changes: 20 additions & 8 deletions packages/pronouns/src/decorations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,29 @@ export function renderDecoration (decoration: DecorationData): Decoration | null
}
}

export async function getDecoration (decoration: string): Promise<Decoration | null> {
const data = await fetchDecoration(decoration)
return data ? renderDecoration(data) : null
const BADGES_CACHE = /* @__PURE__ */ new Map<string, Decoration | null>()

export async function getDecoration (decorationId: string): Promise<Decoration | null> {
let decoration = BADGES_CACHE.get(decorationId)
if (decoration === void 0) {
const data = await fetchDecoration(decorationId)
BADGES_CACHE.set(decorationId, decoration = (data ? renderDecoration(data) : null))
}

return decoration
}

export async function getDecorationExtension (decoration: string): Promise<Decoration | null> {
const res = await chrome.runtime.sendMessage({ kind: 'decoration', decoration: decoration })
if (!res.success) throw new Error(res.error)
export async function getDecorationExtension (decorationId: string): Promise<Decoration | null> {
let decoration = BADGES_CACHE.get(decorationId)
if (decoration === void 0) {
const res = await chrome.runtime.sendMessage({ kind: 'decoration', decoration: decorationId })
if (!res.success) throw new Error(res.error)

const data = res.data as DecorationData | null
BADGES_CACHE.set(decorationId, decoration = (data ? renderDecoration(data) : null))
}

const data = res.data as DecorationData | null
return data ? renderDecoration(data) : null
return decoration
}

export {
Expand Down
1 change: 0 additions & 1 deletion packages/pronouns/src/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export const LegacyPronouns: Record<string, string | string[]> = {
avoid: 'Avoid pronouns, use my name',
}


export function transformSetsToIdentifier (sets: Sets | null | undefined) {
if (!sets || !sets[0]) {
return 'unspecified'
Expand Down

0 comments on commit c74ad53

Please sign in to comment.