Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit 5cb2ee9

Browse files
committed
Run promises one-after-another
1 parent bf47275 commit 5cb2ee9

File tree

14 files changed

+397
-306
lines changed

14 files changed

+397
-306
lines changed

config/websockets.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,4 +279,19 @@
279279

280280
],
281281

282+
/*
283+
|--------------------------------------------------------------------------
284+
| Promise Resolver
285+
|--------------------------------------------------------------------------
286+
|
287+
| The promise resolver is a class that takes a input value and is
288+
| able to make sure the PHP code runs async by using ->then(). You can
289+
| use your own Promise Resolver. This is usually changed when you want to
290+
| intercept values by the promises throughout the app, like in testing
291+
| to switch from async to sync.
292+
|
293+
*/
294+
295+
'promise_resolver' => \React\Promise\FulfilledPromise::class,
296+
282297
];

src/API/FetchChannels.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,9 @@ public function __invoke(Request $request)
6464
}
6565

6666
return $info;
67-
})
68-
->sortBy(function ($content, $name) {
67+
})->sortBy(function ($content, $name) {
6968
return $name;
70-
})
71-
->all();
69+
})->all();
7270

7371
return [
7472
'channels' => $channels ?: new stdClass,

src/ChannelManagers/LocalChannelManager.php

Lines changed: 60 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use BeyondCode\LaravelWebSockets\Channels\PresenceChannel;
77
use BeyondCode\LaravelWebSockets\Channels\PrivateChannel;
88
use BeyondCode\LaravelWebSockets\Contracts\ChannelManager;
9+
use BeyondCode\LaravelWebSockets\Helpers;
910
use Illuminate\Support\Str;
1011
use Ratchet\ConnectionInterface;
1112
use React\EventLoop\LoopInterface;
@@ -104,7 +105,7 @@ public function getLocalConnections(): PromiseInterface
104105
->values()->collapse()
105106
->toArray();
106107

107-
return new FulfilledPromise($connections);
108+
return Helpers::createFulfilledPromise($connections);
108109
}
109110

110111
/**
@@ -116,7 +117,7 @@ public function getLocalConnections(): PromiseInterface
116117
*/
117118
public function getLocalChannels($appId): PromiseInterface
118119
{
119-
return new FulfilledPromise(
120+
return Helpers::createFulfilledPromise(
120121
$this->channels[$appId] ?? []
121122
);
122123
}
@@ -137,12 +138,12 @@ public function getGlobalChannels($appId): PromiseInterface
137138
* Remove connection from all channels.
138139
*
139140
* @param \Ratchet\ConnectionInterface $connection
140-
* @return void
141+
* @return PromiseInterface[bool]
141142
*/
142-
public function unsubscribeFromAllChannels(ConnectionInterface $connection)
143+
public function unsubscribeFromAllChannels(ConnectionInterface $connection): PromiseInterface
143144
{
144145
if (! isset($connection->app)) {
145-
return;
146+
return new FuilfilledPromise(false);
146147
}
147148

148149
$this->getLocalChannels($connection->app->id)
@@ -162,6 +163,8 @@ public function unsubscribeFromAllChannels(ConnectionInterface $connection)
162163
unset($this->channels[$connection->app->id]);
163164
}
164165
});
166+
167+
return Helpers::createFulfilledPromise(true);
165168
}
166169

167170
/**
@@ -170,13 +173,15 @@ public function unsubscribeFromAllChannels(ConnectionInterface $connection)
170173
* @param \Ratchet\ConnectionInterface $connection
171174
* @param string $channelName
172175
* @param stdClass $payload
173-
* @return void
176+
* @return PromiseInterface[bool]
174177
*/
175-
public function subscribeToChannel(ConnectionInterface $connection, string $channelName, stdClass $payload)
178+
public function subscribeToChannel(ConnectionInterface $connection, string $channelName, stdClass $payload): PromiseInterface
176179
{
177180
$channel = $this->findOrCreate($connection->app->id, $channelName);
178181

179-
$channel->subscribe($connection, $payload);
182+
return Helpers::createFulfilledPromise(
183+
$channel->subscribe($connection, $payload)
184+
);
180185
}
181186

