Reactive programming, Event Sourcing & CQRS in the frontend
David Sherman
Frontend Dev @ Essent
Henk Bakker
Frontend Dev @ Essent
Making the browser do stuff it wasn't made for
* Command Query Responsibility Segregation
DOM events
0-N values
Animations
cancelable
AJAX
1 value
WebSockets
0-N values
ServerSendEvents
0-N values
Observable
+
Iterator pattern
=
Observer pattern
fs.readdir(source, function (err, files) {
if (err) {
console.log('Error finding files: ' + err)
} else {
files.forEach(function (filename, fileIndex) {
console.log(filename)
gm(source + filename).size(function (err, values) {
if (err) {
console.log('Error identifying file size: ' + err)
} else {
console.log(filename + ' : ' + values)
aspect = (values.width / values.height)
widths.forEach(function (width, widthIndex) {
height = Math.round(width / aspect)
console.log('resizing ' + filename + 'to ' + height + 'x' + height)
this.resize(width, height).write(dest + 'w' + width + '_' + filename, function(err) {
if (err) console.log('Error writing file: ' + err)
})
}.bind(this))
}
})
})
}
})
Operators
Observables
Schedulers
+
+
=
RxJS
Operators are functions that you can use on Observables that allow you to change the original observable in some manner and return a new observable
const r = [1, 2, 3, 4, 5, 6]
.map(x => x * x)
.filter(y => y % 2 == 0);
console.log(r);
// log: [4, 16, 36]
from([1, 2, 3, 4, 5, 6]).pipe(
map(x => x * x)
filter(y => y % 2 == 0)
).subscribe(r => console.log(r));
// log: 4
// log: 16
// log: 36
Ben Lesh about Hot vs Cold observables
Observables are "cold" by default
"Cold" observables create a new producer each time a consumer subscribes to them
"Hot" observables share a single producer with every consumer that subscribes to them