Angular Observables
for Fun and Profit
Agenda
- What are observables?
- vs Promises
- Creating a simple observable data stream
- Subscribing to an observable
- Data leaks and unsubscription
- Basic operators
- Observable pipe
- Operator params: log, Boolean
- Hot vs cold
- Multiple subscriptions
- The async pipe
Gil Steiner
FE/FS developer
Game developer &
designer
il.linkedin.com/in/gil-steiner
gilsteiner.com
gamesgil@github
What are observables?
- Functional solution for handling data streams -
not another type of promise!
- The core part of the RxJS library
- Not specific to Angular
Observables vs Promises
- Promises are intended to solve a problem of “what to do when an
async operation completes?”
- Partially solve “callback hell”
- Observables handle data streams: how to relate to?
- Allow manipulating the stream (data, behavior)
- Allow switching and merging multiple streams
- Introduce a concise chain (pipe) of operators
- Extensible & modular
Observables vs Promises
- Promises are intended to solve a problem of “what to do when an
async operation completes?”
- Partially solve “callback hell”
- Observables handle data streams: how to relate to?
- Allow manipulating the stream (data, behavior)
- Allow switching and merging multiple streams
- Introduce a concise chain (pipe) of operators
- Extensible & modular
Observable structure
Observable - emitting
Observer - handling
Subscription - reading
Observable structure
obs$ = Observable.create(observer => {
observer.next(...);
observer.next(...);
observer.complete(); // observer.error(...)
}
sub = obs$.subscribe(value => …)
sub.unsubscribe();
A simpler method of creation
import {...} from ‘rxjs’;
of(value)
from(values)
fromEvent(target, event)
generate(...)
StackBlitz
Cold Observables
One shot - of(<value>)
Hot Observables
Perpetual / Multicast - interval
Multicasting - Multiple Subscriptions
Subscribing to a cold observable - late subscription
Hot observable - value changes
Subjects & BehaviorSubject
- Are both observers and observables
- BehaviorSubject allows caching the last value for late subscribers
Operators in the Pipe
Observable.pipe(
rxjsOp1,
rxjsOp2,
…
)
tap (was ‘do’)
Do something related or unrelated to the value
StackBlitz
map / mapTo
Returns an observable with a value based on the current value - not
to be confused with Array.prototype.map
MapTo returns an observable regardless of the current value
StackBlitz
filter
Allows data passing only if adheres to condition
StackBlitz
delay
Emit a value after a given amount of time
StackBlitz
Throwing & Catching
throwError operator
2nd parameter in subscription
StackBlitz
Preventing Memory Leaks
Direct unsubscribe
The async pipe
- take
- takeWhile
- takeUntil
- untilDestroyed
The async pipe
Allows handling observables directly in the template
Auto unsubscribes!
Allows adding Angular async pipes dynamically
StackBlitz

Angular observables for fun and profit

  • 1.
  • 2.
    Agenda - What areobservables? - vs Promises - Creating a simple observable data stream - Subscribing to an observable - Data leaks and unsubscription - Basic operators - Observable pipe - Operator params: log, Boolean - Hot vs cold - Multiple subscriptions - The async pipe
  • 3.
    Gil Steiner FE/FS developer Gamedeveloper & designer il.linkedin.com/in/gil-steiner gilsteiner.com gamesgil@github
  • 4.
    What are observables? -Functional solution for handling data streams - not another type of promise! - The core part of the RxJS library - Not specific to Angular
  • 5.
    Observables vs Promises -Promises are intended to solve a problem of “what to do when an async operation completes?” - Partially solve “callback hell” - Observables handle data streams: how to relate to? - Allow manipulating the stream (data, behavior) - Allow switching and merging multiple streams - Introduce a concise chain (pipe) of operators - Extensible & modular
  • 6.
    Observables vs Promises -Promises are intended to solve a problem of “what to do when an async operation completes?” - Partially solve “callback hell” - Observables handle data streams: how to relate to? - Allow manipulating the stream (data, behavior) - Allow switching and merging multiple streams - Introduce a concise chain (pipe) of operators - Extensible & modular
  • 7.
    Observable structure Observable -emitting Observer - handling Subscription - reading
  • 8.
    Observable structure obs$ =Observable.create(observer => { observer.next(...); observer.next(...); observer.complete(); // observer.error(...) } sub = obs$.subscribe(value => …) sub.unsubscribe();
  • 9.
    A simpler methodof creation import {...} from ‘rxjs’; of(value) from(values) fromEvent(target, event) generate(...) StackBlitz
  • 10.
  • 11.
    Hot Observables Perpetual /Multicast - interval
  • 12.
    Multicasting - MultipleSubscriptions Subscribing to a cold observable - late subscription Hot observable - value changes
  • 13.
    Subjects & BehaviorSubject -Are both observers and observables - BehaviorSubject allows caching the last value for late subscribers
  • 14.
    Operators in thePipe Observable.pipe( rxjsOp1, rxjsOp2, … )
  • 15.
    tap (was ‘do’) Dosomething related or unrelated to the value StackBlitz
  • 16.
    map / mapTo Returnsan observable with a value based on the current value - not to be confused with Array.prototype.map MapTo returns an observable regardless of the current value StackBlitz
  • 17.
    filter Allows data passingonly if adheres to condition StackBlitz
  • 18.
    delay Emit a valueafter a given amount of time StackBlitz
  • 19.
    Throwing & Catching throwErroroperator 2nd parameter in subscription StackBlitz
  • 20.
    Preventing Memory Leaks Directunsubscribe The async pipe - take - takeWhile - takeUntil - untilDestroyed
  • 21.
    The async pipe Allowshandling observables directly in the template Auto unsubscribes! Allows adding Angular async pipes dynamically StackBlitz