diff --git a/common/nfile_watcher.hh b/common/nfile_watcher.hh index 3671d90..11ed453 100644 --- a/common/nfile_watcher.hh +++ b/common/nfile_watcher.hh @@ -3,6 +3,7 @@ #include #include #include +#include #include "common/file_base.hh" @@ -18,29 +19,39 @@ class NFileWatcher final : public nf7::FileBase::Feature { NFileWatcher& operator=(NFileWatcher&&) = delete; void Watch(const std::filesystem::path& npath) noexcept { - npath_ = npath; + npaths_.push_back(npath); + lastmod_ = std::nullopt; + } + void Clear() noexcept { + npaths_.clear(); lastmod_ = std::nullopt; } std::function onMod; protected: - void Update() noexcept override - try { - if (npath_) { - const auto lastmod = std::filesystem::last_write_time(*npath_); - if (lastmod_ && lastmod > *lastmod_) { - onMod(); + void Update() noexcept override { + auto latest = std::filesystem::file_time_type::duration::min(); + for (const auto& npath : npaths_) { + try { + const auto lastmod = std::filesystem::last_write_time(npath).time_since_epoch(); + latest = std::max(latest, lastmod); + } catch (std::filesystem::filesystem_error&) { } - lastmod_ = lastmod; } - } catch (std::filesystem::filesystem_error&) { - lastmod_.emplace(); + if (!lastmod_) { + lastmod_ = latest; + } + if (*lastmod_ < latest) { + onMod(); + lastmod_ = latest; + } } private: - std::optional npath_; - std::optional lastmod_; + std::vector npaths_; + + std::optional lastmod_; }; } // namespace nf7 diff --git a/file/gl_obj.cc b/file/gl_obj.cc index 56e9dd8..bcd392b 100644 --- a/file/gl_obj.cc +++ b/file/gl_obj.cc @@ -137,8 +137,7 @@ class ObjBase : public nf7::FileBase, watch_->AddHandler(nf7::File::Event::kUpdate, [self = life_.ref()](auto&) { if (self) self->Drop(); }); - - // TODO: clear nwatch + nwatch_->Clear(); fu_ = mem_->Create(CreateParam { .file = this,