|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace WyriHaximus\React\ObservableBunny; |
|
4
|
|
|
|
|
5
|
|
|
use Bunny\Async\Client; |
|
6
|
|
|
use Bunny\Channel; |
|
7
|
|
|
use Bunny\Message as BunnyMessage; |
|
8
|
|
|
use Bunny\Protocol\MethodBasicConsumeOkFrame; |
|
9
|
|
|
use React\EventLoop\LoopInterface; |
|
10
|
|
|
use React\EventLoop\Timer\TimerInterface; |
|
11
|
|
|
use React\Promise\PromiseInterface; |
|
12
|
|
|
use Rx\Subject\Subject; |
|
13
|
|
|
use Throwable; |
|
14
|
|
|
|
|
15
|
|
|
final class ObservableBunny |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var LoopInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
private $loop; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var Client |
|
24
|
|
|
*/ |
|
25
|
|
|
private $bunny; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param LoopInterface $loop |
|
29
|
1 |
|
* @param Client $bunny |
|
30
|
|
|
*/ |
|
31
|
1 |
|
public function __construct(LoopInterface $loop, Client $bunny) |
|
32
|
1 |
|
{ |
|
33
|
1 |
|
$this->loop = $loop; |
|
34
|
|
|
$this->bunny = $bunny; |
|
35
|
1 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function consume( |
|
38
|
|
|
string $queue = '', |
|
39
|
|
|
string $consumerTag = '', |
|
40
|
|
|
bool $noLocal = false, |
|
41
|
|
|
bool $noAck = false, |
|
42
|
|
|
bool $exclusive = false, |
|
43
|
|
|
bool $nowait = false, |
|
44
|
1 |
|
array $arguments = [] |
|
45
|
1 |
|
): Subject { |
|
46
|
|
|
$subject = new Subject(); |
|
47
|
1 |
|
$consumeArgs = [$queue, $consumerTag, $noLocal, $noAck, $exclusive, $nowait, $arguments]; |
|
48
|
1 |
|
|
|
49
|
|
|
$channel = $this->bunny->channel(); |
|
50
|
1 |
|
$channel->then(function (Channel $channel) use ($subject, $consumeArgs) { |
|
|
|
|
|
|
51
|
1 |
|
/** @var string $consumerTag */ |
|
52
|
1 |
|
$consumerTag = null; |
|
53
|
|
|
$timer = $this->loop->addPeriodicTimer(1, function () use ($channel, $subject, &$timer, &$consumerTag) { |
|
54
|
|
|
if (!$subject->isDisposed()) { |
|
55
|
|
|
return; |
|
56
|
1 |
|
} |
|
57
|
1 |
|
|
|
58
|
1 |
|
$this->cancelSubscription( |
|
|
|
|
|
|
59
|
1 |
|
$timer, |
|
60
|
1 |
|
$channel, |
|
61
|
1 |
|
$consumerTag |
|
62
|
1 |
|
)->done([$subject, 'onComplete'], $this->onError($subject, $timer)); |
|
63
|
1 |
|
}); |
|
64
|
1 |
|
$channel->consume( |
|
|
|
|
|
|
65
|
|
|
function (BunnyMessage $message, Channel $channel) use ($subject, &$timer, &$consumerTag) { |
|
66
|
|
|
if ($subject->isDisposed()) { |
|
67
|
|
|
$channel->nack($message); |
|
68
|
|
|
$this->cancelSubscription( |
|
|
|
|
|
|
69
|
|
|
$timer, |
|
70
|
|
|
$channel, |
|
71
|
|
|
$consumerTag |
|
72
|
|
|
)->done([$subject, 'onComplete'], $this->onError($subject, $timer)); |
|
73
|
|
|
|
|
74
|
|
|
return; |
|
75
|
1 |
|
} |
|
76
|
1 |
|
|
|
77
|
1 |
|
$subject->onNext(new Message($message, $channel)); |
|
78
|
1 |
|
}, |
|
79
|
1 |
|
...$consumeArgs |
|
|
|
|
|
|
80
|
1 |
|
)->then(function (MethodBasicConsumeOkFrame $response) use (&$consumerTag) { |
|
81
|
1 |
|
$consumerTag = $response->consumerTag; |
|
82
|
|
|
})->done(null, $this->onError($subject, $timer)); |
|
83
|
1 |
|
})->done(null, [$subject, 'onError']); |
|
84
|
|
|
|
|
85
|
|
|
return $subject; |
|
86
|
1 |
|
} |
|
87
|
|
|
|
|
88
|
1 |
|
private function cancelSubscription(TimerInterface $timer, Channel $channel, string $consumerTag): PromiseInterface |
|
89
|
|
|
{ |
|
90
|
1 |
|
$this->loop->cancelTimer($timer); |
|
91
|
1 |
|
|
|
92
|
1 |
|
return $channel->cancel($consumerTag)->then(function () use ($channel) { |
|
|
|
|
|
|
93
|
|
|
return $channel->close(); |
|
94
|
|
|
}); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
private function onError(Subject $subject, TimerInterface $timer): callable |
|
98
|
|
|
{ |
|
99
|
|
|
return function (Throwable $et) use ($subject, $timer) { |
|
100
|
|
|
if ($this->loop->isTimerActive($timer)) { |
|
101
|
|
|
$this->loop->cancelTimer($timer); |
|
102
|
|
|
} |
|
103
|
|
|
$subject->onError($et); |
|
104
|
|
|
}; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: