-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
executable file
·368 lines (355 loc) · 17 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Angular 2: Intruduction to the RxJS</title>
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/night.css">
<link rel="stylesheet" href="css/styles.css">
<!-- Theme used for syntax highlighting of code -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<h1>Angular 2:</h1>
<h3>Intruduction to the RxJS</h3>
</section>
<section>
<section><h2>Reactive Programming</h2></section>
<section>
<blockquote cite="https://en.wikipedia.org/wiki/Reactive_programming">
"In computing, reactive programming is a programming paradigm oriented around data flows and the propagation of change."
<i style="text-align: right;">- Wikipedia</i>
</blockquote>
</section>
<section>
<blockquote cite="https://gist.github.com/staltz/868e7e9bc2a7b8c1f754">
"Reactive programming is programming with asynchronous data streams."
<i style="text-align: right;">- Andre Staltz</i>
</blockquote>
</section>
<section>
<div style="float: left; width: 40%" >
<h5> Non-reactive way </h5>
<pre><code class="hljs javascript">
var a = 5;
var b = 10;
var c = a + b;
a = 10;
c == 20; // false
</code></pre>
</div>
<div style="float: right; width: 40%" >
<h5> Reactive way </h5>
<pre><code class="hljs javascript">
var a = 5;
var b = 10;
var c = a + b;
a = 10;
c == 20; // true
</code></pre>
</div>
</section>
<section>
<h3> Object-oriented reactive programming </h3>
<p>Instead of methods and fields, objects have reactions that automatically re-evaluate when the other reactions they depend on have been modified.</p>
</section>
<section>
<h3> Functional reactive programming </h3>
<p> Functional Reactive Programming is a variant of Reactive Programming that follows Functional Programming principles such as referential transparency, and seeks to be purely functional. </p>
<a href="https://medium.com/@andrestaltz/why-i-cannot-say-frp-but-i-just-did-d5ffaa23973b#.8sx7elq7v">Why I cannot say FRP but I just did</a>
</section>
<section>
<h3>Implementations in JS:</h3>
<ul>
<li class="fragment"> <strong> Bacon.js - </strong> small functional reactive programming lib for JavaScript. <iframe src="https://ghbtns.com/github-btn.html?user=baconjs&repo=bacon.js&type=star&count=true" frameborder="0" scrolling="0" width="170px" height="20px"></iframe></li>
<li class="fragment"> <strong> Cycle.js - </strong> fully reactive JavaScript framework for Human-Computer Interaction. <iframe src="https://ghbtns.com/github-btn.html?user=cyclejs&repo=core&type=star&count=true" frameborder="0" scrolling="0" width="170px" height="20px"></iframe></li>
<li class="fragment"> <strong> Elm - </strong> type inferred, functional reactive language that compiles to HTML, CSS, and JavaScript. <iframe src="https://ghbtns.com/github-btn.html?user=elm-lang&repo=elm-compiler&type=star&count=true" frameborder="0" scrolling="0" width="170px" height="20px"></iframe></li>
<li class="fragment"> <strong> Kefir </strong> - reactive programming library for JavaScript. <iframe src="https://ghbtns.com/github-btn.html?user=rpominov&repo=kefir&type=star&count=true" frameborder="0" scrolling="0" width="170px" height="20px"></iframe></li>
<li class="fragment"> <strong> RxJS </strong> - reactive extensions for JavaScript. <iframe src="https://ghbtns.com/github-btn.html?user=Reactive-Extensions&repo=RxJS&type=star&count=true" frameborder="0" scrolling="0" width="170px" height="20px"></iframe></li>
</ul>
</section>
</section>
<section>
<section>
<h2>ReactiveX</h2>
<p class="fragment">ReactiveX is more than an API, it's an idea and a breakthrough in programming. It has inspired several other APIs, frameworks, and even programming languages.</p>
</section>
<section>
<h4 id="languages">Languages</h4>
<ul>
<li>Java: <a href="https://github.com/ReactiveX/RxJava">RxJava</a></li>
<li>JavaScript: <a href="https://github.com/Reactive-Extensions/RxJS">RxJS</a></li>
<li>C#: <a href="https://github.com/Reactive-Extensions/Rx.NET">Rx.NET</a></li>
<li>C#(Unity): <a href="https://github.com/neuecc/UniRx">UniRx</a></li>
<li>Scala: <a href="https://github.com/ReactiveX/RxScala">RxScala</a></li>
<li>Clojure: <a href="https://github.com/ReactiveX/RxClojure">RxClojure</a></li>
<li>C++: <a href="https://github.com/Reactive-Extensions/RxCpp">RxCpp</a></li>
<li>Ruby: <a href="https://github.com/Reactive-Extensions/Rx.rb">Rx.rb</a></li>
<li>Python: <a href="https://github.com/ReactiveX/RxPY">RxPY</a></li>
<li>Groovy: <a href="https://github.com/ReactiveX/RxGroovy">RxGroovy</a></li>
<li>JRuby: <a href="https://github.com/ReactiveX/RxJRuby">RxJRuby</a></li>
<li>Kotlin: <a href="https://github.com/ReactiveX/RxKotlin">RxKotlin</a></li>
<li>Swift: <a href="https://github.com/kzaher/RxSwift">RxSwift</a></li>
<li>PHP: <a href="https://github.com/ReactiveX/RxPHP">RxPHP</a></li>
</ul>
</section>
<section>
<h4 id="reactivex-for-platforms-and-frameworks">ReactiveX for platforms and frameworks</h4>
<ul>
<li><a href="https://github.com/ReactiveX/RxNetty">RxNetty</a></li>
<li><a href="https://github.com/ReactiveX/RxAndroid">RxAndroid</a></li>
<li><a href="https://github.com/kzaher/RxSwift">RxCocoa</a></li>
</ul>
</section>
<section>
<h3>Who uses ReactiveX?</h3>
<div style="float: right; width: 50%" >
<a href="http://rx.codeplex.com/" class="frontpage-logo"><img src="images/microsoft-logo.png"></a>
<a href="http://netflix.github.io" class="frontpage-logo"><img src="images/netflix-logo.png"></a>
<a href="http://github.com/" class="frontpage-logo"><img src="images/github-logo.png"></a>
</div>
<div style="float: left; width: 50%" >
<a href="http://soundcloud.com/" class="frontpage-logo"><img src="images/soundcloud-logo.png"></a>
<a href="https://www.trello.com" class="frontpage-logo"><img src="images/trello-logo.png"></a>
<a href="https://www.airbnb.com/" class="frontpage-logo"><img src="images/airbnb-logo.jpg"></a>
</div>
</section>
</section>
<section>
<section>
<h2>RxJS</h2>
<p>Set of libraries to compose asynchronous and event-based programs using observable collections and Array#extras style composition in JavaScript</p>
</section>
<section>
<h3>RxJS 4.0 vs 5.0</h3>
<p>RxJS 5 is a ground-up rewrite of RxJS that actually began development when RxJS was in 2.0. This new version of RxJS had
three basic goals:</p>
<ol>
<li>Better performance</li>
<li>Better debugging</li>
<li>Compliance with the <a href="https://github.com/zenparsing/es-observable">ES7 Observable Spec</a></li>
</ol>
</section>
<section>
<h3>Differences:</h3>
<ul>
<li>Observer Interface Changes (.onNext(value) -> .next(value))</li>
<li>Subscription dispose is unsubscribe</li>
<li>Operators Renamed or Removed (flatMapLatest -> switchMap, where -> filter)</li>
<li>Operator Splits (map -> map and mapTo)</li>
</ul>
<br><br><br>
<a href="https://github.com/ReactiveX/rxjs/blob/master/MIGRATION.md" target="_blank"> Migrating from RxJS 4 to 5 </a>
</section>
<section>
<p>The essential concepts in RxJS which solve async event management are:</p>
<ul>
<li class="fragment"><strong>Observable:</strong> represents the idea of an invokable collection of future values or events.</li>
<li class="fragment"><strong>Observer:</strong> is a collection of callbacks that knows how to listen to values delivered by the Observable.</li>
<li class="fragment"><strong>Subscription:</strong> represents the execution of an Observable, is primarily useful for cancelling the execution.</li>
<li class="fragment"><strong>Operators:</strong> are pure functions that enable a functional programming style of dealing with collections with operations like <code>map</code>, <code>filter</code>, <code>concat</code>, <code>flatMap</code>, etc.</li>
<li class="fragment"><strong>Subject:</strong> is the equivalent to an EventEmitter, and the only way of multicasting a value or event to multiple Observers.</li>
<li class="fragment"><strong>Schedulers:</strong> are centralized dispatchers to control concurrency, allowing us to coordinate when computation happens on e.g. <code>setTimeout</code> or <code>requestAnimationFrame</code> or others.</li>
</ul>
</section>
</section>
<section>
<section>
<h1>Observable</h1>
<video src="./observable.webm" autoplay loop>
Your browser does not support the <code>video</code> element.
</video>
</section>
<section>
<h3>Creating observable</h3>
<pre><code class="hljs javascript">
var observable = Rx.Observable.create(function (observer) {
observer.next(1);
observer.next(2);
observer.next(3);
setTimeout(() => {
observer.next(4);
observer.complete();
}, 1000);
});
</code></pre>
</section>
<section>
<h3>Subscribing observable</h3>
<pre><code class="hljs javascript">
observable.subscribe(
(x) => console.log('got value ' + x),
(err) => console.error('something wrong occurred: ' + err),
() => console.log('done')
);
</code></pre>
<a href="https://embed.plnkr.co/X3785Q5lUq0SThnr2jOK/" target="_blank">Source</a>
</section>
<section>
<h3>Observables can be created from:</h3>
<ul>
<li class="fragment">Array</li>
<li class="fragment">Array-like object (e.g. DOM elements)</li>
<li class="fragment">Event</li>
<li class="fragment">Iterable object</li>
<li class="fragment">Observable-like</li>
<li class="fragment">Promise</li>
</ul>
<a class="fragment" href="http://embed.plnkr.co/AANaOLWcVfStpzJMHhyh/" target="_blank"> Create from event </a>
</section>
</section>
<section>
<h2>Observable vs Promise</h2>
<ul>
<li class="fragment">Observables are cancellable</li>
<!--<li class="fragment">Observables can be retried</li>-->
<li class="fragment">Observables are not executed without observer</li>
<li class="fragment">Observables have operators</li>
</ul>
<a class="fragment" href="https://plnkr.co/edit/IqexueV4QKT7JgDOcudA?p=preview" target="_blank"> Source </a>
</section>
<section>
<section>
<h2>Cold vs hot observable</h2>
<p class="fragment">Producer - source of values for observable.</p>
</section>
<section>
<h3>Cold observable</h3>
<ol class="postList">
<li class="fragment">creates the producer</li>
<li class="fragment">activates the producer</li>
<li class="fragment">starts listening to the producer</li>
<li class="fragment">unicast</li></ol>
</section>
<section>
<h3>Hot observable</h3>
<ol class="postList">
<li class="fragment">shares a reference to a producer</li>
<li class="fragment">starts listening to the producer</li>
<li class="fragment">multicast</li>
</section>
<section>
<div>
<h5> Cold observable </h5>
<pre><code class="hljs javascript">
const source = new Observable((observer) => {
const socket = new WebSocket('ws://someurl');
socket.addEventListener('message', (e) => observer.next(e));
return () => socket.close();
});
</code></pre>
</div>
<br><br>
<div class="fragment">
<h5> Hot observable </h5>
<pre><code class="hljs javascript">
const socket = new WebSocket('ws://someurl');
const source = new Observable((observer) => {
socket.addEventListener('message', (e) => observer.next(e));
});
</code></pre>
</div>
<a class="fragment" href="https://plnkr.co/edit/s2vriUDpbtqEPk1t1IZv?p=preview" target="_blank"> Http Angular 2 </a>
</section>
</section>
<section>
<section>
<h2>Most useful operators</h2>
</section>
<section>
<h3>map</h3>
<p>Like <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map">Array.prototype.map()</a>,
it passes each source value through a transformation function to get
corresponding output values.</p>
<img src="images/map.png">
</section>
<section>
<h3>filter</h3>
<p>Like <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter">Array.prototype.filter()</a>,
it only emits a value from the source if it passes a criterion function.
</p>
<img src="images/filter.png">
</section>
<section>
<h3>scan</h3>
<p>It's like reduce, but emits the current accumulation whenever the source emits a value.</p>
<img src="images/scan.png">
</section>
<section>
<h3>do</h3>
<p>Intercepts each emission on the source and runs a function, but returns an output which is identical to the source.</p>
<img src="images/do.png">
<a href="http://plnkr.co/edit/7JZyZeWHxIGQCMS5Zvud?p=preview" target="_blank"> Source </a>
</section>
<section>
<h3>withLatestFrom</h3>
<p>Whenever the source Observable emits a value, it computes a formula using that value plus the latest values from other input Observables, then emits the output of that formula.</p>
<img style="height: 25rem;" src="images/withLatestFrom.png">
</section>
<section>
<h3>combineLatest</h3>
<p>Whenever any input Observable emits a value, it computes a formula using the latest values from all the inputs, then emits the output of that formula.
<a href="http://plnkr.co/edit/ZUCMSiqhsKdawUieYf3C?p=preview" target="_blank"> Source </a> </p>
<img style="height: 25rem;" src="images/combineLatest.png">
</section>
<section>
<h3>switchMap (flatMapLatest in RxJS 4)</h3>
<p>Maps each value to an Observable, then flattens all of
these inner Observables using <a href="http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#instance-method-switch">switch</a> <a href="http://plnkr.co/edit/W7ii4Tp7WtOBYhopJbO2?p=preview target="_blank"> Source </a></p>
<img style="height: 25rem;" src="images/switchMap.png">
</section>
</section>
<section>
<section>
<h2>What's next?</h2>
<ul>
<li class="fragment"><a href="https://gist.github.com/staltz/868e7e9bc2a7b8c1f754">The introduction to Reactive Programming you've been missing</a></li>
<li class="fragment"><a href="http://reactivex.io/learnrx/">Functional Programming in Javascript</a></li>
<li class="fragment"><a href="https://github.com/ngrx/router">Reactive Router for Angular 2</a></li>
<li class="fragment"><a href="https://github.com/ngrx/store">RxJS powered state management for Angular2 apps, inspired by Redux</a></li>
<li class="fragment"><a href="https://github.com/Reactive-Extensions/rx.angular.js/">AngularJS Bindings for RxJS</a></li>
<li class="fragment"><a href="http://staltz.com/">Andre Staltz webpage</a></li>
<li class="fragment"><a href="https://www.youtube.com/watch?v=COviCoUtwx4">Netflix JavaScript Talks - RxJS Version 5</a></li>
</ul>
</section>
<section>
<h2>Resources</h2>
<ul>
<li class="fragment"><a href="http://reactivex.io/">Official ReactiveX page</a></li>
<li class="fragment"><a href="http://reactivex.io/rxjs/">Official RxJS 5 documentation</a></li>
<li class="fragment"><a href="https://github.com/Reactive-Extensions/RxJS/tree/master/doc">Official RxJS 4 documentation on github repo</a></li>
<li class="fragment"><a href="http://rxmarbles.com/">Interactive diagrams of Rx Observables</a></li>
</ul>
</section>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.js"></script>
<script>
// More info https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
history: true,
// More info https://github.com/hakimel/reveal.js#dependencies
dependencies: [
{ src: 'plugin/markdown/marked.js' },
{ src: 'plugin/markdown/markdown.js' },
{ src: 'plugin/notes/notes.js', async: true },
{ src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } }
]
});
</script>
</body>
</html>