Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Beide Seiten der vorigen Revision Vorhergehende Überarbeitung Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
xojo:objectnotifier:start [2021/04/01 17:42] mz |
xojo:objectnotifier:start [2021/04/06 09:27] (aktuell) mz [Object notification] |
||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
- | ====== Object | + | ====== Object |
- | /* {{tag> }} */ | + | |
- | {{indexmenu>.#3|ns tsort hsort nsort}} | + | <fs x-large> |
- | ---- | + | <WRAP round download 40%> |
+ | **{{ : | ||
- | ===== Interface objectNotifier ===== | + | {{ : |
+ | </ | ||
- | ===== Interface objectNotificationReceiver ===== | + | This works similar to the built-in interfaces // |
+ | However the **ObjectChanged** method receives two parameters: an **Object** from the notifier and any **info** from the original subscriber.\\ | ||
- | ===== Class ObjectNotifierBox | + | The **notifier** may send himself by setting " |
+ | The **subscriber** stores any sort of (static) information together with the callback-address when subscribing by // | ||
+ | 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 **// | ||
+ | Simply instantiate this class and call their members like: | ||
+ | |||
+ | Declare: | ||
+ | private property onb as ObjectNotifierBox | ||
+ | in // | ||
+ | onb = new ObjectNotifierBox | ||
+ | Actual implementation of the // | ||
+ | Sub addObjectNotificationReceiver(receiver as objectNotificationReceiver, | ||
+ | | ||
+ | End Sub | ||
+ | |||
+ | Sub removeObjectNotificationReceiver(receiver As objectNotificationReceiver) | ||
+ | | ||
+ | End Sub | ||
+ | |||
+ | For calling all subscribed receivers write somewhere in your class: | ||
+ | |||
+ | onb.callReceivers(me) | ||
+ | |||
+ | Instead of " | ||
+ | |||
+ | <WRAP round important 100%> | ||
+ | Be careful with // | ||
+ | < | ||
+ | xy.addObjectNotificationReceiver(me, | ||
+ | xy.addObjectNotificationReceiver(me, | ||
+ | </ | ||
+ | Because // | ||
+ | It's therefore not possible to delete only the second entry (with //info// 2). | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | ====== Sourcecode ====== | ||
+ | |||
+ | {{ : | ||
+ | |||
+ | |||
+ | ===== Interface objectNotifier | ||
- | | ||
- | | ||
- | Object Notification | ||
Interface objectNotifier | Interface objectNotifier | ||
Methods | Methods | ||
- | Sub addObjectNotificationReceiver(receiver as objectNotificationReceiver, | + | |
- | nt = nil) | + | |
End Sub | End Sub | ||
+ | | ||
Sub removeObjectNotificationReceiver(receiver As objectNotificationReceiver) | Sub removeObjectNotificationReceiver(receiver As objectNotificationReceiver) | ||
End Sub | End Sub | ||
+ | | ||
End Interface | End Interface | ||
+ | |||
+ | |||
+ | ===== Interface objectNotificationReceiver ===== | ||
+ | |||
Interface objectNotificationReceiver | Interface objectNotificationReceiver | ||
Methods | Methods | ||
+ | | ||
Sub objectChanged(obj as Object, info as Variant) | Sub objectChanged(obj as Object, info as Variant) | ||
End Sub | End Sub | ||
+ | | ||
End Interface | End Interface | ||
+ | |||
+ | |||
+ | ===== Class ObjectNotifierBox ===== | ||
+ | |||
| | ||
Class ObjectNotifierBox | Class ObjectNotifierBox | ||
- | | ||
Methods | Methods | ||
| | ||
Sub addReceiver(receiver as objectNotificationReceiver, | Sub addReceiver(receiver as objectNotificationReceiver, | ||
- | | + | var i as Integer = mReceivers.IndexOf(receiver) |
- | if i = -1 then | + | |
- | mReceivers.Add(receiver) | + | mReceivers.Add(receiver) |
- | mInfos.Add(info) | + | mInfos.Add(info) |
- | end if | + | |
- | // | + | |
End Sub | End Sub | ||
+ | | ||
Sub callReceivers(obj as Object) | Sub callReceivers(obj as Object) | ||
- | | + | For i As Integer = 0 To mReceivers.LastIndex |
- | mReceivers(i).objectChanged(obj, | + | mReceivers(i).objectChanged(obj, |
- | Next | + | |
- | // | + | |
End Sub | End Sub | ||
+ | | ||
Sub removeReceiver(receiver As objectNotificationReceiver) | Sub removeReceiver(receiver As objectNotificationReceiver) | ||
- | | + | var i as Integer = mReceivers.IndexOf(receiver) |
- | if i > -1 then | + | |
- | mReceivers.RemoveAt(i) | + | mReceivers.RemoveAt(i) |
- | mInfos.RemoveAt(i) | + | mInfos.RemoveAt(i) |
- | end if | + | |
- | // | + | |
End Sub | End Sub | ||
| | ||
Properties | Properties | ||
- | mInfos() As Variant | + | |
- | mReceivers() As objectNotificationReceiver | + | Private |
+ | | ||
| | ||
End Class | End Class | ||
| | ||
- | |||
---- | ---- | ||
/*eof*/ | /*eof*/ | ||