A way to emit items after sourceObservable completed - rx-java2

Is there a way to wrap Observable in a way that chain will switch to another Observable, the way it works with Maybe.switchIfEmpty(anotherMaybe), but something like
sourceObservable
.switchOnComplete(anotherObservable)

I just found that Observable.concatWith() is exactly what I needed.

Related

In RxSwift, how can I unit test that an observable sent no event?

I am new to testing un RxSwift.
I manage to test that an Observable emits values with RxBlocking. But I can't find the syntax to make a test pass if an Observable did not send any value.
I think I should use toBlocking(timeout: ),but I can't find how (I have an error that it timed out, but the test fail).
Thank you for your help
Does your observable complete? It looks like you're testing to see if the observable emits any events not values
Assuming you are testing on the XCTest Framework,
If you are testing to see if the observable emits no elements before it completes,
XCTAssertNil(try observable.toBlocking().first())
If you are testing to see if the observable does not emit any elements, nor completes
// Check error if necessary
XCTAssertThrowsError(try observable.toBlocking(timeout: timeout).first())

reactive 4.2.0 net Subject<T> ReplaySubject<T>

I am using ReplaySubject as a pipe when I feed the Observer with OnNext messages and then I subscribe to the message in another part of the code.
I tried ISubject first, by using Subject.Create(observer:, observable:) but this seems to do nothing, or in other words, the Observer is a sink that goes nowhere. Is there some code that will make the Subject do something pipe-like. For example, I want to inject onNext messages with a integral countN and I want the observable to broadcast N-many 0.00E0(s) in sequence.
UPDATE:
It seems I need to implement the ISubject interface. Is there source code for a simple implementation that functions the same as ReplaySubject, with simple, proper memory handling of buffer size and Observer collection and Disposable return object.
Ok, so you use
new Subject<Int32>()
to get a subject that is a pipe-line.
Someone care to explain what Subject.Create does?
Can you inherit from Subject or ReplaySubject? Should you be able to?

How to convert RxSwift's Single to Observable and ignore `complete` event?

The case is following.
I have a Single that I receive from a third-party API.
I want to transform this single to Observable, but the issue is Single transformed to Observable still completes my observable chain after emitting a value which I don't want to happen. I want that my observable never completes.
The question is is there any way easier than 'single.flatMap{ Observable.just($0)}' to do what I want?
I'd love to see some more code, because single.flatMap { Observable.just($0) } will not stop a completion event from happening so if you think it does, then something's wrong.
Frankly, .flatMap { Observable.just($0) } does nothing at all, i.e., you could remove it completely and not change your code at all.
That said, the most obvious way to stop a completed event is single.concat(Observable.never()).

RxSwift throttle() to get first element

I am looking for a way in which the throttle() operator gives up the first element in an Observable within a given time-interval, rather than the last element.
A similar question has been asked (and answered) in RxSwift - Debounce/Throttle "inverse", but there is an imperfection in those answers that I would like to avoid. That is, in an ideal scenario, the first element emitted in the Observable is obtained and any future ones are ignored for the duration of the time-interval. However, when working with window() or timer(), it is possible that two elements proceed within the given time-interval, because the timer happened not to run parallel to the start of the throttle() call. Example:
|..........|..........|..........|
^ ^
first second
They fall into different windows and are therefore both accepted by the imperfect solution described in the linked answer.
Ideally, there would be a way to restart the timer as soon as the first element in a window comes in, so that the above example would instead look something like:
|..........|.......|..........|
^ ^
first second (ignored)
Any ideas?
Edit: to be clear, I am not sure how the throttle() variety of RxSwift 3.0-beta1 handles this, but I am looking for a solution for RxSwift 2.x implementations.
As it turns out, rxSwift 3.0 deals with throttle() in exactly the way I desired. It takes an element and then ignores any further elements for x seconds (without having the 'window problem' I alluded to in the question).
For the sake of completeness: debounce() still works in accordance with rxSwift's 'old' definition of throttling. That is, it takes the last item in a time-interval.

Create BehaviorSubject from Observable

Say I have an observableA which gets data from network and emits it, if I do subscription to this observable each time it tries to request data from network which is heavy.
I want to create BehaviorSubject and connect it to observableA, so that any other threads/objects will subscribe to BehaviorSubject in order to get latest emitted data.
So far I couldnt manage it to code. I cannot create empty BehaviorSubject and call inside observableA, because they are not related to each other.
I cannot subscribe to observableA and get BehaviorSubject as observer, any idea how can I accomplish it? or maybe even better?
You can use multicast. E.g.,
Observable<String> o = ...;
ConnectableObservable<String> co = o.multicast(BehaviorSubject.<String> create());
co.connect();
co.subscribe(...);
co.subscribe(...);
co.subscribe(...);
I had similar scenario in javascript where I wrapped promise within Rx and I didn't want to make network call again when I subscribe to observable. This is how I ended up implementing:
subject = new Rx.AsyncSubject();
observable = Rx.Observable.fromPromise(....).subscribe(subject);
You can now consume the promise response as subject.subscribe(...) and it will always emit promise result from subject as we are using AsyncSubject (AsyncSubject - Represents the result of an asynchronous operation. The last value before the OnCompleted notification, or the error received through OnError, is sent to all subscribed observers).
In Rx.NET we'd use one of the Publish overloads that accepts an initialValue parameter. Essentially, it's just a shorter way of doing a multicast like zsxwing described. I'm not sure whether Rx.Java offers these overloaded Publish methods.