Skip to content

Commit

Permalink
Add cancelation support for split (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
xtrime-ru committed Nov 19, 2023
1 parent 3a4a0fa commit 0a4b0e8
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ function getStderr(): WritableResourceStream
*
* @return \Traversable<int, string>
*/
function split(ReadableStream $source, string $delimiter): \Traversable
function split(ReadableStream $source, string $delimiter, ?Cancellation $cancellation = null): \Traversable
{
$buffer = '';

while (null !== $chunk = $source->read()) {
while (null !== $chunk = $source->read($cancellation)) {
$buffer .= $chunk;

$split = \explode($delimiter, $buffer);
Expand All @@ -149,7 +149,7 @@ function split(ReadableStream $source, string $delimiter): \Traversable
*
* @return \Traversable<int, string>
*/
function splitLines(ReadableStream $source): \Traversable
function splitLines(ReadableStream $source, ?Cancellation $cancellation = null): \Traversable
{
foreach (split($source, "\n") as $line) {
yield \rtrim($line, "\r");
Expand All @@ -168,8 +168,9 @@ function parseLineDelimitedJson(
bool $associative = false,
int $depth = 512,
int $flags = 0,
?Cancellation $cancellation = null
): \Traversable {
foreach (splitLines($source) as $line) {
foreach (splitLines($source, $cancellation) as $line) {
$line = \trim($line);

if ($line === '') {
Expand Down

0 comments on commit 0a4b0e8

Please sign in to comment.