Skip to content
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

Added Await::trap() #106

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Added Await::trap() #106

wants to merge 2 commits into from

Conversation

SOF3
Copy link
Owner

@SOF3 SOF3 commented Jan 23, 2022

No description provided.

@codecov
Copy link

codecov bot commented Jan 23, 2022

Codecov Report

Patch coverage: 3.84% and project coverage change: -6.33 ⚠️

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%) ⬇️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@ColinHDev
Copy link

ColinHDev commented Feb 2, 2022

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)

@SOF3
Copy link
Owner Author

SOF3 commented Feb 6, 2022

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

@Endermanbugzjfc
Copy link
Collaborator

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

@SOF3
Copy link
Owner Author

SOF3 commented Mar 9, 2022

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.

@Endermanbugzjfc
Copy link
Collaborator

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

@SOF3
Copy link
Owner Author

SOF3 commented Mar 10, 2022

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.

@Endermanbugzjfc
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants