Created
January 5, 2017 16:54
-
-
Save acrylic-origami/ccb1c0c4220a9e789f788c9f0521fd45 to your computer and use it in GitHub Desktop.
Called using HH\Asio\join(new Forever()->loop()); in the top level
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?hh // strict | |
class Forever { | |
private ?Awaitable<?(mixed, int)> $hook = null; | |
public function __construct() { | |
} | |
public async function loop(): Awaitable<void> { | |
$generator = $this->sub(); | |
await ($this->hook = $generator->next()); | |
} | |
private async function sub(): \AsyncIterator<int> { | |
await \HH\Asio\usleep(1); | |
invariant(!is_null($this->hook), ''); | |
await $this->hook; | |
yield 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment