Skip to content

Commit

Permalink
Fix reload loop
Browse files Browse the repository at this point in the history
  • Loading branch information
calumy committed Dec 21, 2021
1 parent a94c3a1 commit 568dadb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
20 changes: 12 additions & 8 deletions livereload/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,21 @@ def start_tasks(cls):

@classmethod
def poll_tasks(cls):
filepath, delay = cls.watcher.examine()
if not filepath or delay == 'forever' or not cls.waiters:
filepaths, delay = cls.watcher.examine()
if len(filepaths) == 0 or delay == 'forever' or not cls.waiters:
return
reload_time = 3

if delay:
reload_time = max(3 - delay, 1)
if filepath == '__livereload__':
reload_time = max(reload_time - delay, 1)
if '__livereload__' in filepaths:
reload_time = 0

if time.time() - cls._last_reload_time < reload_time:
# if you changed lot of files in one time
# it will refresh too many times
logger.info('Ignore: %s', filepath)
for filepath in filepaths:
logger.info('Ignore: %s', filepath)
return
if delay:
loop = ioloop.IOLoop.current()
Expand All @@ -87,13 +88,16 @@ def poll_tasks(cls):
@classmethod
def reload_waiters(cls, path=None):
logger.info(
'Reload %s waiters: %s',
'Reload %s waiters: %s files modified',
len(cls.waiters),
cls.watcher.filepath,
len(cls.watcher.filepaths),
)

if path is None:
path = cls.watcher.filepath or '*'
if len(cls.watcher.filepaths) == 1:
path = cls.watcher.filepaths[0]
else:
path = '*'

msg = {
'command': 'reload',
Expand Down
2 changes: 1 addition & 1 deletion livereload/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def serve(self, liveport=None, host=None, restart_delay=2):
self.application(host, liveport=liveport)

try:
self.watcher._changes.append(('__livereload__', restart_delay))
self.watcher._changes.append((['__livereload__'], restart_delay))
LiveReloadHandler.start_tasks()
IOLoop.instance().start()
except KeyboardInterrupt:
Expand Down
19 changes: 10 additions & 9 deletions livereload/watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ def __init__(self):
# setting changes
self._changes = []

# filepath that is changed
self.filepath = None
# filepaths that are changed
self.filepaths = []
self._start = time.time()

# ignored file extensions
Expand Down Expand Up @@ -69,8 +69,8 @@ def examine(self):
if self._changes:
return self._changes.pop()

# clean filepath
self.filepath = None
# clean filepaths
self.filepaths = []
delays = set([0])
for path in self._tasks:
item = self._tasks[path]
Expand All @@ -85,7 +85,7 @@ def examine(self):
delay = 'forever'
else:
delay = max(delays)
return self.filepath, delay
return self.filepaths, delay

def is_changed(self, path, ignore=None):
if isinstance(path, (list, tuple)):
Expand All @@ -110,18 +110,19 @@ def is_file_changed(self, path, ignore=None):

if path not in self._mtimes:
self._mtimes[path] = mtime
self.filepath = path
self.filepaths.append(path)
return mtime > self._start

if self._mtimes[path] != mtime:
self._mtimes[path] = mtime
self.filepath = path
self.filepaths.append(path)
return True

self._mtimes[path] = mtime
return False

def is_folder_changed(self, path, ignore=None):
change = False
for root, dirs, files in os.walk(path, followlinks=True):
if '.git' in dirs:
dirs.remove('.git')
Expand All @@ -134,8 +135,8 @@ def is_folder_changed(self, path, ignore=None):

for f in files:
if self.is_file_changed(os.path.join(root, f), ignore):
return True
return False
change = True
return change

def is_glob_changed(self, path, ignore=None):
try:
Expand Down

0 comments on commit 568dadb

Please sign in to comment.