ReactiveX

参考ReactiveX官方:http://reactivex.io/intro.html

什么是ReactiveX

ReactiveX is a library for composing asynchronous and event-based programs by using observable sequences.
ReactiveX是函数库,利用观察序列, 构建基于事件驱动和异步的程序。

  • Observables Are Composable (被观察者是可以随意组合的)
  • Observables Are Flexible (被观察者非常灵活)

ReactiveX provides a collection of operators with which you can filter, select, transform, combine, and compose Observables.

ReactiveX 提供了一系列的操作符,用于过滤,选择,转换,合并,组合等。

构建 Observers

http://reactivex.io/documentation/observable.html

构建过程:

1
2
3
4
5
6
def myOnNext     = { item -> /* do something useful with item */ };
def myError = { throwable -> /* react sensibly to a failed call */ };
def myComplete = { /* clean up after the final response */ };
def myObservable = someMethod(itsParameters);
myObservable.subscribe(myOnNext, myError, myComplete);
// go on about my business

“热” 和 ”冷” Observable

  • A “hot” Observable may begin emitting items as soon as it is created,
    and so any observer who later subscribes to that Observable may start observing the sequence somewhere in the middle.
    意思就是说Observable被创建后就开始发射, Observer之后才订阅

  • A “cold” Observable, on the other hand, waits until an observer subscribes to it before it begins to emit items,
    and so such an observer is guaranteed to see the whole sequence from the beginning.
    意思就是说Observable等到有个Observer订阅才开始发射

Observable 操作符

The real power comes with the “reactive extensions” (hence “ReactiveX”)
— operators that allow you to transform, combine, manipulate, and work with the sequences of items emitted by Observables.
Rx真正强大在于 —— 操作符允许你转换,整合,处理 然后和Observable发射的Item序列一起工作。

一系列的操作符:

参考文档: http://reactivex.io/documentation/operators.html

构造型操作符

Create, Defer, Empty/Never/Throw, From, Interval, Just, Range, Repeat, Start, and Timer

转换型操作符

Buffer, FlatMap, GroupBy, Map, Scan, and Window

过滤型操作符

Debounce, Distinct, ElementAt, Filter, First, IgnoreElements, Last, Sample, Skip, SkipLast, Take, and TakeLast

组合型操作符

And/Then/When, CombineLatest, Join, Merge, StartWith, Switch, and Zip

错误处理型操作符

Catch and Retry

工具型操作符

Delay, Do, Materialize/Dematerialize, ObserveOn, Serialize, Subscribe, SubscribeOn, TimeInterval, Timeout, Timestamp, and Using

Conditional and Boolean Operators

All, Amb, Contains, DefaultIfEmpty, SequenceEqual, SkipUntil, SkipWhile, TakeUntil, and TakeWhile

数学型操作符 Mathematical and Aggregate Operators

Average, Concat, Count, Max, Min, Reduce, and Sum

转化型操作符 Converting Observables

To

可连接型操作符 Connectable Observable Operators

Connect, Publish, RefCount, and Replay

Backpressure Operators

a variety of operators that enforce particular flow-control policies

Loading comments box needs to over the wall