diff --git a/src/browser/rollbar.js b/src/browser/rollbar.js index e2b8755fb..8c3ad7bf6 100644 --- a/src/browser/rollbar.js +++ b/src/browser/rollbar.js @@ -26,10 +26,34 @@ 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; @@ -37,6 +61,13 @@ Rollbar.prototype.configure = function(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); @@ -44,6 +75,14 @@ Rollbar.prototype.log = function() { 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); @@ -51,6 +90,14 @@ Rollbar.prototype.debug = function() { 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); @@ -58,6 +105,14 @@ Rollbar.prototype.info = function() { 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); @@ -65,6 +120,14 @@ Rollbar.prototype.warn = function() { 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); @@ -72,6 +135,14 @@ Rollbar.prototype.warning = function() { 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); @@ -79,6 +150,14 @@ Rollbar.prototype.error = function() { 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); @@ -86,6 +165,14 @@ Rollbar.prototype.critical = function() { 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; @@ -191,6 +278,13 @@ Rollbar.prototype.wrap = function(f, context) { return f; } }; +Rollbar.wrap = function(f, context) { + if (_instance) { + return _instance.wrap(f, context); + } else { + handleUninitialized(); + } +}; /* Internal */ @@ -275,6 +369,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 */ diff --git a/src/server/rollbar.js b/src/server/rollbar.js index e8deef093..a4969f40d 100644 --- a/src/server/rollbar.js +++ b/src/server/rollbar.js @@ -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; diff --git a/test/browser.rollbar.test.js b/test/browser.rollbar.test.js index 419687f02..cf4aa2f89 100644 --- a/test/browser.rollbar.test.js +++ b/test/browser.rollbar.test.js @@ -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(); + }); +});