RxJSJavaScript 的 Reactive 扩展
ReactiveX 是一个通过使用可观察序列来合成异步和基于事件的程序的库。
它扩展了 observer 模式,以支持数据和/或事件序列,并增加了操作符,允许你声明式地将序列组合在一起,同时抽象出低级线程、同步、线程安全、并发数据结构和非阻塞 I/O 等问题。
示例代码:
var $input = $('#input'),
$results = $('#results');
/* Only get the value from each key up */
var keyups = Rx.Observable.fromEvent(input, 'keyup')
.map(function (e) {
return e.target.value;
})
.filter(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 and other control characters */
var distinct = keyups
.distinctUntilChanged();评论
