Memoize media proxies since we look them up a lot when doing previews

This commit is contained in:
Mathias Biilmann Christensen
2016-12-27 23:14:19 -08:00
parent 06218e55eb
commit 5ff2942435

View File

@ -17,15 +17,18 @@ const medias = (state = Map(), action) => {
export default medias; export default medias;
const memoizedProxies = {};
export const getMedia = (publicFolder, state, path) => { export const getMedia = (publicFolder, state, path) => {
// No path provided, skip // No path provided, skip
if (!path) return null; if (!path) return null;
if (state.has(path)) { let proxy = state.get(path) || memoizedProxies[path];
if (proxy) {
// There is already a MediaProxy in memmory for this path. Use it. // There is already a MediaProxy in memmory for this path. Use it.
return state.get(path); return proxy;
} }
// Create a new MediaProxy (for consistency) and return it. // Create a new MediaProxy (for consistency) and return it.
return new MediaProxy(resolvePath(path, publicFolder), null, true); proxy = memoizedProxies[path] = new MediaProxy(resolvePath(path, publicFolder), null, true);
return proxy;
}; };