reactive programming - RxJava- Placing several Observable values in a static body of text? -
i have heard several reactive programming folks "don't break monad, continue it". see merit in this. there still instances i'm confused about, when observable consumed or subscribed to. more confusing when several observables have consumed @ once, , doesn't feel practical combine them single observable.
let's instance have trackedaircraft type. of properties final while other properties observable.
public interface trackedaircraft { public int getflightnumber(); public int getcapacity(); public observable<string> getorigin(); public observable<string> getdestination(); public observable<integer> getmileage(); public observable<point> getlocation(); } i wire gui pretty easily, , subscribe controls updated each emission of each property. email or body of static text? not straightforward because way can think of not breaking monad combine observables sounds pain if have observable emitting trackedflights.
is okay block in situation? or there more monadic way accomplish haven't thought of?
public static string generateemailreportforflight(trackedaircraft flight) { return new stringbuilder().append("flight number: ").append(flight.getflightnumber()).append("\r\n") .append("capacity: ").append(flight.getcapacity()).append("\r\n") .append("origin: ").append(flight.getorigin() /*what do here without blocking?*/) .append("destination: ").append(flight.getdestination() /*what do here without blocking?*/) .append("mileage: ").append(flight.getmileage() /*what do here without blocking?*/) .append("location: ").append(flight.getlocation() /*what do here without blocking?*/) .tostring(); } ///
observable<trackedaircraft> trackedflights = ...; trackedflights.map(f -> generateemailreportforflight(f));
you can use flatmap + combinelatest:
observable<trackedaircraft> trackedflights = ... trackedflights .flatmap(flight -> emailreport(flight)) .subscribe(msg -> sendemail(msg)); observable<string> emailreport(trackedaircraft flight) { return observable.combinelatest( flight.getorigin(), flight.getdestination(), flight.getmileage(), flight.getlocation() (origin, destination, mileage, location) -> { return new stringbuilder() .append("flight number: ").append(flight.getflightnumber()) .append("\r\n") .append("capacity: ").append(flight.getcapacity()) .append("\r\n") .append("origin: ").append(origin) .append("\r\n") .append("destination: ").append(destination) .append("\r\n") .append("mileage: ").append(mileage) .append("\r\n") .append("location: ").append(location) .tostring(); } ) }
Comments
Post a Comment