Skip to content

Commit 06b5b85

Browse files
authored
Merge pull request #296 from rollbar/browser-singleton
Browser singleton
2 parents f8dd4ff + cf254c0 commit 06b5b85

File tree

3 files changed

+121
-1
lines changed

3 files changed

+121
-1
lines changed

Diff for: src/browser/rollbar.js

+103
Original file line numberDiff line numberDiff line change
@@ -26,66 +26,153 @@ function Rollbar(options, client) {
2626
}
2727
}
2828

29+
var _instance = null;
30+
Rollbar.init = function(options, client) {
31+
if (_instance) {
32+
return _instance.global(options).configure(options);
33+
}
34+
_instance = new Rollbar(options, client);
35+
return _instance;
36+
};
37+
38+
function handleUninitialized(maybeCallback) {
39+
var message = 'Rollbar is not initialized';
40+
logger.error(message);
41+
if (maybeCallback) {
42+
maybeCallback(new Error(message));
43+
}
44+
}
45+
2946
Rollbar.prototype.global = function(options) {
3047
this.client.global(options);
3148
return this;
3249
};
50+
Rollbar.global = function(options) {
51+
if (_instance) {
52+
return _instance.global(options);
53+
} else {
54+
handleUninitialized();
55+
}
56+
};
3357

3458
Rollbar.prototype.configure = function(options) {
3559
var oldOptions = this.options;
3660
this.options = _.extend(true, {}, oldOptions, options);
3761
this.client.configure(options);
3862
return this;
3963
};
64+
Rollbar.configure = function(options) {
65+
if (_instance) {
66+
return _instance.configure(options);
67+
} else {
68+
handleUninitialized();
69+
}
70+
};
4071

4172
Rollbar.prototype.log = function() {
4273
var item = this._createItem(arguments);
4374
var uuid = item.uuid;
4475
this.client.log(item);
4576
return {uuid: uuid};
4677
};
78+
Rollbar.log = function() {
79+
if (_instance) {
80+
return _instance.log.apply(_instance, arguments);
81+
} else {
82+
var maybeCallback = _getFirstFunction(arguments);
83+
handleUninitialized(maybeCallback);
84+
}
85+
};
4786

4887
Rollbar.prototype.debug = function() {
4988
var item = this._createItem(arguments);
5089
var uuid = item.uuid;
5190
this.client.debug(item);
5291
return {uuid: uuid};
5392
};
93+
Rollbar.debug = function() {
94+
if (_instance) {
95+
return _instance.debug.apply(_instance, arguments);
96+
} else {
97+
var maybeCallback = _getFirstFunction(arguments);
98+
handleUninitialized(maybeCallback);
99+
}
100+
};
54101

55102
Rollbar.prototype.info = function() {
56103
var item = this._createItem(arguments);
57104
var uuid = item.uuid;
58105
this.client.info(item);
59106
return {uuid: uuid};
60107
};
108+
Rollbar.info = function() {
109+
if (_instance) {
110+
return _instance.info.apply(_instance, arguments);
111+
} else {
112+
var maybeCallback = _getFirstFunction(arguments);
113+
handleUninitialized(maybeCallback);
114+
}
115+
};
61116

62117
Rollbar.prototype.warn = function() {
63118
var item = this._createItem(arguments);
64119
var uuid = item.uuid;
65120
this.client.warn(item);
66121
return {uuid: uuid};
67122
};
123+
Rollbar.warn = function() {
124+
if (_instance) {
125+
return _instance.warn.apply(_instance, arguments);
126+
} else {
127+
var maybeCallback = _getFirstFunction(arguments);
128+
handleUninitialized(maybeCallback);
129+
}
130+
};
68131

69132
Rollbar.prototype.warning = function() {
70133
var item = this._createItem(arguments);
71134
var uuid = item.uuid;
72135
this.client.warning(item);
73136
return {uuid: uuid};
74137
};
138+
Rollbar.warning = function() {
139+
if (_instance) {
140+
return _instance.warning.apply(_instance, arguments);
141+
} else {
142+
var maybeCallback = _getFirstFunction(arguments);
143+
handleUninitialized(maybeCallback);
144+
}
145+
};
75146

76147
Rollbar.prototype.error = function() {
77148
var item = this._createItem(arguments);
78149
var uuid = item.uuid;
79150
this.client.error(item);
80151
return {uuid: uuid};
81152
};
153+
Rollbar.error = function() {
154+
if (_instance) {
155+
return _instance.error.apply(_instance, arguments);
156+
} else {
157+
var maybeCallback = _getFirstFunction(arguments);
158+
handleUninitialized(maybeCallback);
159+
}
160+
};
82161

83162
Rollbar.prototype.critical = function() {
84163
var item = this._createItem(arguments);
85164
var uuid = item.uuid;
86165
this.client.critical(item);
87166
return {uuid: uuid};
88167
};
168+
Rollbar.critical = function() {
169+
if (_instance) {
170+
return _instance.critical.apply(_instance, arguments);
171+
} else {
172+
var maybeCallback = _getFirstFunction(arguments);
173+
handleUninitialized(maybeCallback);
174+
}
175+
};
89176

90177
Rollbar.prototype.handleUncaughtException = function(message, url, lineno, colno, error, context) {
91178
var item;
@@ -193,6 +280,13 @@ Rollbar.prototype.wrap = function(f, context) {
193280
return f;
194281
}
195282
};
283+
Rollbar.wrap = function(f, context) {
284+
if (_instance) {
285+
return _instance.wrap(f, context);
286+
} else {
287+
handleUninitialized();
288+
}
289+
};
196290

197291
/* Internal */
198292

@@ -277,6 +371,15 @@ Rollbar.prototype._createItem = function(args) {
277371
return item;
278372
};
279373

374+
function _getFirstFunction(args) {
375+
for (var i = 0, len = args.length; i < len; ++i) {
376+
if (_.isFunction(args[i])) {
377+
return args[i];
378+
}
379+
}
380+
return undefined;
381+
}
382+
280383
/* global __NOTIFIER_VERSION__:false */
281384
/* global __DEFAULT_BROWSER_SCRUB_FIELDS__:false */
282385
/* global __DEFAULT_LOG_LEVEL__:false */

Diff for: src/server/rollbar.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function Rollbar(options, client) {
4242
var _instance = null;
4343
Rollbar.init = function(options, client) {
4444
if (_instance) {
45-
return _instance;
45+
return _instance.global(options).configure(options);
4646
}
4747
_instance = new Rollbar(options, client);
4848
return _instance;

Diff for: test/browser.rollbar.test.js

+17
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,20 @@ describe('createItem', function() {
250250
});
251251
});
252252

253+
describe('singleton', function() {
254+
it('should pass through the underlying client after init', function(done) {
255+
var client = new (TestClientGen())();
256+
var options = {};
257+
var rollbar = Rollbar.init(options, client);
258+
259+
rollbar.log('hello 1');
260+
Rollbar.log('hello 2');
261+
262+
var loggedItemDirect = client.logCalls[0].item;
263+
var loggedItemSingleton = client.logCalls[1].item;
264+
expect(loggedItemDirect.message).to.eql('hello 1');
265+
expect(loggedItemSingleton.message).to.eql('hello 2');
266+
267+
done();
268+
});
269+
});

0 commit comments

Comments
 (0)