@@ -280,66 +280,80 @@ Promise<void> File::close() {
280
280
co_await awaiter;
281
281
}
282
282
283
+ FsWatch::FsWatch () : uv_handle_{std::make_unique<uv_fs_event_t >()} {}
284
+
283
285
FsWatch::~FsWatch () {
284
- BOOST_ASSERT_MSG (dataIsNull (&uv_handle_),
286
+ if (!uv_handle_) {
287
+ return ;
288
+ }
289
+ BOOST_ASSERT_MSG (dataIsNull (uv_handle_.get ()),
285
290
" don't drop an FsWatch while a watch() generator is "
286
291
" running (use stopWatch()!)" );
287
- uv_fs_event_stop (&uv_handle_);
292
+ uv_fs_event_stop (uv_handle_.get ());
293
+ }
294
+
295
+ Promise<FsWatch> FsWatch::create (const Loop &loop, std::string_view path) {
296
+ return createWithFlag (loop, path, {});
288
297
}
289
298
290
- FsWatch FsWatch::create (const Loop &loop, std::string_view path) {
291
- return FsWatch{loop, path, {}};
299
+ Promise<FsWatch> FsWatch::createRecursive (const Loop &loop,
300
+ std::string_view path) {
301
+ return createWithFlag (loop, path, UV_FS_EVENT_RECURSIVE);
292
302
}
293
303
294
- FsWatch FsWatch::createRecursive (const Loop &loop, std::string_view path) {
295
- return FsWatch{loop, path, UV_FS_EVENT_RECURSIVE};
304
+ Promise<FsWatch> FsWatch::createWithFlag (const Loop &loop,
305
+ std::string_view path,
306
+ uv_fs_event_flags flags) {
307
+ FsWatch fsWatch;
308
+ uv_fs_event_t &uv_handle = *fsWatch.uv_handle_ ;
309
+ const uv_status initStatus = uv_fs_event_init (loop.uvloop (), &uv_handle);
310
+ if (initStatus != 0 ) {
311
+ throw UvcoException{
312
+ initStatus,
313
+ " uv_fs_event_init returned error while initializing FsWatch" };
314
+ }
315
+ const auto startStatus =
316
+ callWithNullTerminated<uv_status>(path, [&](std::string_view safePath) {
317
+ return uv_fs_event_start (&uv_handle, onFsWatcherEvent, safePath.data (),
318
+ flags);
319
+ });
320
+ if (startStatus != 0 ) {
321
+ uv_fs_event_stop (&uv_handle);
322
+ // This works with the current Loop::~Loop implementation.
323
+ co_await closeHandle (&uv_handle);
324
+ fsWatch.uv_handle_ .reset ();
325
+ throw UvcoException{
326
+ startStatus, " uv_fs_event_start returned error while starting FsWatch" };
327
+ }
328
+ co_return fsWatch;
296
329
}
297
330
298
331
MultiPromise<FsWatch::FileEvent> FsWatch::watch () {
299
- if (!dataIsNull (& uv_handle_)) {
332
+ if (!dataIsNull (uv_handle_. get () )) {
300
333
throw UvcoException (UV_EBUSY, " FsWatch::watch() is already active!" );
301
334
}
302
335
return watch_ ();
303
336
}
304
337
305
338
MultiPromise<FsWatch::FileEvent> FsWatch::watch_ () {
306
339
FsWatchAwaiter_ awaiter;
307
- setData (& uv_handle_, &awaiter);
308
- ZeroAtExit<void > zeroAtExit{&uv_handle_. data };
340
+ setData (uv_handle_. get () , &awaiter);
341
+ ZeroAtExit<void > zeroAtExit{&uv_handle_-> data };
309
342
310
343
while (co_await awaiter) {
311
344
co_yield awaiter.events_ .get ();
312
345
}
313
346
}
314
347
315
348
Promise<void > FsWatch::stopWatch (MultiPromise<FsWatch::FileEvent> watcher) {
316
- auto *awaiter = getData<FsWatchAwaiter_>(& uv_handle_);
349
+ auto *awaiter = getData<FsWatchAwaiter_>(uv_handle_. get () );
317
350
awaiter->stop ();
318
351
co_await watcher;
319
352
}
320
353
321
- Promise<void > FsWatch::close () { return closeHandle (&uv_handle_); }
322
-
323
- FsWatch::FsWatch (const Loop &loop, std::string_view path,
324
- uv_fs_event_flags flags) {
325
- const uv_status initStatus = uv_fs_event_init (loop.uvloop (), &uv_handle_);
326
- if (initStatus != 0 ) {
327
- throw UvcoException{
328
- initStatus,
329
- " uv_fs_event_init returned error while initializing FsWatch" };
330
- }
331
- const auto startStatus =
332
- callWithNullTerminated<uv_status>(path, [&](std::string_view safePath) {
333
- return uv_fs_event_start (&uv_handle_, onFsWatcherEvent, safePath.data (),
334
- flags);
335
- });
336
- if (startStatus != 0 ) {
337
- uv_fs_event_stop (&uv_handle_);
338
- // This works with the current Loop::~Loop implementation.
339
- uv_close ((uv_handle_t *)&uv_handle_, nullptr );
340
- throw UvcoException{
341
- startStatus, " uv_fs_event_start returned error while starting FsWatch" };
342
- }
354
+ Promise<void > FsWatch::close () {
355
+ co_await closeHandle (uv_handle_.get ());
356
+ uv_handle_.reset ();
343
357
}
344
358
345
359
void FsWatch::onFsWatcherEvent (uv_fs_event_t *handle, const char *path,
0 commit comments