function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
suneel.patchipulusu@gmail.comsuneel.patchipulusu@gmail.com 

Explanation of code!!!!!!!!

Hello all

 

 

Could any one explain this part of code, I am unable to understand 

	// Internal mapping of handlers
	Map<String, List<Handler>> eventHandlerMapping = new Map<String, List<Handler>>();

	/**
	 * Core API to bind handlers with events
	 */
	public Triggers bind(Evt event, Handler eh) 
	{
		List<Handler> handlers = eventHandlerMapping.get(event.name());
		if (handlers == null) 
		{
			handlers = new List<Handler>();
			eventHandlerMapping.put(event.name(), handlers);
		}
		handlers.add(eh);
		return this;
	}

 The above code belongs to the standard trigger template

 

 

Thanks in advance 

Suneel

Eugene NeimanEugene Neiman

For each event, create a list of handlers, then add the event and list of handlers to a map of events to their handler lists.

suneel.patchipulusu@gmail.comsuneel.patchipulusu@gmail.com

Hi

 

Thanks for ur response and I have a small doubt 

What does it mean

 

public Triggers bind(Evt event, Handler eh) 

 Is it a constructor with arguments or??

sfdcfoxsfdcfox

Triggers, Evt, and Handler are all classes. This function is not a constructor, just a normal function that adds a handler to an event key, creating the list if it doesn't exist first. It then returns a reference to itself, which allows function chaining:

 

Triggers t = new Triggers();
t.bind(new Evt(),new Handler()).bind(new Evt(),new Handler());

By the way, this function could have been written as:

 

public Triggers bind(Evt event, Handler eh) {
  if(!eventHandlerMapping.containsKey(event.name())) {
    eventHandlerMapping.put(event.name(),new Handler[0]);
  }
  eventHandlerMapping.get(event.name()).add(eh);
  return this;
}

Which might seem more intuitive for you to read.