Description
We have quite a few PRs and the discussion is spread accross those PRs instead of being in one issue. I know, adding yet another discussion thread seems kind of pointless then, but I think it's important to gather some clear statements about expectations regarding scoping.
PRs
- Safe execute #139
- One Loop to Rule Them All #142
- Reuse current loop with execute #144
- Resolve #142 by adding a run() method #145
- Use a simple get/set accessor instead #149
My own statement
I think a clear scope avoids bugs with different drivers. We allow passing a driver to be run to Loop::execute
and should therefore only rely on the driver being accessible with the Loop
accessor inside that Loop::execute
scope.
It has been mentioned that this would prevent the adapter for React having a correct run
method and additionally would prevent first setting things up and then running the loop.
The React adapter currently uses Loop::execute(function () {}, Loop::get());
to run the default / current loop. This continues to work with a strict Loop::execute
scope (#139) as long as the whole application is wrapped in Loop::execute
. It uses a stacked driver then, but as both are the same, everything will work fine.
Another solution for the adapter is to use an explicit driver first and then simply pass it to Loop::execute
as React uses an explicit loop that's passed around anyway.
<?php
$loop = new LoopAdapter($interopLoop);
$connector = new DnsConnector($loop);
// ...
$loop->run(); // Executes Loop::execute
Registering events on a specific loop instance without the accessor will always register those events on the right driver (obviously) and is therefore totally fine outside of Loop::execute
.
But after all, this is something which doesn't affect any library code. It only affects tests and application code, which both have to be changed anyway to make use of the accessor and interop loop.
I really want to avoid having resetDriver
and a default driver at all if we can have a clear scoping and thus avoid bugs with different drivers and forgot-to-reset drivers in tests. It's going to be less confusing for newcomers if the following code just errors instead of just not working, because it's using two different loops.
<?php
Loop::defer(function () { /* never executed */ });
// Uses a new driver and thus "ignores" the previous `Loop::defer()`
Loop::execute(function () { print "hello"; });
I'm very interested in opinions especially from @WyriHaximus @jsor @cboden @davidwdan @mbonneau
Activity