11"""CloudEvents class"""
2+
23from __future__ import annotations
34
45from scratchattach .cloud import _base
56from ._base import BaseEventHandler
67from scratchattach .site import cloud_activity
78import time
8- import json
9+ import traceback
910from collections .abc import Iterator
1011
12+
1113class CloudEvents (BaseEventHandler ):
1214 """
1315 Class that calls events when on cloud updates that are received through a websocket connection.
1416 """
17+
1518 def __init__ (self , cloud : _base .AnyCloud ):
1619 super ().__init__ ()
1720 self .cloud = cloud
@@ -37,34 +40,48 @@ def _updater(self):
3740 self .source_stream .timeout = 1
3841 for data in self .source_stream .read ():
3942 try :
40- _a = cloud_activity .CloudActivity (timestamp = time .time ()* 1000 , _session = self ._session , cloud = self .cloud )
41- if _a .timestamp < self .startup_time + 500 : # catch the on_connect message sent by TurboWarp's (and sometimes Scratch's) cloud server
43+ _a = cloud_activity .CloudActivity (
44+ timestamp = time .time () * 1000 ,
45+ _session = self ._session ,
46+ cloud = self .cloud ,
47+ )
48+ if (
49+ _a .timestamp < self .startup_time + 500
50+ ): # catch the on_connect message sent by TurboWarp's (and sometimes Scratch's) cloud server
51+ # print(f"Skipped as {_a.timestamp} < {self.startup_time + 500}")
4252 continue
4353 data ["variable_name" ] = data ["name" ]
4454 data ["name" ] = data ["variable_name" ].replace ("☁ " , "" )
4555 _a ._update_from_dict (data )
46- self .call_event ("on_" + _a .type , [_a ])
56+ # print(f"sending event {_a}")
57+ self .call_event ("on_" + _a .type , [_a ])
4758 except Exception as e :
4859 pass
4960 except Exception :
5061 # print("CloudEvents: Disconnected. Reconnecting ...", time.time())
51- time .sleep (0.1 ) # cooldown
62+ traceback .print_exc () # always print blanketed exceptions!!
63+ self .subsequent_reconnects += 1
64+ time .sleep (0.1 ) # cooldown
5265
5366 # print("CloudEvents: Reconnected.", time.time())
5467 self .call_event ("on_reconnect" , [])
5568
69+
5670class ManualCloudLogEvents :
5771 """
5872 Class that calls events on cloud updates that are received from a clouddata log.
5973 """
74+
6075 def __init__ (self , cloud : _base .LogCloud ):
6176 if not isinstance (cloud , _base .LogCloud ):
62- raise ValueError ("Cloud log events can't be used with a cloud that has no logs available" )
77+ raise ValueError (
78+ "Cloud log events can't be used with a cloud that has no logs available"
79+ )
6380 self .cloud = cloud
6481 self .source_cloud = cloud
6582 self ._session = cloud ._session
6683 self .last_timestamp = 0
67-
84+
6885 def update (self ) -> Iterator [tuple [str , list [cloud_activity .CloudActivity ]]]:
6986 """
7087 Update once and yield all packets
@@ -75,19 +92,22 @@ def update(self) -> Iterator[tuple[str, list[cloud_activity.CloudActivity]]]:
7592 if _a .timestamp <= self .last_timestamp :
7693 continue
7794 self .last_timestamp = _a .timestamp
78- yield ("on_" + _a .type , [_a ])
95+ yield ("on_" + _a .type , [_a ])
7996 except Exception :
8097 pass
81-
98+
8299
83100class CloudLogEvents (BaseEventHandler ):
84101 """
85102 Class that calls events on cloud updates that are received from a clouddata log.
86103 """
104+
87105 def __init__ (self , cloud : _base .LogCloud , * , update_interval = 0.1 ):
88106 super ().__init__ ()
89107 if not isinstance (cloud , _base .LogCloud ):
90- raise ValueError ("Cloud log events can't be used with a cloud that has no logs available" )
108+ raise ValueError (
109+ "Cloud log events can't be used with a cloud that has no logs available"
110+ )
91111 self .cloud = cloud
92112 self .source_cloud = cloud
93113 self .update_interval = update_interval
0 commit comments