Skip to content

Commit

Permalink
Merge pull request #1789 from BladeRunnerJS/PCTLIBRARY-1935/switching…
Browse files Browse the repository at this point in the history
…-to-typeof-in-observable-class

Pctlibrary 1935/switching to typeof in observable class
  • Loading branch information
thecapdan authored Jul 13, 2017
2 parents cd21781 + 39ad85a commit 55b2395
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 48 deletions.
41 changes: 26 additions & 15 deletions brjs-sdk/sdk/libs/javascript/br-util/src/br/util/Observable.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';
"use strict";

/**
* @module br/util/Observable
*/

var Errors = require('br/Errors');
var Errors = require("br/Errors");

/**
* Constructs a new <code>Observable</code>.
Expand Down Expand Up @@ -66,13 +66,16 @@ Observable.prototype.getCount = function() {
* <code>String</code>, <code>Number</code>, <code>Boolean</code> or <code>Function</code>.
*/
Observable.prototype.addObserver = function(observer) {
if (!(observer instanceof Object) ||
(observer instanceof String ||
observer instanceof Number ||
observer instanceof Boolean ||
observer instanceof Function)) {

throw new Errors.InvalidParametersError('An observer must be an object');
if (
!(typeof observer === "object" && observer !== null) ||
(typeof observer === "string" ||
(typeof observer === "number" && !isNan(observer)) ||
typeof observer === "boolean" ||
typeof observer === "function")
) {
throw new Errors.InvalidParametersError(
"An observer must be an object"
);
}

this.m_pObservers.push(observer);
Expand All @@ -90,7 +93,7 @@ Observable.prototype.addObserver = function(observer) {
* <code>String</code>, <code>Number</code>, <code>Boolean</code> or <code>Function</code>.
*/
Observable.prototype.addUniqueObserver = function(observer) {
var observerNotAdded = (this._getObserverIndex(observer) == -1);
var observerNotAdded = this._getObserverIndex(observer) == -1;

if (observerNotAdded) {
this.addObserver(observer);
Expand Down Expand Up @@ -147,7 +150,7 @@ Observable.prototype.removeAllObservers = function() {
this.m_pObservers = [];
};

/**
/**
* Gets a list of all the observers that have been registered with this <code>Observable</code>.
*
* @return {Array} A list of the observers that have been registered.
Expand Down Expand Up @@ -177,8 +180,10 @@ Observable.prototype.notifyObservers = function(methodName, parameters) {
var observersCopy = this.m_pObservers.slice();
for (var idx = 0, len = observersCopy.length; idx < len; idx++) {
var observer = observersCopy[idx];
if (typeof observer[methodName] !== 'function') {
throw new Errors.NotSupportedError("Observer does not implement '" + methodName + "'");
if (typeof observer[methodName] !== "function") {
throw new Errors.NotSupportedError(
"Observer does not implement '" + methodName + "'"
);
}

observer[methodName].apply(observer, parameters);
Expand Down Expand Up @@ -215,7 +220,11 @@ Observable.prototype.notify = function(methodName) {
*
* @see br.util.Observable#notifyObservers to notify without guarding against exceptions.
*/
Observable.prototype.notifyObserversWithTryCatch = function(methodName, parameters, throwExceptions) {
Observable.prototype.notifyObserversWithTryCatch = function(
methodName,
parameters,
throwExceptions
) {
if (!parameters) {
parameters = [];
}
Expand All @@ -227,7 +236,9 @@ Observable.prototype.notifyObserversWithTryCatch = function(methodName, paramete
try {
observer[methodName].apply(observer, parameters);
} catch (e) {
failedNotifications.push(new Observable.FailedNotification(observer, methodName, e));
failedNotifications.push(
new Observable.FailedNotification(observer, methodName, e)
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,23 +139,15 @@
assertException(function() { oThis.m_oObservable.addObserver(10); }, "InvalidParametersError");
};

ObservableTest.prototype.test_addNumericObserverFails = function()
{
var oThis = this;
assertException(function() { oThis.m_oObservable.addObserver(new Number(10)); }, "InvalidParametersError");
};


ObservableTest.prototype.test_addStringObserverFails = function()
{
var oThis = this;
assertException(function() { oThis.m_oObservable.addObserver("test"); }, "InvalidParametersError");
};

ObservableTest.prototype.test_addNewStringObserverFails = function()
{
var oThis = this;
assertException(function() { oThis.m_oObservable.addObserver(new String("test")); }, "InvalidParametersError");
};


ObservableTest.prototype.test_addFunctionObserverFails = function()
{
Expand All @@ -170,11 +162,7 @@
assertException(function() { oThis.m_oObservable.addObserver(false); }, "InvalidParametersError");
};

ObservableTest.prototype.test_addBooleanObserverFails = function()
{
var oThis = this;
assertException(function() { oThis.m_oObservable.addObserver(new Boolean(false)); }, "InvalidParametersError");
};


ObservableTest.prototype.test_addDuplicateObserverSucceeds = function()
{
Expand Down Expand Up @@ -213,24 +201,12 @@
assertException(function() { oThis.m_oObservable.addUniqueObserver(10); }, "InvalidParametersError");
};

ObservableTest.prototype.test_addNumericUniqueObserverFails = function()
{
var oThis = this;
assertException(function() { oThis.m_oObservable.addUniqueObserver(new Number(10)); }, "InvalidParametersError");
};

ObservableTest.prototype.test_addStringUniqueObserverFails = function()
{
var oThis = this;
assertException(function() { oThis.m_oObservable.addUniqueObserver("test"); }, "InvalidParametersError");
};

ObservableTest.prototype.test_addNewStringUniqueObserverFails = function()
{
var oThis = this;
assertException(function() { oThis.m_oObservable.addUniqueObserver(new String("test")); }, "InvalidParametersError");
};

ObservableTest.prototype.test_addFunctionUniqueObserverFails = function()
{
var oThis = this;
Expand All @@ -244,12 +220,6 @@
assertException(function() { oThis.m_oObservable.addUniqueObserver(false); }, "InvalidParametersError");
};

ObservableTest.prototype.test_addBooleanUniqueObserverFails = function()
{
var oThis = this;
assertException(function() { oThis.m_oObservable.addUniqueObserver(new Boolean(false)); }, "InvalidParametersError");
};

ObservableTest.prototype.test_addDuplicateUniqueObserverFails = function()
{
this.m_oObserver1.expects(once()).method1();
Expand Down

0 comments on commit 55b2395

Please sign in to comment.