{"id":1604,"date":"2018-02-26T20:36:19","date_gmt":"2018-02-26T12:36:19","guid":{"rendered":"http:\/\/coderbee.net\/?p=1604"},"modified":"2018-02-26T20:36:19","modified_gmt":"2018-02-26T12:36:19","slug":"rxjava","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/framework\/20180226\/1604","title":{"rendered":"RxJava"},"content":{"rendered":"<h1>ReactiveX<\/h1>\n<p><a href=\"http:\/\/reactivex.io\/\">ReactiveX<\/a> \u662f\u4e00\u4e2a\u7528\u4e8e\u5f02\u6b65\u7f16\u7a0b\u7684 API \u89c4\u8303\u3002 ReactiveX \u7ed3\u5408\u4e86 Observer \u6a21\u5f0f\u3001Iterator \u6a21\u5f0f\u548c\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u6700\u4f73\u7406\u5ff5\u3002<\/p>\n<p>ReactiveX \u5e26\u6765\u4e86\u66f4\u597d\u7684\u4ee3\u7801\u57fa\u7840\uff1a<\/p>\n<ul>\n<li>Functional, \u51fd\u6570\u5f0f\uff1a\u907f\u514d\u4e86\u590d\u6742\u7684\u6709\u72b6\u6001\u7684\u7a0b\u5e8f\uff0c\u5728\u53ef\u89c2\u5bdf\u6d41\u4e0a\u4f7f\u7528\u5e72\u51c0\uff08\u65e0\u526f\u4f5c\u7528\uff09\u7684 \u8f93\u5165\/\u8f93\u51fa \u51fd\u6570\u3002<\/li>\n<li>Less is more, \u5c11\u5373\u662f\u591a\uff1aReactiveX \u7684\u64cd\u4f5c\u5b50\u901a\u5e38\u628a\u7cbe\u5fc3\u5236\u4f5c\u7684\u4fee\u6539\u7b80\u5316\u4e3a\u51e0\u884c\u4ee3\u7801\u3002<\/li>\n<li>Async error handling, \u5f02\u6b65\u9519\u8bef\u5904\u7406\uff1a\u4f20\u7edf\u7684 try\/catch \u5bf9\u4e8e\u5f02\u6b65\u8ba1\u7b97\u7684\u9519\u8bef\u975e\u5e38\u4e4f\u529b\uff0c\u4f46 ReactiveX \u5177\u6709\u6070\u5f53\u7684\u673a\u5236\u6765\u5904\u7406\u9519\u8bef\u3002<\/li>\n<li>Concurrency made easy, \u66f4\u5bb9\u6613\u7684\u5e76\u53d1\uff1aReactiveX \u7684 Observables \u548c Schedulers \u5141\u8bb8\u7a0b\u5e8f\u5458\u4ece\u5e95\u5c42\u7684\u7ebf\u7a0b\u3001\u540c\u6b65\u548c\u5e76\u53d1\u95ee\u9898\u4e2d\u62bd\u8c61\u51fa\u6765\u3002<\/li>\n<\/ul>\n<h1>RxJava<\/h1>\n<p><a href=\"https:\/\/github.com\/ReactiveX\/RxJava\">RxJava<\/a> \u662f ReactiveX \u5728 Java \u7f16\u7a0b\u8bed\u8a00\u91cc\u7684\u4e00\u4e2a\u5b9e\u73b0\u3002<\/p>\n<h2>\u57fa\u672c\u6982\u5ff5\uff1a<\/h2>\n<ul>\n<li>\u4e8b\u4ef6\uff1a\u4e3b\u9898\u751f\u6210\u7684\u3001\u8ba2\u9605\u8005\u611f\u5174\u8da3\u7684\u4e1c\u897f\u3002<\/li>\n<li>\u8ba2\u9605\u8005\uff1aObserver\uff0c\u62bd\u8c61\u57fa\u7c7b\u662f <code>Subscriber<\/code>\u3002<\/li>\n<li>\u4e3b\u9898\uff1a\u88ab\u89c2\u5bdf\u7684\u5bf9\u8c61\uff0c\u62bd\u8c61\u57fa\u7c7b\u662f <code>Observable<\/code>\u3002\u6bcf\u4e2a\u4e3b\u9898\u90fd\u6709\u4e00\u4e2a <code>OnSubscribe<\/code> \u7684\u5b9e\u4f8b\uff0c<code>OnSubscribe<\/code> \u4ece\u7c7b\u540d\u770b\u662f\u5bf9\u8ba2\u9605\u884c\u4e3a\u7684\u53cd\u5e94\uff0c\u5176 <code>call(Subscriber subscriber)<\/code> \u65b9\u6cd5\u5c01\u88c5\u4e86\u4e8b\u4ef6\u53d1\u751f\u3001\u901a\u77e5\u7684\u903b\u8f91\uff0c\u4f9b\u6bcf\u6b21\u8ba2\u9605\u65f6\u8c03\u7528\u3002<\/li>\n<li>\n<p>\u8ba2\u9605\uff1asubscribe\uff0c\u662f\u4e00\u79cd\u52a8\u4f5c\uff0cRxJava \u5728\u8ba2\u9605\u65f6\u5efa\u7acb\u4e3b\u9898\u4e0e\u76d1\u542c\u8005\u7684\u5173\u7cfb\uff0c\u6bcf\u6b21\u8ba2\u9605\uff0c\u4e3b\u9898\u90fd\u4f1a\u8c03\u7528\u5176\u5185\u90e8 <code>OnSubscribe.call(Subscriber subscriber)<\/code> \u65b9\u6cd5\u3002<\/p>\n<\/li>\n<li>\n<p>\u5bf9\u4e8e <code>Observable.doOnNext\/doOnCompleted\/doOnError\/doOnEach\/map<\/code> \u8fd9\u7c7b\u4e2d\u95f4\u64cd\u4f5c\uff0c\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u8ba2\u9605\u8005 <code>Subscriber<\/code>\uff0c\u5c01\u88c5\u4e86\u76f8\u5173\u884c\u4e3a\uff0c\u7528\u4e8e\u6dfb\u52a0\u65b0\u7684\u903b\u8f91\uff0c\u5e76\u4ee3\u7406\u4e86\u5bf9\u4e4b\u524d\u8ba2\u9605\u8005\u7684\u8c03\u7528\uff1b\u7528\u65b0\u7684\u8ba2\u9605\u8005\u548c\u5f53\u524d\u4e3b\u9898\u521b\u5efa\u65b0\u7684\u4e3b\u9898\u5e76\u8fd4\u56de\u3002\uff08\u91c7\u7528\u7684\u662f\u5305\u88c5\u5668\u6a21\u5f0f\uff09<br \/>\n<!--more--><\/p>\n<\/li>\n<\/ul>\n<h2>\u6838\u5fc3\u4ee3\u7801<\/h2>\n<p><code>rx.Observable<\/code>\uff1a<\/p>\n<pre><code class=\"java\">\/\/ \u521b\u5efa\u4e3b\u9898\nprotected Observable(OnSubscribe&lt;T&gt; f) {\n    this.onSubscribe = f;\n}\n\npublic static &lt;T&gt; Observable&lt;T&gt; create(OnSubscribe&lt;T&gt; f) {\n    return new Observable&lt;T&gt;(RxJavaHooks.onCreate(f));\n}\n\n\/\/ \u8ba2\u9605\npublic final Subscription subscribe(Subscriber&lt;? super T&gt; subscriber) {\n    return Observable.subscribe(subscriber, this);\n}\n\n\/\/ \u7cbe\u7b80\u540e\u7684\u8ba2\u9605\u884c\u4e3a\nstatic &lt;T&gt; Subscription subscribe(Subscriber&lt;? super T&gt; subscriber, Observable&lt;T&gt; observable) {\n    subscriber.onStart();\n\n    \/\/ \u4e3b\u9898\u5728 call \u65b9\u6cd5\u91cc \u901a\u77e5\u8ba2\u9605\u8005 \u4e8b\u4ef6\n    observable.onSubscribe.call(subscriber);\n    return RxJavaHooks.onObservableReturn(subscriber);\n}\n\n\/\/ \u4e2d\u95f4\u64cd\u4f5c\u3002\u628a onNext \u884c\u4e3a\u5305\u88c5\u4e00\u4e2a\u52a8\u4f5c\u91cc\npublic final Observable&lt;T&gt; doOnNext(final Action1&lt;? super T&gt; onNext) {\n    Action1&lt;Throwable&gt; onError = Actions.empty();\n    Action0 onCompleted = Actions.empty();\n    Observer&lt;T&gt; observer = new ActionObserver&lt;T&gt;(onNext, onError, onCompleted);\n\n    return create(new OnSubscribeDoOnEach&lt;T&gt;(this, observer));\n}\n\npublic final Subscription unsafeSubscribe(Subscriber&lt;? super T&gt; subscriber) {\n    try {\n        subscriber.onStart();\n\n        RxJavaHooks.onObservableStart(this, onSubscribe).call(subscriber);\n        return RxJavaHooks.onObservableReturn(subscriber);\n}\n<\/code><\/pre>\n<p><code>rx.internal.operators.OnSubscribeDoOnEach<\/code> \u6e90\u7801\uff0c\u4ece\u8fd9\u4e2a\u5b9e\u73b0\u53ef\u4ee5\u770b\u5230\uff0c\u76f4\u5230 <code>subscribe<\/code> \u76d1\u542c\u65f6\u624d\u4f1a\u628a\u76d1\u542c\u8005\u5305\u88c5\u8d77\u6765\uff0c\u7136\u540e\u4e8b\u4ef6\u6d41\u8fc7\u6240\u6709\u7684\u4e2d\u95f4\u5904\u7406\u5668\uff0c\u6700\u7ec8\u4f20\u5230\u76d1\u542c\u8005\u3002<\/p>\n<pre><code class=\"java\">public class OnSubscribeDoOnEach&lt;T&gt; implements OnSubscribe&lt;T&gt; {\n    private final Observer&lt;? super T&gt; doOnEachObserver;\n    private final Observable&lt;T&gt; source;\n\n    public OnSubscribeDoOnEach(Observable&lt;T&gt; source, Observer&lt;? super T&gt; doOnEachObserver) {\n        this.source = source;\n        this.doOnEachObserver = doOnEachObserver;\n    }\n\n    @Override\n    public void call(final Subscriber&lt;? super T&gt; subscriber) {\n        source.unsafeSubscribe(new DoOnEachSubscriber&lt;T&gt;(subscriber, doOnEachObserver));\n    }\n\n    private static final class DoOnEachSubscriber&lt;T&gt; extends Subscriber&lt;T&gt; {\n        private final Subscriber&lt;? super T&gt; subscriber;\n        private final Observer&lt;? super T&gt; doOnEachObserver;\n        private boolean done;\n\n        DoOnEachSubscriber(Subscriber&lt;? super T&gt; subscriber, Observer&lt;? super T&gt; doOnEachObserver) {\n            super(subscriber);\n            this.subscriber = subscriber;\n            this.doOnEachObserver = doOnEachObserver;\n        }\n\n        @Override\n        public void onCompleted() {\n            if (done) {\n                return;\n            }\n            try {\n                doOnEachObserver.onCompleted();\n            } catch (Throwable e) {\n                Exceptions.throwOrReport(e, this);\n                return;\n            }\n            \/\/ Set `done` here so that the error in `doOnEachObserver.onCompleted()` can be noticed by observer\n            done = true;\n            subscriber.onCompleted();\n        }\n\n        @Override\n        public void onError(Throwable e) {\n            if (done) {\n                RxJavaHooks.onError(e);\n                return;\n            }\n            done = true;\n            try {\n                doOnEachObserver.onError(e);\n            } catch (Throwable e2) {\n                Exceptions.throwIfFatal(e2);\n                subscriber.onError(new CompositeException(Arrays.asList(e, e2)));\n                return;\n            }\n            subscriber.onError(e);\n        }\n\n        @Override\n        public void onNext(T value) {\n            if (done) {\n                return;\n            }\n            try {\n                doOnEachObserver.onNext(value);\n            } catch (Throwable e) {\n                Exceptions.throwOrReport(e, this, value);\n                return;\n            }\n            subscriber.onNext(value);\n        }\n    }\n}\n<\/code><\/pre>\n<p><code>doOnNext\/doOnCompleted<\/code> \u7b49\u4e2d\u95f4\u64cd\u4f5c\u901a\u8fc7\u5305\u88c5\u5668\u6a21\u5f0f\u628a Observable \u5c01\u88c5\u4e3a <code>OnSubsrcibe<\/code> \u7684\u5b50\u7c7b\u5b9e\u73b0\uff0c\u6301\u6709\u8981\u6dfb\u52a0\u7684\u903b\u8f91\u3002<\/p>\n<p><code>OnSubscribe<\/code> \u7684 <code>call<\/code> \u65b9\u6cd5\u8c03\u7528\u65f6\uff0c\u628a\u6301\u6709\u7684\u4e2d\u95f4\u64cd\u4f5c\u5305\u88c5\u5728 \u4f20\u5165\u7684\u8ba2\u9605\u8005 \u5916\u5c42 \u4f5c\u4e3a\u65b0\u7684\u8ba2\u9605\u8005\uff0c\u65b0\u8ba2\u9605\u8005\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165 \u89e3\u5305\u88c5\u51fa\u6765\u7684 <code>Observable.unsafeSubscribe<\/code> \uff0c\u8be5\u65b9\u6cd5\u53c8\u4f1a\u8c03\u7528\u5230\u5176\u6301\u6709\u7684 <code>OnSubscribe<\/code> \u7684 <code>call<\/code> \u65b9\u6cd5\u4e0a\uff0c\u5f62\u6210\u4e00\u4e2a\u9012\u5f52\u8c03\u7528\uff0c\u76f4\u81f3\u6700\u5f00\u59cb\u7684 <code>OnSubscribe<\/code> \u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u7684 <code>call<\/code> \u65b9\u6cd5\u91cc\u6267\u884c \u4e8b\u4ef6\u751f\u6210\u3001\u901a\u77e5\u7684 \u8c03\u7528\uff0c\u4e8b\u4ef6\u901a\u8fc7 <code>Observer<\/code> \u7684\u5c42\u5c42\u5305\u88c5\u8c03\u7528\u6700\u7ec8\u5230\u4e1a\u52a1\u7684\u8ba2\u9605\u8005\u3002<\/p>\n<hr\/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong>\uff0c\u53ef\u4ee5\u66f4\u53ca\u65f6\u56de\u590d\u4f60\u7684\u8ba8\u8bba\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>ReactiveX ReactiveX \u662f\u4e00\u4e2a\u7528\u4e8e\u5f02\u6b65\u7f16\u7a0b\u7684 API \u89c4\u8303\u3002 R &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/framework\/20180226\/1604\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[255],"tags":[294],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1604"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=1604"}],"version-history":[{"count":3,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1604\/revisions"}],"predecessor-version":[{"id":1607,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1604\/revisions\/1607"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=1604"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=1604"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=1604"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}