L21 Streams and subscriptions
22 minSuggested first: L20 Reactive dependencies
Outcome
Follow values through an operator and identify the effect of completion or unsubscribe.
Learn
A stream can deliver values over time. A subscription connects an observer to a producer. An operator can transform or filter what reaches the observer. Completion ends delivery normally; an error ends it abnormally.
Unsubscribing stops future delivery to that observer. Don't assume it sends a completion notification, or stops shared upstream work.
Worked example
The source emits 1, 2 and 3, then completes. map doubles each value, then filter keeps values greater than 3. What does the observer see?
Source: 1 2 3 complete
After map: 2 4 6 complete
Observer: 4 6 complete
Predict first, then show the worked explanation
Doubling gives 2, 4 and 6; the filter drops 2, so the observer sees 4, 6, then completion. If you reverse the two operators, no source value passes the filter first, so the observer sees only completion.
Practice and check
Common mistake
Assuming every stream is asynchronous, shared, or replayed. Check the producer and the exact subscription behavior.
Practice on a real repository
Try the first puzzle below; after it, return to the second if you skipped it in L11.
- The errors the single operator raises itselfArchitecture · Easy · reactivex-rxjs
- How forkJoin recognizes a dictionary of sourcesTrace · Easy · reactivex-rxjs
Related topics: Observables
Practice is optional. Solving a puzzle doesn't complete a lesson, and lessons never change a puzzle's points.
References
- RxJS source and documentation (ReactiveX)