-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature Request: Provide a way to find out if a calbackId is valid #91
Comments
What is $this->performHalfSecondTask() doing? repeat(0) is a good indicator that there's something solved the wrong way and better options exist. |
In this case If our main task (handling a web request) is complete then we want to stop pre-warming caches and end the request. |
I've pushed a more complete example in Kingdutch/revolt-playground@528a931 :) |
One suggestion is to use a reference in the closures to set a flag that the callback has been cancelled. $cancelled = false;
$callbackId = EventLoop::repeat(0, function (string $callbackId) use (&$cancelled): void {
EventLoop::disable($callbackId);
$this->performHalfSecondTask();
if (!$cancelled) {
EventLoop::enable($callbackId);
}
});
EventLoop::delay(1.25, function () use (&$cancelled, $callbackId): void {
EventLoop::cancel($callbackId);
$cancelled = true;
}); If you use object properties instead you can completely avoid the by-reference use, but you will be creating a circular reference to the object. This may or may not matter for your application. As @kelunik pointed out, this is a bit of an odd use of event-loop callbacks. Other async abstractions would be useful here, such as Promises/Futures, cancellations, or async iterators (pipelines as we call them in |
Problem Description
I want to run code like the following:
The problem with this is that there's no efficient way to check if
$callbackId
is still valid when runningEventLoop::enable($callbackId);
which means that there's a guaranteed exception somewhere in the program.Workarounds
There's two possible ways around this, neither of which are great:
Feature Request
It would be great if there is a method saying
EventLoop::isValidIdentifier
which does anisset
behind the scenes.Alternatively a second parameter to
enable
/disable
to indicate it may fail silently would work too:The text was updated successfully, but these errors were encountered: