From 22446eb871541004f839470e394e5f069e710986 Mon Sep 17 00:00:00 2001 From: Dustin Deus Date: Sun, 4 Dec 2016 23:38:22 +0100 Subject: [PATCH] correct getter, prefix private variable with underscore --- lib/index.js | 114 +++++++++++++++++++++++++++++++++++++------------- lib/logger.js | 12 +++--- 2 files changed, 91 insertions(+), 35 deletions(-) diff --git a/lib/index.js b/lib/index.js index 977cb860c..c21b59f42 100644 --- a/lib/index.js +++ b/lib/index.js @@ -45,25 +45,81 @@ class Hemera extends EventEmitter { constructor(transport, params) { - super() - - this.catalog = Bloomrun() - this.config = Hoek.applyToDefaults(defaultConfig, params || {}) - this.transport = transport - this.events = new Kilt([this, this.transport]) - this.topics = {} - this.plugins = {} - //Special variables for act and add - this.context$ = {} - this.meta$ = {} - this.plugin$ = {} - - this.log = this.config.logger || new DefaultLogger({ - level: this.config.logLevel - }) + super() + + this._config = Hoek.applyToDefaults(defaultConfig, params || {}) + this._catalog = Bloomrun() + this._transport = transport + this._events = new Kilt([this, this.transport]) + this._topics = {} + this._plugins = {} + + //Special variables for act and add + this.context$ = {} + this.meta$ = {} + this.plugin$ = {} + + this.log = this._config.logger || new DefaultLogger({ + level: this._config.logLevel + }) + + this.payloadValidator = this._config.payloadValidator || DefaultPayloadValidator + + } + /** + * + * + * @readonly + * + * @memberOf Hemera + */ + get plugins() { + + return this._plugins; + } + /** + * + * + * @readonly + * + * @memberOf Hemera + */ + get catalog() { + + return this._catalog; + } + /** + * + * + * @readonly + * + * @memberOf Hemera + */ + get transport() { - this.payloadValidator = this.config.payloadValidator || DefaultPayloadValidator + return this._transport; + } + /** + * + * + * @readonly + * + * @memberOf Hemera + */ + get topics() { + + return this._topics; + } + /** + * + * + * @returns + * + * @memberOf Hemera + */ + get events() { + return this._events; } /** * @@ -74,7 +130,7 @@ class Hemera extends EventEmitter { */ use(params) { - if (this.plugins[params.attributes.name]) { + if (this._plugins[params.attributes.name]) { let error = new Errors.HemeraError(Constants.PLUGIN_ALREADY_IN_USE, { plugin: params.attributes.name }) @@ -87,7 +143,7 @@ class Hemera extends EventEmitter { params.plugin.call(ctx, params.options) this.log.info(params.attributes.name, Constants.PLUGIN_ADDED) - this.plugins[params.attributes.name] = ctx.plugin$ + this._plugins[params.attributes.name] = ctx.plugin$ } /** @@ -193,7 +249,7 @@ class Hemera extends EventEmitter { //Avoid duplicate subscribers of the emit stream //We use one subscriber per topic - if (self.topics[topic]) { + if (self._topics[topic]) { return } @@ -221,7 +277,7 @@ class Hemera extends EventEmitter { } let pattern = result.value.pattern - let actMeta = this.catalog.lookup(pattern) + let actMeta = this._catalog.lookup(pattern) //Check if a handler is registered with this pattern if (actMeta) { @@ -274,7 +330,7 @@ class Hemera extends EventEmitter { this.send(replyTo, ctx.reply(error), () => { //let it crash - if (this.config.crashOnFatal) { + if (this._config.crashOnFatal) { this.fatal() } @@ -298,7 +354,7 @@ class Hemera extends EventEmitter { }) - self.topics[topic] = true + self._topics[topic] = true } /** @@ -351,7 +407,7 @@ class Hemera extends EventEmitter { action: cb } - let handler = this.catalog.lookup(origPattern) + let handler = this._catalog.lookup(origPattern) //Check if pattern is already registered if (handler) { @@ -364,7 +420,7 @@ class Hemera extends EventEmitter { } //Add to bloomrun - this.catalog.add(origPattern, actMeta) + this._catalog.add(origPattern, actMeta) this.log.info(origPattern, Constants.ADD_ADDED) @@ -469,7 +525,7 @@ class Hemera extends EventEmitter { this.log.fatal(error) //Let it crash - if (this.config.crashOnFatal) { + if (this._config.crashOnFatal) { this.fatal() } @@ -493,7 +549,7 @@ class Hemera extends EventEmitter { handleTimeout(sid, pattern, cb) { //Handle timeout - this.timeout(sid, pattern.timeout$ || this.config.timeout, 1, () => { + this.timeout(sid, pattern.timeout$ || this._config.timeout, 1, () => { let error = new Errors.TimeoutError(Constants.ACT_TIMEOUT_ERROR, { pattern @@ -513,7 +569,7 @@ class Hemera extends EventEmitter { this.log.fatal(error) //Let it crash - if (this.config.crashOnFatal) { + if (this._config.crashOnFatal) { this.fatal() } @@ -554,7 +610,7 @@ class Hemera extends EventEmitter { */ list(params) { - return this.catalog.list(params) + return this._catalog.list(params) } /** diff --git a/lib/logger.js b/lib/logger.js index 2ea11b5e3..67b0cf5ad 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -40,21 +40,21 @@ class Logger { let self = this - self.config = Hoek.applyToDefaults(defaultConfig, params || {}) + self._config = Hoek.applyToDefaults(defaultConfig, params || {}) //Leads to too much listeners in tests - if (this.config.level !== 'silent') { + if (this._config.level !== 'silent') { Pretty.pipe(process.stdout) } - this.logger = Pino({ + this._logger = Pino({ name: 'app', safe: true, - level: this.config.level + level: this._config.level }, Pretty) //Set levels, create new prototype methods - self.config.levels.forEach((level) => { + self._config.levels.forEach((level) => { self[level] = function (msg) { @@ -72,7 +72,7 @@ class Logger { */ log() { - this.logger[arguments[0]].apply(this.logger, Array.prototype.slice.call(arguments).slice(1)) + this._logger[arguments[0]].apply(this._logger, Array.prototype.slice.call(arguments).slice(1)) } }