diff --git a/src/Channels/Channel.php b/src/Channels/Channel.php index bfc0b6c4..8b0fae42 100644 --- a/src/Channels/Channel.php +++ b/src/Channels/Channel.php @@ -49,8 +49,6 @@ public function unsubscribe(Connection $connection): void */ public function broadcast(Application $app, array $payload, Connection $except = null): void { - return; - App::make(ChannelManager::class) ->for($app) ->connections($this) diff --git a/src/Contracts/ConnectionManager.php b/src/Contracts/ConnectionManager.php index 7515b70c..05a91895 100644 --- a/src/Contracts/ConnectionManager.php +++ b/src/Contracts/ConnectionManager.php @@ -47,21 +47,14 @@ public function find(string $identifier): ?Connection; /** * Get all of the connections from the cache. * - * @return \Laravel\Reverb\Managers\Connections|\Laravel\Reverb\Connection[]|string[] + * @return */ - public function all(): Connections; - - /** - * Synchronize the connections with the manager. - * - * @param \Laravel\Reverb\Managers\Connections|\Laravel\Reverb\Connection[]|string[] $connections - */ - public function sync(Connections $connections): void; + public function all(): array; /** * Synchronize a connection with the manager. */ - public function syncConnection(Connection $connection): void; + public function save(Connection $connection): void; /** * Flush the channel manager repository. diff --git a/src/Jobs/PruneStaleConnections.php b/src/Jobs/PruneStaleConnections.php index 784e92f9..81963629 100644 --- a/src/Jobs/PruneStaleConnections.php +++ b/src/Jobs/PruneStaleConnections.php @@ -19,9 +19,7 @@ public function handle(ConnectionManager $connections): void app(ApplicationProvider::class) ->all() ->each(function ($application) use ($connections) { - $connections - ->for($application) - ->all() + collect($connections->for($application)->all()) ->each(function ($connection) { if (! $connection->isStale()) { return; diff --git a/src/Managers/ConnectionManager.php b/src/Managers/ConnectionManager.php index 9fac3877..969320ff 100644 --- a/src/Managers/ConnectionManager.php +++ b/src/Managers/ConnectionManager.php @@ -6,7 +6,6 @@ use Illuminate\Contracts\Cache\Repository; use Illuminate\Support\Facades\App; use Laravel\Reverb\Application; -use Laravel\Reverb\Concerns\EnsuresIntegrity; use Laravel\Reverb\Concerns\InteractsWithApplications; use Laravel\Reverb\Connection; use Laravel\Reverb\Contracts\ApplicationProvider; @@ -14,9 +13,14 @@ class ConnectionManager implements ConnectionManagerInterface { - use EnsuresIntegrity, InteractsWithApplications; + use InteractsWithApplications; - protected $connections; + /** + * Connection store. + * + * @var array> + */ + protected $connections = []; /** * The appliation instance. @@ -25,12 +29,6 @@ class ConnectionManager implements ConnectionManagerInterface */ protected $application; - public function __construct( - protected Repository $repository, - protected $prefix = 'reverb' - ) { - } - /** * Get the application instance. */ @@ -46,7 +44,7 @@ public function connect(Connection $connection): Connection { $connection->touch(); - $this->syncConnection($connection); + $this->save($connection); return $connection; } @@ -68,11 +66,7 @@ public function reconnect(string $identifier): ?Connection */ public function disconnect(string $identifier): void { - $connections = $this->all(); - - $this->sync( - $connections->reject(fn ($connection, $id) => (string) $id === $identifier) - ); + unset($this->connections[$this->application->id()][$identifier]); } /** @@ -92,62 +86,25 @@ public function resolve(string $identifier, Closure $newConnection): Connection */ public function find(string $identifier): ?Connection { - if ($connection = $this->all()->find($identifier)) { - return $connection; - } - - return null; + return $this->connections[$this->application->id()][$identifier] ?? null; } /** * Get all of the connections from the cache. * - * @return \Laravel\Reverb\Managers\Connections|\Laravel\Reverb\Connection[]|string[] - */ - public function all(): Connections - { - return $this->mutex(function () { - return $this->repository->get($this->key()) ?: new Connections; - }); - } - - /** - * Synchronize the connections with the manager. - * - * @param \Laravel\Reverb\Managers\Connections|\Laravel\Reverb\Connection[]|string[] $connections + * @return array */ - public function sync(Connections $connections): void + public function all(): array { - $this->mutex(function () use ($connections) { - $this->repository->forever($this->key(), $connections); - }); + return $this->connections[$this->application->id()] ?? []; } /** * Synchronize a connection with the manager. */ - public function syncConnection(Connection $connection): void - { - $connections = $this->all()->put( - $connection->identifier(), - Connection::dehydrate($connection) - ); - - $this->sync($connections); - } - - /** - * Get the key for the channels. - */ - protected function key(): string + public function save(Connection $connection): void { - $key = $this->prefix; - - if ($this->application) { - $key .= ":{$this->application->id()}"; - } - - return $key .= ':connections'; + $this->connections[$this->application->id()][$connection->identifier()] = $connection; } /** @@ -158,8 +115,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 ebbbe1a6..f4b17fe3 100644 --- a/src/Servers/Reverb/ReverbProvider.php +++ b/src/Servers/Reverb/ReverbProvider.php @@ -99,10 +99,7 @@ public function withPublishing(): void */ public function buildConnectionManager(): ConnectionManagerInterface { - return new ConnectionManager( - $this->app['cache']->store('array'), - $this->config['connection_manager']['prefix'] ?? 'reverb' - ); + return new ConnectionManager; } /**