Skip to content

Commit 1efc12c

Browse files
authored
Reformat code + fix Thread!
1 parent e6a4334 commit 1efc12c

File tree

7 files changed

+70
-45
lines changed

7 files changed

+70
-45
lines changed

src/vennv/vapm/ClosureThread.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ public function onRun(): void
7676
} elseif ($callback instanceof \Generator) {
7777
$callback = json_encode(iterator_to_array($callback));
7878
} else {
79-
$callback = (string) $callback;
79+
$callback = (string)$callback;
8080
}
81-
if (is_bool($callback)) $callback = (string) $callback;
81+
if (is_bool($callback)) $callback = (string)$callback;
8282
self::post($callback);
8383
}
8484
}

src/vennv/vapm/PHPUtils.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ final class PHPUtils
2929
{
3030

3131
/**
32-
* @throws Throwable
3332
* @param array<int|float|string|object> $array
3433
* @param callable $callback
3534
* @return Async
3635
*
3736
* @phpstan-param array<int|float|string|object> $array
37+
* @throws Throwable
3838
*/
3939
public static function forEach(array $array, callable $callback): Async
4040
{
@@ -47,12 +47,12 @@ public static function forEach(array $array, callable $callback): Async
4747
}
4848

4949
/**
50-
* @throws Throwable
5150
* @param array<int|float|string|object> $array
5251
* @param callable $callback
5352
* @return Async
5453
*
5554
* @phpstan-param array<int|float|string|object> $array
55+
* @throws Throwable
5656
*/
5757
public static function arrayMap(array $array, callable $callback): Async
5858
{
@@ -67,12 +67,12 @@ public static function arrayMap(array $array, callable $callback): Async
6767
}
6868

6969
/**
70-
* @throws Throwable
7170
* @param array<int|float|string|object> $array
7271
* @param callable $callback
7372
* @return Async
7473
*
7574
* @phpstan-param array<int|float|string|object> $array
75+
* @throws Throwable
7676
*/
7777
public static function arrayFilter(array $array, callable $callback): Async
7878
{

src/vennv/vapm/Promise.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,8 @@ public function __construct(callable $callback, bool $justGetResult = false)
263263
return $result;
264264
};
265265

266-
$this->callbackFinally = function (): void {};
266+
$this->callbackFinally = function (): void {
267+
};
267268

268269
EventLoop::addQueue($this);
269270
}
@@ -396,7 +397,6 @@ public function finally(callable $callback): Promise
396397
*/
397398
public function useCallbacks(): void
398399
{
399-
$hasCallbacks = false;
400400
$result = $this->result;
401401

402402
if ($this->isResolved()) {
@@ -411,18 +411,16 @@ public function useCallbacks(): void
411411
}
412412

413413
if (count($callbacks) > 0) {
414-
$hasCallbacks = true;
415414
/** @var callable $callback */
416415
$callback = $callbacks[0];
417416
$resultFirstCallback = call_user_func($callback, $this->result);
418417
$this->result = $resultFirstCallback;
419418
$this->return = $resultFirstCallback;
420419
$this->checkStatus($callbacks, $this->return);
421420
}
422-
} else if ($this->isRejected()) {
421+
} elseif ($this->isRejected()) {
423422
if (is_callable($this->callbackReject)) $this->result = call_user_func($this->callbackReject, $result);
424423
}
425-
if (!$hasCallbacks && is_callable($this->callbackFinally)) call_user_func($this->callbackFinally);
426424
}
427425