182187
/**
@@ -185,35 +190,39 @@ public function subscribeToChannel(ConnectionInterface $connection, string $chan
185190
* @param \Ratchet\ConnectionInterface $connection
186191
* @param string $channelName
187192
* @param stdClass $payload
188-
* @return void
193+
* @return PromiseInterface[bool]
189194
*/
190-
public function unsubscribeFromChannel(ConnectionInterface $connection, string $channelName, stdClass $payload)
195+
public function unsubscribeFromChannel(ConnectionInterface $connection, string $channelName, stdClass $payload): PromiseInterface
191196
{
192197
$channel = $this->findOrCreate($connection->app->id, $channelName);
193198

194-
$channel->unsubscribe($connection, $payload);
199+
return Helpers::createFulfilledPromise(
200+
$channel->unsubscribe($connection, $payload)
201+
);
195202
}
196203

197204
/**
198-
* Subscribe the connection to a specific channel.
205+
* Subscribe the connection to a specific channel, returning
206+
* a promise containing the amount of connections.
199207
*
200208
* @param string|int $appId
201-
* @return void
209+
* @return PromiseInterface[int]
202210
*/
203-
public function subscribeToApp($appId)
211+
public function subscribeToApp($appId): PromiseInterface
204212
{
205-
//
213+
return Helpers::createFulfilledPromise(0);
206214
}
207215

208216
/**
209-
* Unsubscribe the connection from the channel.
217+
* Unsubscribe the connection from the channel, returning
218+
* a promise containing the amount of connections after decrement.
210219
*
211220
* @param string|int $appId
212-
* @return void
221+
* @return PromiseInterface[int]
213222
*/
214-
public function unsubscribeFromApp($appId)
223+
public function unsubscribeFromApp($appId): PromiseInterface
215224
{
216-
//
225+
return Helpers::createFulfilledPromise(0);
217226
}
218227

219228
/**
@@ -222,23 +231,21 @@ public function unsubscribeFromApp($appId)
222231
*
223232
* @param string|int $appId
224233
* @param string|null $channelName
225-
* @return \React\Promise\PromiseInterface
234+
* @return PromiseInterface[int]
226235
*/
227236
public function getLocalConnectionsCount($appId, string $channelName = null): PromiseInterface
228237
{
229238
return $this->getLocalChannels($appId)
230239
->then(function ($channels) use ($channelName) {
231-
return collect($channels)
232-
->when(! is_null($channelName), function ($collection) use ($channelName) {
233-
return $collection->filter(function (Channel $channel) use ($channelName) {
234-
return $channel->getName() === $channelName;
235-
});
236-
})
237-
->flatMap(function (Channel $channel) {
238-
return collect($channel->getConnections())->pluck('socketId');
239-
})
240-
->unique()
241-
->count();
240+
return collect($channels)->when(! is_null($channelName), function ($collection) use ($channelName) {
241+
return $collection->filter(function (Channel $channel) use ($channelName) {
242+
return $channel->getName() === $channelName;
243+
});
244+
})
245+
->flatMap(function (Channel $channel) {
246+
return collect($channel->getConnections())->pluck('socketId');
247+
})
248+
->unique()->count();
242249
});
243250
}
244251

