Skip to content

Commit 4d8331f

Browse files
committed
Refactor SimpleFiber to simplify async code flow
1 parent c5d53ee commit 4d8331f

File tree

1 file changed

+10
-22
lines changed

1 file changed

+10
-22
lines changed

src/SimpleFiber.php

+10-22
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,13 @@ public function __construct()
2020

2121
public function resume(mixed $value): void
2222
{
23-
if ($this->fiber === null) {
24-
$suspend = static fn() => $value;
25-
if (\Fiber::getCurrent() !== self::$scheduler) {
26-
self::$suspend = $suspend;
27-
} else {
28-
\Fiber::suspend($suspend);
29-
}
30-
return;
23+
if ($this->fiber !== null) {
24+
$this->fiber->resume($value);
25+
} else {
26+
self::$suspend = static fn() => $value;
3127
}
3228

33-
$this->fiber->resume($value);
34-
35-
if (self::$suspend) {
29+
if (self::$suspend !== null && \Fiber::getCurrent() === self::$scheduler) {
3630
$suspend = self::$suspend;
3731
self::$suspend = null;
3832

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

4337
public function throw(\Throwable $throwable): void
4438
{
45-
if ($this->fiber === null) {
46-
$suspend = static fn() => throw $throwable;
47-
if (\Fiber::getCurrent() !== self::$scheduler) {
48-
self::$suspend = $suspend;
49-
} else {
50-
\Fiber::suspend($suspend);
51-
}
52-
return;
39+
if ($this->fiber !== null) {
40+
$this->fiber->throw($throwable);
41+
} else {
42+
self::$suspend = static fn() => throw $throwable;
5343
}
5444

55-
$this->fiber->throw($throwable);
56-
57-
if (self::$suspend) {
45+
if (self::$suspend !== null && \Fiber::getCurrent() === self::$scheduler) {
5846
$suspend = self::$suspend;
5947
self::$suspend = null;
6048

0 commit comments

Comments
 (0)