I have an app that consumes a REST service. It makes requests every time someone e.g. clicks a button. However it needs to get a token first and then refresh every 20 minutes thereafter. Here is a marble diagram that represents the desired combining of the streams ...
TOKEN SOURCE ---------A--------------B--------------C----------------D-
REQUEST SOURCE ----1-----------2--3---------4-----------5-------6--------
RESULT SOURCE ---------A1-----A2-A3--------B4----------C5------C6-------
Typically the request source triggers the result which combines with the latest value of the token source, however there is an exception when no items have been emitted on the token stream - in this case the request is buffered until the first token arrives and then it is sent.
The combineLatest
operator is almost there but it triggers when either stream emits. The marble diagram for sample
operator also seems close but it throttles the output based on a time interval which is not what I want.
Which operator/chain of operators would work for this instance?
I'm prototyping with RxJS but I need to implement in RxSwift.
You were close to the solution. The important detail is that Source (2) projects all its elements, while Source (1) does not directly project, and having that extra bit of information can help us discard when only Source (1) changes.
Here's the test setup:
Output:
["A",1] ["A",2] ["A",3] ["A",4] ["B",5] ["B",6] ["B",7] ["B",8] ["C",9]