diff --git a/lib/serviceruntime/roleenvironment.js b/lib/serviceruntime/roleenvironment.js index 7970007a5..d836698c4 100644 --- a/lib/serviceruntime/roleenvironment.js +++ b/lib/serviceruntime/roleenvironment.js @@ -40,7 +40,9 @@ var currentGoalState = null; var currentEnvironmentData = null; var lastState = null; var maxDateTime = new Date('9999-12-31T23:59:59.9999999'); -var eventEmitter = new events.EventEmitter(); + +// Merge event emitter properties into the RoleEnvironment (export) object. +RoleEnvironment = azureutil.merge(RoleEnvironment, new events.EventEmitter()); /** * Returns a RoleInstance object that represents the role instance @@ -246,11 +248,6 @@ RoleEnvironment.clearStatus = function (callback) { }); }; -// TODO: find a better way to hook eventing up -RoleEnvironment.on = function (event, callback) { - eventEmitter.on(event, callback); -}; - RoleEnvironment._initialize = function (callback) { var getCurrentGoalState = function (finalCallback) { RoleEnvironment.runtimeClient.getCurrentGoalState(function (error, goalState) { @@ -289,9 +286,9 @@ RoleEnvironment._initialize = function (callback) { } else { RoleEnvironment.runtimeClient = rtClient; - getCurrentGoalState(function (error) { - if (error) { - callback(error); + getCurrentGoalState(function (errorGetCurrentGoalState) { + if (errorGetCurrentGoalState) { + callback(errorGetCurrentGoalState); } else { if (RoleEnvironment.runtimeClient.listeners(ServiceRuntimeConstants.CHANGED).length === 0) { RoleEnvironment.runtimeClient.on(ServiceRuntimeConstants.CHANGED, function (newGoalState) { @@ -336,7 +333,7 @@ RoleEnvironment._processGoalStateChange = function (newGoalState, callback) { if (changes.length === 0) { RoleEnvironment._acceptLatestIncarnation(newGoalState, last); } else { - eventEmitter.emit(ServiceRuntimeConstants.CHANGING, changes); + RoleEnvironment.emit(ServiceRuntimeConstants.CHANGING, changes); RoleEnvironment._acceptLatestIncarnation(newGoalState, last); @@ -346,7 +343,7 @@ RoleEnvironment._processGoalStateChange = function (newGoalState, callback) { } else { currentEnvironmentData = environmentData; - eventEmitter.emit(ServiceRuntimeConstants.CHANGED, changes); + RoleEnvironment.emit(ServiceRuntimeConstants.CHANGED, changes); callback(); } }); @@ -506,5 +503,5 @@ RoleEnvironment._calculateChanges = function (callback) { }; RoleEnvironment._raiseStoppingEvent = function () { - eventEmitter.emit(ServiceRuntimeConstants.STOPPING); + RoleEnvironment.emit(ServiceRuntimeConstants.STOPPING); }; \ No newline at end of file diff --git a/lib/util/util.js b/lib/util/util.js index 0a8f6d322..7926b50f3 100644 --- a/lib/util/util.js +++ b/lib/util/util.js @@ -195,4 +195,28 @@ exports.stringIsDate = function(date) { } return !isNaN(date.getTime()); -}; \ No newline at end of file +}; + +/** +* Merges multiple objects. +* +* @param {object} object The objects to be merged +* @return {object} The merged object. +*/ +exports.merge = function () { + var source, sourceValue; + var target = arguments[0] || {}; + var length = arguments.length; + + for (var i = 1; i < length; i++) { + if ((source = arguments[i]) != null) { + for (var property in source) { + sourceValue = source[property]; + if (sourceValue !== undefined) { + target[property] = sourceValue; + } + } + } + } + return target; +} \ No newline at end of file diff --git a/test/serviceruntime/roleenvironment-tests.js b/test/serviceruntime/roleenvironment-tests.js index 4bf082aef..8bff5343b 100644 --- a/test/serviceruntime/roleenvironment-tests.js +++ b/test/serviceruntime/roleenvironment-tests.js @@ -62,6 +62,22 @@ suite('roleenvironment-tests', function () { done(); }); + test('eventEmitter', function (done) { + // No event listeners registered yet + assert.equal(azure.RoleEnvironment.listeners(ServiceRuntimeConstants.CHANGED).length, 0); + + // Register one event listener + var listener = function () { }; + azure.RoleEnvironment.on(ServiceRuntimeConstants.CHANGED, listener); + assert.equal(azure.RoleEnvironment.listeners(ServiceRuntimeConstants.CHANGED).length, 1); + + // Remove the event listener + azure.RoleEnvironment.removeListener(ServiceRuntimeConstants.CHANGED, listener); + assert.equal(azure.RoleEnvironment.listeners(ServiceRuntimeConstants.CHANGED).length, 0); + + done(); + }); + test('IsAvailable', function (done) { azure.RoleEnvironment.isAvailable(function (error1, isAvailable1) { assert.notEqual(error1, null); @@ -163,7 +179,6 @@ suite('roleenvironment-tests', function () { } }; - var originalFileInputChannelReadData = runtimeKernel.fileInputChannel._readData; runtimeKernel.fileInputChannel._readData = function (name, callback) { if (name === 'C:\\file.xml') { callback(undefined, @@ -609,7 +624,7 @@ suite('roleenvironment-tests', function () { }); // Make sure incarnation 1 is read - azure.RoleEnvironment.getConfigurationSettings(function (error, configurationSettings) { + azure.RoleEnvironment.getConfigurationSettings(function (error) { // Update to incarnation 2 goalStateXml = "" + "" + "