|
| 1 | +import { |
| 2 | + createProjectFaviconCache, |
| 3 | + createProjectFaviconImageLoader, |
| 4 | + PROJECT_FAVICON_MAX_DATA_URL_LENGTH, |
| 5 | + PROJECT_FAVICON_THUMBNAIL_SIZE, |
| 6 | + type ProjectFaviconEntry, |
| 7 | +} from "@t3tools/client-runtime/project-favicon-cache"; |
| 8 | +import * as Effect from "effect/Effect"; |
| 9 | + |
| 10 | +import * as MobileDatabase from "../persistence/mobile-database"; |
| 11 | + |
| 12 | +const CACHE_KIND = "project-favicon"; |
| 13 | +const CACHE_SCHEMA_VERSION = 1; |
| 14 | + |
| 15 | +let database: MobileDatabase.MobileDatabase["Service"] | undefined; |
| 16 | + |
| 17 | +/** |
| 18 | + * The cache is a module singleton because the favicon atom family holds it outside |
| 19 | + * any Effect runtime. Its rows live in `client_cache`, so the environment cache store |
| 20 | + * hands over the database it already owns instead of the cache re-entering the runtime. |
| 21 | + */ |
| 22 | +export function attachProjectFaviconDatabase(service: MobileDatabase.MobileDatabase["Service"]) { |
| 23 | + database = service; |
| 24 | +} |
| 25 | + |
| 26 | +const runDatabase = <A, E>( |
| 27 | + use: (database: MobileDatabase.MobileDatabase["Service"]) => Effect.Effect<A, E>, |
| 28 | +) => |
| 29 | + database |
| 30 | + ? Effect.runPromise(use(database)) |
| 31 | + : Promise.reject(new Error("Project icon storage is not attached.")); |
| 32 | + |
| 33 | +/** |
| 34 | + * Rasterizes a bitmap that is too large to inline. The native decoder writes the |
| 35 | + * downsized frame to expo-image's disk cache, which is the only encode path it |
| 36 | + * exposes; the temporary entry is removed once its bytes are read. |
| 37 | + */ |
| 38 | +export async function downscaleProjectFavicon( |
| 39 | + image: { readonly url: string }, |
| 40 | + signal: AbortSignal, |
| 41 | +) { |
| 42 | + const [{ Image }, { File }] = await Promise.all([ |
| 43 | + import("expo-image"), |
| 44 | + import("expo-file-system"), |
| 45 | + ]); |
| 46 | + for (const size of [PROJECT_FAVICON_THUMBNAIL_SIZE, PROJECT_FAVICON_THUMBNAIL_SIZE / 2]) { |
| 47 | + signal.throwIfAborted(); |
| 48 | + const decoded = await Image.loadAsync(image.url, { maxWidth: size, maxHeight: size }); |
| 49 | + const cacheKey = `t3-favicon-thumbnail:${size}:${image.url}`; |
| 50 | + try { |
| 51 | + signal.throwIfAborted(); |
| 52 | + if (decoded.width > size || decoded.height > size) { |
| 53 | + throw new Error("Project icon was not resized."); |
| 54 | + } |
| 55 | + await Image.writeToCacheAsync(decoded, cacheKey); |
| 56 | + const path = await Image.getCachePathAsync(cacheKey); |
| 57 | + if (!path) throw new Error("Project icon thumbnail was not written."); |
| 58 | + const file = new File(path.startsWith("file:") ? path : `file://${path}`); |
| 59 | + try { |
| 60 | + if (file.size > PROJECT_FAVICON_MAX_DATA_URL_LENGTH) continue; |
| 61 | + const base64 = await file.base64(); |
| 62 | + // SDWebImage chooses JPEG for opaque images and PNG for transparency; Glide always writes PNG. |
| 63 | + const mimeType = base64.startsWith("/9j/") |
| 64 | + ? "image/jpeg" |
| 65 | + : base64.startsWith("iVBORw0KGgo") |
| 66 | + ? "image/png" |
| 67 | + : null; |
| 68 | + if (!mimeType) throw new Error("Unsupported project icon thumbnail encoding."); |
| 69 | + const dataUrl = `data:${mimeType};base64,${base64}`; |
| 70 | + if (dataUrl.length <= PROJECT_FAVICON_MAX_DATA_URL_LENGTH) return dataUrl; |
| 71 | + } finally { |
| 72 | + file.delete(); |
| 73 | + } |
| 74 | + } finally { |
| 75 | + decoded.release(); |
| 76 | + } |
| 77 | + } |
| 78 | + throw new Error("Project icon thumbnail exceeds the cache limit."); |
| 79 | +} |
| 80 | + |
| 81 | +/** Rows live in `client_cache` so Settings → Client storage counts and clears them. */ |
| 82 | +export const projectFaviconCache = createProjectFaviconCache({ |
| 83 | + storage: { |
| 84 | + list: () => |
| 85 | + runDatabase((database) => |
| 86 | + database.listCache(CACHE_KIND).pipe( |
| 87 | + Effect.map((payloads) => |
| 88 | + payloads.flatMap((payload): Array<unknown> => { |
| 89 | + try { |
| 90 | + return [JSON.parse(payload)]; |
| 91 | + } catch { |
| 92 | + return []; |
| 93 | + } |
| 94 | + }), |
| 95 | + ), |
| 96 | + ), |
| 97 | + ), |
| 98 | + put: (key, entry: ProjectFaviconEntry) => |
| 99 | + runDatabase((database) => |
| 100 | + database.saveCache( |
| 101 | + entry.environmentId, |
| 102 | + CACHE_KIND, |
| 103 | + key, |
| 104 | + CACHE_SCHEMA_VERSION, |
| 105 | + JSON.stringify(entry), |
| 106 | + ), |
| 107 | + ), |
| 108 | + remove: (key, entry) => |
| 109 | + runDatabase((database) => database.removeCache(entry.environmentId, CACHE_KIND, key)), |
| 110 | + }, |
| 111 | + load: createProjectFaviconImageLoader({ downscale: downscaleProjectFavicon }), |
| 112 | +}); |
0 commit comments