@@ -248,7 +255,7 @@ public function getLocalConnectionsCount($appId, string $channelName = null): Pr
248255
*
249256
* @param string|int $appId
250257
* @param string|null $channelName
251-
* @return \React\Promise\PromiseInterface
258+
* @return PromiseInterface[int]
252259
*/
253260
public function getGlobalConnectionsCount($appId, string $channelName = null): PromiseInterface
254261
{
@@ -263,11 +270,11 @@ public function getGlobalConnectionsCount($appId, string $channelName = null): P
263270
* @param string $channel
264271
* @param stdClass $payload
265272
* @param string|null $serverId
266-
* @return bool
273+
* @return PromiseInterface[bool]
267274
*/
268-
public function broadcastAcrossServers($appId, ?string $socketId, string $channel, stdClass $payload, string $serverId = null)
275+
public function broadcastAcrossServers($appId, ?string $socketId, string $channel, stdClass $payload, string $serverId = null): PromiseInterface
269276
{
270-
return true;
277+
return Helpers::createFulfilledPromise(true);
271278
}
272279

273280
/**
@@ -277,12 +284,14 @@ public function broadcastAcrossServers($appId, ?string $socketId, string $channe
277284
* @param stdClass $user
278285
* @param string $channel
279286
* @param stdClass $payload
280-
* @return void
287+
* @return PromiseInterface[bool]
281288
*/
282-
public function userJoinedPresenceChannel(ConnectionInterface $connection, stdClass $user, string $channel, stdClass $payload)
289+
public function userJoinedPresenceChannel(ConnectionInterface $connection, stdClass $user, string $channel, stdClass $payload): PromiseInterface
283290
{
284291
$this->users["{$connection->app->id}:{$channel}"][$connection->socketId] = json_encode($user);
285292
$this->userSockets["{$connection->app->id}:{$channel}:{$user->user_id}"][] = $connection->socketId;
293+
294+
return Helpers::createFulfilledPromise(true);
286295
}
287296

288297
/**
@@ -292,9 +301,9 @@ public function userJoinedPresenceChannel(ConnectionInterface $connection, stdCl
292301
* @param stdClass $user
293302
* @param string $channel
294303
* @param stdClass $payload
295-
* @return void
304+
* @return PromiseInterface[bool]
296305
*/
297-
public function userLeftPresenceChannel(ConnectionInterface $connection, stdClass $user, string $channel)
306+
public function userLeftPresenceChannel(ConnectionInterface $connection, stdClass $user, string $channel): PromiseInterface
298307
{
299308
unset($this->users["{$connection->app->id}:{$channel}"][$connection->socketId]);
300309

@@ -310,6 +319,8 @@ public function userLeftPresenceChannel(ConnectionInterface $connection, stdClas
310319
unset($this->userSockets["{$connection->app->id}:{$channel}:{$user->user_id}"]);
311320
}
312321
}
322+
323+
return Helpers::createFulfilledPromise(true);
313324
}
314325

315326
/**
@@ -327,7 +338,7 @@ public function getChannelMembers($appId, string $channel): PromiseInterface
327338
return json_decode($user);
328339
})->unique('user_id')->toArray();
329340

330-
return new FulfilledPromise($members);
341+
return Helpers::createFulfilledPromise($members);
331342
}
332343

333344
/**
@@ -341,7 +352,7 @@ public function getChannelMember(ConnectionInterface $connection, string $channe
341352
{
342353
$member = $this->users["{$connection->app->id}:{$channel}"][$connection->socketId] ?? null;
343354

344-
return new FulfilledPromise($member);
355+
return Helpers::createFulfilledPromise($member);
345356
}
346357

347358
/**
@@ -362,7 +373,7 @@ public function getChannelsMembersCount($appId, array $channelNames): PromiseInt
362373
return $results;
363374
}, []);
364375

365-
return new FulfilledPromise($results);
376+
return Helpers::createFulfilledPromise($results);
366377
}
367378

368379
/**
@@ -375,7 +386,7 @@ public function getChannelsMembersCount($appId, array $channelNames): PromiseInt
375386
*/
376387
public function getMemberSockets($userId, $appId, $channelName): PromiseInterface
377388
{
378-
return new FulfilledPromise(
389+
return Helpers::createFulfilledPromise(
379390
$this->userSockets["{$appId}:{$channelName}:{$userId}"] ?? []
380391
);
381392
}
@@ -384,21 +395,21 @@ public function getMemberSockets($userId, $appId, $channelName): PromiseInterfac
384395
* Keep tracking the connections availability when they pong.
385396
*
386397
* @param \Ratchet\ConnectionInterface $connection
387-
* @return bool
398+
* @return PromiseInterface[bool]
388399
*/
389-
public function connectionPonged(ConnectionInterface $connection): bool
400+
public function connectionPonged(ConnectionInterface $connection): PromiseInterface
390401
{
391-
return true;
402+
return Helpers::createFulfilledPromise(true);
392403
}
393404

394405
/**
395406
* Remove the obsolete connections that didn't ponged in a while.
396407
*
397-
* @return bool
408+
* @return PromiseInterface[bool]
398409
*/
399-
public function removeObsoleteConnections(): bool
410+
public function removeObsoleteConnections(): PromiseInterface
400411
{
401-
return true;
412+
return Helpers::createFulfilledPromise(true);
402413
}
403414

404415
/**

0 commit comments

Comments
 (0)