3. Observables
• TC39 proposal to add them to ES2016 (ES7)
• Angular 2 uses them and supports them in a
first class way
• ReactJS will use them and support them in a
first class way
• (Ask me about Ember after :P)
34. Web Socket Connectivity Issues
• If you walked between buildings you lost the
socket connection
• If you closed your lid for too long, you lost the
socket connection
• If the network hiccupped or went down, you lost
the socket connection
• If the server has an issue or restarts, you lose the
socket connection
• If you’re using 4G and your singal gets too low,
you lose the socket connection
36. Multiplexing Web Sockets
• Connect to the socket
• Send a subscription message for each data
stream you care about
• Group the incoming messages by data stream
• Send an unsubscribe message when you’re
done with a stream
• If all streams are disconnected, close the
socket.
37. Reconnecting Multiplexed
Web Sockets Is A Pain
• Reconnect the socket
• Resend all subscriptions for previously
subscribed streams
• If you’re offline, wait for an online event,
and reconnect
• Add a delay and retry again (exponential)
• What if the user switches views and needs
different data streams during this?
38. Observables Are Lazy
• Execute code upon subscription to set up the
underlying data stream
• Execute code upon disposal to teardown the
underlying data stream
39. So you can do things like
• Setup a web socket on subscription
• Close the web socket on disposal
• Send an AJAX request
• Abort the AJAX request on disposal
• Setup an event listener
• Automatically remove event listeners on
disposal
40. This is nice…
• Setup a web socket on subscription
• Close the web socket on disposal
45. How does your data
flow through your app?
• What events cause variables to be produced
• How are those variables used and for what?
• Are you sure you’re containing side-effects?
47. Thinking in streams
• Imagine every variable (not consts) is a stream
• To get it, you need to figure out where it
comes from
• It could come from other variables, or it could
come from an event
• Figure out your side effects, these are the exit
points for your data flow
49. Thinking in streams
var cStream = Observable.combineLatest(aStream, bStream,
(a, b) => a + b);
cStream.subscribe(c => {
// do something with c
});
50. Data Flow Entry and Exit
Common entry points
• User input
• Network I/O
• Properties set
• Events handled
Common exit points
• State
• Persistence
• View Renders
• Emitting events
51. RxJS entry and exit
observable sources for entries
do() and subscribe() for exits
52. The idea is to keep
your data flow contained
And control those side effects
53. Recap
• Any number of values, any amount of time
• Are lazy, don’t do anything until you subscribe
• Embody the set up and tear down of their
underlying data sources
• Can retry/repeat
• Use do() and subscribe() for side-effects
• Observables were designed from the
beginning to be cancellable