infinite watch for ephemeral nodes or not-created nodes
Closed this issue · 4 comments
Hello, I had a need where I would want to watch for a path that whom I don't know if it's already created or not.
So I need a watch function that will handle this case.
I wrote one, basically It will give you errors when the node does not exists, has been deleted, or value is not JSON compliant.
But if it has a value, you wil be notified each time it changes.
It goes like this (Yes ugly right now):
function onupdate(zkpath, callback) {
var
path = require('path'),
parts = zkpath.split('/'),
parent = parts.slice(0, parts.length - 1).join('/') || '/',
child = parts[parts.length - 1];
zk.on('error', callback);
zk.watch(parent, {method: 'list', initialData: true}, function(err, listener) {
if (err !== null) {
listener.stop();
return callback(err);
} else {
listener.on('error', function(err) {
listener.stop();
callback(err)
});
listener.on('children', function (children) {
if (children.indexOf(child) !== -1) {
zk.watch(zkpath, {initialData: true}, function(err, listener) {
listener.on('error', function(err) {
listener.stop();
callback(err);
});
listener.on('data', function(data) {
callback(null, data);
});
});
} else {
callback(new Error('Children does not exists atm'));
}
});
}
});
}
Usage:
onupdate('/mynode', function(err, data) {
});
Do you think it would fit into ZKplus?
I find it very handy to just give a path and be notified each time it changes, without having to specify options, flags, etc..
Yes, actually a coworker had the same problem (and a related other one
involving "pre-watching" child nodes), so yeah I would definitely take a
higher-level API for this.
On Fri, Sep 28, 2012 at 8:29 AM, Vincent Voyer notifications@github.comwrote:
Hello, I had a need where I would want to watch for a path that whom I
don't know if it's already created or not.So I need a watch function that will handle this case.
I wrote one, basically It will give you errors when the node does not
exists, has been deleted, or value is not JSON compliant.But if it has a value, you wil be notified each time it changes.
It goes like this (Yes ugly right now):
function onupdate(zkpath, callback) {
var
path = require('path'),
parts = zkpath.split('/'),
parent = parts.slice(0, parts.length - 1).join('/') || '/',
child = parts[parts.length - 1];zk.on('error', callback);
zk.watch(parent, {method: 'list', initialData: true}, function(err, listener) {
if (err !== null) {
listener.stop();
return callback(err);
} else {
listener.on('error', function(err) {
listener.stop();
callback(err)
});
listener.on('children', function (children) {
if (children.indexOf(child) !== -1) {
zk.watch(zkpath, {initialData: true}, function(err, listener) {
listener.on('error', function(err) {
listener.stop();
callback(err);
});
listener.on('data', function(data) {
callback(null, data);
});
});
} else {
callback(new Error('Children does not exists atm'));
}
});
}
});}Do you think it would fit into ZKplus?
I find it very handy to just give a path and be notified each time it
changes, without having to specify options, flags, etc..—
Reply to this email directly or view it on GitHubhttps://github.com//issues/16.
Yep- thanks - I added some comments to your PR, but overall looks good.
Let me know what you think - really my only "meat" change would be to
rename pathExists
.
On Tue, Oct 9, 2012 at 1:53 AM, Vincent Voyer notifications@github.comwrote:
See #19 #19 which solves
both of you co workers needs I guess.—
Reply to this email directly or view it on GitHubhttps://github.com//issues/16#issuecomment-9253535.