Inhaltsverzeichnis

Object notification

A simple subscribe-callback mechanism for inter-object communication

This works similar to the built-in interfaces dataNotifier and dataNotificationReceiver.
However the ObjectChanged method receives two parameters: an Object from the notifier and any info from the original subscriber.

The notifier may send himself by setting „me“ as Object, or he sends simply the current data which have been changed.
The subscriber stores any sort of (static) information together with the callback-address when subscribing by addObjectNotificationReceiver.
This info may be a simple index number, but it can also be a whole object (means a reference to an object) so that the receiver can distinguish which subscription called him.

The ObjectNotifierBox contains the complete implementation of the objectNotifier interface.
Simply instantiate this class and call their members like:

Declare:

private property onb as ObjectNotifierBox

in Constructor Method:

onb = new ObjectNotifierBox 

Actual implementation of the objectNotifier Interface:

Sub addObjectNotificationReceiver(receiver as objectNotificationReceiver, optional info as Variant = nil)
   onb.addReceiver(receiver, info)
End Sub
Sub removeObjectNotificationReceiver(receiver As objectNotificationReceiver)
   onb.removeReceiver(receiver)
End Sub

For calling all subscribed receivers write somewhere in your class:

onb.callReceivers(me)

Instead of „me“ you may send any object you need, of course…

Be careful with removing if you have multiple subscriptions from the same object (perhaps with different info).

xy.addObjectNotificationReceiver(me, 1)
xy.addObjectNotificationReceiver(me, 2)

Because removeObjectNotificationReceiver obtains no info parameter, it cancels only the first object found in the array, thus acting as fifo.
It's therefore not possible to delete only the second entry (with info 2).

Sourcecode

Interface objectNotifier

Interface objectNotifier
Methods

Sub addObjectNotificationReceiver(receiver as objectNotificationReceiver, optional info as Variant = nil)
End Sub

Sub removeObjectNotificationReceiver(receiver As objectNotificationReceiver)
End Sub

End Interface

Interface objectNotificationReceiver

Interface objectNotificationReceiver
Methods

Sub objectChanged(obj as Object, info as Variant)
End Sub

End Interface

Class ObjectNotifierBox

Class ObjectNotifierBox
Methods

Sub addReceiver(receiver as objectNotificationReceiver, info as Variant)
   var i as Integer = mReceivers.IndexOf(receiver)
   if i = -1 then
      mReceivers.Add(receiver)
      mInfos.Add(info)
   end if
End Sub

Sub callReceivers(obj as Object)
   For i As Integer = 0 To mReceivers.LastIndex
      mReceivers(i).objectChanged(obj, mInfos(i))
   Next
End Sub

Sub removeReceiver(receiver As objectNotificationReceiver)
   var i as Integer = mReceivers.IndexOf(receiver)
   if i > -1 then
      mReceivers.RemoveAt(i)
      mInfos.RemoveAt(i)
   end if
End Sub

Properties

Private mInfos() As Variant
Private mReceivers() As objectNotificationReceiver

End Class

—-