Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handler or Listeners. What is better?

Handler or Listeners. What is better use for notification of event? What is faster, more efficient etc.?

like image 471
user479211 Avatar asked Dec 10 '10 09:12

user479211


People also ask

What is the difference between listener and handler?

Note: Event handlers are sometimes called event listeners — they are pretty much interchangeable for our purposes, although strictly speaking, they work together. The listener listens out for the event happening, and the handler is the code that is run in response to it happening.

Is it better to use onclick or addEventListener?

Summary: addEventListener can add multiple events, whereas with onclick this cannot be done. onclick can be added as an HTML attribute, whereas an addEventListener can only be added within <script> elements. addEventListener can take a third argument which can stop the event propagation.

What is the difference between an event handler and an event listener in AEM?

It could happen to copy-paste page, just as ordinary node. In this case EventListener will fire, but EventHandler will not. That is the key difference between them.

Should you always remove event listeners?

TLDR; Always remove event listeners when you don't plan on using them any longer.


1 Answers

That's a good question!

scenario for using a handler

I've got an Android background service running in my app that uses handlers exclusively for web communications - I decided to go this route because the handler will queue requests and execute them one by one so then I know that a sequence is remained intact.

For example, in an instant messenger app you might find it desirable to maintain a sequence for your chatting.

scenario for using a callback

My background service also uses a class that reads from hardware (in a separate thread); some data might come in at any time and needs to be processed immediately. For that class I implemented a listener/callback interface.


My only question is whether there's any etiquette for the size of the handler. I have about 50 unique messages:

  • outgoing web requests consists of about 25 messages (each message is a different API on the web server)
  • Each API returns a response, therefore there's another 25 incoming web responses

The handler requires about 60% of the service's code - as you can imagine this results in a very big switch(case{}) structure (almost 1000 lines of code). Too big? How to break it apart?

like image 52
Someone Somewhere Avatar answered Oct 17 '22 19:10

Someone Somewhere