8000 Cleaned up event decorator a bit. · pythonanywhere/tornadio2@fe9b8a6 · GitHub
[go: up one dir, main page]

Skip to content

Commit fe9b8a6

Browse files
committed
Cleaned up event decorator a bit.
1 parent bbd0b1d commit fe9b8a6

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

doc/events.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ To handle event on client side, use following code:
3939
However, take care - if method signature does not match (missing parameters, extra
4040
parameters, etc), your connection will blow up and self destruct.
4141

42+
``event`` decorator can be used without parameter and it will use event handler name
43+
in this case::
44+
45+
class MyConnection(SocketConnection):
46+
@event
47+
def hello(self, name):
48+
print 'Hello %s' % name
49+
4250
If you don't like this event handling approach, just override ``on_event`` in your
4351
socket connection class and handle them by yourself:
4452
::

tornadio2/conn.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,29 @@
2626
from tornadio2 import proto
2727

2828

29-
def event(name_or_f):
30-
"""Event handler decorator."""
31-
if callable(name_or_f):
32-
name_or_f._event_name = name_or_f.__name__
33-
return name_or_f
29+
def event(name_or_func):
30+
"""Event handler decorator.
31+
32+
Can be used with event name or will automatically use function name
33+
if not provided::
34+
35+
# Will handle 'foo' event
36+
@event('foo')
37+
def bar(self):
38+
pass
39+
40+
# Will handle 'baz' event
41+
@event
42+
def baz(self):
43+
pass
44+
"""
45+
46+
if callable(name_or_func):
47+
name_or_func._event_name = name_or_func.__name__
48+
return name_or_func
3449

3550
def handler(f):
36-
f._event_name = name_or_f
51+
f._event_name = name_or_func
3752
return f
3853

3954
return handler

0 commit comments

Comments
 (0)
0