Skip to content

Commit

Permalink
WIP-FLASH: basic working version for but GHThread is off
Browse files Browse the repository at this point in the history
  • Loading branch information
9and3 committed Feb 3, 2024
1 parent d9243e2 commit 5be9f20
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions GH/PyGH/scriptsyncGH_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,25 @@
class GHThread(threading.Thread, metaclass=abc.ABCMeta):
def __init__(self, name : str):
super().__init__(name=name, daemon=False)
self.component_on_canvas = True # TODO: need getter and setter

self.component_on_canvas = True # FIXME: property not working
self._component_enabled = True
self.lock = threading.Lock()

@abc.abstractmethod
def run(self):
""" Run the thread. """
pass

# FIXME: this should be integrate the Rhino.RhinoApp.InvokeOnUiThread
# FIXME: function contanirization not working
def check_if_component_on_canvas(self):
""" Check if the component is on canvas. """
if ghenv.Component.OnPingDocument() is None:
self.component_on_canvas = False
return False
return True
else:
self.component_on_canvas = True
return True

def check_if_component_enabled(self):
""" Check if the component is enabled from thread. """
Expand Down Expand Up @@ -90,6 +94,11 @@ def component_enabled(self):
self.check_if_component_enabled()
return self._component_enabled

# @property
# def component_on_canvas(self):
# self.check_if_component_on_canvas()
# return self._component_on_canvas



class ClientThread(GHThread):
Expand All @@ -110,7 +119,7 @@ def run(self):

self.client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

while self.check_if_component_on_canvas() and self.component_enabled: # FIXME: check also when component is disabled
while self.component_on_canvas and self.component_enabled:
try:
if not self.is_connected:
self.connect_to_vscode_server()
Expand All @@ -125,6 +134,8 @@ def run(self):
# self.add_runtime_remark(f"script-sync::Received from server: {data}")

time.sleep(self.refresh_rate)
self.check_if_component_on_canvas() #test

except Exception as e:
if e.winerror == 10054:
# Connection was forcibly closed by the vscode-server
Expand Down Expand Up @@ -188,7 +199,7 @@ def receive_from_server(self, client_socket : socket.socket) -> None:
:param client_socket: The client socket.
"""
while self.component_on_canvas:
while self.component_on_canvas and self.component_enabled:
try:
data = client_socket.recv(1024).decode()
if not data:
Expand Down Expand Up @@ -229,7 +240,6 @@ def check_file_change(self, path : str, path_lock : threading.Lock) -> None:
if not self.component_on_canvas:
print(f"script-sync::Thread {self.name} aborted")
break

current_modified = os.path.getmtime(path)
if current_modified != last_modified:
last_modified = current_modified
Expand Down

0 comments on commit 5be9f20

Please sign in to comment.