@remotion/studio
Version:
APIs for interacting with the Remotion Studio
66 lines (65 loc) • 2.36 kB
JavaScript
;
// Cache the thumbnails of the timeline
Object.defineProperty(exports, "__esModule", { value: true });
exports.addFrameToCache = exports.getAspectRatioFromCache = exports.getTimestampFromFrameDatabaseKey = exports.aspectRatioCache = exports.frameDatabase = exports.getFrameDatabaseKeyPrefix = exports.makeFrameDatabaseKey = void 0;
const KEY_SEPARATOR = '|';
const makeFrameDatabaseKey = (src, timestamp) => `${src}${KEY_SEPARATOR}${timestamp}`;
exports.makeFrameDatabaseKey = makeFrameDatabaseKey;
const getFrameDatabaseKeyPrefix = (src) => {
return `${src}${KEY_SEPARATOR}`;
};
exports.getFrameDatabaseKeyPrefix = getFrameDatabaseKeyPrefix;
const MAX_CACHE_SIZE_BYTES = 10 * 1024 * 1024; // 10 MB
let totalCacheSize = 0;
exports.frameDatabase = new Map();
exports.aspectRatioCache = new Map();
const getTimestampFromFrameDatabaseKey = (key) => {
const split = key.split(KEY_SEPARATOR);
return Number(split[split.length - 1]);
};
exports.getTimestampFromFrameDatabaseKey = getTimestampFromFrameDatabaseKey;
const getAspectRatioFromCache = (src) => {
const cached = exports.aspectRatioCache.get(src);
if (cached) {
return cached;
}
return null;
};
exports.getAspectRatioFromCache = getAspectRatioFromCache;
const evictLRU = () => {
while (totalCacheSize > MAX_CACHE_SIZE_BYTES && exports.frameDatabase.size > 0) {
let oldestKey;
let oldestTime = Infinity;
for (const [key, candidate] of exports.frameDatabase) {
if (candidate.lastUsed < oldestTime) {
oldestTime = candidate.lastUsed;
oldestKey = key;
}
}
if (!oldestKey) {
break;
}
const entry = exports.frameDatabase.get(oldestKey);
if (entry) {
totalCacheSize -= entry.size;
entry.frame.close();
exports.frameDatabase.delete(oldestKey);
}
}
};
const addFrameToCache = (key, frame) => {
const existing = exports.frameDatabase.get(key);
if (existing) {
totalCacheSize -= existing.size;
existing.frame.close();
}
const size = frame.allocationSize();
totalCacheSize += size;
exports.frameDatabase.set(key, {
frame,
lastUsed: Date.now(),
size,
});
evictLRU();
};
exports.addFrameToCache = addFrameToCache;