Skip to content

Commit

Permalink
bc - rename/reorder return_in to flush_value, corrections, insure…
Browse files Browse the repository at this point in the history
… child output only happen for `stdout` not return values/data
  • Loading branch information
TheTechsTech committed Apr 13, 2021
1 parent b454598 commit 83b3dc8
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 28 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ $process = spawn(function (ChanneledInterface $channel) {
echo $channel->read(); // same as echo fgets(STDIN);
echo $channel->read();

// The `return_in` is needed otherwise last output will be mixed in with the encoded return data.
// The `flush_value` is needed otherwise last output will be mixed in with the encoded return data.
// Or some other processing could be done instead to make this unnecessary.
// All returned `data/results` are encoded, then decode by the parent.
return \return_in(50, 'return whatever'); // same as echo '___uv_spawn___'; usleep(50); return 'return whatever';
return \flush_value('return whatever', 50); // same as echo '___uv_spawn___'; usleep(50); return 'return whatever';
}, 0, $ipc)
->progress(function ($type, $data) use ($ipc) {
if ('ping' === $data) {
Expand Down Expand Up @@ -151,9 +151,9 @@ $process = Spawn::create(function () {

/////////////////////////////////////////////////////////////
// This following statement is needed or some other processing performed before returning data.
\return_in($delay, $with); // will print '___uv_spawn___' and sleep for 50 microseconds with data;
return \flush_value($with, $delay); // will print '___uv_spawn___' and sleep for 50 microseconds with data;
/////////////////////////////////////////////////////////////

// Or Just
return `result`; // `result` will be encoded, then decoded by parent.
}, int $timeout = 0 , $input = null)
->then(function ($result) {
Expand Down
11 changes: 5 additions & 6 deletions Spawn/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ function is_base64($input): ?bool
*/
function serializer($input)
{
return \base64_encode(\serialize($input));
return \base64_encode(@\serialize($input));
}

/**
Expand Down Expand Up @@ -439,23 +439,22 @@ function deserialize($input)
* - The parent is given no time to read data stream before the `return`, there was no
* delay or processing preformed between child last output and the `return` statement.
*
* @param mixed $with to return to parent process.
* @param int $microsecond - `50` when using `uv_spawn`, otherwise `1500` or so higher with `proc_open`.
*
* @param mixed $withData to return to parent process.
*
* @return void|mixed
*
* @codeCoverageIgnore
*/
function return_in(int $microsecond = 50, $withData = null)
function flush_value($with = null, int $microsecond = 50)
{
\fwrite(\STDOUT, LauncherInterface::INVALID[1]);
\fflush(\STDOUT);
\usleep($microsecond);
\fflush(\STDOUT);

if (!\is_null($withData))
return $withData;
if (!\is_null($with))
return $with;
}

/**
Expand Down
11 changes: 5 additions & 6 deletions Spawn/Launcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,16 +457,15 @@ public function displayOff(): LauncherInterface
*/
protected function display($buffer = null)
{
$output = $this->decoded($buffer);
if ($this->showOutput) {
if (\IS_CLI) {
$output = $this->decoded($buffer);
} else {
if (\is_string($output)) {
// @codeCoverageIgnoreStart
$output = \htmlspecialchars((string) $this->decoded($buffer), ENT_COMPAT, 'UTF-8');
if (!\IS_CLI)
$output = \htmlspecialchars($output, \ENT_COMPAT, 'UTF-8');
// @codeCoverageIgnoreEnd
\printf('%s', $output);
}

\printf('%s', $output);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Spawn/Spawn.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public static function create(

$useYield = ($isYield === null) ? self::$isYield : $isYield;

if (\function_exists('uv_default_loop') && self::$useUv) {
if (\IS_UV && self::$useUv) {
return Launcher::add(
$task,
(int) self::getId(),
Expand Down
2 changes: 1 addition & 1 deletion examples/progress.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function (ChanneledInterface $channel) {
$channel->write('ping');
echo $channel->read();
echo $channel->read();
return \return_in(50, 'The game!');
return \flush_value('The game!');
}
)->progress(function ($type, $data) use ($ipc) {
if ('ping' === $data) {
Expand Down
2 changes: 1 addition & 1 deletion examples/signal.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function (ChanneledInterface $channel) {
$channel->write('ping');
echo $channel->read();
echo $channel->read();
return \return_in(50, 'The game!');
return \flush_value('The game!');
}
)->signal(\SIGKILL, function ($signal) {
echo "the process has been terminated with 'SIGKILL - " . $signal . "' signal!" . \PHP_EOL;
Expand Down
16 changes: 8 additions & 8 deletions tests/ChanneledFallbackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class ChanneledFallbackTest extends TestCase
{
protected function setUp(): void
protected function setUp(): void
{
Spawn::setup(null, false, false, false);
}
Expand All @@ -22,7 +22,7 @@ public function testSimpleChanneled()
$channel->write('ping');
echo $channel->read();
echo $channel->read();
return \return_in(1500, 9);
return \flush_value(9, 1500);
}, 10, $ipc)
->progress(
function ($type, $data) use ($ipc) {
Expand Down Expand Up @@ -55,7 +55,7 @@ public function testSimpleChanneledError()
function ($type, $data) use ($ipc) {
if ('ping' === $data) {
$ipc->close()
->send('pang' . \PHP_EOL);
->send('pang' . \PHP_EOL);
}
}
);
Expand Down Expand Up @@ -232,11 +232,11 @@ public function testLiveStreamAsInput()
$p = spawn(function (ChanneledInterface $ipc) {
$ipc->passthru();
}, 10, $stream)
->progress(function ($type, $data) use ($stream) {
if ('hello' === $data) {
fclose($stream);
}
});
->progress(function ($type, $data) use ($stream) {
if ('hello' === $data) {
fclose($stream);
}
});

$p->run();

Expand Down
2 changes: 1 addition & 1 deletion tests/ChanneledTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function testSimpleChanneled()
echo $channel->read();
echo $channel->read();

return \return_in(50, 9);
return \flush_value(9);
}, 6)
->progress(
function ($type, $data) use ($ipc) {
Expand Down

0 comments on commit 83b3dc8

Please sign in to comment.