Skip to content

🎥 Make videos programmatically with React

Merge branch 'claude/waveform-thumbnail-cache-35c80c' into claude/filmstrip-zoom-frames

JonnyBurgere01d72f14e7e
diff --cc packages/timeline-utils/src/persistent-cache/persistent-cache.ts
index 3a25c67dd4,889cf411c9..f5f5cc6a1d
--- a/packages/timeline-utils/src/persistent-cache/persistent-cache.ts
+++ b/packages/timeline-utils/src/persistent-cache/persistent-cache.ts
@@@ -53,14 -52,9 +53,9 @@@ type ThumbnailRecord =
readonly image: Blob;
};
- type Fingerprint = {
- readonly value: string;
- readonly maxAgeMs: number | null;
- };
-
type PendingThumbnail = {
readonly src: string;
- readonly aspectRatio: number;
+ readonly aspectRatio: number | null;
readonly durationInSeconds: number | null;
readonly height: number;
readonly timestamp: number;
@@@ -334,40 -329,100 +330,104 @@@ const getReadableDatabase = (src: strin
const transaction = database.transaction(ALL_STORES, 'readwrite');
const sources = transaction.objectStore('sources');
const source: SourceRecord | undefined = await toPromise(sources.get(src));
+ if (!source) {
+ return null;
+ }
+
const now = Date.now();
- const stale =
- source !== undefined &&
- (source.fingerprint !== fingerprint.value ||
- (fingerprint.maxAgeMs !== null &&
- now - source.createdAt > fingerprint.maxAgeMs));
- if (stale) {
+ const usable =
+ staticFingerprint === null
+ ? source.fingerprint !== UNVALIDATED ||
+ now - source.createdAt <= MAX_AGE_WITHOUT_VALIDATOR_MS
+ : source.fingerprint === staticFingerprint;
+ if (!usable) {
deleteSourceData(transaction, src);
+ await transactionDone(transaction);
+ return null;
}
- const record: SourceRecord =
- source && !stale
- ? {...source, lastAccessedAt: now}
- : {
- src,
- fingerprint: fingerprint.value,
- createdAt: now,
- lastAccessedAt: now,
- aspectRatio: null,
- durationInSeconds: null,
- waveformBytes: 0,
- thumbnailBytes: 0,
- };
- sources.put(record);
+ sources.put({...source, lastAccessedAt: now} satisfies SourceRecord);
await transactionDone(transaction);
- if (record.aspectRatio !== null && !aspectRatioCache.has(src)) {
- aspectRatioCache.set(src, record.aspectRatio);
+ if (source.aspectRatio !== null && !aspectRatioCache.has(src)) {
+ aspectRatioCache.set(src, source.aspectRatio);
+ }
+
++ if (source.durationInSeconds !== null && !mediaDurationCache.has(src)) {
++ mediaDurationCache.set(src, source.durationInSeconds);
++ }
++
+ if (staticFingerprint === null) {
+ getRemoteFingerprint(src)
+ .then((fingerprint) => {
+ if (fingerprint === null || fingerprint === source.fingerprint) {
+ return;
+ }
+
+ const purge = database.transaction(ALL_STORES, 'readwrite');
+ deleteSourceData(purge, src);
+ return transactionDone(purge);
+ })
+ .catch(() => undefined);
}
- if (record.durationInSeconds !== null && !mediaDurationCache.has(src)) {
- mediaDurationCache.set(src, record.durationInSeconds);
+ scheduleEviction();
+ return database;
+ })().catch(() => null);
+ readableSources.set(key, promise);
+ return promise;
+ };
+
+ // Resolves to the database once `src` has a record that new data can be
+ // added to. Creating one requires the fingerprint of the source.
+ const getWritableDatabase = (src: string): Promise<IDBDatabase | null> => {
+ if (!isCacheable(src)) {
+ return Promise.resolve(null);
+ }
+
+ const staticFingerprint = getStaticFileFingerprint(src);
+ const key = `${staticFingerprint ?? 'remote'}|${src}`;
+ const existing = writableSources.get(key);
+ if (existing) {
+ return existing;
+ }
+
+ const promise = (async () => {
+ // Avoid the fingerprint request if there is no database to use it with.
+ const database = await openDatabase();
+ if (!database) {
+ return null;
+ }
+
+ // Also purges data of a source that has changed.
+ if (await getReadableDatabase(src)) {
+ return database;
+ }
+
+ const fingerprint = staticFingerprint ?? (await getRemoteFingerprint(src));
+ if (fingerprint === null) {
+ return null;
}
+ const transaction = database.transaction(ALL_STORES, 'readwrite');
+ const sources = transaction.objectStore('sources');
+ const current: SourceRecord | undefined = await toPromise(sources.get(src));
+ if (current?.fingerprint !== fingerprint) {
+ deleteSourceData(transaction, src);
+ const now = Date.now();
+ sources.put({
+ src,
+ fingerprint,
+ createdAt: now,
+ lastAccessedAt: now,
+ aspectRatio: null,
+ durationInSeconds: null,
+ waveformBytes: 0,
+ thumbnailBytes: 0,
+ } satisfies SourceRecord);
+ }
+
+ await transactionDone(transaction);
scheduleEviction();
return database;
})().catch(() => null);