Skip to content

Instantly share code, notes, and snippets.

@Danack
Last active December 2, 2020 13:19
Show Gist options
  • Save Danack/f89afc0e84763e86666a2e8a7a0fe34d to your computer and use it in GitHub Desktop.
Save Danack/f89afc0e84763e86666a2e8a7a0fe34d to your computer and use it in GitHub Desktop.
WE FEAR CHANGE
<?php
declare(strict_types = 1);
class Scheduler implements FiberScheduler
{
private string $nextId = 'a';
private array $callbacks = [];
public function run(): void
{
while ($this->isQueueEmpty() !== true) {
foreach ($this->callbacks as $id => $callback) {
unset($this->callbacks[$id]);
$callback();
}
}
}
private function isQueueEmpty(): bool
{
return count($this->callbacks) === 0;
}
public function queue(callable $callback): void
{
$this->callbacks[$this->nextId++] = $callback;
}
}
$scheduler = new Scheduler;
// Okay, so this is receiving a fiber...so it's going to be called from within
// the fiber, right?
$give_this_a_name = function (Fiber $fiber) use ($scheduler) {
// why does this need to be wrapped?
$give_this_a_name_also = function () use ($fiber) {
// Why does this need to resume a fiber?
return $fiber->resume("Test");
};
$scheduler->queue($give_this_a_name_also);
};
// "Pause main fiber, which will be resumed by the scheduler."
// I don't understand this comment....or the name of the method.
// shouldn't it be something more 'active' like 'dispatch' or 'switchTo'?
$value = \Fiber::suspend($give_this_a_name, $scheduler);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment