Added Await::trap()
Codecov Report
Patch coverage: 3.84% and project coverage change: -6.33 :warning:
Comparison is base (
fcc0266) 99.45% compared to head (301906e) 93.12%.
Additional details and impacted files
@@ Coverage Diff @@
## master #106 +/- ##
============================================
- Coverage 99.45% 93.12% -6.33%
- Complexity 129 144 +15
============================================
Files 10 10
Lines 367 393 +26
============================================
+ Hits 365 366 +1
- Misses 2 27 +25
| Impacted Files | Coverage Δ | |
|---|---|---|
| await-generator/src/SOFe/AwaitGenerator/Await.php | 89.49% <3.84%> (-10.51%) |
:arrow_down: |
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Do you have feedback about the report comment? Let us know in this issue.
I don't think it helps, but I came up with the following for the basic annotations
/**
* Wraps a generator that executes some action before and after suspension points (`yield`s).
* @phpstan-template TKey
* @phpstan-template TValue
* @phpstan-template TSend
* @phpstan-template TReturn
* @phpstan-param Generator<TKey, TValue, TSend, TReturn> $generator
* @phpstan-param Closure(Generator, TValue, TKey): bool $beforeSuspend
* @phpstan-param Closure(Generator, TValue, TKey, TSend): bool $afterSuspend
* @phpstan-return Generator<TKey, TValue, TSend, TReturn>
*/
public static function trap(Generator $generator, Closure $beforeSuspend, Closure $afterSuspend) : Generator {
(https://github.com/ColinHDev/await-generator/blob/trap/await-generator/src/SOFe/AwaitGenerator/Await.php#L194-L201)
I don't think it helps, but I came up with the following for the basic annotations
/** * Wraps a generator that executes some action before and after suspension points (`yield`s). * @phpstan-template TKey * @phpstan-template TValue * @phpstan-template TSend * @phpstan-template TReturn * @phpstan-param Generator<TKey, TValue, TSend, TReturn> $generator * @phpstan-param Closure(Generator, TValue, TKey): bool $beforeSuspend * @phpstan-param Closure(Generator, TValue, TKey, TSend): bool $afterSuspend * @phpstan-return Generator<TKey, TValue, TSend, TReturn> */ public static function trap(Generator $generator, Closure $beforeSuspend, Closure $afterSuspend) : Generator {(https://github.com/ColinHDev/await-generator/blob/trap/await-generator/src/SOFe/AwaitGenerator/Await.php#L194-L201)
Please send a pull request
How to use this? Does it run the closures for every yield in a generator?
How to use this? Does it run the closures for every
yieldin a generator?
Yep, and you can intercept the signals to see what it is suspending for.
No idea how to use sorry.
<?php
declare(strict_types=1);
require("../../vendor/autoload.php");
function gen() : Generator
{
yield from gen2();
}
function gen2() : Generator
{
yield from [];
}
\SOFe\AwaitGenerator\Await::f2c(function () : Generator {
yield from \SOFe\AwaitGenerator\Await::trap(
gen(),
fn (...$args) => var_dump(
"a1",
$args
),
fn (...$args) => var_dump(
"a2",
$args
)
);
});
/Users/Eurus/Documents/Dev/PocketMine-MP/4/bin/php7/bin/php /Users/Eurus/Documents/Dev/PocketMine-MP/projects/GlassPain/GlassPain/src/asd.php
Process finished with exit code 0
No idea how to use sorry.
<?php declare(strict_types=1); require("../../vendor/autoload.php"); function gen() : Generator { yield from gen2(); } function gen2() : Generator { yield from []; } \SOFe\AwaitGenerator\Await::f2c(function () : Generator { yield from \SOFe\AwaitGenerator\Await::trap( gen(), fn (...$args) => var_dump( "a1", $args ), fn (...$args) => var_dump( "a2", $args ) ); });/Users/Eurus/Documents/Dev/PocketMine-MP/4/bin/php7/bin/php /Users/Eurus/Documents/Dev/PocketMine-MP/projects/GlassPain/GlassPain/src/asd.php Process finished with exit code 0
For example, the Traverser class can be implemented using Await::trap.
I am thinking about providing a better API so that I don't have to expose the internal constants like Await::ONCE. This pull request is just a preview for now.
Oh I didn't realise there is a test file.