thanethomson / httpwatcher

Python-based HTTP server for static files with live reload
MIT License
20 stars 10 forks source link

Add --timeout option to reload after website has fully built #3

Open Evidlo opened 5 years ago

Evidlo commented 5 years ago

I'm using httpwatcher to automatically reload my browser while authoring content with a static website generator. Unfortunately, the website generator does a full rebuild of the website when changes are detected, which can cause httpwatcher to trigger a reload before the generator is finished rebuilding or even trigger httpwatcher multiple times in one rebuild.

This could be solved pretty simply if httpwatcher had a --timeout N option whereby an N millisecond countdown is started when a change is detected before httpwatcher starts to reload the page.

Is this equivalent to watcher_inverval? In any case, it's not yet available as a flag.

blackdaemon commented 3 years ago

This is still a problem. I wanted to use httpwatcher on RaspberryPI and since the files are stored on sdcard and any edit and save of the file with vim takes some time (vim probably first removes the original file and then renames the working copy of the file .swp), the httpwatcher will error with 404.

jobveldhuis commented 3 years ago

Sounds like a good feature to add. Does any one of you want to create a PR, or should I take this upon myself? @blackdaemon @Evidlo

Evidlo commented 3 years ago

@jobveldhuis Go ahead. I actually implemented this to a degree a while ago, but the files are gone now.

jobveldhuis commented 3 years ago

@Evidlo Please check the latest version of the dev branch and see whether or not this is the use case you had in mind. You are now able to use: --interval <number> to increase or decrease the maximum polling of the server.

Evidlo commented 3 years ago

@jobveldhuis The behavior I'm proposing is just subtly different from a simple reload interval. I came up with a contrived example to show how httpwatcher can actually miss file change events if implemented this way:

Current behavior (with --interval 1):

0.00s file changed   <- reload
0.25s file changed
0.50s file changed
0.75s file changed
1.00s file changed   <- reload
1.25s file changed
1.50s file changed

As you can see, the last two file change events are not picked up by httpwatcher. If we instead wait for a period of no activity before reloading, we can catch these events:

Proposed behavior (with --interval 1):

0.00s file changed
0.25s file changed
0.50s file changed
0.75s file changed
1.00s file changed
1.25s file changed
1.50s file changed
1.75s
2.00s
2.25s
2.50s                <- reload

So basically, a file change event should start/reset a timer which then triggers the page reload.

I think this would be implemented with tornado's call_later function with a callback to trigger the page reload.

jobveldhuis commented 3 years ago

So you are proposing a delay of some kind? Sounds good, I will look into this tonight!