I'm having trouble finding an example of how to make a custom operator with RxJava 2. I've considered a few approaches:
- Using
Observable.create, and thenflatMaping on it from the source observable. I can get this working, but it doesn't quite feel right. I end up creating a static function which I provide the sourceObservable, and then flatMap on the source. In the OnSubscribe, I then instantiate an object that I pass the emitter to, which handles and manages the Observable / Emitter (as it's not trivial, and I want everything as encapsulated as possible). - Creating an
ObservableOperatorand providing it toObservable.lift. I can't find any examples of this for RxJava 2. I had to debug into my own example to make sure my understanding of upstream and downstream were correct. Because I can't find any examples or documentation on this for RxJava 2 I'm a little worried I might accidentally do something I'm not supposed to. - Create my own
Observabletype. This seems to be how the underlying operators work, many of which extendAbstractObservableWithUpstream. There is a lot going on here though, and it seems easy to miss something or do something I shouldn't. I'm not sure if I'm supposed to take an approach like this or not. I stepped myself through the mental process, and it seems like it can get hairy pretty quickly.
I'm going to proceed forward with option #2, but thought it worthwhile to ask what the supported method for doing this was in RxJava2 and also find out if there was any documentation or examples for this.
buffer,window, etc. So you could go to github and look for the source code for those, to see how they're implemented. - LucianoRxAssembly,DisposableHelper, and they are used in a precise way. It could definitely be done, but I'd just be copying a format. I'd like to make sure I understand what's going on in my code. - spierce7