await-generator icon indicating copy to clipboard operation
await-generator copied to clipboard

Added Await::trap()

Open SOF3 opened this issue 4 years ago • 8 comments

SOF3 avatar Jan 23 '22 09:01 SOF3

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.

codecov[bot] avatar Jan 23 '22 09:01 codecov[bot]

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)

ColinHDev avatar Feb 02 '22 21:02 ColinHDev

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

SOF3 avatar Feb 06 '22 03:02 SOF3

How to use this? Does it run the closures for every yield in a generator?

Endermanbugzjfc avatar Mar 09 '22 12:03 Endermanbugzjfc

How to use this? Does it run the closures for every yield in a generator?

Yep, and you can intercept the signals to see what it is suspending for.

SOF3 avatar Mar 09 '22 13:03 SOF3

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

Endermanbugzjfc avatar Mar 09 '22 18:03 Endermanbugzjfc

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.

SOF3 avatar Mar 10 '22 15:03 SOF3

Oh I didn't realise there is a test file.

Endermanbugzjfc avatar Mar 15 '22 00:03 Endermanbugzjfc