Framework / System Layer / Document Object Model / Services / Node Event Sink Service

Node Event Sink Service
The node event sink service (or simply event sink) is a service, which provides centralized event handling. It is represented by the NNodeEventSinkService class. Instead of having events attached to nodes, nodes call the respective "Fire" methods of the event sink to fire the sink events and thus update subscribers. This approach has the following advantages:
  • Smaller memory footprint - nodes, which use the event sink do not have to declare events as members, which reduces the memory footprint of the entire nodes tree.
  • Easier events handling - you do not have to hook/unhook the events of a particular node when it is added/removed from the nodes tree, since the node will automatically fire the sink events if it is attached to an event sink service.
  • Events consistency - since the events (and hence the respective delegates) declared in the event sink are reused by all nodes in the tree, this ensures that nodes fire events with the same signature. This also leads to more generic events declarations, because they need to be reused.
  • Events control - since the event sink is a service, you can stop or pause it, which is a powerful way to disable the events firing of all nodes, which are connected to the event sink.
 Subscribing for Events

There are two ways in which you can subscribe for an event fired by the node event sink service:

  • Subscribe for a broadcased event - this is achieved by adding an event handler for one of the node events exposed by the service. For example:
    C#
    Copy Code
    ...
    // subscribe for a broadcasted event
    nodeEventSinkService.NodePropertyChanged += new NodePropertyEventHandler(OnAnyNodePropertyChanged);
    ...
    private void OnAnyNodePropertyChanged(NNodePropertyEventArgs args)
    {
    }
    
    Visual Basic
    Copy Code
    ...
    ' subscribe for a broadcasted event
    AddHandler nodeEventSinkService.NodePropertyChanged, AddressOf OnAnyNodePropertyChanged
    ...
    Private Sub OnAnyNodePropertyChanged(ByVal args As NNodePropertyEventArgs)
    End Sub
    
    The OnAnyNodePropertyChanged method will be called when a property of any of the nodes, which are connected to this service has changed. This is particularly useful if you do not want to hook/unhook the events of all nodes residing in the node scene.
  • Install an event listener - this is achieved with the help of the AddNodeEventListener method. For example:
    C#
    Copy Code
    ...
    // install node event listener for the NodePropertyChanged event
    nodeEventSinkService.AddNodeEventListener("NodePropertyChanged", node1, new NodePropertyEventHandler(OnNode1PropertyChanged));
    ...
    private void OnNode1PropertyChanged(NNodePropertyEventArgs args)
    {
    }
    
    Visual Basic
    Copy Code
    ...
    '  install node event listener for the NodePropertyChanged event
    Dim handler As NodePropertyEventHandler = AddressOf OnNode1PropertyChanged
    nodeEventSinkService.AddNodeEventListener("NodePropertyChanged", node1, handler)
    ...
    Private Sub OnNode1PropertyChanged(ByVal args As NNodePropertyEventArgs)
    End Sub
    
    The OnNode1PropertyChanged method will only be called when a property of node1 has changed. This method for event subscription is usefull if you want to monitor only the events of a particular node.

From a nodes' point of view it is enough to only call the respective FireXXX method in order to update all listeners and broadcasted event subscribers. By design the service first invokes the event handlers of the registered node listeners and then invokes the broadcasted event handlers.

 Filtering Events
You can also specify a node event filter. This is achieved with the help of the NodeFilter property. If a node filter is specified (the NodeFilter property is not null), then a node event will only be fired if the node satisfies the provided filter. This helps you globally block any node event if the node for which it will be fired does not satisfy the filter criteria.
See Also