Skip to content

Commit 2dfe566

Browse files
authored
Merge pull request clue#62 from clue-labs/data
Remove uneeded data event for performance and consistency reasons
2 parents 6718cb4 + ebf8257 commit 2dfe566

File tree

5 files changed

+21
-39
lines changed

5 files changed

+21
-39
lines changed

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,6 @@ Incoming events and errors will be forwarded to registered event handler callbac
210210

211211
```php
212212
// global events:
213-
$client->on('data', function (MessageInterface $message) {
214-
// process an incoming message (raw message object)
215-
});
216213
$client->on('close', function () {
217214
// the connection to Redis just closed
218215
});

examples/cli.php

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
<?php
22

3-
use Clue\Redis\Protocol\Model\ErrorReply;
43
use Clue\React\Redis\Client;
54
use Clue\React\Redis\Factory;
6-
use Clue\Redis\Protocol\Model\ModelInterface;
5+
use React\Promise\PromiseInterface;
76

87
require __DIR__ . '/../vendor/autoload.php';
98

@@ -15,30 +14,33 @@
1514
$factory->createClient('localhost')->then(function (Client $client) use ($loop) {
1615
echo '# connected! Entering interactive mode, hit CTRL-D to quit' . PHP_EOL;
1716

18-
$client->on('data', function (ModelInterface $data) {
19-
if ($data instanceof ErrorReply) {
20-
echo '# error reply: ' . $data->getMessage() . PHP_EOL;
21-
} else {
22-
echo '# reply: ' . json_encode($data->getValueNative()) . PHP_EOL;
23-
}
24-
});
25-
2617
$loop->addReadStream(STDIN, function () use ($client, $loop) {
2718
$line = fgets(STDIN);
2819
if ($line === false || $line === '') {
2920
echo '# CTRL-D -> Ending connection...' . PHP_EOL;
30-
$client->end();
31-
} else {
32-
$line = rtrim($line);
21+
$loop->removeReadStream(STDIN);
22+
return $client->end();
23+
}
3324

34-
if ($line === '') {
25+
$line = rtrim($line);
26+
if ($line === '') {
27+
return;
28+
}
29+
30+
$params = explode(' ', $line);
31+
$method = array_shift($params);
32+
$promise = call_user_func_array(array($client, $method), $params);
3533

36-
} else {
37-
$params = explode(' ', $line);
38-
$method = array_shift($params);
39-
call_user_func_array(array($client, $method), $params);
40-
}
34+
// special method such as end() / close() called
35+
if (!$promise instanceof PromiseInterface) {
36+
return;
4137
}
38+
39+
$promise->then(function ($data) {
40+
echo '# reply: ' . json_encode($data) . PHP_EOL;
41+
}, function ($e) {
42+
echo '# error reply: ' . $e->getMessage() . PHP_EOL;
43+
});
4244
});
4345

4446
$client->on('close', function() use ($loop) {

src/Client.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44

55
use Evenement\EventEmitterInterface;
66
use React\Promise\PromiseInterface;
7-
use Clue\Redis\Protocol\Model\ModelInterface;
87

98
/**
109
* Simple interface for executing redis commands
1110
*
12-
* @event data(ModelInterface $messageModel)
1311
* @event error(Exception $error)
1412
* @event close()
1513
*

src/StreamingClient.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,6 @@ public function __call($name, $args)
124124

125125
public function handleMessage(ModelInterface $message)
126126
{
127-
$this->emit('data', array($message));
128-
129127
if ($this->monitoring && $this->isMonitorMessage($message)) {
130128
$this->emit('monitor', array($message));
131129
return;

tests/StreamingClientTest.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,8 @@ public function testReceiveParseErrorEmitsErrorEvent()
5656
$this->stream->emit('data', array('message'));
5757
}
5858

59-
public function testReceiveMessageEmitsEvent()
60-
{
61-
$this->client->on('data', $this->expectCallableOnce());
62-
63-
$this->parser->expects($this->once())->method('pushIncoming')->with($this->equalTo('message'))->will($this->returnValue(array(new IntegerReply(2))));
64-
$this->stream->emit('data', array('message'));
65-
}
66-
6759
public function testReceiveThrowMessageEmitsErrorEvent()
6860
{
69-
$this->client->on('data', $this->expectCallableOnce());
70-
$this->client->on('data', function() {
71-
throw new UnderflowException();
72-
});
73-
7461
$this->client->on('error', $this->expectCallableOnce());
7562

7663
$this->parser->expects($this->once())->method('pushIncoming')->with($this->equalTo('message'))->will($this->returnValue(array(new IntegerReply(2))));

0 commit comments

Comments
 (0)