Is there a way to create a generic setprop handler?

Richard Gaskin ambassador at fourthworld.com
Wed Aug 3 15:28:10 EDT 2022


David Epstein wrote:

 > Control “A” has many custom properties that are set by a variety
 > of other controls.  If Control “A” has a "setProp property1”
 > handler, it can react when property1 gets set.  But is there a
 > way to give Control “A” a general handler that will be triggered
 > whenever any of its custom properties is set?

I don't know of a way to trap getProp and setProp generically, and that 
may not be bad since it would affect the performance of all custom 
property access, whether it's the subset of properties you're interested 
in or not.

An alternative could be to put the properties you want custom handling 
for into a custom property set of their own, e.g.:

    getPRop uPropSet[pKey]
       put the params
    end uPropSet

    on mouseUp
       get the uPropSet["key1"] of me
    end mouseUp

...puts this in the Message Box:

    uPropSet "key1"

This will allow open-ended trapping of property keys, but limited in 
scope to the set you need that for.


A word of caution with dependency on getProp and setProp:

Those messages are subject to suspension during lockMessages, which may 
be exactly what you want, or may have unintended side effects that can 
be maddening to track down if you don't keep that in mind.

Using getProp and setProp requires review of your code base, including 
any third-party libraries, which may lock messages so you can evaluate 
the implications for your circumstance.


If you need consistently reliable ways to trigger custom evaluation 
consider accessor handlers instead.  LockMessages only prevents system 
messages; calling a custom handler is not affected by lockMessages.

Examples:

-- Using an object as the value container:
on SetMyProp pKey, pVal
    set the uPropSet[pKey] of <someobject> to pVal
end SetMyProp

function GetMyProp pKey
   return the uPropSet[pKey] of <someObject>
end GetMyProp


-- Using an array variable as the value container:
on SetMyProp pKey, pVal
    put pVal into sPropVarA[pKey]
end SetMyProp

function GetMyProp pKey
   return sPropVarA[pKey]
end GetMyProp



-- Usage for either storage method:

on mouseUp
   SetMyProp "key1", "val1"
end mouseUp

on mouseUp
   put GetMyProp("key1") into fld 1
end mouseUp



Fun: If you abstract data access using normal custom handlers, you have 
one-stop-shopping to change the storage mechanism.  If you don't need 
persistence across sessions a variable may do, and if you need 
persistence you can store in an object whose stackfile gets saved, or 
encode the array variable and save that to disk, or use a local 
database, or even use any form of remote storage across the internet if 
you like -- all by updating just two handlers.


-- 
  Richard Gaskin
  Fourth World Systems
  Software Design and Development for the Desktop, Mobile, and the Web
  ____________________________________________________________________
  Ambassador at FourthWorld.com                http://www.FourthWorld.com



More information about the use-livecode mailing list