Skip to content

Feature Request: Provide a way to find out if a calbackId is valid #91

@Kingdutch

Description

@Kingdutch

Problem Description

I want to run code like the following:

// Perform eager tasks as often as possible but never more than one at a time.
$callbackId = EventLoop::repeat(0, function ($callbackId) {
  EventLoop::disable($callbackId);
  $this->performHalfSecondTask();
  EventLoop::enable($callbackId);
});

// Simulate some long task that mostly suspends.
EventLoop::delay(1.25, function() use ($callbackId) {
  echo "Cancelling repeat\n";
  EventLoop::cancel($callbackId);
});

EventLoop::run();

The problem with this is that there's no efficient way to check if $callbackId is still valid when running EventLoop::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:

  1. Check all the identifiers, this is potentially expensive if the list of identifiers is large due to a high number of async tasks.
if (in_array($callbackId, EventLoop::getIdentifiers, TRUE)) {}
  1. Catch the exception. This feels quite verbose and it's unclear whether this is intentional or whether a developer unintentionally hides a bug here.
try {
    EventLoop::enable($callbackId);
}
catch (EventLoop\InvalidCallbackError $e) {}

Feature Request

It would be great if there is a method saying EventLoop::isValidIdentifier which does an isset behind the scenes.

if (EventLoop::isValidIdentifier($callbackId)) {
   EventLoop::enable($callbackId);
}

Alternatively a second parameter to enable/disable to indicate it may fail silently would work too:

EventLoop::enable($callbackId, TRUE);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions