@@ -133,34 +133,88 @@ async def subscribe(
133
133
134
134
sub_ids : List [HexStr ] = []
135
135
for sub in subscriptions :
136
- await self .subscribe (sub )
136
+ sub_ids . append ( await self .subscribe (sub ) )
137
137
return sub_ids
138
138
raise Web3TypeError ("Expected a Subscription or a sequence of Subscriptions." )
139
139
140
- async def unsubscribe (self , subscription : EthSubscription [Any ]) -> bool :
140
+ @overload
141
+ async def unsubscribe (self , subscriptions : EthSubscription [Any ]) -> bool :
142
+ ...
143
+
144
+ @overload
145
+ async def unsubscribe (self , subscriptions : HexStr ) -> bool :
146
+ ...
147
+
148
+ @overload
149
+ async def unsubscribe (
150
+ self ,
151
+ subscriptions : Sequence [Union [EthSubscription [Any ], HexStr ]],
152
+ ) -> bool :
153
+ ...
154
+
155
+ async def unsubscribe (
156
+ self ,
157
+ subscriptions : Union [
158
+ EthSubscription [Any ],
159
+ HexStr ,
160
+ Sequence [Union [EthSubscription [Any ], HexStr ]],
161
+ ],
162
+ ) -> bool :
141
163
"""
142
- Used to unsubscribe from a subscription .
164
+ Used to unsubscribe from one or multiple subscriptions .
143
165
144
- :param subscription: The subscription to unsubscribe from.
145
- :type subscription: EthSubscription
146
- :return: ``True`` if unsubscribing was successful, ``False`` otherwise.
166
+ :param subscriptions: The subscription(s) to unsubscribe from.
167
+ :type subscriptions: Union[EthSubscription, Sequence[EthSubscription], HexStr,
168
+ Sequence[HexStr]]
169
+ :return: ``True`` if unsubscribing to all was successful, ``False`` otherwise
170
+ with a warning.
147
171
:rtype: bool
148
172
"""
149
- if subscription not in self .subscriptions :
150
- raise Web3ValueError (
151
- "Subscription not found or is not being managed by the subscription "
152
- f"manager.\n label: { subscription .label } \n id: { subscription ._id } "
153
- )
154
- if await self ._w3 .eth ._unsubscribe (subscription .id ):
155
- self ._remove_subscription (subscription )
156
- self .logger .info (
157
- "Successfully unsubscribed from subscription:\n "
158
- f"label: { subscription .label } \n id: { subscription .id } "
159
- )
160
- if len (self ._subscription_container .handler_subscriptions ) == 0 :
161
- queue = self ._provider ._request_processor ._handler_subscription_queue
162
- await queue .put (SubscriptionProcessingFinished ())
163
- return True
173
+ if isinstance (subscriptions , EthSubscription ) or isinstance (subscriptions , str ):
174
+ if isinstance (subscriptions , str ):
175
+ subscription_id = subscriptions
176
+ subscriptions = self .get_by_id (subscription_id )
177
+ if subscriptions is None :
178
+ raise Web3ValueError (
179
+ "Subscription not found or is not being managed by the "
180
+ f"subscription manager.\n id: { subscription_id } "
181
+ )
182
+
183
+ if subscriptions not in self .subscriptions :
184
+ raise Web3ValueError (
185
+ "Subscription not found or is not being managed by the "
186
+ "subscription manager.\n "
187
+ f"label: { subscriptions .label } \n id: { subscriptions ._id } "
188
+ )
189
+
190
+ if await self ._w3 .eth ._unsubscribe (subscriptions .id ):
191
+ self ._remove_subscription (subscriptions )
192
+ self .logger .info (
193
+ "Successfully unsubscribed from subscription:\n "
194
+ f"label: { subscriptions .label } \n id: { subscriptions .id } "
195
+ )
196
+
197
+ if len (self ._subscription_container .handler_subscriptions ) == 0 :
198
+ queue = (
199
+ self ._provider ._request_processor ._handler_subscription_queue
200
+ )
201
+ await queue .put (SubscriptionProcessingFinished ())
202
+ return True
203
+
204
+ elif isinstance (subscriptions , Sequence ):
205
+ if len (subscriptions ) == 0 :
206
+ raise Web3ValueError ("No subscriptions provided." )
207
+
208
+ unsubscribed : List [bool ] = []
209
+ for sub in subscriptions :
210
+ if isinstance (sub , str ):
211
+ sub = HexStr (sub )
212
+ unsubscribed .append (await self .unsubscribe (sub ))
213
+ return all (unsubscribed )
214
+
215
+ self .logger .warning (
216
+ f"Failed to unsubscribe from subscription\n subscription={ subscriptions } "
217
+ )
164
218
return False
165
219
166
220
async def unsubscribe_all (self ) -> bool :
@@ -195,15 +249,15 @@ async def handle_subscriptions(self, run_forever: bool = False) -> None:
195
249
:type run_forever: bool
196
250
:return: None
197
251
"""
198
- if not self ._subscription_container .handler_subscriptions :
252
+ if not self ._subscription_container .handler_subscriptions and not run_forever :
199
253
self .logger .warning (
200
254
"No handler subscriptions found. Subscription handler did not run."
201
255
)
202
256
return
203
257
204
258
queue = self ._provider ._request_processor ._handler_subscription_queue
205
- try :
206
- while run_forever or self . _subscription_container . handler_subscriptions :
259
+ while run_forever or self . _subscription_container . handler_subscriptions :
260
+ try :
207
261
response = cast (RPCResponse , await queue .get ())
208
262
formatted_sub_response = cast (
209
263
FormattedEthSubscriptionResponse ,
@@ -225,18 +279,20 @@ async def handle_subscriptions(self, run_forever: bool = False) -> None:
225
279
** sub ._handler_context ,
226
280
)
227
281
)
228
- except SubscriptionProcessingFinished :
229
- self .logger .info (
230
- "All handler subscriptions have been unsubscribed from. "
231
- "Stopping subscription handling."
232
- )
233
- except TaskNotRunning :
234
- await asyncio .sleep (0 )
235
- self ._provider ._handle_listener_task_exceptions ()
236
- self .logger .error (
237
- "Message listener background task for the provider has stopped "
238
- "unexpectedly. Stopping subscription handling."
239
- )
282
+ except SubscriptionProcessingFinished :
283
+ if not run_forever :
284
+ self .logger .info (
285
+ "All handler subscriptions have been unsubscribed from. "
286
+ "Stopping subscription handling."
287
+ )
288
+ break
289
+ except TaskNotRunning :
290
+ await asyncio .sleep (0 )
291
+ self ._provider ._handle_listener_task_exceptions ()
292
+ self .logger .error (
293
+ "Message listener background task for the provider has stopped "
294
+ "unexpectedly. Stopping subscription handling."
295
+ )
240
296
241
297
# no active handler subscriptions, clear the handler subscription queue
242
298
self ._provider ._request_processor ._reset_handler_subscription_queue ()
0 commit comments