Skip to content

Commit 73b896a

Browse files
authored
Merge pull request #4 from Thavarshan/feature/concurrency
Feature/concurrency
2 parents 4230ce8 + e11bddf commit 73b896a

File tree

3 files changed

+43
-50
lines changed

3 files changed

+43
-50
lines changed

src/Matrix/AsyncPromise.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,12 @@ public function then(callable $onFulfilled): self
4242
/**
4343
* Adds a rejection handler to the promise and returns $this for chaining.
4444
*
45-
* The catch() method is analogous to otherwise() in ReactPHP promises.
46-
*
4745
* @param callable $onRejected The callback to execute if the promise rejects.
4846
* @return $this
4947
*/
5048
public function catch(callable $onRejected): self
5149
{
52-
// ReactPHP doesn't have a catch() method, but otherwise() attaches an error handler.
53-
$this->promise = $this->promise->otherwise($onRejected);
50+
$this->promise = $this->promise->catch($onRejected);
5451

5552
return $this;
5653
}

tests/AsyncForkManagerTest.php

-44
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,9 @@
33
declare(strict_types=1);
44

55
use Matrix\AsyncProcessManager;
6-
use React\EventLoop\Loop;
7-
use React\Promise\PromiseInterface;
86

97
uses()->group('manager', 'async');
108

11-
/**
12-
* Runs the event loop until the given promise settles or a timeout is reached.
13-
*
14-
* @param PromiseInterface<mixed, \Throwable> $promise The promise to await.
15-
* @param float $timeout Maximum time in seconds to wait.
16-
* @return mixed The resolved value of the promise.
17-
*
18-
* @throws \Throwable If the promise rejects.
19-
*/
20-
function awaitPromise(PromiseInterface $promise, float $timeout = 2.0)
21-
{
22-
$resolved = false;
23-
$rejected = false;
24-
$result = null;
25-
$error = null;
26-
27-
$promise->then(
28-
function ($val) use (&$resolved, &$result) {
29-
$resolved = true;
30-
$result = $val;
31-
Loop::stop();
32-
},
33-
function ($err) use (&$rejected, &$error) {
34-
$rejected = true;
35-
$error = $err;
36-
Loop::stop();
37-
}
38-
);
39-
40-
Loop::addTimer($timeout, function () {
41-
Loop::stop();
42-
});
43-
44-
Loop::run();
45-
46-
if ($rejected) {
47-
throw $error;
48-
}
49-
50-
return $result;
51-
}
52-
539
it('resolves with the child result when the callable succeeds', function () {
5410
$manager = new AsyncProcessManager;
5511
$promise = $manager->fork(function () {

tests/Pest.php

+42-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
declare(strict_types=1);
44

5+
use React\EventLoop\Loop;
6+
use React\Promise\PromiseInterface;
7+
58
/*
69
|--------------------------------------------------------------------------
710
| Test Case
@@ -41,7 +44,44 @@
4144
|
4245
*/
4346

44-
function something()
47+
/**
48+
* Runs the event loop until the given promise settles or a timeout is reached.
49+
*
50+
* @param PromiseInterface<mixed, \Throwable> $promise The promise to await.
51+
* @param float $timeout Maximum time in seconds to wait.
52+
* @return mixed The resolved value of the promise.
53+
*
54+
* @throws \Throwable If the promise rejects.
55+
*/
56+
function awaitPromise(PromiseInterface $promise, float $timeout = 2.0)
4557
{
46-
// ..
58+
$resolved = false;
59+
$rejected = false;
60+
$result = null;
61+
$error = null;
62+
63+
$promise->then(
64+
function ($val) use (&$resolved, &$result) {
65+
$resolved = true;
66+
$result = $val;
67+
Loop::stop();
68+
},
69+
function ($err) use (&$rejected, &$error) {
70+
$rejected = true;
71+
$error = $err;
72+
Loop::stop();
73+
}
74+
);
75+
76+
Loop::addTimer($timeout, function () {
77+
Loop::stop();
78+
});
79+
80+
Loop::run();
81+
82+
if ($rejected) {
83+
throw $error;
84+
}
85+
86+
return $result;
4787
}

0 commit comments

Comments
 (0)