@@ -13,6 +13,7 @@ def __init__(self, key_id=None, secret_key=None, base_url=None):
1313 base_url = re .sub (r'^http' , 'ws' , base_url or get_base_url ())
1414 self ._endpoint = base_url + '/stream'
1515 self ._handlers = {}
16+ self ._handler_symbols = {}
1617 self ._base_url = base_url
1718 self ._ws = None
1819 self .polygon = None
@@ -65,7 +66,8 @@ async def _ensure_polygon(self):
6566 if 'staging' in self ._base_url :
6667 key_id += '-staging'
6768 self .polygon = polygon .StreamConn (key_id )
68- self .polygon .register (r'.*' , self ._dispatch_polygon )
69+ self .polygon ._handlers = self ._handlers
70+ self .polygon ._handler_symbols = self ._handler_symbols
6971 await self .polygon .connect ()
7072
7173 async def _ensure_ws (self ):
@@ -121,20 +123,17 @@ def _cast(self, channel, msg):
121123 return Account (msg )
122124 return Entity (msg )
123125
124- async def _dispatch_polygon (self , conn , subject , data ):
125- for pat , handler in self ._handlers .items ():
126- if pat .match (subject ):
127- await handler (self , subject , data )
128-
129126 async def _dispatch (self , channel , msg ):
130127 for pat , handler in self ._handlers .items ():
131128 if pat .match (channel ):
132129 ent = self ._cast (channel , msg ['data' ])
133130 await handler (self , channel , ent )
134131
135- def on (self , channel_pat ):
132+ def on (self , channel_pat , symbols = None ):
136133 def decorator (func ):
137134 self .register (channel_pat , func )
135+ if symbols :
136+ self ._handler_symbols [func ] = symbols
138137 return func
139138
140139 return decorator
@@ -145,8 +144,14 @@ def register(self, channel_pat, func):
145144 if isinstance (channel_pat , str ):
146145 channel_pat = re .compile (channel_pat )
147146 self ._handlers [channel_pat ] = func
147+ if self .polygon :
148+ self .polygon .register (channel_pat , func )
148149
149150 def deregister (self , channel_pat ):
150151 if isinstance (channel_pat , str ):
151152 channel_pat = re .compile (channel_pat )
153+ handler = self ._handlers [channel_pat ]
154+ del self ._handler_symbols [handler ]
152155 del self ._handlers [channel_pat ]
156+ if self .polygon :
157+ self .polygon .deregister (channel_pat )
0 commit comments