node-etcd
Version:
etcd library for node.js (etcd v2 api)
118 lines (102 loc) • 3.95 kB
JavaScript
// Generated by CoffeeScript 1.12.7
var EventEmitter, Watcher, exports,
bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
hasProp = {}.hasOwnProperty;
EventEmitter = require('events').EventEmitter;
Watcher = (function(superClass) {
extend(Watcher, superClass);
function Watcher(etcd, key, index, options) {
this.etcd = etcd;
this.key = key;
this.index = index != null ? index : null;
this.options = options != null ? options : {};
this._retry = bind(this._retry, this);
this._respHandler = bind(this._respHandler, this);
this._resync = bind(this._resync, this);
this._unexpectedData = bind(this._unexpectedData, this);
this._valueChanged = bind(this._valueChanged, this);
this._missingValue = bind(this._missingValue, this);
this._error = bind(this._error, this);
this._watch = bind(this._watch, this);
this.stop = bind(this.stop, this);
this.stopped = false;
this.retryAttempts = 0;
this._watch();
}
Watcher.prototype.stop = function() {
this.stopped = true;
this.request.abort();
return this.emit('stop', "Watcher for '" + this.key + "' aborted.");
};
Watcher.prototype._watch = function() {
if (this.index === null) {
return this.request = this.etcd.watch(this.key, this.options, this._respHandler);
} else {
return this.request = this.etcd.watchIndex(this.key, this.index, this.options, this._respHandler);
}
};
Watcher.prototype._error = function(err) {
var error;
error = new Error('Connection error, reconnecting.');
error.error = err;
error.reconnectCount = this.retryAttempts;
this.emit('reconnect', error);
return this._retry();
};
Watcher.prototype._missingValue = function(headers) {
var error;
error = new Error('Etcd timed out watcher, reconnecting.');
error.headers = headers;
this.retryAttempts = 0;
this.emit('reconnect', error);
return this._watch();
};
Watcher.prototype._valueChanged = function(val, headers) {
this.retryAttempts = 0;
this.index = val.node.modifiedIndex + 1;
this.emit('change', val, headers);
if (val.action != null) {
this.emit(val.action, val, headers);
}
return this._watch();
};
Watcher.prototype._unexpectedData = function(val, headers) {
var error;
error = new Error('Received unexpected response');
error.response = val;
this.emit('error', error);
return this._retry();
};
Watcher.prototype._resync = function(err) {
this.index = err.error.index;
this.retryAttempts = 0;
this.emit('resync', err);
return this._watch();
};
Watcher.prototype._respHandler = function(err, val, headers) {
var ref, ref1;
if (this.stopped) {
return;
}
if ((err != null ? err.errorCode : void 0) === 401 && (((ref = err.error) != null ? ref.index : void 0) != null)) {
return this._resync(err);
} else if (err) {
return this._error(err);
} else if (((headers != null ? headers['x-etcd-index'] : void 0) != null) && (val == null)) {
return this._missingValue(headers);
} else if ((val != null ? (ref1 = val.node) != null ? ref1.modifiedIndex : void 0 : void 0) != null) {
return this._valueChanged(val, headers);
} else {
return this._unexpectedData(val, headers);
}
};
Watcher.prototype._retry = function() {
var timeout;
timeout = (Math.pow(2, this.retryAttempts) * 300) + (Math.round(Math.random() * 1000));
setTimeout(this._watch, timeout);
return this.retryAttempts++;
};
return Watcher;
})(EventEmitter);
exports = module.exports = Watcher;