This repository has been archived on 2025-07-31. You can view files and clone it, but cannot push or open issues or pull requests.
Files
fbchat/fbchat/event_hook.py
Mads Marquart a76ebbb22a Added python 2.7 support, reworked events
- Reworked events, so now they support python 2.7 (I had to remove some
functionality though, but that was a little unnecessary anyway)
- Events now support the old style of writing, for people who's more
comfortable with that: ```python
class EchoBot(fbchat.Client):
    def onMessage(self, *args, **kwargs):
        self.something(*args, **kwargs)
```
While still supporting the new method:
```python
class EchoBot(fbchat.Client):
    def __init__(self, *args, **kwargs):
         super(EchoBot, self).__init__(*args, **kwargs)
         self.onMessage += lamda *args, **kwargs: self.something(*args,
**kwargs)
```
- Included `msg` as a parameter in every event function, since it's
useful if you want to extract some of the other data
- Moved test data to the folder `tests`
- Fixed various other functions, and improved stability
2017-05-22 20:33:00 +02:00

29 lines
705 B
Python

# -*- coding: UTF-8 -*-
class EventHook(object):
"""
A simple implementation of the Observer-Pattern.
All listeners added to this will be called, regardless of parameters
"""
def __init__(self, *args):
self._handlers = list(args)
def add(self, handler):
return self.__iadd__(handler)
def remove(self, handler):
return self.__isub__(handler)
def __iadd__(self, handler):
self._handlers.append(handler)
return self
def __isub__(self, handler):
self._handlers.remove(handler)
return self
def __call__(self, *args, **kwargs):
for handler in self._handlers:
handler(*args, **kwargs)