improve nf7::NFileWatcher to watch multiple native paths
This commit is contained in:
parent
c1f7328628
commit
694e9e34bb
@ -3,6 +3,7 @@
|
||||
#include <functional>
|
||||
#include <filesystem>
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
|
||||
#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<void()> 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<std::filesystem::path> npath_;
|
||||
std::optional<std::filesystem::file_time_type> lastmod_;
|
||||
std::vector<std::filesystem::path> npaths_;
|
||||
|
||||
std::optional<std::filesystem::file_time_type::duration> lastmod_;
|
||||
};
|
||||
|
||||
} // namespace nf7
|
||||
|
@ -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,
|
||||
|
Loading…
x
Reference in New Issue
Block a user