Skip to content

Commit

Permalink
Merge pull request #798 from meejah/newapi-improvements.6-reg-sub-dec…
Browse files Browse the repository at this point in the history
…orators

A decorator-based API for subscribe and register
  • Loading branch information
oberstet committed Mar 29, 2017
2 parents 6e8cb7b + 49021bf commit f620b27
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion autobahn/wamp/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

from autobahn.util import ObservableMixin
from autobahn.websocket.util import parse_url
from autobahn.wamp.types import ComponentConfig
from autobahn.wamp.types import ComponentConfig, SubscribeOptions, RegisterOptions
from autobahn.wamp.exception import SessionNotReady


Expand Down Expand Up @@ -300,6 +300,50 @@ class Component(ObservableMixin):
The factory of the session we will instantiate.
"""

def subscribe(self, topic, options=None):
"""
A decorator as a shortcut for subscribing during on-join
For example::
@component.subscribe(
u"some.topic",
options=SubscribeOptions(match=u'prefix'),
)
def topic(*args, **kw):
print("some.topic({}, {}): event received".format(args, kw))
"""
assert options is None or isinstance(options, SubscribeOptions)

def decorator(fn):

def do_subscription(session, details):
return session.subscribe(fn, topic=topic, options=options)
self.on('join', do_subscription)
return decorator

def register(self, uri, options=None):
"""
A decorator as a shortcut for registering during on-join
For example::
@component.register(
u"com.example.add",
options=RegisterOptions(invoke='round_robin'),
)
def add(*args, **kw):
print("add({}, {}): event received".format(args, kw))
"""
assert options is None or isinstance(options, RegisterOptions)

def decorator(fn):

def do_registration(session, details):
return session.register(fn, procedure=uri, options=options)
self.on('join', do_registration)
return decorator

def __init__(self, main=None, transports=None, config=None, realm=u'default', extra=None):
"""
:param main: After a transport has been connected and a session
Expand Down

0 comments on commit f620b27

Please sign in to comment.