Skip to content

Commit 8912e90

Browse files
committed
Merge pull request clue#11 from clue/prepare
Refactor / prepare to support dedicated pattern APIs
2 parents 317a675 + 15e1a04 commit 8912e90

File tree

3 files changed

+19
-38
lines changed

3 files changed

+19
-38
lines changed

src/Client.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
use Clue\Redis\Protocol\Model\ErrorReplyException;
1010
use Clue\Redis\Protocol\Serializer\SerializerInterface;
1111
use Clue\Redis\Protocol\Factory as ProtocolFactory;
12-
use Clue\React\Redis\Request;
1312
use UnderflowException;
1413
use RuntimeException;
14+
use React\Promise\Deferred;
15+
use Clue\Redis\Protocol\Model\ErrorReply;
16+
use Clue\Redis\Protocol\Model\ModelInterface;
1517

1618
class Client extends EventEmitter
1719
{
@@ -46,7 +48,7 @@ public function __construct(Stream $stream, ParserInterface $parser = null, Seri
4648

4749
foreach ($models as $data) {
4850
try {
49-
$that->handleReply($data);
51+
$that->handleMessage($data);
5052
}
5153
catch (UnderflowException $error) {
5254
$that->emit('error', array($error));
@@ -67,36 +69,34 @@ public function __construct(Stream $stream, ParserInterface $parser = null, Seri
6769

6870
public function __call($name, $args)
6971
{
70-
if ($this->ending) {
71-
$e = new RuntimeException('Connection closed');
72+
$request = new Deferred();
7273

73-
if (class_exists('React\Promise\When')) {
74-
return \React\Promise\When::reject($e);
75-
} else {
76-
return \React\Promise\reject($e);
77-
}
74+
if ($this->ending) {
75+
$request->reject(new RuntimeException('Connection closed'));
76+
} else {
77+
$this->stream->write($this->serializer->getRequestMessage($name, $args));
78+
$this->requests []= $request;
7879
}
7980

80-
$this->stream->write($this->serializer->getRequestMessage($name, $args));
81-
82-
$request = new Request($name);
83-
$this->requests []= $request;
84-
8581
return $request->promise();
8682
}
8783

88-
public function handleReply($data)
84+
public function handleMessage(ModelInterface $message)
8985
{
90-
$this->emit('message', array($data, $this));
86+
$this->emit('message', array($message, $this));
9187

9288
if (!$this->requests) {
9389
throw new UnderflowException('Unexpected reply received, no matching request found');
9490
}
9591

9692
$request = array_shift($this->requests);
97-
/* @var $request Request */
93+
/* @var $request Deferred */
9894

99-
$request->handleReply($data);
95+
if ($message instanceof ErrorReply) {
96+
$request->reject($message);
97+
} else {
98+
$request->resolve($message->getValueNative());
99+
}
100100

101101
if ($this->ending && !$this->isBusy()) {
102102
$this->close();

src/Request.php

Lines changed: 0 additions & 19 deletions
This file was deleted.

tests/ClientTest.php renamed to tests/FunctionalTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
use Clue\React\Redis\Client;
1010

11-
class ClientTest extends TestCase
11+
class FunctionalTest extends TestCase
1212
{
1313
protected static $loop;
1414
protected static $factory;

0 commit comments

Comments
 (0)