4. “we believe that the essential material to be
addressed by a subject at this level is not the
syntax of particular programming-language
constructs, nor clever algorithms for
computing particular functions efficiently, nor
even the mathematical analysis of
algorithms and the foundations of
computing, but rather the techniques used to
control the intellectual complexity of
large software systems.”
Structure and Interpretation of Computer Programs
16. События оборачиваются в потоки,
которые возможно
• Фильтровать
• Объединять (merge)
• Комбинировать (zip)
• Тротлить (throttle)
• Преобразовывать
• Буферизировать
• и так далее...
17. Сервис подсказок (RxJS)
/* Only get the value from each key up */var keyups =
Rx.Observable.fromEvent(input, 'keyup')
.select(function (e) {
return e.target.value;
})
.where(function (text) {
return text.length > 2;
});/* Now throttle/debounce the input for 500ms */
var throttled = keyups
.throttle(500 /* ms */);/* Now get only distinct values,
so we eliminate the arrows */
var distinct = keyups
.distinctUntilChanged();
26. Подписчик - тот же observable, у
которого значение всегда
соответствует значению заданной
функции, аргументы которой другие
observable.
27. Computed
var x = observable(0);
var y = observable(0);
var r = computed(function() {
return Math.sqrt(x()*x() + y()*y());
});
28. Computed. Обратная операция
var r = computed({
read: readHandler,// previous
write: function(r) {
if (r >= 0) {
x(r*Math.cos(Math.PI/4));
y(r*Math.sin(Math.PI/4));
} else {
throw new Error(‘Не может быть отрицательным’);
}
}
});
29. Computed. Пример из жизни
var isFormValid = computed(function() {
return (login().length > 2) && (password().length > 5);
});
var canSend = computed(function() {
return !messageSent() && !loading() && isFormValid();
});
30. Observable коллекции – скажут
подписчикам, что изменился
набор её элементов (а не сами
элементы)