diff --git a/src/Channels/Channel.php b/src/Channels/Channel.php index 8b0fae42..c6c75e8f 100644 --- a/src/Channels/Channel.php +++ b/src/Channels/Channel.php @@ -49,9 +49,7 @@ public function unsubscribe(Connection $connection): void */ public function broadcast(Application $app, array $payload, Connection $except = null): void { - App::make(ChannelManager::class) - ->for($app) - ->connections($this) + collect(App::make(ChannelManager::class)->for($app)->connections($this)) ->each(function ($connection) use ($payload, $except) { if ($except && $except->identifier() === $connection->identifier()) { return; diff --git a/src/Contracts/ChannelManager.php b/src/Contracts/ChannelManager.php index 20e98109..90f34247 100644 --- a/src/Contracts/ChannelManager.php +++ b/src/Contracts/ChannelManager.php @@ -40,17 +40,12 @@ public function all(): Collection; */ public function unsubscribeFromAll(Connection $connection): void; - /** - * Get all connection keys for the given channel. - */ - public function connectionKeys(Channel $channel): Collection; - /** * Get all connections for the given channel. * - * @return \Laravel\Reverb\Managers\Connections|\Laravel\Reverb\Connection[]|string[] + * @return */ - public function connections(Channel $channel): Collection; + public function connections(Channel $channel): array; /** * Flush the channel manager repository. diff --git a/src/Managers/ChannelManager.php b/src/Managers/ChannelManager.php index a846c8f9..32565900 100644 --- a/src/Managers/ChannelManager.php +++ b/src/Managers/ChannelManager.php @@ -13,12 +13,18 @@ use Laravel\Reverb\Connection; use Laravel\Reverb\Contracts\ApplicationProvider; use Laravel\Reverb\Contracts\ChannelManager as ChannelManagerInterface; -use Laravel\Reverb\Contracts\ConnectionManager; class ChannelManager implements ChannelManagerInterface { use EnsuresIntegrity, InteractsWithApplications; + /** + * Connection store. + * + * @var array>> + */ + protected $connections = []; + /** * The appliation instance. * @@ -26,13 +32,6 @@ class ChannelManager implements ChannelManagerInterface */ protected $application; - public function __construct( - protected Repository $repository, - protected ConnectionManager $connections, - protected $prefix = 'reverb' - ) { - } - /** * Get the application instance. */ @@ -46,12 +45,7 @@ public function app(): ?Application */ public function subscribe(Channel $channel, Connection $connection, $data = []): void { - $this->mutex(function () use ($channel, $connection, $data) { - $connections = $this->connectionKeys($channel) - ->put($connection->identifier(), $data); - - $this->syncConnections($channel, $connections); - }); + $this->connections[$this->application->id()][$channel->name()][$connection->identifier()] = $connection; } /** @@ -59,12 +53,7 @@ public function subscribe(Channel $channel, Connection $connection, $data = []): */ public function unsubscribe(Channel $channel, Connection $connection): void { - $this->mutex(function () use ($channel, $connection) { - $connections = $this->connectionKeys($channel) - ->reject(fn ($data, $identifier) => (string) $identifier === $connection->identifier()); - - $this->syncConnections($channel, $connections); - }); + unset($this->connections[$this->application->id()][$channel->name()][$connection->identifier()]); } /** @@ -87,51 +76,14 @@ public function unsubscribeFromAll(Connection $connection): void }); } - /** - * Get all connection keys for the given channel. - */ - public function connectionKeys(Channel $channel): Collection - { - return $this->channel($channel); - } - /** * Get all connections for the given channel. * - * @return \Laravel\Reverb\Managers\Connections|\Laravel\Reverb\Connection[]|string[] + * @return */ - public function connections(Channel $channel): Collection + public function connections(Channel $channel): array { - return collect($this->connections->for($this->application)->all()) - ->intersectByKeys( - $this->connectionKeys($channel) - ); - } - - /** - * Sync the connections for a channel. - */ - protected function syncConnections(Channel $channel, Collection $connections): void - { - $channels = $this->channels(); - - $channels[$channel->name()] = $connections; - - $this->repository->forever($this->key(), $channels); - } - - /** - * Get the key for the channels. - */ - protected function key(): string - { - $key = $this->prefix; - - if ($this->application) { - $key .= ":{$this->application->id()}"; - } - - return $key.':channels'; + return $this->connections[$this->application->id()][$channel->name()] ?? []; } /** @@ -147,7 +99,7 @@ protected function channel(Channel $channel): Collection */ protected function channels(Channel $channel = null): Collection { - $channels = $this->repository->get($this->key(), []); + $channels = $this->connections[$this->application->id()]; if ($channel) { return collect($channels[$channel->name()] ?? []); @@ -156,18 +108,6 @@ protected function channels(Channel $channel = null): Collection return collect($channels ?: []); } - /** - * Get the data stored for a connection. - */ - public function data(Channel $channel, Connection $connection): array - { - if (! $data = $this->connectionKeys($channel)->get($connection->identifier())) { - return []; - } - - return (array) $data; - } - /** * Flush the channel manager repository. */ @@ -176,8 +116,7 @@ public function flush(): void App::make(ApplicationProvider::class) ->all() ->each(function (Application $application) { - $this->for($application); - $this->repository->forget($this->key()); + $this->connections[$application->id()] = []; }); } } diff --git a/src/Servers/Reverb/ReverbProvider.php b/src/Servers/Reverb/ReverbProvider.php index f4b17fe3..1c485da0 100644 --- a/src/Servers/Reverb/ReverbProvider.php +++ b/src/Servers/Reverb/ReverbProvider.php @@ -107,10 +107,6 @@ public function buildConnectionManager(): ConnectionManagerInterface */ public function buildChannelManager(): ChannelManagerInterface { - return new ChannelManager( - $this->app['cache']->store('array'), - $this->app->make(ConnectionManagerInterface::class), - $this->config['connection_manager']['prefix'] ?? 'reverb' - ); + return new ChannelManager; } }