dl
Version:
DreamLab Libs
128 lines (99 loc) • 3.98 kB
JavaScript
var core = require('core');
var Assertions = core.common.Assertions;
var EventDispatcher = core.event.EventDispatcher;
var Event = core.event.Event;
var ErrorEvent = core.event.ErrorEvent;
var AbstractDataProvider = require('../dataprovider/AbstractDataProvider.js').AbstractDataProvider;
var ConfigurationManager = function (driver) {
EventDispatcher.call(this);
Assertions.isInstanceOf(driver, AbstractDataProvider, ConfigurationManager.Exception.WRONG_DRIVER);
this._ready = false;
this._driver = driver;
};
ConfigurationManager.prototype = Object.create(EventDispatcher.prototype);
ConfigurationManager.prototype.initialize = function () {
var that = this;
this._driver.init(function (error, data) {
if (error) {
console.log('ConfigurationManager/ready: error while initializing dataProvider');
that.dispatchEvent(new ErrorEvent(ConfigurationManager.Event.ERROR, error, -1, "Error while initializing dataProvider"));
return;
}
console.log('ConfigurationManager/ready: dispatching');
that._ready = true;
that.dispatchEvent(new Event(ConfigurationManager.Event.READY));
}, function (error, data) {
if (error) {
console.log('ConfigurationManager/reloaded: error while reloading dataProvider');
that.dispatchEvent(new ErrorEvent(ConfigurationManager.Event.ERROR, error, -1, "Error while reloading dataProvider"));
return;
}
console.log('ConfigurationManager/reloaded: dispatching');
that.dispatchEvent(new Event(ConfigurationManager.Event.RELOADED));
});
};
ConfigurationManager.prototype.isReady = function () {
return this._ready;
};
ConfigurationManager.prototype.destroy = function () {
this._driver.destroy();
};
ConfigurationManager.prototype.set = function (key, value, options, callback) {
if (!this._ready) {
callback("Not ready", null);
return;
}
this._driver.set(key, value, options, callback);
};
ConfigurationManager.prototype.get = function (key, callback) {
if (!this._ready) {
callback("Not ready", null);
return;
}
this._driver.get(key, callback);
};
ConfigurationManager.prototype.remove = function (key, options, callback) {
if (!this._ready) {
callback("Not ready", null);
return;
}
this._driver.remove(key, options, callback);
};
ConfigurationManager.prototype.watch = function (key, callback) {
if (!this._ready) {
callback("Not ready", null);
return;
}
this._driver.watch(key, callback);
};
ConfigurationManager.prototype.exists = function (key, callback) {
if (!this._ready) {
callback("Not ready", null);
return;
}
this._driver.exists(key, callback);
};
ConfigurationManager.prototype.sync = function (key, callback) {
if (!this._ready) {
callback("Not ready", null);
return;
}
this._driver.sync(key, callback);
};
ConfigurationManager.prototype.setConfig = function (value, callback) {
this.set(null, value, callback);
};
ConfigurationManager.prototype.getConfig = function (callback) {
this.get(null, callback);
};
ConfigurationManager.prototype.watchConfig = function (callback) {
this.watch(null, callback);
};
ConfigurationManager.Event = {};
ConfigurationManager.Event.READY = "ConfigurationManager_Event_READY";
ConfigurationManager.Event.CHANGED = "ConfigurationManager_Event_CHANGED";
ConfigurationManager.Event.RELOADED = "ConfigurationManager_Event_RELOADED";
ConfigurationManager.Event.ERROR = "ConfigurationManager_Event_ERROR";
ConfigurationManager.Exception = {};
ConfigurationManager.Exception.WRONG_DRIVER = "Given driver doesn't inherit from AbstractDataProvider";
exports.ConfigurationManager = ConfigurationManager;