Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Browser singleton #296

Merged
merged 3 commits into from
Jun 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions 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 @@ -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 */

Expand Down Expand Up @@ -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 */
Expand Down
2 changes: 1 addition & 1 deletion 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 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();
});
});