4
4
from pubnub .event_engine .models import events
5
5
from pubnub .exceptions import PubNubException
6
6
from typing import List , Union
7
+ from pubnub .models .consumer .pn_error_data import PNErrorData
7
8
8
9
9
10
class PNContext (dict ):
@@ -122,7 +123,15 @@ def subscription_changed(self, event: events.SubscriptionChangedEvent, context:
122
123
123
124
return PNTransition (
124
125
state = HandshakingState ,
125
- context = self ._context
126
+ context = self ._context ,
127
+ invocation = [
128
+ invocations .EmitStatusInvocation (PNStatusCategory .PNSubscriptionChangedCategory ,
129
+ operation = PNOperationType .PNSubscribeOperation ,
130
+ context = self ._context ),
131
+ invocations .EmitStatusInvocation (PNStatusCategory .PNAcknowledgmentCategory ,
132
+ operation = PNOperationType .PNSubscribeOperation ,
133
+ context = self ._context ),
134
+ ]
126
135
)
127
136
128
137
def subscription_restored (self , event : events .SubscriptionRestoredEvent , context : PNContext ) -> PNTransition :
@@ -148,7 +157,7 @@ def reconnecting(self, event: events.HandshakeFailureEvent, context: PNContext)
148
157
149
158
return PNTransition (
150
159
state = HandshakeReconnectingState ,
151
- context = self ._context
160
+ context = self ._context ,
152
161
)
153
162
154
163
def disconnect (self , event : events .DisconnectEvent , context : PNContext ) -> PNTransition :
@@ -183,8 +192,14 @@ def unsubscribe_all(self, event: events.UnsubscribeAllEvent, context: PNContext)
183
192
return PNTransition (
184
193
state = UnsubscribedState ,
185
194
context = self ._context ,
186
- invocation = invocations .EmitStatusInvocation (PNStatusCategory .PNAcknowledgmentCategory ,
187
- operation = PNOperationType .PNUnsubscribeOperation )
195
+ invocation = [
196
+ invocations .EmitStatusInvocation (PNStatusCategory .PNDisconnectedCategory ,
197
+ operation = PNOperationType .PNSubscribeOperation ,
198
+ context = self ._context ),
199
+ invocations .EmitStatusInvocation (PNStatusCategory .PNAcknowledgmentCategory ,
200
+ operation = PNOperationType .PNSubscribeOperation ,
201
+ context = self ._context ),
202
+ ]
188
203
)
189
204
190
205
@@ -218,7 +233,10 @@ def disconnect(self, event: events.DisconnectEvent, context: PNContext) -> PNTra
218
233
219
234
return PNTransition (
220
235
state = HandshakeStoppedState ,
221
- context = self ._context
236
+ context = self ._context ,
237
+ invocation = invocations .EmitStatusInvocation (PNStatusCategory .PNDisconnectedCategory ,
238
+ operation = PNOperationType .PNSubscribeOperation ,
239
+ context = self ._context )
222
240
)
223
241
224
242
def subscription_changed (self , event : events .SubscriptionChangedEvent , context : PNContext ) -> PNTransition :
@@ -230,7 +248,10 @@ def subscription_changed(self, event: events.SubscriptionChangedEvent, context:
230
248
231
249
return PNTransition (
232
250
state = HandshakeReconnectingState ,
233
- context = self ._context
251
+ context = self ._context ,
252
+ invocation = invocations .EmitStatusInvocation (PNStatusCategory .PNSubscriptionChangedCategory ,
253
+ operation = PNOperationType .PNSubscribeOperation ,
254
+ context = self ._context )
234
255
)
235
256
236
257
def handshake_reconnect (self , event : events .HandshakeReconnectFailureEvent , context : PNContext ) -> PNTransition :
@@ -240,7 +261,7 @@ def handshake_reconnect(self, event: events.HandshakeReconnectFailureEvent, cont
240
261
241
262
return PNTransition (
242
263
state = HandshakeReconnectingState ,
243
- context = self ._context
264
+ context = self ._context ,
244
265
)
245
266
246
267
def give_up (self , event : events .HandshakeReconnectGiveupEvent , context : PNContext ) -> PNTransition :
@@ -252,8 +273,15 @@ def give_up(self, event: events.HandshakeReconnectGiveupEvent, context: PNContex
252
273
if isinstance (event , Exception ) and 'status' in event .reason :
253
274
status_invocation = invocations .EmitStatusInvocation (status = event .reason .status .category ,
254
275
operation = PNOperationType .PNUnsubscribeOperation )
276
+ elif isinstance (context .reason , PNErrorData ):
277
+ status_invocation = invocations .EmitStatusInvocation (PNStatusCategory .PNConnectionErrorCategory ,
278
+ context = self ._context )
279
+ elif isinstance (context .reason , PubNubException ):
280
+ status = context .reason .status
281
+ status .category = PNStatusCategory .PNConnectionErrorCategory
282
+ status_invocation = invocations .EmitStatusInvocation (status )
255
283
else :
256
- status_invocation = invocations .EmitStatusInvocation (PNStatusCategory .PNDisconnectedCategory )
284
+ status_invocation = invocations .EmitStatusInvocation (PNStatusCategory .PNConnectionErrorCategory )
257
285
258
286
return PNTransition (
259
287
state = HandshakeFailedState ,
@@ -305,7 +333,10 @@ def subscription_changed(self, event: events.SubscriptionChangedEvent, context:
305
333
306
334
return PNTransition (
307
335
state = HandshakingState ,
308
- context = self ._context
336
+ context = self ._context ,
337
+ invocation = invocations .EmitStatusInvocation (PNStatusCategory .PNSubscriptionChangedCategory ,
338
+ operation = PNOperationType .PNSubscribeOperation ,
339
+ context = self ._context )
309
340
)
310
341
311
342
def reconnect (self , event : events .ReconnectEvent , context : PNContext ) -> PNTransition :
@@ -340,8 +371,14 @@ def unsubscribe_all(self, event: events.UnsubscribeAllEvent, context: PNContext)
340
371
return PNTransition (
341
372
state = UnsubscribedState ,
342
373
context = self ._context ,
343
- invocation = invocations .EmitStatusInvocation (PNStatusCategory .PNAcknowledgmentCategory ,
344
- operation = PNOperationType .PNUnsubscribeOperation )
374
+ invocation = [
375
+ invocations .EmitStatusInvocation (PNStatusCategory .PNDisconnectedCategory ,
376
+ operation = PNOperationType .PNSubscribeOperation ,
377
+ context = self ._context ),
378
+ invocations .EmitStatusInvocation (PNStatusCategory .PNAcknowledgmentCategory ,
379
+ operation = PNOperationType .PNSubscribeOperation ,
380
+ context = self ._context ),
381
+ ]
345
382
)
346
383
347
384
@@ -374,8 +411,14 @@ def unsubscribe_all(self, event: events.UnsubscribeAllEvent, context: PNContext)
374
411
return PNTransition (
375
412
state = UnsubscribedState ,
376
413
context = self ._context ,
377
- invocation = invocations .EmitStatusInvocation (PNStatusCategory .PNAcknowledgmentCategory ,
378
- operation = PNOperationType .PNUnsubscribeOperation )
414
+ invocation = [
415
+ invocations .EmitStatusInvocation (PNStatusCategory .PNDisconnectedCategory ,
416
+ operation = PNOperationType .PNSubscribeOperation ,
417
+ context = self ._context ),
418
+ invocations .EmitStatusInvocation (PNStatusCategory .PNAcknowledgmentCategory ,
419
+ operation = PNOperationType .PNSubscribeOperation ,
420
+ context = self ._context ),
421
+ ]
379
422
)
380
423
381
424
@@ -412,7 +455,10 @@ def subscription_changed(self, event: events.SubscriptionChangedEvent, context:
412
455
413
456
return PNTransition (
414
457
state = self .__class__ ,
415
- context = self ._context
458
+ context = self ._context ,
459
+ invocation = invocations .EmitStatusInvocation (PNStatusCategory .PNSubscriptionChangedCategory ,
460
+ operation = PNOperationType .PNSubscribeOperation ,
461
+ context = self ._context )
416
462
)
417
463
418
464
def subscription_restored (self , event : events .SubscriptionRestoredEvent , context : PNContext ) -> PNTransition :
@@ -446,7 +492,7 @@ def receiving_failure(self, event: events.ReceiveFailureEvent, context: PNContex
446
492
self ._context .timetoken = event .timetoken
447
493
return PNTransition (
448
494
state = ReceiveReconnectingState ,
449
- context = self ._context
495
+ context = self ._context ,
450
496
)
451
497
452
498
def disconnect (self , event : events .DisconnectEvent , context : PNContext ) -> PNTransition :
@@ -477,8 +523,14 @@ def unsubscribe_all(self, event: events.UnsubscribeAllEvent, context: PNContext)
477
523
return PNTransition (
478
524
state = UnsubscribedState ,
479
525
context = self ._context ,
480
- invocation = invocations .EmitStatusInvocation (PNStatusCategory .PNAcknowledgmentCategory ,
481
- operation = PNOperationType .PNUnsubscribeOperation )
526
+ invocation = [
527
+ invocations .EmitStatusInvocation (PNStatusCategory .PNDisconnectedCategory ,
528
+ operation = PNOperationType .PNSubscribeOperation ,
529
+ context = self ._context ),
530
+ invocations .EmitStatusInvocation (PNStatusCategory .PNAcknowledgmentCategory ,
531
+ operation = PNOperationType .PNSubscribeOperation ,
532
+ context = self ._context ),
533
+ ]
482
534
)
483
535
484
536
@@ -515,7 +567,10 @@ def reconnect_failure(self, event: events.ReceiveReconnectFailureEvent, context:
515
567
516
568
return PNTransition (
517
569
state = ReceiveReconnectingState ,
518
- context = self ._context
570
+ context = self ._context ,
571
+ invocation = invocations .EmitStatusInvocation (PNStatusCategory .UnexpectedDisconnectCategory ,
572
+ operation = PNOperationType .PNSubscribeOperation ,
573
+ context = self ._context )
519
574
)
520
575
521
576
def subscription_changed (self , event : events .SubscriptionChangedEvent , context : PNContext ) -> PNTransition :
@@ -527,7 +582,10 @@ def subscription_changed(self, event: events.SubscriptionChangedEvent, context:
527
582
528
583
return PNTransition (
529
584
state = ReceiveReconnectingState ,
530
- context = self ._context
585
+ context = self ._context ,
586
+ invocation = invocations .EmitStatusInvocation (PNStatusCategory .PNSubscriptionChangedCategory ,
587
+ operation = PNOperationType .PNSubscribeOperation ,
588
+ context = self ._context )
531
589
)
532
590
533
591
def disconnect (self , event : events .DisconnectEvent , context : PNContext ) -> PNTransition :
@@ -546,7 +604,9 @@ def give_up(self, event: events.ReceiveReconnectGiveupEvent, context: PNContext)
546
604
return PNTransition (
547
605
state = ReceiveFailedState ,
548
606
context = self ._context ,
549
- invocation = invocations .EmitStatusInvocation (PNStatusCategory .PNDisconnectedCategory )
607
+ invocation = invocations .EmitStatusInvocation (PNStatusCategory .PNUnexpectedDisconnectCategory ,
608
+ operation = PNOperationType .PNSubscribeOperation ,
609
+ context = self ._context )
550
610
)
551
611
552
612
def reconnect_success (self , event : events .ReceiveReconnectSuccessEvent , context : PNContext ) -> PNTransition :
@@ -602,7 +662,10 @@ def subscription_changed(self, event: events.SubscriptionChangedEvent, context:
602
662
603
663
return PNTransition (
604
664
state = ReceivingState ,
605
- context = self ._context
665
+ context = self ._context ,
666
+ invocation = invocations .EmitStatusInvocation (PNStatusCategory .PNSubscriptionChangedCategory ,
667
+ operation = PNOperationType .PNSubscribeOperation ,
668
+ context = self ._context )
606
669
)
607
670
608
671
def reconnect (self , event : events .ReconnectEvent , context : PNContext ) -> PNTransition :
@@ -637,8 +700,14 @@ def unsubscribe_all(self, event: events.UnsubscribeAllEvent, context: PNContext)
637
700
return PNTransition (
638
701
state = UnsubscribedState ,
639
702
context = self ._context ,
640
- invocation = invocations .EmitStatusInvocation (PNStatusCategory .PNAcknowledgmentCategory ,
641
- operation = PNOperationType .PNUnsubscribeOperation )
703
+ invocation = [
704
+ invocations .EmitStatusInvocation (PNStatusCategory .PNDisconnectedCategory ,
705
+ operation = PNOperationType .PNSubscribeOperation ,
706
+ context = self ._context ),
707
+ invocations .EmitStatusInvocation (PNStatusCategory .PNAcknowledgmentCategory ,
708
+ operation = PNOperationType .PNSubscribeOperation ,
709
+ context = self ._context ),
710
+ ]
642
711
)
643
712
644
713
@@ -671,8 +740,14 @@ def unsubscribe_all(self, event: events.UnsubscribeAllEvent, context: PNContext)
671
740
return PNTransition (
672
741
state = UnsubscribedState ,
673
742
context = self ._context ,
674
- invocation = invocations .EmitStatusInvocation (PNStatusCategory .PNAcknowledgmentCategory ,
675
- operation = PNOperationType .PNUnsubscribeOperation )
743
+ invocation = [
744
+ invocations .EmitStatusInvocation (PNStatusCategory .PNDisconnectedCategory ,
745
+ operation = PNOperationType .PNSubscribeOperation ,
746
+ context = self ._context ),
747
+ invocations .EmitStatusInvocation (PNStatusCategory .PNAcknowledgmentCategory ,
748
+ operation = PNOperationType .PNSubscribeOperation ,
749
+ context = self ._context ),
750
+ ]
676
751
)
677
752
678
753
0 commit comments