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

Promise v3 #1157

Merged
merged 22 commits into from
Nov 20, 2024
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
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"trafficcophp/bytebuffer": "^0.3",
"monolog/monolog": "^2.1.1 || ^3.0",
"react/event-loop": "^1.2",
"react/promise": "^2.2",
"ext-json": "*",
"ext-zlib": "*",
"discord-php/http": "^10.1.7",
Expand All @@ -38,7 +37,7 @@
"friendsofphp/php-cs-fixer": "^3",
"phpunit/phpunit": "^9.4.4",
"davidcole1340/reactsh": "dev-master",
"wyrihaximus/react-cache-redis": "^3.0 || >=4.0 <4.4",
"wyrihaximus/react-cache-redis": "^4.5",
"symfony/cache": "^5.4"
},
"autoload": {
Expand Down
45 changes: 21 additions & 24 deletions src/Discord/Discord.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Discord\Factory\Factory;
use Discord\Helpers\BigInt;
use Discord\Helpers\CacheConfig;
use Discord\Helpers\Deferred;
use Discord\Helpers\RegisteredCommand;
use Discord\Http\Drivers\React;
use Discord\Http\Endpoint;
Expand Down Expand Up @@ -51,7 +50,8 @@
use React\EventLoop\Loop;
use React\EventLoop\LoopInterface;
use React\EventLoop\TimerInterface;
use React\Promise\ExtendedPromiseInterface;
use React\Promise\Deferred;
use React\Promise\PromiseInterface;
use React\Socket\Connector as SocketConnector;
use Symfony\Component\OptionsResolver\OptionsResolver;

Expand Down Expand Up @@ -468,10 +468,10 @@ protected function handleReady(object $data)
$unavailable = [];

foreach ($content->guilds as $guild) {
/** @var ExtendedPromiseInterface */
/** @var PromiseInterface */
$promise = coroutine([$event, 'handle'], $guild);

$promise->done(function ($d) use (&$unavailable) {
$promise->then(function ($d) use (&$unavailable) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All Instances of $promise->then need to take parameters (callable $onFulfilled, callable $onRejected); Let's get these functions either defined as $variable = function()s or make some generic handlers we can throw in that will make relevant calls to Monolog

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per the release notes; It is no longer possible to resolve a promise without a value (use null instead) or reject a promise without a reason (use Throwable instead).

if (! empty($d->unavailable)) {
$unavailable[$d->id] = $d->unavailable;
}
Expand All @@ -497,7 +497,7 @@ protected function handleReady(object $data)
unset($unavailable[$guild->id]);
}
if (count($unavailable) < 1) {
$guildLoad->resolve();
$guildLoad->resolve(null);
}
};
$this->on(Event::GUILD_CREATE, $onGuildCreate);
Expand All @@ -512,18 +512,18 @@ protected function handleReady(object $data)
unset($unavailable[$guild->id]);
}
if (count($unavailable) < 1) {
$guildLoad->resolve();
$guildLoad->resolve(null);
}
};
$this->on(Event::GUILD_DELETE, $onGuildDelete);

$guildLoad->promise()->always(function () use ($onGuildCreate, $onGuildDelete) {
$guildLoad->promise()->finally(function () use ($onGuildCreate, $onGuildDelete) {
$this->removeListener(Event::GUILD_CREATE, $onGuildCreate);
$this->removeListener(Event::GUILD_DELETE, $onGuildDelete);
$this->logger->info('all guilds are now available', ['count' => $this->guilds->count()]);

$this->setupChunking();
})->done();
});
}

/**
Expand Down Expand Up @@ -775,7 +775,7 @@ protected function handleDispatch(object $data): void
$handler = new $hData['class']($this);

$deferred = new Deferred();
$deferred->promise()->done(function ($d) use ($data, $hData) {
$deferred->promise()->then(function ($d) use ($data, $hData) {
if (is_array($d) && count($d) == 2) {
list($new, $old) = $d;
} else {
Expand Down Expand Up @@ -809,14 +809,14 @@ protected function handleDispatch(object $data): void

if (! $this->emittedInit && (! in_array($data->t, $parse))) {
$this->unparsedPackets[] = function () use (&$handler, &$deferred, &$data) {
/** @var ExtendedPromiseInterface */
/** @var PromiseInterface */
$promise = coroutine([$handler, 'handle'], $data->d);
$promise->done([$deferred, 'resolve'], [$deferred, 'reject']);
$promise->then([$deferred, 'resolve'], [$deferred, 'reject']);
};
} else {
/** @var ExtendedPromiseInterface */
/** @var PromiseInterface */
$promise = coroutine([$handler, 'handle'], $data->d);
$promise->done([$deferred, 'resolve'], [$deferred, 'reject']);
$promise->then([$deferred, 'resolve'], [$deferred, 'reject']);
}
}

