2017-11-26 19:52:16 -06:00
|
|
|
//@flow
|
|
|
|
|
import { ReplaySubject } from 'rxjs';
|
|
|
|
|
import type { QuantityResponse, QuantityFragment } from '../constants/QuantityConstants';
|
|
|
|
|
import { Map } from 'immutable';
|
|
|
|
|
|
|
|
|
|
const observable: ReplaySubject<QuantityResponse> = new ReplaySubject();
|
|
|
|
|
|
|
|
|
|
export function emit(val: ?QuantityResponse) {
|
|
|
|
|
observable.next(val);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// force our observable to emit an initial empty map so that food items will load
|
|
|
|
|
emit(null);
|
|
|
|
|
|
2020-04-17 22:50:23 -05:00
|
|
|
export default observable.scan(
|
|
|
|
|
(quantitiesByProductId: Map<string, QuantityFragment>, quantity?: QuantityResponse) => {
|
|
|
|
|
if (!quantity) {
|
|
|
|
|
return quantitiesByProductId;
|
|
|
|
|
}
|
2017-11-26 19:52:16 -06:00
|
|
|
|
2020-04-17 22:50:23 -05:00
|
|
|
return quantitiesByProductId.set(quantity.food_item_id, {
|
|
|
|
|
quantity: quantity.quantity,
|
|
|
|
|
lastupdated: quantity.date,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
new Map()
|
|
|
|
|
);
|