The Problem with Cold Observables
So far, you’ve learned that each new subscription to an observable runs the root creation function:
| let myObs$ = new Observable(o => { |
| console.log('Creation Function'); |
| setInterval(() => o.next('hello', Math.random()), 1000); |
| }); |
| |
| myObs$.subscribe(x => console.log('streamA', x)); |
| |
| setTimeout(() => { |
| myObs$.subscribe(x => console.log('streamB', x)); |
| }, 500); |
When you run the above snippet, you see Creation Function logged to the console twice, showing that you’ve created two entirely separate observable streams. Each observable stream sees hello at different times, with separate random numbers attached. Rx does this by default to ensure every stream is isolated from every other stream, ...
Get Build Reactive Websites with RxJS now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.