2016-06-10 00:16:01 -03:00
|
|
|
import { Map } from 'immutable';
|
2016-11-17 15:33:02 -02:00
|
|
|
import { resolvePath } from '../lib/pathHelper';
|
2017-01-10 22:23:22 -02:00
|
|
|
import { ADD_ASSET, REMOVE_ASSET } from '../actions/media';
|
|
|
|
import AssetProxy from '../valueObjects/AssetProxy';
|
2016-06-10 00:16:01 -03:00
|
|
|
|
|
|
|
const medias = (state = Map(), action) => {
|
|
|
|
switch (action.type) {
|
2017-01-10 22:23:22 -02:00
|
|
|
case ADD_ASSET:
|
2016-08-29 17:09:04 -03:00
|
|
|
return state.set(action.payload.public_path, action.payload);
|
2017-01-10 22:23:22 -02:00
|
|
|
case REMOVE_ASSET:
|
2016-06-10 14:01:14 -03:00
|
|
|
return state.delete(action.payload);
|
2016-06-10 18:48:38 -03:00
|
|
|
|
2016-06-10 00:16:01 -03:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export default medias;
|
|
|
|
|
2016-12-27 23:14:19 -08:00
|
|
|
const memoizedProxies = {};
|
2017-01-10 22:23:22 -02:00
|
|
|
export const getAsset = (publicFolder, state, path) => {
|
2016-11-17 15:33:02 -02:00
|
|
|
// No path provided, skip
|
|
|
|
if (!path) return null;
|
|
|
|
|
2016-12-27 23:14:19 -08:00
|
|
|
let proxy = state.get(path) || memoizedProxies[path];
|
|
|
|
if (proxy) {
|
2017-01-10 22:23:22 -02:00
|
|
|
// There is already an AssetProxy in memmory for this path. Use it.
|
2016-12-27 23:14:19 -08:00
|
|
|
return proxy;
|
2016-06-10 00:16:01 -03:00
|
|
|
}
|
2016-11-17 11:12:14 -02:00
|
|
|
|
2017-01-10 22:23:22 -02:00
|
|
|
// Create a new AssetProxy (for consistency) and return it.
|
|
|
|
proxy = memoizedProxies[path] = new AssetProxy(resolvePath(path, publicFolder), null, true);
|
2016-12-27 23:14:19 -08:00
|
|
|
return proxy;
|
2016-06-10 00:16:01 -03:00
|
|
|
};
|