From ebf825709e91acc337bc1130ec27d7866c09e9bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Tue, 19 Sep 2017 21:05:50 +0200 Subject: [PATCH] Remove uneeded data event for performance and consistency reasons --- README.md | 3 --- examples/cli.php | 40 ++++++++++++++++++----------------- src/Client.php | 2 -- src/StreamingClient.php | 2 -- tests/StreamingClientTest.php | 13 ------------ 5 files changed, 21 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index 62077d1..3a13e35 100644 --- a/README.md +++ b/README.md @@ -231,9 +231,6 @@ Incoming events and errors will be forwarded to registered event handler callbac ```php // global events: -$client->on('data', function (MessageInterface $message) { - // process an incoming message (raw message object) -}); $client->on('close', function () { // the connection to Redis just closed }); diff --git a/examples/cli.php b/examples/cli.php index cde81ea..a3a0a78 100644 --- a/examples/cli.php +++ b/examples/cli.php @@ -1,9 +1,8 @@ createClient()->then(function (Client $client) use ($loop) { echo '# connected! Entering interactive mode, hit CTRL-D to quit' . PHP_EOL; - $client->on('data', function (ModelInterface $data) { - if ($data instanceof ErrorReply) { - echo '# error reply: ' . $data->getMessage() . PHP_EOL; - } else { - echo '# reply: ' . json_encode($data->getValueNative()) . PHP_EOL; - } - }); - $loop->addReadStream(STDIN, function () use ($client, $loop) { $line = fgets(STDIN); if ($line === false || $line === '') { echo '# CTRL-D -> Ending connection...' . PHP_EOL; - $client->end(); - } else { - $line = rtrim($line); + $loop->removeReadStream(STDIN); + return $client->end(); + } - if ($line === '') { + $line = rtrim($line); + if ($line === '') { + return; + } + + $params = explode(' ', $line); + $method = array_shift($params); + $promise = call_user_func_array(array($client, $method), $params); - } else { - $params = explode(' ', $line); - $method = array_shift($params); - call_user_func_array(array($client, $method), $params); - } + // special method such as end() / close() called + if (!$promise instanceof PromiseInterface) { + return; } + + $promise->then(function ($data) { + echo '# reply: ' . json_encode($data) . PHP_EOL; + }, function ($e) { + echo '# error reply: ' . $e->getMessage() . PHP_EOL; + }); }); $client->on('close', function() use ($loop) { diff --git a/src/Client.php b/src/Client.php index 42eb591..d97b5d1 100644 --- a/src/Client.php +++ b/src/Client.php @@ -4,12 +4,10 @@ use Evenement\EventEmitterInterface; use React\Promise\PromiseInterface; -use Clue\Redis\Protocol\Model\ModelInterface; /** * Simple interface for executing redis commands * - * @event data(ModelInterface $messageModel) * @event error(Exception $error) * @event close() * diff --git a/src/StreamingClient.php b/src/StreamingClient.php index 69b870d..e72cc3f 100644 --- a/src/StreamingClient.php +++ b/src/StreamingClient.php @@ -124,8 +124,6 @@ public function __call($name, $args) public function handleMessage(ModelInterface $message) { - $this->emit('data', array($message)); - if ($this->monitoring && $this->isMonitorMessage($message)) { $this->emit('monitor', array($message)); return; diff --git a/tests/StreamingClientTest.php b/tests/StreamingClientTest.php index 6ac30db..528849c 100644 --- a/tests/StreamingClientTest.php +++ b/tests/StreamingClientTest.php @@ -56,21 +56,8 @@ public function testReceiveParseErrorEmitsErrorEvent() $this->stream->emit('data', array('message')); } - public function testReceiveMessageEmitsEvent() - { - $this->client->on('data', $this->expectCallableOnce()); - - $this->parser->expects($this->once())->method('pushIncoming')->with($this->equalTo('message'))->will($this->returnValue(array(new IntegerReply(2)))); - $this->stream->emit('data', array('message')); - } - public function testReceiveThrowMessageEmitsErrorEvent() { - $this->client->on('data', $this->expectCallableOnce()); - $this->client->on('data', function() { - throw new UnderflowException(); - }); - $this->client->on('error', $this->expectCallableOnce()); $this->parser->expects($this->once())->method('pushIncoming')->with($this->equalTo('message'))->will($this->returnValue(array(new IntegerReply(2))));