Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Live Reloading #727

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open

Live Reloading #727

wants to merge 5 commits into from

Conversation

bjornbytes
Copy link
Owner

@bjornbytes bjornbytes commented Dec 4, 2023

This adds live reloading! You can use the --watch or -w command line flag to watch files in the source folder for changes. The watching uses efficient native APIs (inotify, FSEvent, IOCP).

When a file is changed, the lovr.filechanged event will be called with the file path and the type of change (create, delete, modify, rename). The default implementation of lovr.filechanged will simply call lovr.event.restart to reload the project, but projects can override the callback to filter paths or add more fine-grained reloading logic (like hotswapping assets without doing a full restart).

This is still an early version of the design. Some stuff I'm still thinking about:

  • There are currently lovr.filesystem.watch/unwatch methods, but these will only watch the source if it's a folder. It might be cool to add support for watching custom paths, but the interactions with the virtual filesystem are kinda complicated.
  • It would be really nice if Android was able to watch the save directory.
  • It would be nice if t.filesystem.watch was a conf.lua option (useful on Android where there isn't a CLI).
  • It might be nice to ignore hidden folders by default (.git).
  • There are race conditions! When a file is saved in a text editor, some editors will issue several file events to save the file. For example, when saving main.lua:
CREATE tmp
RENAME main.lua main.lua.tmp
RENAME tmp main.lua
DELETE main.lua.tmp

When lovr restarts after the first event, if it reads main.lua between events 2 and 3 then it won't find any data there. I'm not sure how to handle this yet. Debouncing is possible but it's hard to integrate into the default callback. Maybe just a lovr.timer.sleep(.005)? It's awful, but could work well.

Use -w or --watch to watch source directory for changes and call
lovr.filechanged when a file is changed.

By default lovr.filechanged will restart lovr.

It's powered by lovr.filesystem.watch/lovr.filesystem.unwatch functions,
but these are currently hardcoded to only watch the whole source folder.

Currently there is a bug where it only restarts on the first change,
this is seemingly a problem with dmon.
Stale state prevented library from working when reinitialized.
Another solution might be retain/release.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant