(picture)

August 20, 2002

Instance ID

Here's a trick for the list (sometime I'll sort my own categories out...): generating your own instance ID for properties. Read on.

I use the PropertyList for all sorts of things. When listening to a propertylist, the event handler looks like this:
PropertyList_OnPropertyChanged( sPropName, pValue, bIsLocal )
{
// do stuff
}

The "bIsLocal" boolean flag tells you whether that property changed locally or on another user. It's useful, in principle: a good pattern is to have your property "setter" independent of the property "listener", so you might set a property in response to a UI gesture, then use OnPropertyChanged to hear what other users have been doing and update your own UI appropriately. In that case you really don't want to listen to events which you caused.

Unfortunately, the "local" flag is a sledgehammer. If you use "open tool in new window" (so you have two instances of the tool UI running), any properties set by your tool won't appear "remote" to the other instance.

So, I like to wrap properties up inside an XML element (which buys some more flexibility anyway), and tag that element with an instance-ID which is unique to that actual instance of the UI code.

To do this:

var g_sSessionID;
function Initialize()
{
// UI gluecode initialization
var pGuidGenerator = GROOVE_GLOBAL_MISC_POINTER.OpenGUIDGenerator();
g_sSessionID = pGuidGenerator.CreateGUIDBase16();
}

function SomethingWhichNeedsToSetAProperty( sPropertyName, x, y, z )
{
var pEl = CreateNewElement( "urn:groove.net:something" );
pEl.SetAttribute( "SessionID", g_sSessionID );
pEl.SetAttribute( "x", x ); // etcetera
PropertyList.SetPropertyAsElement( sPropertyName, pEl );
}

// and finally the listener

function OnPropertyChanged( sKey, pValue, bLocal )
{
// assuming pValue is an element (may depend on the propertyname)
var sSessionID = pValue.OpenAttribute( "SessionID" );
var x = pValue.OpenAttribute( "x" ); // etcetera
var bNonLocal = (g_sSessionID != sSessionID);
if( bNonLocal )
{
// do stuff with that propertychange
}
}