Best practice: how to write local plugin functions for library handlers?

David Bovill david at architex.tv
Thu Jun 18 07:07:58 EDT 2009


Say we have a complex, but generally useful handler that loops through a lot
of things, and want to call a function on each element found. For example it
may be a "search" function that loops through all objects in a stack, or a
"process' function that loops through certain nodes in an XML file. Now
there may be all sorts of "test functions" that we would like to call during
this search - the question is how to structure the library handler and be
able to call custom test functions / handlers in the local script you are
working on?

I've no real elegant way of doing this in Rev. The best I can think of is to
get the library handler to call out to the script that the local handler is
in, and allow the scripter to define a local function there. To make the
example concrete using some pseudoCode, lets say we have a function in a
library called "stack_SearchAllScripts" and we have a local search button in
another stack that is going to call this function to search scripts in a
given stack for the phrase "Hello World". So we have something that looks
like (in simplified pseudo code):

Library Stack Script
> -------------------------
>
> function stack_SearchAllScripts stackName, pluginFunctionName
>     put the long id of the target into callingObject


>     repeat for each line testObject in allStackObjects
>         if the script_ContainsHello [testObject] of callingObject is true
> then
>
>             put callingObject & CR after callingObjects
>
>         end if
>     end repeat
>     return callingObjects
> end stack_SearchAllScripts
>
>
> Local Button Script
> -------------------------
>
> on mouseUp
>      put stack_SearchAllScripts ("Test", "script_ContainsHello")
> end mouseUp
>
> getprop script_ContainsHello [testObject]
>     return the script of testObject contains "Hello World"
> end script_ContainsHello


I find it more elegant and faster to use "getprop handlers" than "send" or
"dispatch" - especially for functions that you want to return results, but
whichever way you choose, it is still ugly though - especially if you start
to get fussy about really determining which object "called" the library
handler. That is because the "calling object" is not the same as the target,
and you need to use a handler like this:

function script_CallingObject
>    put line -3 of the executioncontexts into callingObject
>    delete item 2 to -1 of callingObject
>    return callingObject
> end script_CallingObject
>

With all of Rev's built in ability to send messages around in various ways,
it seems to me that there may be a more elegant way of writing these sorts
of library handlers that you can effectively plug in smaller function tests
into. I don"t want to use "do" for speed reasons as you want to search
through hundreds of items as fast as possible. Does anyone have a neat way
of doing this?



More information about the use-livecode mailing list