aretherecookies-mobile/js/streams/ImagesStream.js
2020-04-17 22:50:23 -05:00

27 lines
831 B
JavaScript

//@flow
import { Map, OrderedSet } from 'immutable';
import { ReplaySubject } from 'rxjs';
import { buildImageRecord, type ImageFragment } from '../records/ImageRecord';
import type { ImageRaw } from '../records/ImageRecord';
const observable: ReplaySubject<ImageFragment> = new ReplaySubject();
export function emit(val: ?ImageRaw) {
observable.next(val);
}
// force our observable to emit an initial empty map so that food items will load
emit(null);
export default observable.scan((imagesByProductId: Map<string, ImageFragment>, image: ImageRaw) => {
if (!image || !image.food_item_id) {
return imagesByProductId;
}
return imagesByProductId.update(image.food_item_id, ({ images = OrderedSet() } = {}) => {
return {
id: image.food_item_id,
images: images.add(buildImageRecord(image)),
};
});
}, new Map());