Skip to content

Commit

Permalink
🐛 Fix FiberDriver
Browse files Browse the repository at this point in the history
  • Loading branch information
matyo91 committed Sep 17, 2024
1 parent 3eef034 commit 2df0a34
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
10 changes: 5 additions & 5 deletions examples/yflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ function factorialYMemo(int $n): int
};

$factorialYJobDeferBefore = static function (YFlowData $data) {
printf("...*.. #%d - Job 4 : Calculating factorialYJobDefer(%d)\n", $data->id, $data->number);
printf("...*. #%d - Job 4 : Calculating factorialYJobDefer(%d)\n", $data->id, $data->number);

return new YFlowData($data->id, $data->number, $data->number);
};
Expand All @@ -131,14 +131,14 @@ function factorialYMemo(int $n): int
return $defer(static function ($complete, $async) use ($data, $defer, $factorial, $driver) {
if ($data->result <= 1) {
$delay = random_int(1, 3);
printf("...*.. #%d - Job 4 : Step factorialYJobDefer(%d) with delay %d\n", $data->id, $data->number, $delay);
printf("...*. #%d - Job 4 : Step factorialYJobDefer(%d) with delay %d\n", $data->id, $data->number, $delay);
$driver->delay($delay);
$complete([new YFlowData($data->id, $data->number, 1), $defer]);
} else {
$async($factorial([new YFlowData($data->id, $data->number, $data->result - 1), $defer]), static function ($result) use ($data, $complete, $driver) {
[$resultData, $defer] = $result;
$delay = random_int(1, 3);
printf("...*.. #%d - Job 4 : Step async factorialYJobDefer(%d) with delay %d\n", $data->id, $data->number, $delay);
printf("...*. #%d - Job 4 : Step async factorialYJobDefer(%d) with delay %d\n", $data->id, $data->number, $delay);
$driver->delay($delay);
$complete([new YFlowData($data->id, $data->number, $data->result * $resultData->result), $defer]);
});
Expand All @@ -151,7 +151,7 @@ function factorialYMemo(int $n): int
[$data, $defer] = $args;

return $defer(static function ($complete) use ($data, $defer) {
printf("...*.. #%d - Job 4 : Result for factorialYJobDefer(%d) = %d\n", $data->id, $data->number, $data->result);
printf("...*. #%d - Job 4 : Result for factorialYJobDefer(%d) = %d\n", $data->id, $data->number, $data->result);

$complete([new YFlowData($data->id, $data->number), $defer]);
});
Expand Down Expand Up @@ -226,7 +226,7 @@ function factorialYMemo(int $n): int
yield [$fibonacciYJobDeferAfter, null, null, null, new DeferAsyncHandler()];
}, ['driver' => $driver]);

for ($i = 5; $i <= 5; $i++) {
for ($i = 1; $i <= 5; $i++) {
$ip = new Ip(new YFlowData($i, $i));
$flow($ip);
}
Expand Down
16 changes: 13 additions & 3 deletions src/Driver/FiberDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,16 @@ public function await(array &$stream): void
};
};

$defer = static function ($isTick) {
return static function (Closure|JobInterface $job) use ($isTick) {
return static function (Closure $next) use ($isTick, $job) {
$defer = static function ($isTick) use (&$fiberDatas) {
return static function (Closure|JobInterface $job) use ($isTick, &$fiberDatas) {
return static function (Closure $next) use ($isTick, $job, &$fiberDatas) {
$fiber = new Fiber(static function () use ($isTick, $job, $next) {
try {
$job(static function ($return) use ($isTick, $next) {
if ($isTick === false) {
$next($return);
}
Fiber::suspend();
}, static function ($fn, $next) {
$fn($next);
});
Expand All @@ -117,6 +118,15 @@ public function await(array &$stream): void
});

$fiber->start();

$fiberDatas[] = [
'fiber' => $fiber,
'next' => static function ($return) {}, /*function ($return) use ($isTick, $next) {
if ($isTick === false) {
$next($return);
}
},*/
];
};
};
};
Expand Down

0 comments on commit 2df0a34

Please sign in to comment.