square / otto

An enhanced Guava-based event bus with emphasis on Android support.
http://square.github.io/otto/
Apache License 2.0
5.16k stars 847 forks source link

Topic based subscriptions #174

Closed erikash closed 8 years ago

erikash commented 8 years ago

Hi, I was wondering if topic based subscriptions are on your roadmap?

For example, exchange quote:

Clients can subscribe to get all market quotes: @Subscribe public void answerAvailable(QuoteRecievedEvent event) { // TODO: React to the event somehow! }

Clients can subscribe to get part of the market quotes: otto.setTopics(this, QuoteRecievedEvent, "AAPL;GOOG"); // NOT proposing an api, just conveying an example.

Thanks, Erik.

JakeWharton commented 8 years ago

We have no plans for this, no. The library is meant for pure publish/subscribe and higher-level operations like filtering and combining are pushed onto the consuming code.

Consumers of events can filter events directly using normal mechanisms of Java:

@Subscribe public void onAnswerAvailable(QuoteReceivedEvent event) {
  if (!"GOOG".equals(event.symbol()) && !"AAPL".equals(event.symbol()) {
    return;
  }
  // Normal event handling code.
}

You can also use a mechanisms where filtering is a first-class citizen like RxJava:

ticker.quotes()
    .fitler(q -> "GOOG".equals(q.symbol()) || "APPL".equals(q.symbol()))
    .subscribe(q -> doSomethingWithQuote(q));