Observables - the why, what & how

24
Observables the why, what and how… Filip Bruun Bech-Larsen Frontend Manager at IMPACT @filipbech

Transcript of Observables - the why, what & how

Page 1: Observables - the why, what & how

Observablesthe why, what and how…

Filip Bruun Bech-LarsenFrontend Manager at IMPACT@filipbech

Page 2: Observables - the why, what & how

Filip

Page 3: Observables - the why, what & how
Page 4: Observables - the why, what & how

WE ARE HIRING IN CPH !!!

Page 5: Observables - the why, what & how

Who knows observables

already?

Page 6: Observables - the why, what & how

Why• Can make your code easier to write and read

• Helps you handle asynchronous events

• Handles multiple values

• Array + Promise => FP over time

Page 7: Observables - the why, what & how

Thinking reactively• A stream of form-submits => ajax responds

=> succesmessages

• A stream of mousedowns => stream of mousemoves => instructions to DOM (drag-drop)

• A stream of basket-clicks => ajax responds => new basket states

Page 8: Observables - the why, what & how

Whats so cool about it

• Clean code with fewer exceptions (its a stream)

• Combining observables

• Cancellation

• They are lazy (by default)

• They can be shared

Page 9: Observables - the why, what & how

and operators

• but don’t worry about those for now

• (the 40+ operators are why people think observables are hard)

Page 10: Observables - the why, what & how

How

• Might be standard (not the operators) someday

• Today: RxJS (we use v5 beta)

Page 11: Observables - the why, what & how

How to use an observable

Page 12: Observables - the why, what & how

How to use an observable

subscribe is like a forEach of then’s…

Page 13: Observables - the why, what & how

Where do they come from

• Rx.Observable.create()

• Rx.Subject()

• Rx.Observable.from()• Rx.Observable.of()• Rx.Observable.interval()

Page 14: Observables - the why, what & how

lets write some code

create interval observable with diff techniques

Page 15: Observables - the why, what & how
Page 16: Observables - the why, what & how
Page 17: Observables - the why, what & how
Page 18: Observables - the why, what & how
Page 19: Observables - the why, what & how

Things to (possibly) cover

• Map => switch, merge, concat

• Other operators

• Catching errors

• Sharing is caring

Page 20: Observables - the why, what & how
Page 21: Observables - the why, what & how

Angular 1.x• Use it when you want to be notified - basketupdate$

• Classic observable use cases (n values over n amount of time) - searchword$ (where its more of an implementation detail than a feature)

• $Ohttp (wraps $http in an observable - supports cancellation) https://gist.github.com/filipbech/98160440663c4f64e6f5fab08c97dc79

• Start with Rx.Subject()’s if you are used to deferred’s

• Remember to unsubscribe in $onDestroy or on the $destroy-event

Page 22: Observables - the why, what & how
Page 23: Observables - the why, what & how

Thank you for your attention

any questions?

@filipbech

Page 24: Observables - the why, what & how