airbnb / RxGroups

Easily group RxJava Observables together and tie them to your Android Activity lifecycle
Apache License 2.0
693 stars 44 forks source link

[WIP] Reimagining how subscribing observers works #31

Closed elihart closed 7 years ago

elihart commented 7 years ago

@felipecsl I've been brainstorming how to addressthe shortcomings of rxgroups for a while and think I'm onto something decent. This is a working proof of concept, but doesn't have updated tests/styles/comments etc.

Problems this tries to solve:

The previous tag system required you to specify the tag when creating an observable and when creating an observer. It made sense to me to instead have the observer be the main definition for the tag, and to have that tag be generated by an annotation processor. In the code below I call that class AutoResubscribingObserver. For a 1-1 mapping of observer to observable you just provide that observer field instead of the tag that used to be provided. This makes usage clearer and less error prone.

For a many observables to one observer scenario I set up a two tag system. The first tag is the observer tag. All observables executed with the same observer are grouped under that observer's tag and are keyed again by a second tag that they specify. Our AirRequest already includes a notion of a tag so it should be easy to have that tagging definition exist in the request object and keep the class executing the requests clean.

There's still a lot of work to be done, but the basics are working in the sample app! Let me know what you think.

elihart commented 7 years ago

I obviously changed the interface a bunch. It would be hard to make all of these changes in the best way possible while providing backwards compatibility, so I'm thinking we can release a 1.0.0 version finally and do these breaking API changes then.

elihart commented 7 years ago

Case in point

image

felipecsl commented 7 years ago

@elihart just had the chance to finally scan though these changes and I like where it's going. It clearly makes sense to get rid of the manual and error-prone tag system we have to day for something more automatic and smart that "just works". We'd have to be able to still manually specify a tag if needed, so we'd need to take that into account. I think this could make for a nice 1.0 release once it's finished 👍

elihart commented 7 years ago

@felipecsl Thanks for looking! I've love to walk through it with you in person some time to outline what changes are needed and talk through some design decisions. Maybe next week sometime? Or I could probably do Friday

felipecsl commented 7 years ago

Friday works for me!

elihart commented 7 years ago

Example of the generated code

public class MainActivity_ObservableResubscriber {
  public MainActivity_ObservableResubscriber(MainActivity target, ObservableGroup group) {
    target.observer.tag = "MainActivity_observer";
    group.resubscribe(target.observer);
  }
}