From 65c0336d2f14ceb77c3a7153c319ef49537b40e1 Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Sat, 4 Jun 2011 18:23:10 +0200 Subject: [PATCH] Use the shared initializer to initialize Pub/Sub and accept callbacks. When a callback is specified, the client does not return a new instance of Predis\PubSubContext but it invokes that callback on each message received. The context can be aborted simply by returning FALSE from the callback. --- lib/Predis/Client.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/Predis/Client.php b/lib/Predis/Client.php index 20fc88ca..792ae0d3 100644 --- a/lib/Predis/Client.php +++ b/lib/Predis/Client.php @@ -201,8 +201,20 @@ class Client { return isset($block) ? $transaction->execute($block) : $transaction; } - public function pubSubContext(Array $options = null) { - return new PubSubContext($this, $options); + public function pubSubContext(/* arguments */) { + return $this->sharedInitializer(func_get_args(), 'initPubSub'); + } + + protected function initPubSub(Array $options = null, $block = null) { + $pubsub = new PubSubContext($this, $options); + if (!isset($block)) { + return $pubsub; + } + foreach ($pubsub as $message) { + if ($block($pubsub, $message) === false) { + $pubsub->closeContext(); + } + } } public function monitor() {