Weirdness Passing Messages

David Burgun dburgun at dsl.pipex.com
Fri Mar 24 07:36:03 EST 2006


Hi All,

On 23 Mar 2006, at 22:00, J. Landman Gay wrote:

> This isn't to say it shouldn't be fixed, only that I can see how it  
> would get mucked up. The way the sample scripts are written is  
> fairly convoluted; I would never have thought to do the job this  
> way. I suspect that is why this problem has never been reported in  
> all these years, since as Richard mentioned, it really takes a very  
> specific setup to reproduce this problem.
>
> A much better approach would be to move all the handlers into the  
> stack script and take advantage of the natural message hierarchy,  
> rather than relying on all those "sends".

This is how I used to do it and the problem with that is that you  
need to change a lot of scripts etc. in order to build a new app or  
to extend an existing app. What I am trying to achieve is the ability  
to "drop" groups into a stack (via paste) and have them just work  
with no code fiddling! You just saw a demo of the problem not the  
real app. I have developed a message passing library stack that  
allows this to happen. Since I was going do donate this stack to  
RevOnline once I have tested and improved it, I thought I'd explain  
how it works and what I am hoping to achieve now (since we are on the  
subject!) so as to get some input and ideas.

I have a Stack called "StackISM"  (Inter-Stack Messaging), this is  
accessed from the main stack by use of the "start using" statement.

When a Stack instantiates "StackISM" the initialization routine (in  
StackISM) walks thru the objects (all objects in all cards) of the  
calling stack looking for a Handler called "ISM_Initialize", if found  
it calls it.

The "ISM_initialize" handler in the object of the calling stack does  
any object specific initialization and may then call  
"ISM_ListenForMessages(theMessageID)" if it wants to receive message  
of type "theMessageID". It may call "ISM_ListenForMessages" multiple  
times, once for each message it wants to receive.

When an event happens in an object (e.g. mouseUp in a button) that  
needs action to be performed by other objects, "ISM_PutMessage 
(theMessageID,theSubCode,theMessageData)" is called. This will send a  
message(s) to all "listening" objects.

As a "real" example of this in action, take the following case. I  
have a simple Group that just gets a Folder from the user. The  
intention is that the folder will be used as a base by other groups/ 
objects. I have another Group that lists the files in a folder in a  
field, and yet another group that does something with a file selected  
in the "File List" Group.

Here's how it works:

GroupFolder: ISM_Initialize - Sets the Folder to empty and calls  
ISM_PutMessage("ISM_FolderSelected", kFolderKind, FolderPathName  
[empty])

GroupFolder:ButtonChooseFolder:mouseUp - Gets the folder selected by  
the user and calls ISM_PutMessage("ISM_FolderSelected", kFolderKind,  
FolderPathName)

GroupFiles:FieldFileList: ISM_Initialize - calls "ISM_LIstenForMessage 
("ISM_FolderSelected", kFolderKind)

GroupFiles:FieldFileList: ISM_FolderSelected(theMessageID,  
theMessageKind, theFolderPathName) - Gets a list of the files in the  
specified folder and puts them into "me" (hence the problem that  
started this thread!).

GroupFiles:FieldFileList:mouseUp - Gets line selected  (file name)  
from "me" and then calls ISM_PutMessage("ISM_FileSelected",  
kFileKind, FileName)

GroupProcessFile:FieldFileContents: ISM_Initialize - calls  
"ISM_LIstenForMessage("ISM_FileSelected", kFileKind)

GroupProcessFile:FieldFileContents: ISM_FileSelected(theMessageID,  
theMessageKind, theFilePathName) - Put the contents of the file into  
"me".

This is the basic mode of operation, I will do some better  
documentation when I release it to RevOnline.

Incidentally, the above works  just fine regardless of the stack/card/ 
object using "put <something> into me". The problem I described  
yesterday was brought to light because when a new folder is selected,  
I wanted to select the first line (file) in the list, so inside  
ISM_FolderSelect(), I did a ISMPutMessage("ISM_FileSelected,  
kFileKind, line 1 of me), when this happened, "me" in the receiving  
handler got Beswick'ed!

The really good thing about this is, say I wanted to extended this  
app, so it had two base folders (maybe to to give a list of the files  
in two folders and then compare them) and two file lists. All I need  
do is to paste GroupFolder twice and GroupFiles twice and change a  
custom property in the second folder and second file Group and hey  
presto it works! No changing object names! No fiddling around duping  
scripts!

The custom property of each object contains the "kind", this is used  
to identify the origin of the Message and Message Listeners only get  
the "kind" they are listening for!

Although this may be thought of as convoluted at the atomic level, if  
you take a system-wide view of it, it's really much less convoluted  
than doing it using separate scripts in a lots of different objects  
and hard-coding object names.

All the Best
Dave




















More information about the Use-livecode mailing list