mirror of
https://gitlab.com/wheres-the-tp/ui-mobile.git
synced 2026-01-25 09:54:55 -06:00
27 lines
809 B
JavaScript
27 lines
809 B
JavaScript
//@flow
|
|
import { Map, Set } from 'immutable';
|
|
import { ReplaySubject } from 'rxjs';
|
|
import ImageRecord, { buildImageRecord } from '../records/ImageRecord';
|
|
import type { ImageRaw } from '../records/ImageRecord';
|
|
|
|
const observable: ReplaySubject<ImageRecord> = 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((imagesByFoodItemId: Map<string, ImageRecord>, image: ImageRaw) => {
|
|
if (!image || !image.food_item_id) {
|
|
return imagesByFoodItemId;
|
|
}
|
|
|
|
return imagesByFoodItemId.update(image.food_item_id, ({ images = Set() } = {}) => {
|
|
return {
|
|
id: image.food_item_id,
|
|
images: images.add(buildImageRecord(image)),
|
|
};
|
|
});
|
|
}, new Map());
|