1#!/usr/bin/env python3 2# 3# Copyright 2018 - The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17 18class EventSubscription(object): 19 """A class that defines the way a function is subscribed to an event. 20 21 Attributes: 22 event_type: The type of the event. 23 _func: The subscribed function. 24 _event_filter: A lambda that returns True if an event should be passed 25 to the subscribed function. 26 order: The order value in which this subscription should be called. 27 """ 28 def __init__(self, event_type, func, event_filter=None, order=0): 29 self._event_type = event_type 30 self._func = func 31 self._event_filter = event_filter 32 self.order = order 33 34 @property 35 def event_type(self): 36 return self._event_type 37 38 def deliver(self, event): 39 """Delivers an event to the subscriber. 40 41 This function will not deliver the event if the event filter rejects the 42 event. 43 44 Args: 45 event: The event to send to the subscriber. 46 """ 47 if self._event_filter and not self._event_filter(event): 48 return 49 self._func(event) 50