Skip to content

Commit 28c4c7f

Browse files
committed
Merge pull request #392 from getsentry/plugins
Lazy load plugins
2 parents 6f069a8 + 50060a4 commit 28c4c7f

File tree

9 files changed

+79
-27
lines changed

9 files changed

+79
-27
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
* NEW: Add `Raven.clearContext()` to empty all of the context.
1010
* NEW: Add `Raven.getContext()` to get a copy of the current context.
1111
* NEW: `Raven.set{Extra,Tags}Context(ctx)` now merges with existing values instead of overwriting.
12+
* NEW: Add `Raven.addPlugin()` to register a plugin to be initialized when installed.
13+
* NEW: Plugins are now initialized and loaded when calling `Raven.install()`. This avoid some race conditions with load order.
1214

1315
## 1.1.22
1416

plugins/angular.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33
*
44
* Provides an $exceptionHandler for Angular.js
55
*/
6-
;(function(Raven, angular) {
6+
;(function(window) {
77
'use strict';
88

9+
var angular = window.angular,
10+
Raven = window.Raven;
11+
912
// quit if angular isn't on the page
10-
if (!angular) {
11-
return;
12-
}
13+
if (!(angular && Raven)) return;
14+
15+
// Angular plugin doesn't go through the normal `Raven.addPlugin`
16+
// since this bootstraps the `install()` automatically.
1317

1418
function ngRavenProvider($provide) {
1519
$provide.decorator('$exceptionHandler', [
@@ -37,4 +41,4 @@ angular.module('ngRaven', [])
3741
.config(['$provide', ngRavenProvider])
3842
.value('Raven', Raven);
3943

40-
})(window.Raven, window.angular);
44+
}(typeof window !== 'undefined' ? window : this));

plugins/backbone.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
*
44
* Patches Backbone.Events callbacks.
55
*/
6-
;(function(window, Raven, Backbone) {
6+
;(function(window) {
77
'use strict';
88

9+
if (window.Raven) Raven.addPlugin(function backbonePlugin() {
10+
11+
var Backbone = window.Backbone;
12+
913
// quit if Backbone isn't on the page
10-
if (!Backbone) {
11-
return;
12-
}
14+
if (!Backbone) return;
1315

1416
function makeBackboneEventsOn(oldOn) {
1517
return function BackboneEventsOn(name, callback, context) {
@@ -52,4 +54,6 @@ for (; i < l; i++) {
5254
affected.bind = affected.on;
5355
}
5456

55-
}(window, window.Raven, window.Backbone));
57+
});
58+
59+
}(typeof window !== 'undefined' ? window : this));

plugins/console.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44
* Monkey patches console.* calls into Sentry messages with
55
* their appropriate log levels. (Experimental)
66
*/
7-
;(function(window, Raven, console) {
7+
;(function(window) {
88
'use strict';
99

10+
if (window.Raven) Raven.addPlugin(function ConsolePlugin() {
11+
12+
var console = window.console || {};
13+
1014
var originalConsole = console,
1115
logLevels = ['debug', 'info', 'warn', 'error'],
1216
level = logLevels.pop();
@@ -37,7 +41,12 @@ while(level) {
3741
console[level] = logForGivenLevel(level);
3842
level = logLevels.pop();
3943
}
44+
4045
// export
4146
window.console = console;
4247

43-
}(window, window.Raven, window.console || {}));
48+
// End of plugin factory
49+
});
50+
51+
// console would require `window`, so we don't allow it to be optional
52+
}(window));

plugins/ember.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
*
44
* Patches event handler callbacks and ajax callbacks.
55
*/
6-
;(function(window, Raven, Ember) {
6+
;(function(window) {
77
'use strict';
88

9+
if (window.Raven) Raven.addPlugin(function EmberPlugin() {
10+
11+
var Ember = window.Ember;
12+
913
// quit if Ember isn't on the page
10-
if (!Ember) {
11-
return;
12-
}
14+
if (!Ember) return;
1315

1416
var _oldOnError = Ember.onerror;
1517
Ember.onerror = function EmberOnError(error) {
@@ -26,4 +28,7 @@ Ember.RSVP.on('error', function (reason) {
2628
}
2729
});
2830

29-
}(window, window.Raven, window.Ember));
31+
// End of plugin factory
32+
});
33+
34+
}(typeof window !== 'undefined' ? window : this));

plugins/jquery.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
*
44
* Patches event handler callbacks and ajax callbacks.
55
*/
6-
;(function(window, Raven, $) {
6+
;(function(window) {
77
'use strict';
88

9+
if (window.Raven) Raven.addPlugin(function jQueryPlugin() {
10+
11+
var $ = window.jQuery;
12+
913
// quit if jQuery isn't on the page
10-
if (!$) {
11-
return;
12-
}
14+
if (!$) return;
1315

1416
var _oldEventAdd = $.event.add;
1517
$.event.add = function ravenEventAdd(elem, types, handler, data, selector) {
@@ -100,4 +102,7 @@ $.Deferred = function ravenDeferredWrapper(func) {
100102
});
101103
};
102104

103-
}(window, window.Raven, window.jQuery));
105+
// End of plugin factory
106+
});
107+
108+
}(typeof window !== 'undefined' ? window : this));

plugins/native.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
* Extends support for global error handling for asynchronous browser
55
* functions. Adopted from Closure Library's errorhandler.js.
66
*/
7-
;(function extendToAsynchronousCallbacks(window, Raven) {
8-
"use strict";
7+
;(function(window) {
8+
'use strict';
9+
10+
if (window.Raven) Raven.addPlugin(function nativePlugin() {
911

1012
var _helper = function _helper(fnName) {
1113
var originalFn = window[fnName];
@@ -30,4 +32,7 @@ var _helper = function _helper(fnName) {
3032
_helper('setTimeout');
3133
_helper('setInterval');
3234

33-
}(window, window.Raven));
35+
// End of plugin factory
36+
});
37+
38+
}(typeof window !== 'undefined' ? window : this));

plugins/require.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33
*
44
* Automatically wrap define/require callbacks. (Experimental)
55
*/
6-
;(function(window, Raven) {
6+
;(function(window) {
77
'use strict';
88

9+
if (window.Raven) Raven.addPlugin(function RequirePlugin() {
10+
911
if (typeof define === 'function' && define.amd) {
1012
window.define = Raven.wrap({deep: false}, define);
1113
window.require = Raven.wrap({deep: false}, require);
1214
}
1315

14-
}(window, window.Raven));
16+
// End of plugin factory
17+
});
18+
19+
}(window));

src/raven.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ var _Raven = window.Raven,
2929
// before the console plugin has a chance to monkey patch
3030
originalConsole = window.console || {},
3131
originalConsoleMethods = {},
32+
plugins = [],
3233
startTime = now();
3334

3435
for (var method in originalConsole) {
@@ -133,6 +134,12 @@ var Raven = {
133134
install: function() {
134135
if (isSetup() && !isRavenInstalled) {
135136
TraceKit.report.subscribe(handleStackInfo);
137+
138+
// Install all of the plugins
139+
each(plugins, function(_, plugin) {
140+
plugin();
141+
});
142+
136143
isRavenInstalled = true;
137144
}
138145

@@ -288,6 +295,12 @@ var Raven = {
288295
return Raven;
289296
},
290297

298+
addPlugin: function(plugin) {
299+
plugins.push(plugin);
300+
if (isRavenInstalled) plugin();
301+
return Raven;
302+
},
303+
291304
/*
292305
* Set/clear a user to be sent along with the payload.
293306
*

0 commit comments

Comments
 (0)