Пример #1
0
    def trigger(self, event):
        """Fire an event and return a list of results from all listeners.

        Dispatches an event to all associated listeners and returns a
        list of results. If the event is stopped (Event.stopped) then the
        Result returned will only contain the response from the
        first listener in the stack.

        Args:
            watson.events.types.Event event: The event to trigger

        Returns:
            Result: A list of all the responses
        """
        if not isinstance(event, types.Event):
            raise TypeError('event must be of type watson.events.type.Event')
        results = collections.Result()
        event.params['dispatcher'] = self
        if event.name in self.events:
            collection = self.events[event.name]
            collection.sort_priority()
            for (callback, priority, only_once) in collection[:]:
                results.append(callback(event))
                if only_once:
                    collection.remove(callback)
                if event.stopped:
                    break
        return results
Пример #2
0
 def test_first_last(self):
     collection = collections.Result()
     collection.append('first')
     collection.append('middle')
     collection.append('last')
     assert collection.first() == 'first'
     assert collection.last() == 'last'
Пример #3
0
 def test_first_last_not_exists(self):
     collection = collections.Result()
     assert collection.first() == None
     assert collection.last() == None
Пример #4
0
 def test_create_result(self):
     collection = collections.Result()
     collection.append('result')
     assert repr(
         collection) == '<watson.events.collections.Result results:1>'