Skip to content

Commit 2de51ab

Browse files
committed
refactor: instate stream-to-promise converter
1 parent 28fe002 commit 2de51ab

File tree

2 files changed

+29
-53
lines changed

2 files changed

+29
-53
lines changed

src/internal/proc.php

Lines changed: 20 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
use React\ChildProcess\Process;
1616
use React\EventLoop\LoopInterface;
17-
use React\Promise\Deferred;
17+
use React\Promise\Promise;
1818
use React\Promise\PromiseInterface;
1919

2020
const proc = __NAMESPACE__ . '\\proc';
@@ -41,54 +41,31 @@
4141
*/
4242
function proc(string $process, ?LoopInterface $loop = null): PromiseInterface
4343
{
44-
$proc = new Process($process);
45-
$result = new Deferred();
44+
$proc = new Process($process);
4645
$proc->start($loop);
4746

48-
if (!$proc->stdout->isReadable()) {
49-
$result->reject(
50-
new \Exception(
51-
\sprintf('Could not process "%s"', $process)
52-
)
53-
);
47+
$data = '';
48+
$action = function (string $chunk) use (&$data) {
49+
$data .= $chunk;
50+
};
5451

55-
return $result;
56-
}
52+
$proc->stdout->on('data', $action);
5753

58-
$proc->stdout->on(
59-
'data',
60-
function ($chunk) use (&$result) {
61-
$result->resolve($chunk);
62-
}
63-
);
64-
65-
// reject promise in the event of failure
66-
$proc->stdout->on(
67-
'error',
68-
function (\Throwable $err) use (&$result, &$proc) {
69-
$result->reject($err);
70-
}
71-
);
72-
73-
// handle successful closure of the process stream
74-
$proc->stdout->on(
75-
'end',
76-
function () use (&$result) {
77-
$result->resolve(true);
78-
}
79-
);
54+
return new Promise(
55+
function (callable $resolve, callable $reject) use (&$data, $proc) {
56+
$proc->stdout->on(
57+
'error',
58+
function (\Throwable $err) use ($reject) {
59+
$reject($err);
60+
}
61+
);
8062

81-
// handle unsuccessful closure of process stream
82-
$proc->stdout->on(
83-
'close',
84-
function () use (&$result, $process) {
85-
$result->reject(
86-
new \Exception(
87-
\sprintf('Closed process "%s"', $process)
88-
)
63+
$proc->stdout->on(
64+
'end',
65+
function () use (&$data, $resolve) {
66+
$resolve($data);
67+
}
8968
);
9069
}
9170
);
92-
93-
return $result->promise();
9471
}

tests/Internal/InternalTest.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use PHPUnit\Framework\TestCase;
88

99
use function Chemem\Asyncify\Internal\proc;
10-
use function Chemem\Bingo\Functional\toException;
1110
use function React\Async\await;
1211

1312
class InternalTest extends TestCase
@@ -20,7 +19,7 @@ public function procProvider(): array
2019
// php commandline process
2120
[['php -r \'echo "foo";\''], 'foo'],
2221
// invalid input
23-
[['kat --foo'], 'Closed process "kat --foo"'],
22+
[['kat --foo'], ''],
2423
];
2524
}
2625

@@ -29,14 +28,14 @@ public function procProvider(): array
2928
*/
3029
public function testprocExecutesCommandAsynchronouslyInChildProcess($args, $result): void
3130
{
32-
$exec = toException(
33-
function (...$args) {
34-
return await(proc(...$args));
35-
},
36-
function ($err) {
37-
return $err->getMessage();
38-
}
39-
)(...$args);
31+
$exec = null;
32+
try {
33+
$exec = await(
34+
proc(...$args)
35+
);
36+
} catch (\Throwable $err) {
37+
$exec = $err->getMessage();
38+
}
4039

4140
$this->assertEquals($result, $exec);
4241
}

0 commit comments

Comments
 (0)