Skip to content

Commit

Permalink
fix: Prevent Multiple App Reloads | closes #12
Browse files Browse the repository at this point in the history
Implemented a slower but more robust method for detecting file changes. Before triggering an app reload, the new approach verifies whether the file content has actually changed. This resolves the issue discussed in #12, where unnecessary reloads were occurring.
  • Loading branch information
iamDyeus committed Nov 19, 2024
1 parent 5c5b738 commit 58c9428
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions tkreload/app_event_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,41 @@

class AppFileEventHandler(FileSystemEventHandler):
"""Handles file changes to trigger app reload."""

def __init__(self, callback, app_file, auto_reload_manager):
self.callback = callback
self.app_file = app_file
self.auto_reload_manager = auto_reload_manager
self.last_content = None

def on_modified(self, event):
"""
Called when a file is modified.
This method checks if the modified file is the one being monitored
and if the auto-reload manager is active. If the content of the file
has changed, it triggers the provided callback function.
Args:
event: The event object containing information about the file modification.
"""
if event.src_path.endswith(self.app_file) and self.auto_reload_manager.get_status():
self.callback()
current_content = self.read_file_content(self.app_file)
if current_content != self.last_content:
self.last_content = current_content
self.callback()

def read_file_content(self, file_path):
"""
Reads the content of a file.
This method opens the specified file in read mode and returns its content
as a string.
Args:
file_path (str): The path to the file to be read.
Returns:
str: The content of the file.
"""
with open(file_path, 'r') as file:
return file.read()

0 comments on commit 58c9428

Please sign in to comment.