added wss tools 7

This commit is contained in:
2026-06-18 19:44:23 +03:30
parent 017ab348c7
commit c15136c5e7
4 changed files with 125 additions and 9 deletions
+63 -9
View File
@@ -43,15 +43,34 @@ class PriceBroadcastService
}
$lastChanged = is_array($lastPayload) && $lastPayload !== []
&& $this->rememberHashIfChanged(self::LAST_HASH_KEY, $lastPayload);
$this->publishListIfChanged();
&& $this->rememberHashIfChanged(self::LAST_HASH_KEY, self::lastPriceIdentity($lastPayload));
if ($lastChanged && is_array($lastPayload)) {
$stream->publish('last', $lastPayload);
$this->publishListIfChanged(true);
} else {
$this->publishListIfChanged();
}
}
/**
* @param array<string, mixed> $payload
* @return array<string, int|bool>
*/
public static function lastPriceIdentity(array $payload): array
{
return [
'sell' => (int) ($payload['sell'] ?? 0),
'buy' => (int) ($payload['buy'] ?? 0),
'price_last' => (int) ($payload['price_last'] ?? 0),
'price' => (int) ($payload['price'] ?? 0),
'closed' => (bool) ($payload['closed'] ?? false),
'main_product' => (int) ($payload['main_product'] ?? 0),
'montzar' => (int) ($payload['montzar'] ?? 0),
'zaarbox' => (int) ($payload['zaarbox'] ?? 0),
];
}
public function broadcastLastIfChanged(?array $lastPayload): void
{
$stream = app(PriceStreamService::class);
@@ -63,39 +82,74 @@ class PriceBroadcastService
return;
}
if (! $this->rememberHashIfChanged(self::LAST_HASH_KEY, $lastPayload)) {
if (! $this->rememberHashIfChanged(self::LAST_HASH_KEY, self::lastPriceIdentity($lastPayload))) {
return;
}
$stream->publish('last', $lastPayload);
$this->publishListIfChanged(true);
}
public function broadcastListIfChanged(): bool
public function buildListPayload(): ?array
{
return $this->buildProductListPayload();
}
public function broadcastListIfChanged(bool $force = false): bool
{
$stream = app(PriceStreamService::class);
if (! $stream->isEnabled()) {
return false;
}
return $this->publishListIfChanged();
return $this->publishListIfChanged($force);
}
private function publishListIfChanged(): bool
private function publishListIfChanged(bool $force = false): bool
{
$listPayload = $this->buildProductListPayload();
if ($listPayload === null) {
if ($listPayload === null || $listPayload === []) {
return false;
}
if (! $this->rememberHashIfChanged(self::LIST_HASH_KEY, $listPayload)) {
if (! $force && ! $this->rememberHashIfChanged(self::LIST_HASH_KEY, $this->listIdentity($listPayload))) {
return false;
}
if ($force) {
$this->rememberHashIfChanged(self::LIST_HASH_KEY, $this->listIdentity($listPayload));
}
app(PriceStreamService::class)->publish('list', $listPayload);
return true;
}
/**
* @param array<int, array<string, mixed>> $listPayload
* @return array<int, array<string, int|bool>>
*/
private function listIdentity(array $listPayload): array
{
$identity = [];
foreach ($listPayload as $product) {
if (! is_array($product)) {
continue;
}
$identity[] = [
'id' => (int) ($product['id'] ?? 0),
'price' => (int) ($product['price'] ?? 0),
'price_profit' => (int) ($product['price_profit'] ?? 0),
'show' => (bool) ($product['show'] ?? true),
'active' => (bool) ($product['active'] ?? true),
];
}
return $identity;
}
/**
* @param array<string, mixed>|array<int, mixed> $payload
*/