<small id='3Ckcy'></small><noframes id='3Ckcy'>

        <bdo id='3Ckcy'></bdo><ul id='3Ckcy'></ul>

        <legend id='3Ckcy'><style id='3Ckcy'><dir id='3Ckcy'><q id='3Ckcy'></q></dir></style></legend>
      1. <i id='3Ckcy'><tr id='3Ckcy'><dt id='3Ckcy'><q id='3Ckcy'><span id='3Ckcy'><b id='3Ckcy'><form id='3Ckcy'><ins id='3Ckcy'></ins><ul id='3Ckcy'></ul><sub id='3Ckcy'></sub></form><legend id='3Ckcy'></legend><bdo id='3Ckcy'><pre id='3Ckcy'><center id='3Ckcy'></center></pre></bdo></b><th id='3Ckcy'></th></span></q></dt></tr></i><div id='3Ckcy'><tfoot id='3Ckcy'></tfoot><dl id='3Ckcy'><fieldset id='3Ckcy'></fieldset></dl></div>
        <tfoot id='3Ckcy'></tfoot>
      2. 从普通 Java 事件创建 Observable

        Creating Observable from normal Java events(从普通 Java 事件创建 Observable)
        <i id='2MRRt'><tr id='2MRRt'><dt id='2MRRt'><q id='2MRRt'><span id='2MRRt'><b id='2MRRt'><form id='2MRRt'><ins id='2MRRt'></ins><ul id='2MRRt'></ul><sub id='2MRRt'></sub></form><legend id='2MRRt'></legend><bdo id='2MRRt'><pre id='2MRRt'><center id='2MRRt'></center></pre></bdo></b><th id='2MRRt'></th></span></q></dt></tr></i><div id='2MRRt'><tfoot id='2MRRt'></tfoot><dl id='2MRRt'><fieldset id='2MRRt'></fieldset></dl></div>

          • <bdo id='2MRRt'></bdo><ul id='2MRRt'></ul>

              • <legend id='2MRRt'><style id='2MRRt'><dir id='2MRRt'><q id='2MRRt'></q></dir></style></legend>
                  <tbody id='2MRRt'></tbody>

                  <small id='2MRRt'></small><noframes id='2MRRt'>

                  <tfoot id='2MRRt'></tfoot>
                  本文介绍了从普通 Java 事件创建 Observable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  从经典 Java 事件模式创建 Rx-Java Observable 的最佳方法是什么?也就是说,给定

                  What is the best way to create an Rx-Java Observable from the classical Java event pattern? That is, given

                  class FooEvent { ... }
                  
                  interface FooListener {
                    void fooHappened(FooEvent arg);
                  }
                  
                  class Bar {
                    public void addFooListener(FooListener l);
                    public void removeFooListener(FooListener l);
                  }
                  

                  我要实现

                  Observable<FooEvent> fooEvents(Bar bar);
                  

                  我想出的实现是:

                  Observable<FooEvent> fooEvents(Bar bar) {
                    return Observable.create(new OnSubscribeFunc<FooEvent>() {
                      public Subscription onSubscribe(Observer<? super FooEvent> obs) {
                        FooListener l = new FooListener() {
                          public void fooHappened(FooEvent arg) {
                            obs.onNext(arg);
                          }
                        };
                  
                        bar.addFooListener(l);
                  
                        return new Subscription() {
                          public void unsubscribe() {
                            bar.removeFooListener(l);
                          }
                        };
                      }
                    }); 
                  }
                  

                  不过,我不是很喜欢:

                  1. 很冗长;

                  1. it's quite verbose;

                  每个 Observer 都需要一个监听器(理想情况下,如果没有观察者,则应该没有监听器,否则只有一个监听器).这可以通过将观察者计数保留为 OnSubscribeFunc 中的一个字段,在订阅时递增,在取消订阅时递减.

                  requires a listener per Observer (ideally there should be no listeners if there are no observers, and one listener otherwise). This can be improved by keeping an observer count as a field in the OnSubscribeFunc, incrementing it on subscribe and decrementing on unsubscribe.

                  有没有更好的解决方案?

                  Is there a better solution?

                  要求:

                  1. 使用现有的事件模式实现而不更改它们(如果我正在控制该代码,我已经可以编写它以返回我需要的 Observable).

                  如果/当源 API 更改时会出现编译器错误.不能使用 Object 而不是实际的事件参数类型或属性名称字符串.

                  Getting compiler errors if/when the source API changes. No working with Object instead of actual event argument type or with property name strings.

                  推荐答案

                  我认为没有办法为每个可能的事件创建一个通用的 observable,但你当然可以在任何需要的地方使用它们.

                  I don't think there's a way to create a generic observable for every possible event, but you can certainly use them wherever you need.

                  RxJava 源代码有一些方便的示例,说明如何从鼠标事件、按钮事件等创建可观察对象.看看这个类,它从 KeyEvents 创建它们:KeyEventSource.java.

                  The RxJava source has some handy examples of how to create observables from mouse events, button events, etc. Take a look at this class, which creates them from KeyEvents: KeyEventSource.java.

                  这篇关于从普通 Java 事件创建 Observable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                  How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                  Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                  Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                  How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                  How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)

                  <i id='qabhA'><tr id='qabhA'><dt id='qabhA'><q id='qabhA'><span id='qabhA'><b id='qabhA'><form id='qabhA'><ins id='qabhA'></ins><ul id='qabhA'></ul><sub id='qabhA'></sub></form><legend id='qabhA'></legend><bdo id='qabhA'><pre id='qabhA'><center id='qabhA'></center></pre></bdo></b><th id='qabhA'></th></span></q></dt></tr></i><div id='qabhA'><tfoot id='qabhA'></tfoot><dl id='qabhA'><fieldset id='qabhA'></fieldset></dl></div>
                      • <bdo id='qabhA'></bdo><ul id='qabhA'></ul>
                          <tbody id='qabhA'></tbody>
                          <legend id='qabhA'><style id='qabhA'><dir id='qabhA'><q id='qabhA'></q></dir></style></legend>

                            <tfoot id='qabhA'></tfoot>

                            <small id='qabhA'></small><noframes id='qabhA'>