3#include "common/config.h"
9#include <Poco/Delegate.h>
10#include <Poco/DirectoryWatcher.h>
23constexpr auto kDebounceDelay = std::chrono::milliseconds(75);
30struct FileWatch::DirWatch {};
33#ifndef EVENGINE_WEBGPU
36std::string basenameOf(
const std::string &
path) {
38 return p.getFileName();
41std::string normalizeDir(
const std::string &
dir) {
56 while (
s.size() > 1 && (
s.back() ==
'/' ||
s.back() ==
'\\'))
s.pop_back();
64 std::unique_ptr<Poco::DirectoryWatcher>
watcher;
65 std::unordered_map<std::string, std::string>
filters;
74bool FileWatch::add(
const std::string &realDir,
const std::string &filterName,
75 const std::string &reportPath,
int scanInterval) {
84 if (realDir.empty() || reportPath.empty())
return false;
85 const std::string
dir = normalizeDir(realDir);
88 if (!
f.exists() || !
f.isDirectory())
return false;
100 std::unique_ptr<DirWatch> doomed;
103 std::lock_guard<std::mutex> lock(mu_);
105 auto prev = reportToDir_.find(reportPath);
106 if (prev != reportToDir_.end()) {
107 auto it = byDir_.find(prev->second);
108 if (it != byDir_.end()) {
111 if (fit->second == reportPath) {
119 doomed = std::move(it->second);
123 reportToDir_.erase(prev);
128 auto it = byDir_.find(
dir);
129 if (it == byDir_.end()) {
130 auto owned = std::make_unique<DirWatch>();
133 owned->watcher = std::make_unique<Poco::DirectoryWatcher>(
134 dir, Poco::DirectoryWatcher::DW_FILTER_ENABLE_ALL, scanInterval);
138 if (
owned->watcher) {
144 dw = it->second.get();
148 dw->
watcher->itemAdded += Poco::delegate(
this, &FileWatch::onAdded);
149 dw->
watcher->itemRemoved += Poco::delegate(
this, &FileWatch::onRemoved);
150 dw->
watcher->itemModified += Poco::delegate(
this, &FileWatch::onModified);
151 dw->
watcher->itemMovedFrom += Poco::delegate(
this, &FileWatch::onMovedFrom);
152 dw->
watcher->itemMovedTo += Poco::delegate(
this, &FileWatch::onMovedTo);
155 dw->
filters[filterName] = reportPath;
157 reportToDir_[reportPath] =
dir;
161 if (doomed) unsubscribeDelegates(doomed.get());
167#ifdef EVENGINE_WEBGPU
174 std::unique_ptr<DirWatch> doomed;
176 std::lock_guard<std::mutex> lock(mu_);
177 auto prev = reportToDir_.find(reportPath);
178 if (prev == reportToDir_.end())
return false;
179 const std::string
dir = prev->second;
180 reportToDir_.erase(prev);
182 auto it = byDir_.find(
dir);
183 if (it == byDir_.end())
return false;
186 if (fit->second == reportPath) {
194 doomed = std::move(it->second);
198 if (doomed) unsubscribeDelegates(doomed.get());
204#ifdef EVENGINE_WEBGPU
205 std::lock_guard<std::mutex> lock(mu_);
213 std::vector<std::unique_ptr<DirWatch>> doomed;
215 std::lock_guard<std::mutex> lock(mu_);
216 for (
auto &kv : byDir_) doomed.push_back(std::move(kv.second));
218 reportToDir_.clear();
221 for (
const auto &dw : doomed) unsubscribeDelegates(dw.get());
226 std::lock_guard<std::mutex> lock(mu_);
227#ifdef EVENGINE_WEBGPU
230 return int(reportToDir_.size());
235 std::lock_guard<std::mutex> lock(mu_);
236 const auto now = std::chrono::steady_clock::now();
237 auto it = std::find_if(queue_.begin(), queue_.end(),
238 [now](
const PendingEvent &event) { return event.readyAt <= now; });
239 if (it == queue_.end())
return false;
240 out = std::move(it->event);
245void FileWatch::enqueue(Event event) {
246 const auto readyAt = std::chrono::steady_clock::now() + kDebounceDelay;
247 auto pending = std::find_if(queue_.begin(), queue_.end(), [&](
const PendingEvent &
queued) {
248 return queued.event.realPath == event.realPath;
251 pending->event = std::move(event);
255 queue_.push_back({std::move(event), readyAt});
258#ifndef EVENGINE_WEBGPU
259void FileWatch::unsubscribeDelegates(DirWatch *dw) {
260 if (!dw || !dw->watcher)
return;
261 dw->watcher->itemAdded -= Poco::delegate(
this, &FileWatch::onAdded);
262 dw->watcher->itemRemoved -= Poco::delegate(
this, &FileWatch::onRemoved);
263 dw->watcher->itemModified -= Poco::delegate(
this, &FileWatch::onModified);
264 dw->watcher->itemMovedFrom -= Poco::delegate(
this, &FileWatch::onMovedFrom);
265 dw->watcher->itemMovedTo -= Poco::delegate(
this, &FileWatch::onMovedTo);
268void FileWatch::handlePocoEvent(
const std::string &
kind,
const std::string &itemPath) {
269 const std::string
name = basenameOf(itemPath);
270 Poco::Path
parent(itemPath);
272 const std::string
dir = normalizeDir(
parent.toString());
274 std::lock_guard<std::mutex> lock(mu_);
275 auto it = byDir_.find(
dir);
276 if (it == byDir_.end()) {
277 for (
auto &kv : byDir_) {
278 if (itemPath.size() >= kv.first.size() && itemPath.compare(0, kv.first.size(), kv.first) == 0) {
279 it = byDir_.find(kv.first);
284 if (it == byDir_.end())
return;
285 DirWatch *dw = it->second.get();
287 auto push = [&](
const std::string &report) {
291 ev.realPath = itemPath;
292 enqueue(std::move(ev));
295 auto all = dw->filters.find(
"");
296 if (
all != dw->filters.end()) {
297 std::string report =
all->second;
298 if (!report.empty() && report.back() !=
'/') report +=
"/";
303 auto fit = dw->filters.find(
name);
304 if (fit != dw->filters.end()) push(fit->second);
307void FileWatch::onAdded(
const void *,
const Poco::DirectoryWatcher::DirectoryEvent &event) {
308 handlePocoEvent(
"added", event.item.path());
311void FileWatch::onRemoved(
const void *,
const Poco::DirectoryWatcher::DirectoryEvent &event) {
312 handlePocoEvent(
"removed", event.item.path());
315void FileWatch::onModified(
const void *,
const Poco::DirectoryWatcher::DirectoryEvent &event) {
316 handlePocoEvent(
"modified", event.item.path());
319void FileWatch::onMovedFrom(
const void *,
const Poco::DirectoryWatcher::DirectoryEvent &event) {
320 handlePocoEvent(
"movedFrom", event.item.path());
323void FileWatch::onMovedTo(
const void *,
const Poco::DirectoryWatcher::DirectoryEvent &event) {
324 handlePocoEvent(
"movedTo", event.item.path());
std::vector< QuestEvent > pending
const ColourScope * parent
Share string attribute map operations through the Windows Foundation DLL.
bool add(const std::string &realDir, const std::string &filterName, const std::string &reportPath, int scanInterval=1)
bool poll(Event &out)
Pop one event; false if empty.
bool remove(const std::string &reportPath)
Remove by reportPath previously passed to add().
std::unique_ptr< Poco::DirectoryWatcher > watcher
std::unordered_map< std::string, std::string > filters