Repository navigation
Expand file tree
/
Copy pathCommitsUpdated.php
More file actions
81 lines (69 loc) · 2.39 KB
/
Copy pathCommitsUpdated.php
File metadata and controls
81 lines (69 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
declare(strict_types=1);
namespace App\Events;
use App\Enums\CommitUpdate;
use App\Models\Commit;
use Illuminate\Broadcasting\Channel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Contracts\Broadcasting\ShouldRescue;
use Illuminate\Contracts\Events\ShouldDispatchAfterCommit;
/**
* Sent once the transaction commits, on each public commit's channel. Whoever shows the commit hears about it, and
* when it appears or disappears so do the threads it is a reply or quote in, and the logs. Callers only pass commits
* everyone could see before or after the change, and only their ids are sent.
*/
final class CommitsUpdated implements ShouldBroadcast, ShouldDispatchAfterCommit, ShouldRescue
{
/**
* @var list<string>
*/
public array $commitIds;
/**
* @var list<string>
*/
public array $channelIds;
/**
* @param iterable<int, Commit> $commits
*/
public function __construct(iterable $commits, public CommitUpdate $update)
{
$commits = collect($commits);
$this->commitIds = array_values(array_unique($commits->map(fn (Commit $commit): string => $commit->id)->all()));
$this->channelIds = $update === CommitUpdate::Visibility
? array_values(array_unique(array_filter($commits->flatMap(fn (Commit $commit): array => [$commit->id, $commit->parent_id, $commit->quoted_commit_id])->all(), is_string(...))))
: $this->commitIds;
}
/**
* @return list<Channel>
*/
public function broadcastOn(): array
{
return [
...array_map(fn (string $id): Channel => new Channel('commits.'.$id), $this->channelIds),
...$this->update === CommitUpdate::Visibility ? [new Channel('log')] : [],
];
}
/**
* One request to the broadcaster carries up to a hundred channels, so that much is sent right away, and a wider
* audience is left to the queue instead of holding up the request.
*/
public function shouldBroadcastNow(): bool
{
return count($this->broadcastOn()) <= 100;
}
public function broadcastAs(): string
{
return $this->update->value;
}
/**
* @return array{ids: list<string>}
*/
public function broadcastWith(): array
{
return ['ids' => $this->commitIds];
}
public function broadcastWhen(): bool
{
return $this->commitIds !== [];
}
}