Dispatch

Mark Wieder ahsoftware at sonic.net
Wed Aug 3 11:53:06 EDT 2022


On 8/3/22 06:36, Sean Cole via use-livecode wrote:
> Hi all,
> I've been thinking about the command 'dispatch'. Is there any
> advantage/disadvantage in using it over just calling your handler?

If you can invoke a command/function directly then for the most part 
there's no real advantage in using dispatch.

I never use send anymore unless I need send a message in time.
I do use dispatch extensively, mostly for two reasons.

One, if I'm calling a handler in a different object that is outside the 
normal message path then I'll dispatch the handler to that object rather 
than using send. One caveat: note that the dispatch command returns a 
three-state value: "handled", "unhandled", and "passed". So on checking 
the return value I use "if it it not unhandled then...". The advantage 
is that there's no runtime error if the command isn't caught, at the 
slight disadvantage of having to check whether it was handled.

The second case where I use dispatch is as a form of publish/subscribe: 
dispatching a command without a receiver object sends the command along 
the message path where it can be handled/passed by any object along the 
way. Or not. Mostly in this paradigm I don't need or want to check the 
return value because it's irrelevant to the sender whether or not the 
message was handled.

The dispatch command can also be useful for feature management. Dispatch 
a command down the message path, and if the given feature is enabled 
then the command will be handled. If not then there's no downside to 
calling the feature because it won't cause a runtime error.


-- 
  Mark Wieder
  ahsoftware at gmail.com



More information about the use-livecode mailing list