Beispiel #1
0
    def publish(self, event):
        """
        Publishes an event to subscribers.
        """
        if not IEvent.providedBy(event):
            raise BusError('Invalid dispatching, event should provide IEvent')

        # Dispatches event to subscribers
        if self.subscribers.has_key(type(event)):
            for subscriber in self.subscribers[type(event)]:
                subscriber(event)

        # Publishes event on connected buses
        for follower in self.followers:
            follower.publish(event)
Beispiel #2
0
    def subscribe(self, klass, callback):
        """
        Subscribe a callable to an event type (class).

        An already subscribed callback is ignored.

        @param klass    The event type to subscribe to
        @param callback The callable object to pass event when an event is
                        dispatched.
        """
        if not IEvent.implementedBy(klass):
            raise BusError('Invalid unsubscription, klass should implenent IEvent')

        if not self.subscribers.has_key(klass):
            self.subscribers[klass] = []

        if not callback in self.subscribers[klass]:
            self.subscribers[klass].append(callback)
Beispiel #3
0
    def unsubscribe(self, klass, callback):
        """
        Unsubscribe a callable from an event type (class).

        An already subscribed callback is ignored.

        @param klass    The event type to subscribe to
        @param callback The callable object to pass event when an event is
                        dispatched.
        """
        if not IEvent.implementedBy(klass):
            raise BusError('Invalid unsubscription, klass should implenent IEvent')

        if self.subscribers.has_key(klass):
            # Removes subscriber callback
            if callback in self.subscribers[klass]:
                self.subscribers[klass].remove(callback)

            # Cleans subscriber disctionnary
            if not len(self.subscribers[klass]):
                del self.subscribers[klass]