132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 1 | export interface AvatarCache { |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 2 | match(key: string): Promise<Response | undefined>; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 3 | put(key: string, response: Response): Promise<void>; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 4 | } |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 5 | |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 6 | export function createAvatarLoader( |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 7 | cache: () => Promise<AvatarCache>, |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 8 | load: (mxc: string) => Promise<Response> |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 9 | ): (key: string, mxc: string) => Promise<Response> { |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 10 | const pending = new Map<string, Promise<Response>>(); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 11 | |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 12 | return async (key, mxc) => { |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 13 | const active = pending.get(mxc); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 14 | if (active) return (await active).clone(); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 15 | |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 16 | const request = (async () => { |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 17 | let store: AvatarCache | null = null; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 18 | try { |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 19 | store = await cache(); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 20 | const cached = await store.match(key); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 21 | if (cached) return cached; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 22 | } catch { |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 23 | store = null; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 24 | } |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 25 | |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 26 | const response = await load(mxc); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 27 | if (response.ok && store) { |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 28 | try { |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 29 | await store.put(key, response.clone()); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 30 | } catch { |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 31 | return response; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 32 | } |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 33 | } |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 34 | return response; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 35 | })(); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 36 | pending.set(mxc, request); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 37 | try { |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 38 | return (await request).clone(); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 39 | } finally { |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 40 | if (pending.get(mxc) === request) pending.delete(mxc); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 41 | } |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 42 | }; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 43 | } |