4

How the Reactive Programming differs from calling a function in Event listener (mouse, key), because both are kind of asynchronous event stream so whats the advantage of Reactive over traditional Event listeners calls?

Jeevanantham
  • 984
  • 3
  • 19
  • 48

1 Answers1

5

Event listener has actually only a subset of the functionalities provided by RxJava, and that's exactly the problem that it tries to solve:

But let's back up a few steps, it's easier to understand what an Observable is if you compare it to an Iterator (push vs. pull).

Iterator.next() is equivalent to Observable.onNext() - when the next item/event occurs - consume it.

Iterator.hasNext() is equivalent to Observable.onComplete() - it allows the publisher to notify the subscriber that there are no more events to consume (one thing that was missing in the EventListener model).

For the third, Observable.onError() there is no equivalent, because with Iterator, when you try to get next() or remove() you know that you might get NoSuchElementException, UnsupportedOperationException or IllegalStateException and you can catch and handle any of them since you're doing it synchronously.

For the publisher, if an error occurs there is no way to notify the Listener/subscriber other then to crash. onError() is the last missing part that was made so that the Observable can enable a graceful handling of any error.

To sum up, Reactive Java came to fix parts that were missing from the event model for a long time. By providing those missing parts, and by providing a functional programming style (supports map, flatmap, filter etc) it helps composing async calls in a natural, readable way reducing boilerplate-code that is necessary when the programming style is imperative (e.g. nested for-loops) and which creates the callback-hell.

Community
  • 1
  • 1
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129