428426
/**
@@ -455,7 +453,7 @@ private function checkStatus(ArrayObject $callbacks, mixed $return): void
455453
$queue1->then($callback);
456454
if (is_callable($this->callbackReject)) $queue1->catch($this->callbackReject);
457455
$lastPromise = $queue1;
458-
} else if (!is_null($queue2)) {
456+
} elseif (!is_null($queue2)) {
459457
$queue2->then($callback);
460458
if (is_callable($this->callbackReject)) $queue2->catch($this->callbackReject);
461459
$lastPromise = $queue2;
@@ -653,4 +651,4 @@ public static function race(array $promises): Promise
653651
return $promise;
654652
}
655653

656-
}
654+
}

src/vennv/vapm/Thread.php

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -411,42 +411,71 @@ public function start(array $mode = DescriptorSpec::BASIC): Promise
411411

412412
$process = proc_open($command, $mode, $pipes);
413413

414+
$output = '';
415+
$error = '';
414416
if (is_resource($process)) {
417+
$data = json_encode(self::getDataMainThread());
418+
419+
if (is_string($data)) fwrite($pipes[0], $data);
420+
fclose($pipes[0]);
421+
415422
stream_set_blocking($pipes[1], false);
416423
stream_set_blocking($pipes[2], false);
417424

418-
$data = json_encode(self::getDataMainThread());
419-
420-
if (is_string($data)) {
421-
fwrite($pipes[0], $data);
422-
fclose($pipes[0]);
425+
$status = proc_get_status($process);
426+
$pid = $status['pid'];
427+
if (!isset(self::$threads[$pid])) {
428+
$this->setPid($pid);
429+
self::$threads[$pid] = $this;
423430
}
424431

425-
while (proc_get_status($process)['running']) {
432+
$thread = self::$threads[$pid];
433+
$thread->setExitCode($status['exitcode']);
434+
$thread->setRunning($status['running']);
435+
$thread->setSignaled($status['signaled']);
436+
$thread->setStopped($status['stopped']);
437+
while ($thread->isRunning()) {
426438
$status = proc_get_status($process);
427-
428-
if (!isset(self::$threads[$status['pid']])) {
429-
$this->setPid($status['pid']);
430-
self::$threads[$status['pid']] = $this;
431-
}
432-
433-
$thread = self::$threads[$status['pid']];
434-
435439
$thread->setExitCode($status['exitcode']);
436440
$thread->setRunning($status['running']);
437441
$thread->setSignaled($status['signaled']);
438442
$thread->setStopped($status['stopped']);
439443

440-
if ($thread->isStopped()) {
444+
if ($thread->isRunning()) {
445+
$read = [$pipes[1], $pipes[2]];
446+
$write = null;
447+
$except = null;
448+
$timeout = 0;
449+
450+
$n = stream_select($read, $write, $except, $timeout);
451+
if ($n === false) break;
452+
if ($n > 0) {
453+
foreach ($read as $stream) {
454+
if (!feof($stream)) {
455+
$data = stream_get_contents($stream, 1024);
456+
if ($data === false || $data === '') {
457+
if (feof($stream)) continue 2;
458+
break;
459+
}
460+
$stream === $pipes[1] ? $output .= $data : $error .= $data;
461+
}
462+
FiberManager::wait();
463+
}
464+
}
465+
} elseif ($thread->isStopped() || $thread->isSignaled()) {
466+
proc_terminate($process);
467+
break;
468+
} else {
441469
proc_terminate($process);
442470
break;
443471
}
444-
445472
FiberManager::wait();
446473
}
447474

448-
$output = stream_get_contents($pipes[1]);
449-
$error = stream_get_contents($pipes[2]);
475+
$outputStream = stream_get_contents($pipes[1]);
476+
$errorStream = stream_get_contents($pipes[2]);
477+
$output .= str_contains($output, $outputStream) ? '' : $outputStream;
478+
$error .= str_contains($error, $errorStream) ? '' : $errorStream;
450479

451480
fclose($pipes[1]);
452481
fclose($pipes[2]);
@@ -455,12 +484,9 @@ public function start(array $mode = DescriptorSpec::BASIC): Promise
455484
return $reject(new ThreadException($error));
456485
} else {
457486
if (!is_bool($output)) {
458-
$explode = explode(PHP_EOL, $output);
459-
foreach ($explode as $item) {
460-
if ($item !== '' && self::isPostMainThread($item)) self::loadSharedData($item);
461-
elseif ($item !== '' && self::isPostThread($item)) {
462-
$output = Utils::getStringAfterSign($item, self::POST_THREAD . '=>');
463-
}
487+
if ($output !== '' && self::isPostMainThread($output)) self::loadSharedData($output);
488+
elseif ($output !== '' && self::isPostThread($output)) {
489+
$output = Utils::getStringAfterSign($output, self::POST_THREAD . '=>');
464490
}
465491
}
466492
}

src/vennv/vapm/Worker.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public function run(callable $callback): Async
229229
if (!$this->isLocked()) {
230230
if (count($promises) < $threads && $work->count() > 0) {
231231
$callbackQueue = $work->dequeue();
232-
232+
233233
if (!is_callable($callbackQueue)) continue;
234234

235235
$thread = new ClosureThread($callbackQueue);

src/vennv/vapm/utils/Property.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ public function update(object $data, array $options): object
4343
* @var mixed $value
4444
*/
4545
foreach ($options as $key => $value) {
46-
if (property_exists($data, $key)) $data->{$key} = $value; /* @phpstan-ignore-line */
46+
if (property_exists($data, $key)) $data->{$key} = $value;
47+
/* @phpstan-ignore-line */
4748
}
4849

4950
return $data;

src/vennv/vapm/utils/Utils.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public static function isClass(string $class): bool;
137137

138138
/**
139139
* @return string
140-
*
140+
*
141141
* Get string after sign
142142
*/
143143
public static function getStringAfterSign(string $string, string $sign): string;
@@ -213,9 +213,10 @@ public static function fixInputCommand(string $text): array|string
213213
*/
214214
public static function removeComments(string $text): null|string|array
215215
{
216-
$text = preg_replace('/\/\/.*?(\r\n|\n|$)/', '', $text);
216+
$text = preg_replace('/(?<!:)\/\/.*?(\r\n|\n|$)/', '', $text);
217217
if ($text === null || is_array($text)) return null;
218-
return preg_replace('/\/\*.*?\*\//ms', '', $text);
218+
$text = preg_replace('/\/\*[\s\S]*?\*\//', '', $text);
219+
return $text;
219220
}
220221

221222
/**
@@ -326,14 +327,13 @@ public static function isClass(string $class): bool
326327

327328
/**
328329
* @return string
329-
*
330+
*
330331
* Get string after sign
331332
*/
332333
public static function getStringAfterSign(string $string, string $sign): string
333334
{
334-
$position = strpos($string, $sign);
335-
if ($position === false) return '';
336-
return substr($string, $position + strlen($sign));
335+
if (preg_match('/' . preg_quote($sign, '/') . '(.*)/s', $string, $matches)) return $matches[1];
336+
return '';
337337
}
338338

339339
}

0 commit comments

Comments
 (0)