Skip to content

Commit

Permalink
Merge pull request #10 from genkgo/return_from_closure
Browse files Browse the repository at this point in the history
return false to stop watching
  • Loading branch information
frederikbosch committed Jul 18, 2024
2 parents 61295c2 + 2b7e4d0 commit d9c5152
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ $watcher->watch(
function (FsNotify\Event $event) {
var_dump($event->getKind()); // kind of file/folder event
var_dump($event->getPaths()); // paths
return true; // return false if you do not want to continue
}
);
```
Expand All @@ -50,7 +51,7 @@ class RecommendedWatcher
public function remove(string $path): void;

/**
* @param callable(Event): void $handle
* @param callable(Event): bool $handle
* @throws WatchException
*/
public function watch(callable $handle): void;
Expand Down
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,10 @@ pub fn get_module() -> Module {
let kind = php_event.get_mut_property("kind");
*kind = php_kind.into();

handler.call([ZVal::from(php_event)])?;
let call_result = handler.call([ZVal::from(php_event)])?;
if call_result.expect_bool()? == false {
break;
}
},
Err(error) => return Err(NotifyError::new(error).into()),
}
Expand Down
2 changes: 1 addition & 1 deletion tests/php/recommended_watcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
$watcher->watch(function (FsNotify\Event $event) {
var_dump($event->getKind());
var_dump($event->getPaths());
exit;
return false;
});
2 changes: 1 addition & 1 deletion tests/php/test_exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
try {
$watcher = new FsNotify\RecommendedWatcher();
$watcher->add(__DIR__ . '/unknown');
$watcher->watch(fn () => null);
$watcher->watch(fn () => false);
} catch (FsNotify\WatchException) {
echo "caught exception";
}

0 comments on commit d9c5152

Please sign in to comment.