Perspective Message Handler vs Custom Method

Hi all,

Just wanted to know if there is going to be a message handler with the scope of ‘view’, why not prefer creating custom method instead?
Is there any benefits of using message handler over method? Again I am saying this only for view scope.

Thanks in advance.

  1. A custom method requires a single component to be updated whenever a new “target” component is added, whereas a message handler allows the new target component to contain its own logic.
  2. Message Handlers allow for a component to broadcast a message and other components to hear the message, all without any hard-coded hierarchical path references.

Consider the following example:
I have a submit button, and when that button is pressed, I need to update a database table, and then wipe all of my input fields. Each of these input fields is nested at the same level as my button.

Both a custom function and a message handler would work here, but the message handler is more “future-proof”, and here’s why:
Suppose I need to wrap one of the inputs in a new container so that I can position a label underneath it and essentially “group” the input and Label. The custom function would no no longer work, because the path to the input component has now changed as it is now nested within a container. A message handler, however, does not rely on knowing the path to any component, so the message handler would continue to work.

Now let’s consider the scenario where I have added a new input to the page. I would need to update any custom function with a reference to the new input. If using a message handler, the new component would be responsible for its own handling of the message, and could potentially just be a copied usage of sibling input.

From a Design viewpoint, a custom function which manipulates other components results in unnecessary coupling of logic, where ComponentA needs to know about ComponentB and how ComponentB works. Message Handling allows for components to be much more ignorant of other components.

1 Like

Okay that makes sense then. So mainly it’s the concern of component path.

Okay now I only have one question, does using message handler causes any performance issue like speed (to search message handler component in entire view) and then call it and same question for Custom Method. Like if the path is confirmed and it’s isn’t going to change nor required same logic for any other component.

The performance impact is going to come down to what either the custom function or message handler is doing. If you have 300 components which all execute some code when a certain handler is broadcast/heard, then that will tire up 300 threads for n seconds each. If you have a custom function which iterates over 300 components and invokes some logic on each, then you are tying up one thread for 300*n seconds. You will notice that the custom function does execute in some sort of order (defined by you), whereas the message handler execution is asynchronous, but the execution time and performance impact is negligible between the two.

1 Like

As a consequence of this however, because there are now no direct links between components, it becomes more difficult to locate exactly which components will be affected when you send a particular message out, especially if you don’t know the project. You may find yourself using the Find tool a bit more. Just something to be aware of (definitely not a reason to not use them though!)

Also @cmallonee correct me if wrong, but message handlers are subscription-based. No walking/searching of components is done to send out messages and have them received by message handlers. The message just tells the subscription service to add a new event which then triggers the subscribed handlers to execute their code (terminology is probably wrong as I don’t really understand the technicals of event subscription)