RxJSJavaScript 的 Reactive 擴(kuò)展
ReactiveX 是一個通過使用可觀察序列來合成異步和基于事件的程序的庫。
它擴(kuò)展了 observer 模式,以支持?jǐn)?shù)據(jù)和/或事件序列,并增加了操作符,允許你聲明式地將序列組合在一起,同時抽象出低級線程、同步、線程安全、并發(fā)數(shù)據(jù)結(jié)構(gòu)和非阻塞 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();評論
圖片
表情
