Skip to content

Commit

Permalink
Merge pull request #296 from rollbar/browser-singleton
Browse files Browse the repository at this point in the history
Browser singleton
  • Loading branch information
rokob authored Jun 20, 2017
2 parents e0ba8cb + 8ac49fe commit 54c66e3
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 1 deletion.
103 changes: 103 additions & 0 deletions sdks/rollbar.js/src/browser/rollbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,66 +26,153 @@ function Rollbar(options, client) {
}
}

var _instance = null;
Rollbar.init = function(options, client) {
if (_instance) {
return _instance.global(options).configure(options);
}
_instance = new Rollbar(options, client);
return _instance;
};

function handleUninitialized(maybeCallback) {
var message = 'Rollbar is not initialized';
logger.error(message);
if (maybeCallback) {
maybeCallback(new Error(message));
}
}

Rollbar.prototype.global = function(options) {
this.client.global(options);
return this;
};
Rollbar.global = function(options) {
if (_instance) {
return _instance.global(options);
} else {
handleUninitialized();
}
};

Rollbar.prototype.configure = function(options) {
var oldOptions = this.options;
this.options = _.extend(true, {}, oldOptions, options);
this.client.configure(options);
return this;
};
Rollbar.configure = function(options) {
if (_instance) {
return _instance.configure(options);
} else {
handleUninitialized();
}
};

Rollbar.prototype.log = function() {
var item = this._createItem(arguments);
var uuid = item.uuid;
this.client.log(item);
return {uuid: uuid};
};
Rollbar.log = function() {
if (_instance) {
return _instance.log.apply(_instance, arguments);
} else {
var maybeCallback = _getFirstFunction(arguments);
handleUninitialized(maybeCallback);
}
};

Rollbar.prototype.debug = function() {
var item = this._createItem(arguments);
var uuid = item.uuid;
this.client.debug(item);
return {uuid: uuid};
};
Rollbar.debug = function() {
if (_instance) {
return _instance.debug.apply(_instance, arguments);
} else {
var maybeCallback = _getFirstFunction(arguments);
handleUninitialized(maybeCallback);
}
};

Rollbar.prototype.info = function() {
var item = this._createItem(arguments);
var uuid = item.uuid;
this.client.info(item);
return {uuid: uuid};
};
Rollbar.info = function() {
if (_instance) {
return _instance.info.apply(_instance, arguments);
} else {
var maybeCallback = _getFirstFunction(arguments);
handleUninitialized(maybeCallback);
}
};

Rollbar.prototype.warn = function() {
var item = this._createItem(arguments);
var uuid = item.uuid;
this.client.warn(item);
return {uuid: uuid};
};
Rollbar.warn = function() {
if (_instance) {
return _instance.warn.apply(_instance, arguments);
} else {
var maybeCallback = _getFirstFunction(arguments);
handleUninitialized(maybeCallback);
}
};

Rollbar.prototype.warning = function() {
var item = this._createItem(arguments);
var uuid = item.uuid;
this.client.warning(item);
return {uuid: uuid};
};
Rollbar.warning = function() {
if (_instance) {
return _instance.warning.apply(_instance, arguments);
} else {
var maybeCallback = _getFirstFunction(arguments);
handleUninitialized(maybeCallback);
}
};

Rollbar.prototype.error = function() {
var item = this._createItem(arguments);
var uuid = item.uuid;
this.client.error(item);
return {uuid: uuid};
};
Rollbar.error = function() {
if (_instance) {
return _instance.error.apply(_instance, arguments);
} else {
var maybeCallback = _getFirstFunction(arguments);
handleUninitialized(maybeCallback);
}
};

Rollbar.prototype.critical = function() {
var item = this._createItem(arguments);
var uuid = item.uuid;
this.client.critical(item);
return {uuid: uuid};
};
Rollbar.critical = function() {
if (_instance) {
return _instance.critical.apply(_instance, arguments);
} else {
var maybeCallback = _getFirstFunction(arguments);
handleUninitialized(maybeCallback);
}
};

Rollbar.prototype.handleUncaughtException = function(message, url, lineno, colno, error, context) {
var item;
Expand Down Expand Up @@ -193,6 +280,13 @@ Rollbar.prototype.wrap = function(f, context) {
return f;
}
};
Rollbar.wrap = function(f, context) {
if (_instance) {
return _instance.wrap(f, context);
} else {
handleUninitialized();
}
};

/* Internal */

Expand Down Expand Up @@ -277,6 +371,15 @@ Rollbar.prototype._createItem = function(args) {
return item;
};

function _getFirstFunction(args) {
for (var i = 0, len = args.length; i < len; ++i) {
if (_.isFunction(args[i])) {
return args[i];
}
}
return undefined;
}

/* global __NOTIFIER_VERSION__:false */
/* global __DEFAULT_BROWSER_SCRUB_FIELDS__:false */
/* global __DEFAULT_LOG_LEVEL__:false */
Expand Down
2 changes: 1 addition & 1 deletion sdks/rollbar.js/src/server/rollbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function Rollbar(options, client) {
var _instance = null;
Rollbar.init = function(options, client) {
if (_instance) {
return _instance;
return _instance.global(options).configure(options);
}
_instance = new Rollbar(options, client);
return _instance;
Expand Down
17 changes: 17 additions & 0 deletions sdks/rollbar.js/test/browser.rollbar.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,20 @@ describe('createItem', function() {
});
});

describe('singleton', function() {
it('should pass through the underlying client after init', function(done) {
var client = new (TestClientGen())();
var options = {};
var rollbar = Rollbar.init(options, client);

rollbar.log('hello 1');
Rollbar.log('hello 2');

var loggedItemDirect = client.logCalls[0].item;
var loggedItemSingleton = client.logCalls[1].item;
expect(loggedItemDirect.message).to.eql('hello 1');
expect(loggedItemSingleton.message).to.eql('hello 2');

done();
});
});

0 comments on commit 54c66e3

Please sign in to comment.