Skip to content

Fix BUG #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions examples/Zookeeper_Example.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,46 +155,51 @@ public function deleteNode($path)
return $this->zookeeper->delete($path);
}
}
/**
/**
* Wath a given path
* @param string $path the path to node
* @param callable $callback callback function
* @return string|null
*/
public function watch($path, $callback)
{
public function watch($path, $callback){
if (!is_callable($callback)) {
return null;
}


$ret = null;

if ($this->zookeeper->exists($path)) {
if (!isset($this->callback[$path])) {
$this->callback[$path] = array();
$ret = $this->zookeeper->get($path, array($this, 'watchCallback'));
} else {
$ret = $this->get($path);
}
if (!in_array($callback, $this->callback[$path])) {
$this->callback[$path][] = $callback;
return $this->zookeeper->get($path, array($this, 'watchCallback'));
}
}

return $ret;
}

/**
* Wath event callback warper
* @param int $event_type
* @param int $stat
* @param string $path
* @return the return of the callback or null
*/
public function watchCallback($event_type, $stat, $path)
{
public function watchCallback($event_type, $stat, $path) {
if (!isset($this->callback[$path])) {
return null;
}


$ret = $this->zookeeper->get($path, array($this, 'watchCallback'));

foreach ($this->callback[$path] as $callback) {
$this->zookeeper->get($path, array($this, 'watchCallback'));
return call_user_func($callback);
call_user_func($callback, $ret);
}
}

Expand Down Expand Up @@ -239,11 +244,15 @@ public function cancelWatch($path, $callback = null)
var_dump($zk->getChildren('/foo'));

//watch example
function callback() {
echo "in watch callback\n";
function callback($data) {
echo "in watch callback {$data}\n";
}
function callback2($data) {
echo "in watch callback2 {$data}\n";
}
$zk->set('/bar', 1);
$ret = $zk->watch('/bar', 'callback');
$ret2 = $zk->watch('/bar', 'callback2');
$zk->set('/bar', 2);
while (true) {
sleep(1);
Expand Down