Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removes connection manager #20

Merged
merged 6 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/Channels/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ public function connections(): array
return $this->connections->all();
}

/**
* Find a connection.
*/
public function find(Connection $connection): ?Connection
{
return $this->connections->find($connection);
}

/**
* Find a connection by its ID.
*/
public function findById(string $id): ?Connection
{
return $this->connections->findById($id);
}

/**
* Subscribe to the given channel.
*/
Expand Down Expand Up @@ -70,11 +86,11 @@ public function broadcast(Application $app, array $payload, Connection $except =
{
collect($this->connections())
->each(function ($connection) use ($payload, $except) {
if ($except && $except->identifier() === $connection->connection()->identifier()) {
if ($except && $except->id() === $connection->connection()->id()) {
return;
}

if (isset($payload['except']) && $payload['except'] === $connection->connection()->identifier()) {
if (isset($payload['except']) && $payload['except'] === $connection->connection()->id()) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/ClientEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static function whisper(Connection $connection, array $payload): void
{
Event::dispatch(
$connection->app(),
$payload + ['except' => $connection->identifier()],
$payload + ['except' => $connection->id()],
$connection
);
}
Expand Down
7 changes: 6 additions & 1 deletion src/Contracts/ChannelConnectionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@ public function add(Connection $connection, array $data): void;
public function remove(Connection $connection): void;

/**
* Find a connection by its identifier.
* Find a connection in the set.
*/
public function find(Connection $connection): ?ChannelConnection;

/**
* Find a connection in the set by its ID.
*/
public function findById(string $id): ?ChannelConnection;

/**
* Get all the connections.
*
Expand Down
7 changes: 6 additions & 1 deletion src/Contracts/ChannelManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@ public function for(Application $application): ChannelManager;
public function all(): Collection;

/**
* Find the given channel
* Find the given channel.
*/
public function find(string $channel): Channel;

/**
* Get all the connections for the given channels.
*/
public function connections(string $channel = null): Collection;

/**
* Unsubscribe from all channels.
*/
Expand Down
10 changes: 3 additions & 7 deletions src/Contracts/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ abstract class Connection
/**
* The last time the connection was seen.
*/
protected string $lastSeenAt;
protected ?string $lastSeenAt = null;

/**
* Stores the ping state of the connection.
Expand Down Expand Up @@ -95,19 +95,15 @@ public function disconnect(): void
->for($this->app())
->unsubscribeFromAll($this);

App::make(ConnectionManager::class)
->for($this->app())
->disconnect($this->identifier());

$this->terminate();
}

/**
* Get the last time the connection was seen.
*/
public function lastSeenAt(): Carbon
public function lastSeenAt(): ?Carbon
{
return Carbon::parse($this->lastSeenAt);
return $this->lastSeenAt ? Carbon::parse($this->lastSeenAt) : null;
}

/**
Expand Down
61 changes: 0 additions & 61 deletions src/Contracts/ConnectionManager.php

This file was deleted.

5 changes: 0 additions & 5 deletions src/Contracts/ServerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@ public function doesNotSubscribeToEvents(): bool
return $this->shouldNotPublishEvents();
}

/**
* Build the connection manager for the server.
*/
abstract public function buildConnectionManager(): ConnectionManager;

/**
* Build the channel manager for the server.
*/
Expand Down
8 changes: 4 additions & 4 deletions src/Jobs/PingInactiveConnections.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Illuminate\Foundation\Bus\Dispatchable;
use Laravel\Reverb\Contracts\ApplicationProvider;
use Laravel\Reverb\Contracts\ConnectionManager;
use Laravel\Reverb\Contracts\ChannelManager;

class PingInactiveConnections
{
Expand All @@ -13,12 +13,12 @@ class PingInactiveConnections
/**
* Execute the job.
*/
public function handle(ConnectionManager $connections): void
public function handle(ChannelManager $channels): void
{
app(ApplicationProvider::class)
->all()
->each(function ($application) use ($connections) {
collect($connections->for($application)->all())
->each(function ($application) use ($channels) {
$channels->for($application)->connections()
->each(function ($connection) {
if ($connection->isActive()) {
return;
Expand Down
11 changes: 5 additions & 6 deletions src/Jobs/PruneStaleConnections.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Illuminate\Foundation\Bus\Dispatchable;
use Laravel\Reverb\Contracts\ApplicationProvider;
use Laravel\Reverb\Contracts\ConnectionManager;
use Laravel\Reverb\Contracts\ChannelManager;
use Laravel\Reverb\Output;

class PruneStaleConnections
Expand All @@ -14,16 +14,15 @@ class PruneStaleConnections
/**
* Execute the job.
*/
public function handle(ConnectionManager $connections): void
public function handle(ChannelManager $channels): void
{
app(ApplicationProvider::class)
->all()
->each(function ($application) use ($connections) {
collect($connections->for($application)->all())
->each(function ($application) use ($channels) {

$channels->for($application)->connections()
->each(function ($connection) {
if (! $connection->isStale()) {
dump('Connection is not stale', $connection->id());

return;
}

Expand Down
16 changes: 12 additions & 4 deletions src/Managers/ArrayChannelConnectionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,31 @@ class ArrayChannelConnectionManager implements ChannelConnectionManager
*/
public function add(Connection $connection, array $data): void
{
$this->connections[$connection->identifier()] = new ChannelConnection($connection, $data);
$this->connections[$connection->id()] = new ChannelConnection($connection, $data);
}

/**
* Remove a connection.
*/
public function remove(Connection $connection): void
{
unset($this->connections[$connection->identifier()]);
unset($this->connections[$connection->id()]);
}

/**
* Find a connection by its identifier.
* Find a connection in the set.
*/
public function find(Connection $connection): ?ChannelConnection
{
return $this->connections[$connection->identifier()] ?? null;
return $this->findById($connection->id());
}

/**
* Find a connection in the set by its ID.
*/
public function findById(string $id): ?ChannelConnection
{
return $this->connections[$id] ?? null;
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/Managers/ArrayChannelManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ public function find(string $channel): Channel
return $this->channels($channel);
}

/**
* Get all the connections for the given channels.
*/
public function connections(string $channel = null): Collection
{
$channels = Collection::wrap($this->channels($channel));

return $channels->reduce(function ($carry, $channel) {
return $carry = $carry->merge($channel->connections());
}, collect());
}

/**
* Unsubscribe from all channels.
*/
Expand Down
Loading