How to get the "current" value of an Observer at subscribe-time
Solution 1:
Rx offers both behaviors (as well as others).
The different Rx Subjects available can let you explore the different ways observables can behave:
the
Rx.Subject
is the most basic fire-and-forget variety -- if you were not subscribed when the event happened, then you do not see it.Use
new Rx.BehaviorSubject(undefined)
instead ofSubject
and you get the behavior you were looking for, since aBehaviorSubject
represents a "value that can change"Use
new Rx.ReplaySubject(5)
and you'll get the 5 most recent values as soon as you subscribeUse
new Rx.AsyncSubject()
and you will get nothing until the observable completes at which time you will get the final value (and continue to get the final value if you subscribe again). This is the true Rx analog of Promises, since it produces nothing until it "resolves" (i.e. completes), and afterwards always gives the value to anyone that subscribes.