reactive-programming

RxJava Observing on calling/subscribing thread

不想你离开。 提交于 2019-12-18 10:24:05
问题 I have some trouble understandig how subscribeOn/observeOn works in RxJava. I've created simple app with observable that emits solar system planet names, does some mapping and filtering and prints results. As I understand, scheduling work to background thread is done via subscribeOn operator (and it seems to work fine). Observing on background thread also works fine with observeOn operator. But I have trouble in understanding, how to observe on calling thread (either if it is main thread or

In RxJava, how to pass a variable along when chaining observables?

匆匆过客 提交于 2019-12-18 10:18:06
问题 I am chaining async operations using RxJava, and I'd like to pass some variable downstream: Observable .from(modifications) .flatmap( (data1) -> { return op1(data1); }) ... .flatmap( (data2) -> { // How to access data1 here ? return op2(data2); }) It seems like a common pattern but I couldn't find information about it. 回答1: The advice I got from the Couchbase forum is to use nested observables: Observable .from(modifications) .flatmap( (data1) -> { return op1(data1) ... .flatmap( (data2) -> {

Reactive Extensions (Rx) + MVVM =?

只愿长相守 提交于 2019-12-18 10:01:46
问题 One of the main examples being used to explain the power of Reactive Extensions (Rx) is combining existing mouse events into a new 'event' representing deltas during mouse drag: var mouseMoves = from mm in mainCanvas.GetMouseMove() let location = mm.EventArgs.GetPosition(mainCanvas) select new { location.X, location.Y}; var mouseDiffs = mouseMoves .Skip(1) .Zip(mouseMoves, (l, r) => new {X1 = l.X, Y1 = l.Y, X2 = r.X, Y2 = r.Y}); var mouseDrag = from _ in mainCanvas.GetMouseLeftButtonDown()

Limiting concurrent requests using Rx and SelectMany

大憨熊 提交于 2019-12-18 09:03:13
问题 I have a list of URLs of pages I want to download concurrently using HttpClient . The list of URLs can be large (100 or more!) I have currently have this code: var urls = new List<string> { @"http:\\www.amazon.com", @"http:\\www.bing.com", @"http:\\www.facebook.com", @"http:\\www.twitter.com", @"http:\\www.google.com" }; var client = new HttpClient(); var contents = urls .ToObservable() .SelectMany(uri => client.GetStringAsync(new Uri(uri, UriKind.Absolute))); contents.Subscribe(Console

Spring 5 Web Reactive - How can we use WebClient to retrieve streamed data in a Flux?

[亡魂溺海] 提交于 2019-12-18 08:58:14
问题 The current milestone (M4) documentation shows and example about how to retrieve a Mono using WebClient : WebClient webClient = WebClient.create(new ReactorClientHttpConnector()); ClientRequest<Void> request = ClientRequest.GET("http://example.com/accounts/{id}", 1L) .accept(MediaType.APPLICATION_JSON).build(); Mono<Account> account = this.webClient .exchange(request) .then(response -> response.body(toMono(Account.class))); How can we get streamed data (from a service that returns text/event

Rx back off and retry

做~自己de王妃 提交于 2019-12-18 04:09:31
问题 This is based on the code presented in this SO : Write an Rx "RetryAfter" extension method I am using the code by Markus Olsson (evaluation only at the moment), and before anyone asks I have tried to get hold of Markus on Github, but that is blocked where I work, so I felt the only thing I could do was ask here at SO. Sorry about that, if this sits badly with any one. So I am using the following code, in a small demo which is this: class Attempt1 { private static bool shouldThrow = true;

How do I implement polling using Observables?

血红的双手。 提交于 2019-12-18 03:35:11
问题 I have a parametrized rest call that should be executed every five seconds with different params: Observable<TResult> restCall = api.method1(param1); I need to create an Observable<TResult> which will poll the restCall every 5 seconds with different values for param1. If the api call fails I need to get an error and make the next call in 5 seconds. The interval between calls should be measured only when restCall is finished (success/error). I'm currently using RxJava, but a .NET example would

Spring webflux and reading from database

巧了我就是萌 提交于 2019-12-17 22:15:38
问题 Spring 5 introduces the reactive programming style for rest APIs with webflux. I'm fairly new to it myself and was wondering wether wrapping synchronous calls to a database into Flux or Mono makes sense preformence-wise? If yes, is this the way to do it: @RestController public class HomeController { private MeasurementRepository repository; public HomeController(MeasurementRepository repository){ this.repository = repository; } @GetMapping(value = "/v1/measurements") public Flux<Measurement>

How to create a RxJS buffer that groups elements in NodeJS but that does not rely on forever running interval?

喜你入骨 提交于 2019-12-17 21:07:03
问题 I'm capturing events from an application using Rx.Observable.fromEvent in a NodeJS. These are sent to another server using request (https://www.npmjs.com/package/request). To avoid a high network load I need to buffer those events at a given timeout between sent requests. Problem Using bufferWithTime(200) will keep the node process running and I can't know when the application has finished to close the stream. Is there any way to use Rx buffers to say: When Element 1 is pushed set a timer

Paginate Observable results without recursion - RxJava

回眸只為那壹抹淺笑 提交于 2019-12-17 09:52:35
问题 I've got a pretty standard API pagination problem which you can handle with some simple recursion. Here's a fabricated example: public Observable<List<Result>> scan() { return scanPage(Optional.empty(), ImmutableList.of()); } private Observable<?> scanPage(Optional<KEY> startKey, List<Result> results) { return this.scanner.scan(startKey, LIMIT) .flatMap(page -> { if (!page.getLastKey().isPresent()) { return Observable.just(results); } return scanPage(page.getLastKey(), ImmutableList.<Result