Expand Down Expand Up @@ -1079,7 +1079,7 @@ protected function setupHeartbeat(int $interval): void
*/
protected function connectWs(): void
{
$this->setGateway()->done(function ($gateway) {
$this->setGateway()->then(function ($gateway) {
if (isset($gateway['session']) && $session = $gateway['session']) {
if ($session['remaining'] < 2) {
$this->logger->error('exceeded number of reconnects allowed, waiting before attempting reconnect', $session);
Expand All @@ -1093,12 +1093,9 @@ protected function connectWs(): void

$this->logger->info('starting connection to websocket', ['gateway' => $this->gateway]);

/** @var ExtendedPromiseInterface */
/** @var PromiseInterface */
$promise = ($this->wsFactory)($this->gateway);
$promise->done(
[$this, 'handleWsConnection'],
[$this, 'handleWsConnectionFailed']
);
$promise->then([$this, 'handleWsConnection'], [$this, 'handleWsConnectionFailed']);
});
}

Expand Down Expand Up @@ -1233,9 +1230,9 @@ public function getVoiceClient(string $guild_id): ?VoiceClient
* @since 10.0.0 Removed argument $check that has no effect (it is always checked)
* @since 4.0.0
*
* @return ExtendedPromiseInterface<VoiceClient>
* @return PromiseInterface<VoiceClient>
*/
public function joinVoiceChannel(Channel $channel, $mute = false, $deaf = true, ?LoggerInterface $logger = null): ExtendedPromiseInterface
public function joinVoiceChannel(Channel $channel, $mute = false, $deaf = true, ?LoggerInterface $logger = null, bool $check = true): PromiseInterface
{
$deferred = new Deferred();

Expand Down Expand Up @@ -1329,9 +1326,9 @@ public function joinVoiceChannel(Channel $channel, $mute = false, $deaf = true,
*
* @param string|null $gateway Gateway URL to set.
*
* @return ExtendedPromiseInterface<array>
* @return PromiseInterface
*/
protected function setGateway(?string $gateway = null): ExtendedPromiseInterface
protected function setGateway(?string $gateway = null): PromiseInterface
{
$deferred = new Deferred();
$defaultSession = [
Expand Down Expand Up @@ -1359,7 +1356,7 @@ protected function setGateway(?string $gateway = null): ExtendedPromiseInterface
};

if (null === $gateway) {
$this->http->get(Endpoint::GATEWAY_BOT)->done(function ($response) use ($buildParams) {
$this->http->get(Endpoint::GATEWAY_BOT)->then(function ($response) use ($buildParams) {
if ($response->shards > 1) {
$this->logger->info('Please contact the DiscordPHP devs at https://discord.gg/dphp or https://github.com/discord-php/DiscordPHP/issues if you are interested in assisting us with sharding support development.');
}
Expand Down
15 changes: 8 additions & 7 deletions src/Discord/Helpers/Buffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
use Discord\Exceptions\BufferTimedOutException;
use Evenement\EventEmitter;
use React\EventLoop\LoopInterface;
use React\Promise\ExtendedPromiseInterface;
use React\Promise\Deferred;
use React\Promise\PromiseInterface;
use React\Stream\WritableStreamInterface;

/**
Expand Down Expand Up @@ -106,11 +107,11 @@ private function readRaw(int $length)
* @param null|string $format Format to read the bytes in. See `pack()`.
* @param int $timeout Time in milliseconds before the read times out.
*
* @return ExtendedPromiseInterface<mixed, \RuntimeException>
* @return PromiseInterface<mixed, \RuntimeException>
*
* @throws \RuntimeException When there is an error unpacking the read bytes.
*/
public function read(int $length, ?string $format = null, ?int $timeout = -1): ExtendedPromiseInterface
public function read(int $length, ?string $format = null, ?int $timeout = -1): PromiseInterface
{
$deferred = new Deferred();

Expand Down Expand Up @@ -152,11 +153,11 @@ public function read(int $length, ?string $format = null, ?int $timeout = -1): E
*
* @param int $timeout Time in milliseconds before the read times out.
*
* @return ExtendedPromiseInterface<int, \RuntimeException>
* @return PromiseInterface<int, \RuntimeException>
*
* @throws \RuntimeException When there is an error unpacking the read bytes.
*/
public function readInt32(int $timeout = -1): ExtendedPromiseInterface
public function readInt32(int $timeout = -1): PromiseInterface
{
return $this->read(4, 'l', $timeout);
}
Expand All @@ -166,11 +167,11 @@ public function readInt32(int $timeout = -1): ExtendedPromiseInterface
*
* @param int $timeout Time in milliseconds before the read times out.
*
* @return ExtendedPromiseInterface<int, \RuntimeException>
* @return PromiseInterface<int, \RuntimeException>
*
* @throws \RuntimeException When there is an error unpacking the read bytes.
*/
public function readInt16(int $timeout = -1): ExtendedPromiseInterface
public function readInt16(int $timeout = -1): PromiseInterface
{
return $this->read(2, 'v', $timeout);
}
Expand Down
28 changes: 0 additions & 28 deletions src/Discord/Helpers/Deferred.php

This file was deleted.

30 changes: 0 additions & 30 deletions src/Discord/Helpers/ExtendedPromisorInterface.php

This file was deleted.

Loading