Brain teaser: Dynamic subscription to observables


  • I can’t quite wrap my head around these two...

    Example 1:

    var obsNumber = observable that produce numbers
    var obsText1 = observable that produce text
    var obsText2 = observable that produce text
    var obsITE = IF obsNumber < 42 THEN obsText1 ELSE obsText2

    obsITE is an observable that will return either data from obsText1 or obsText2, depending on the value of obsNumber. However, I cannot figure out how specify this behavior with Rx (I am using RxJS). It seems to me I need to be able to subscribe/unsubscribe dynamically between obsText1 and obsText2.

    Example 2:

    var arrOfObservables = an array of observables
    var obsNumber = observable that produce numbers
    var obsSelect = arrOfObservables[obsNumber]

    obsSelect is an observable, that will return data from the selected observable from the arrOfObservables array, determined by the value produced by obsNumber. Again, it seems like there is a need to dynamically subscribe and unsubscribe.

    Is there a way to make the two examples above work in Rx (RxJS) out of the box, or do I need to create my own extensions to Rx?

    Thanks, Egil

    Friday, June 28, 2013 10:39 AM