Skip to content

Commit

Permalink
Refactor SimpleFiber to simplify async code flow
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed Feb 17, 2022
1 parent 99882cf commit 61dfe31
Showing 1 changed file with 10 additions and 22 deletions.
32 changes: 10 additions & 22 deletions src/SimpleFiber.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,13 @@ public function __construct()

public function resume(mixed $value): void
{
if ($this->fiber === null) {
$suspend = static fn() => $value;
if (\Fiber::getCurrent() !== self::$scheduler) {
self::$suspend = $suspend;
} else {
\Fiber::suspend($suspend);
}
return;
if ($this->fiber !== null) {
$this->fiber->resume($value);
} else {
self::$suspend = static fn() => $value;
}

$this->fiber->resume($value);

if (self::$suspend) {
if (self::$suspend !== null && \Fiber::getCurrent() === self::$scheduler) {
$suspend = self::$suspend;
self::$suspend = null;

Expand All @@ -42,19 +36,13 @@ public function resume(mixed $value): void

public function throw(\Throwable $throwable): void
{
if ($this->fiber === null) {
$suspend = static fn() => throw $throwable;
if (\Fiber::getCurrent() !== self::$scheduler) {
self::$suspend = $suspend;
} else {
\Fiber::suspend($suspend);
}
return;
if ($this->fiber !== null) {
$this->fiber->throw($throwable);
} else {
self::$suspend = static fn() => throw $throwable;
}

$this->fiber->throw($throwable);

if (self::$suspend) {
if (self::$suspend !== null && \Fiber::getCurrent() === self::$scheduler) {
$suspend = self::$suspend;
self::$suspend = null;

Expand Down

0 comments on commit 61dfe31

Please sign in to comment.