-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
225 lines (178 loc) · 4.41 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
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
<meta charset="utf-8">
<style>
@import url(https://fonts.googleapis.com/css?family=Yanone+Kaffeesatz);
@import url(https://fonts.googleapis.com/css?family=Droid+Serif:400,700,400italic);
@import url(https://fonts.googleapis.com/css?family=Ubuntu+Mono:400,700,400italic);
body { font-family: 'Droid Serif'; }
h1, h2, h3 {
font-family: 'Yanone Kaffeesatz';
font-weight: normal;
}
.remark-code, .remark-inline-code { font-family: 'Ubuntu Mono'; }
</style>
</head>
<body>
<textarea id="source">
class: center, middle
# Reactive Programming using RxJs
---
# Agenda
1. RxJs Introduction
2. How it looks like?
3. Promise Vs Observable
4. Observable Events
5. Stream Vs Array Processing
6. Discussion
---
# RxJs Introduction
RxJs is a set of libraries for composing asynchronous and event-based programs using observable sequences and fluent query operators that many of you already know in Array extras in Javascript (filter, map, reduce, foeEach, etc).
Ref:
* [ReactiveX.io](http://reactivex.io/)
* [RxJs, ReactiveX Js Extension](https://github.com/Reactive-Extensions/RxJS)
---
# How RxJs looks like?
[RxJs v3.0 Documentations](https://github.com/Reactive-Extensions/RxJS/tree/master/doc)
```javascript
var source = Rx.Observable.interval(500).take(6);
source.filter(x => x % 2 === 1)
.map(x => x + '!')
.forEach(x => console.log(x));
```
---
# RxJS subscription
```javascript
var source = Rx.Observable.create(function(observer) {
var id = setTimeout(function() {
try {
// throw 'my bad error';
observer.onNext(42);
observer.onCompleted();
} catch (error) {
observer.onError(error);
}
}, 1000);
console.log('started');
return function() {
console.log('disposal called');
clearTimeout(id);
};
});
var sub = source.subscribe(function(x) {
console.log('next ' + x);
}, function(err) {
console.error(err);
}, function() {
console.info('done');
});
```
---
# Promise Vs Observable
Eager Loading Vs Lazy Loading
---
# Promise Is Eager
It gets executed whenever created.
```javascript
var promise = new Promise((resolve) => {
setTimeout(() => {
console.log('promise timeout hit');
resolve(42);
}, 1000);
console.log('promise started');
});
promise.then(x => console.log(x));
```
---
# Promise Is Lazy
It gets executed only whenever called.
```javascript
var source = Rx.Observable.create((observer) => {
var id = setTimeout(() => {
console.log('observable timeout hit');
observer.onNext(42);
}, 1000);
console.log('observable started');
return () => {
console.log('dispose called');
clearTimeout(id);
};
});
var disposable = source.forEach(x => console.log(x));
setTimeout(() => {
disposable.dispose();
}, 500);
```
---
# Observable Events
### Event is a stream
```javascript
var btn = document.querySelector('#clickMe');
var clicks = Rx.Observable.fromEvent(btn, 'click');
clicks.scan(0, (s) => s + 1)
.buffer(clicks.throttle(1000))
.forEach(x => sendValues(x));
function sendValues(arr) {
var pre = document.createElement('pre');
pre.innerHTML = JSON.stringify(arr);
document.querySelector('#results')
.appendChild(pre);
}
```
---
# Stream Vs Array processing
### Immutable array with functional
```javascript
var source =[0,1,2,3,4,5];
source.filter((x, i, arr) => {
console.log('filtering ' + x);
console.log('Is source = ' + (arr === source));
return x % 2 === 0;
})
.map((x, i, arr) => {
console.log('mapping ' + x);
console.log('Is source = ' + (arr === source))
return x + '!';
})
.reduce((r, x, i, arr) => {
console.log('reducing ' + x);
console.log('Is source = ' + (arr === source));
return r + x;
}, '');
console.log(source)
```
---
# Observable stream Vs Array processing
### Observable with stream processing
```javascript
var source = Rx.Observable.fromArray([0,1,2,3,4,5]);
source.filter((x) => {
console.log('filtering ' + x);
return x % 2 === 0;
})
.map((x) => {
console.log('mapping ' + x);
return x + '!';
})
.reduce((r, x) => {
console.log('reducing ' + x);
return r + x;
}, '')
.subscribe(result => {
console.log(result);
});
```
---
# Discussion
Thank you!
---
</textarea>
<script src="https://gnab.github.io/remark/downloads/remark-latest.min.js">
</script>
<script>
var slideshow = remark.create();
</script>
</body>
</html>