Skip to content

Commit 4a8d679

Browse files
marcvdmMinishlink
authored andcommitted
Make flush really async and use promise wait accurately (#212)
* Fix async issue * Remove unused reference var
1 parent 367fc06 commit 4a8d679

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

src/WebPush.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Base64Url\Base64Url;
1717
use GuzzleHttp\Client;
1818
use GuzzleHttp\Exception\RequestException;
19+
use GuzzleHttp\Promise\Promise;
1920
use GuzzleHttp\Psr7\Request;
2021
use Psr\Http\Message\ResponseInterface;
2122

@@ -166,22 +167,23 @@ public function flush(?int $batchSize = null) : iterable
166167
// for each endpoint server type
167168
$requests = $this->prepare($batch);
168169

169-
foreach ($requests as $request) {
170-
$result = null;
170+
$promises = [];
171171

172-
$this->client->sendAsync($request)
173-
->then(function ($response) use ($request, &$result) {
172+
foreach ($requests as $request) {
173+
$promises[] = $this->client->sendAsync($request)
174+
->then(function ($response) use ($request) {
174175
/** @var ResponseInterface $response * */
175-
$result = new MessageSentReport($request, $response);
176+
return new MessageSentReport($request, $response);
176177
})
177-
->otherwise(function ($reason) use (&$result) {
178+
->otherwise(function ($reason) {
178179
/** @var RequestException $reason **/
179-
$result = new MessageSentReport($reason->getRequest(), $reason->getResponse(), false, $reason->getMessage());
180-
})
181-
->wait(false);
182-
183-
yield $result;
180+
return new MessageSentReport($reason->getRequest(), $reason->getResponse(), false, $reason->getMessage());
181+
});
184182
}
183+
184+
foreach ($promises as $promise) {
185+
yield $promise->wait();
186+
}
185187
}
186188

187189
if ($this->reuseVAPIDHeaders) {

0 commit comments

Comments
 (0)