From 12c5b81345356bc7e9c4002135d4a3ddec156a9d Mon Sep 17 00:00:00 2001 From: David Banham Date: Wed, 15 Mar 2017 11:33:19 +1100 Subject: [PATCH] Clean up setOptions API This moves the concern of whether or not to emit an event to be internal to setOptions. No need for the calling functions to make that decision. --- lib/application.js | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/lib/application.js b/lib/application.js index 2fdd97b0..3b4c5249 100644 --- a/lib/application.js +++ b/lib/application.js @@ -559,7 +559,7 @@ var RequireAuthorizationApplication = Application.extend({ accessSecret: results.oauth_token_secret, sessionHandle: results.oauth_session_handle, tokenExpiry: exp.toString() - }, false); + }); resolve({ results: results }); } callback && callback.apply(callback, arguments); @@ -581,7 +581,7 @@ var RequireAuthorizationApplication = Application.extend({ accessSecret: results.oauth_token_secret, sessionHandle: results.oauth_session_handle, tokenExpiry: exp.toString() - }, true); + }); resolve({ results: results }); } @@ -593,18 +593,23 @@ var RequireAuthorizationApplication = Application.extend({ var q = Object.assign({}, { oauth_token: requestToken }, other); return this.options.baseUrl + this.options.authorizeUrl + '?' + querystring.stringify(q); }, - setOptions: function(options, emitThisEvent) { + setOptions: function(options) { logger.debug("Setting options"); - this.options = Object.assign(this.options, options); - if (emitThisEvent && this.eventEmitter) { - logger.debug("Emitting event"); - try { - this.eventEmitter.emit('xeroTokenUpdate', options); - } catch (e) { - logger.error(e); + if (this.options.accessToken) { + if (options.accessToken !== this.options.accessToken) { + if (this.eventEmitter) { + logger.debug("Emitting event"); + try { + this.eventEmitter.emit('xeroTokenUpdate', options); + } catch (e) { + logger.error(e); + } + } } } + + this.options = Object.assign(this.options, options); } });