UNPKG

dl

Version:

DreamLab Libs

101 lines (75 loc) 3.38 kB
var path = require('path'); var zookeeper = require('zookeeper'); var ConfigServiceClient = require('./ConfigServiceClient').ConfigServiceClient; var ConfigServiceClientExtended = function(application, segment) { // nie ma jak probowac dziedziczenie po multitonie :( this._client = ConfigServiceClient.getInstance(application, segment); this._client._watchDefaultKey = false; }; ConfigServiceClientExtended.prototype.watch = function (key, callback) { console.log('ConfigServiceClientExtended/watch', this._client._getKey(key)); this._client._dataProvider.watch(this._client._getKey(key), callback); }; ConfigServiceClientExtended.prototype.exists = function (key, watch, callback) { console.log('ConfigServiceClientExtended/exists', key); this._client._dataProvider.exists(this._client._getKey(key), callback); }; ConfigServiceClientExtended.prototype.create = function (key, value, ephemeral, sequence, makepath, callback) { if (key == '/') { key = ''; } console.log('ConfigServiceClientExtended/create', this._client._getKey(key), 'ephemeral:', ephemeral, 'sequence:', sequence, 'makepath:', makepath); var options = { flags: 0 }; if (ephemeral) { options.flags |= zookeeper.ZOO_EPHEMERAL; } if (sequence) { options.flags |= zookeeper.ZOO_SEQUENCE; } var that = this; this._client._dataProvider.set(this._client._getKey(key), value, options, function(rc, stat) { if (rc == zookeeper.ZNONODE && makepath) { var parentKey = path.dirname(key); if (parentKey == '.' || parentKey === '') { console.error('Cannot make path, hm?'); return callback(rc, stat); } console.log('no node && makepath. create', that._client._getKey(parentKey), 'and retry'); var cb = that.create.bind(that, key, value, ephemeral, sequence, false, callback); that.create(parentKey, null, false, false, true, cb); } else { return callback(rc, stat); } }); }; ConfigServiceClientExtended.prototype.set = function (key, value, version, callback) { if (key == '/') { key = ''; } console.log('ConfigServiceClientExtended/set', this._client._getKey(key), 'version:', version); var options = {}; if (version) { options.version = version; } this._client._dataProvider.set(this._client._getKey(key), value, options, callback); }; ConfigServiceClientExtended.prototype.delete = function (key, callback) { console.log('ConfigServiceClientExtended/delete', this._client._getKey(key)); this._client._dataProvider.remove(this._client._getKey(key), {}, callback); }; ConfigServiceClientExtended.prototype.get = function (key, watch, callback) { console.log('ConfigServiceClientExtended/get', this._client._getKey(key)); if (watch) { this.watch(key, callback); } else { this._client._dataProvider.get(this._client._getKey(key), callback); } }; ConfigServiceClientExtended.ERROR = {}; ConfigServiceClientExtended.ERROR.NO_NODE = zookeeper.ZNONODE; ConfigServiceClientExtended.ERROR.BAD_VERSION = zookeeper.ZBADVERSION; ConfigServiceClientExtended.ERROR.NODE_EXISTS = zookeeper.ZNODEEXISTS; exports.ConfigServiceClientExtended = ConfigServiceClientExtended;