I suppose we can sleep and poll the timestamp, but I'm running into a problem, mtime is defined in lib/files.arc,
The way I'm settings things up, my experimental "app" is in a completely isolated folder, and once I'm in there, "lib/files.arc" is no longer in the current directory, how should I go about loading stuff from "lib/"?
For now I only need the 'mtime' function so I just snatched it, but I hope there's a better way of importing standard libs.
Otherwise, here's a little file watcher I just wrote, seems to work with my tiny test case:
(def mtime (path)
" Returns the modification time of the file or directory `path' in
seconds since the epoch. "
(= file-ts* (obj))
(def file-ts-changed (file-name)
(no:is (file-ts* file-name) (mtime file-name)))
(def update-file-ts (file-name)
(= (file-ts* file-name) (mtime file-name)))
; watch file system for updates to file-name
; and run 'delegate' when file is updated
(def watch (file-name delegate)
(while (no (file-ts-changed file-name))
(def watch-load (file-name)
(repeat 2 (prn))
(pr "watching " file-name " for changes ..")
(fn() (load file-name) (pr "reloaded " file-name))))
1. Shouldn't the last line be (watch file-name delegate) so that it continues to watch after the first update? Or should that be optional?
2. It might be a good idea to pass file-name to the delegate. Not strictly necessary, and doesn't give any benefit in this example, but it could mean that you sometimes don't need to use a fn as the delegate. If you didn't want to pr "reloaded" for instance, you could just do (watch file-name load).
3. Is the hash table really necessary? Here is a version without it:
Ah, so loading the file makes it watch itself. You need to do the load in a thread then?
I still fail to see how the hash table helps since my version above should do exactly the same as yours. I would see the point if you made a thread check all files in the table, and made the first call to watch launch the thread, while subsequent calls just add entries to the table.