aretherecookies-mobile/js/streams/QuantityStream.js
Bart Akeley 9221b7f9b5 quantities round trip api and stream
make POST requests to /quantity to update the quantity of an existing food item - then mixin the new quantity with the existing food items observable
2017-11-26 19:52:16 -06:00

24 lines
729 B
JavaScript

//@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);
export default observable.scan((quantitiesByFoodItemId: Map<string, QuantityFragment>, quantity?: QuantityResponse) => {
if (!quantity) {
return quantitiesByFoodItemId;
}
return quantitiesByFoodItemId.set(quantity.food_item_id, {
quantity: quantity.quantity,
lastupdated: quantity.date,
});
}, new Map());