Skip to content

Commit

Permalink
Update Thread !
Browse files Browse the repository at this point in the history
  • Loading branch information
VennDev authored Aug 19, 2024
1 parent d311a0d commit f263bea
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 33 deletions.
45 changes: 12 additions & 33 deletions src/vennv/vapm/Thread.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,15 +318,18 @@ public static function postMainThread(array $data): void

private static function isPostMainThread(string $data): bool
{
return explode('=>', $data)[0] === self::POST_MAIN_THREAD;
return strlen(Utils::getStringAfterSign($data, self::POST_MAIN_THREAD . '=>')) > 0;
}

public static function loadSharedData(string $data): void
private static function isPostThread(string $data): bool
{
$data = explode('=>', $data);
return strlen(Utils::getStringAfterSign($data, self::POST_THREAD . '=>')) > 0;
}

if ($data[0] === self::POST_MAIN_THREAD) {
$result = json_decode($data[1], true);
public static function loadSharedData(string $data): void
{
if (self::isPostMainThread($data)) {
$result = json_decode(Utils::getStringAfterSign($data, self::POST_MAIN_THREAD . '=>'), true);
if (is_array($result)) self::setShared(array_merge(self::$shared, $result));
}
}
Expand Down Expand Up @@ -355,33 +358,6 @@ public static function killThread(int $pid): bool
return false;
}

/**
* @param false|string $data
* @return array<int, mixed>
* @phpstan-return array<int, mixed>
*/
private static function getPost(false|string $data): array
{
$result = [];

if (is_string($data)) {
$explode = explode(PHP_EOL, $data);

foreach ($explode as $item) {
if ($item !== '') {
$dataExplode = explode('=>', $data);

if ($dataExplode[0] == self::POST_THREAD) {
$try = json_decode($dataExplode[1], true);
is_array($try) ? $result[] = $try : $result[] = $dataExplode[1];
}
}
}
}

return $result;
}

abstract public function onRun(): void;

/**
Expand Down Expand Up @@ -482,6 +458,9 @@ public function start(array $mode = DescriptorSpec::BASIC): Promise
$explode = explode(PHP_EOL, $output);
foreach ($explode as $item) {
if ($item !== '' && self::isPostMainThread($item)) self::loadSharedData($item);
elseif ($item !== '' && self::isPostThread($item)) {
$output = Utils::getStringAfterSign($item, self::POST_THREAD . '=>');
}
}
}
}
Expand All @@ -491,7 +470,7 @@ public function start(array $mode = DescriptorSpec::BASIC): Promise

proc_close($process);
unset(self::$threads[$this->getPid()]);
return $resolve(self::getPost($output));
return $resolve($output);
});
}

Expand Down
20 changes: 20 additions & 0 deletions src/vennv/vapm/utils/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ public static function splitArray(array $array, int $size): Generator;
*/
public static function isClass(string $class): bool;


/**
* @return string
*
* Get string after sign
*/
public static function getStringAfterSign(string $string, string $sign): string;

}

final class Utils implements UtilsInterface
Expand Down Expand Up @@ -316,4 +324,16 @@ public static function isClass(string $class): bool
return false;
}

/**
* @return string
*
* Get string after sign
*/
public static function getStringAfterSign(string $string, string $sign): string
{
$position = strpos($string, $sign);
if ($position === false) return '';
return substr($string, $position + strlen($sign));
}

}

0 comments on commit f263bea

Please sign in to comment.