Closed JimmyLv closed 6 years ago
对比 MobX #75,放弃的原因:Mobx vs Reactive Stream Libraries (RxJS, Bacon, etc) · mobxjs/mobx Wiki
RxJS 并不只是 JavaScript,而是整个 Reactive-Extensions 体系,还包括现在项目所用的 RxJava,其思想和操作符更为重要和值得学习,而反而是 MobX 只局限于 React 世界里,并且还不一定拼得过 Redux,以及未来的 Relay 和 GraphQL…
@Yanghai
组内分享 RxJava 的 Session 很不错,AGENDA 如下:
RxJava is a Java implementation of ReactiveX: a library for composing asynchronous and event-based programs by using observable sequences.
Observable.from(folders)
.flatMap((Func1) (folder) -> { Observable.from(file.listFiles()) })
.filter((Func1) (file) -> { file.getName().endsWith(".png") })
.map((Func1) (file) -> { getBitmapFromFile(file) })
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe((Action1) (bitmap)-> { imageCollectorView.addImage(bitmap) });
RxJava contains the following core concepts:
Observable: event source Observer/Subscriber: event consumer Scheduler: thread controller Subscribe: the operation to trigger event sequence
Obervable.onSubscrible()
triggers events
Observable observable = Observable.create(new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
subscriber.onNext("Hello");
subscriber.onNext("Hi");
subscriber.onNext("Aloha");
subscriber.onCompleted();
}
});
Observable observable = Observable.just("Hello", "Hi", “Aloha");
It’s equivalent to:
String[] words = {"Hello", "Hi", "Aloha"};
Observable observable = Observable.from(words);
一般不用关心,Subscriber 属于 RxJava 实现上的内部概念。
Subscriber<String> subscriber = new Subscriber<String>() {
@Override
public void onNext(String s) {
Log.d(tag, "Item: " + s);
}
@Override
public void onCompleted() {
Log.d(tag, "Completed!");
}
@Override
public void onError(Throwable e) {
Log.d(tag, "Error!");
}
};
Observer will be converted into Subscriber.
Observer<String> observer = new Observer<String>() {
@Override
public void onNext(String s) {
Log.d(tag, "Item: " + s);
}
@Override
public void onCompleted() {
Log.d(tag, "Completed!");
}
@Override
public void onError(Throwable e) {
Log.d(tag, "Error!");
}
};
String[] words = {"Hello", "Hi", “Aloha”};
Subscriber<String> subscriber = new Subscriber<String>() {
@Override
public void onNext(String s) {
Log.d(tag, “Word: " + s);
}
@Override
public void onCompleted() {
Log.d(tag, "Completed!");
}
@Override
public void onError(Throwable e) {
Log.d(tag, "Error!");
}
};
Observable.from(words).subscribe(subscriber);
通过项目实战以及听 Session 对 RxJava 有了基本的经验,并且更重要的是明白了其背后 Reactive 的理念和思考方式,而 RxJS 则是 JavaScript 世界的一种实现,更多的 RxJS 学习记录将在 #260
项目中的经验非常有用!从问题描述开始,聚合性 portfolio 所 deps 的各种 APIs:
Sam Newman - Backends For Frontends 文中的 And Multiple Downstream Services (Microservices!) 章节:
Making multiple downstream calls to construct a view of a wishlist
References
http://reactivex.io/documentation/operators.html official documents https://www.javadoc.io/doc/io.reactivex/rxjava/1.1.1 rxjava 1.1.1 api docs http://reactivex.io/RxJava/javadoc/ rxjava 2 api docs http://gank.io/post/560e15be2dca930e00da1083 useful tutorial for beginners