@@ -204,9 +204,10 @@ Raven.prototype = {
204
204
*
205
205
* @param {object } options A specific set of options for this context [optional]
206
206
* @param {function } func The function to be wrapped in a new context
207
+ * @param {function } func A function to call before the try/catch wrapper [optional, private]
207
208
* @return {function } The newly wrapped functions with a context
208
209
*/
209
- wrap : function ( options , func ) {
210
+ wrap : function ( options , func , _before ) {
210
211
var self = this ;
211
212
// 1 argument has been passed, and it's not a function
212
213
// so just return it
@@ -246,9 +247,13 @@ Raven.prototype = {
246
247
function wrapped ( ) {
247
248
var args = [ ] , i = arguments . length ,
248
249
deep = ! options || options && options . deep !== false ;
250
+
251
+ if ( _before && isFunction ( _before ) ) {
252
+ _before . apply ( this , arguments ) ;
253
+ }
254
+
249
255
// Recursively wrap all of a function's arguments that are
250
256
// functions themselves.
251
-
252
257
while ( i -- ) args [ i ] = deep ? self . wrap ( options , arguments [ i ] ) : arguments [ i ] ;
253
258
254
259
try {
@@ -260,29 +265,20 @@ Raven.prototype = {
260
265
}
261
266
}
262
267
263
- this . _imitate ( wrapped , func ) ;
264
-
265
- func . __raven_wrapper__ = wrapped ;
266
- // Signal that this function has been wrapped already
267
- // for both debugging and to prevent it to being wrapped twice
268
- wrapped . __raven__ = true ;
269
- wrapped . __inner__ = func ;
270
-
271
- return wrapped ;
272
- } ,
273
-
274
- /**
275
- * Give a wrapper function the properties/prototype
276
- * of the wrapepd (inner) function
277
- */
278
- _imitate : function ( wrapped , func ) {
279
268
// copy over properties of the old function
280
269
for ( var property in func ) {
281
270
if ( hasKey ( func , property ) ) {
282
271
wrapped [ property ] = func [ property ] ;
283
272
}
284
273
}
285
274
wrapped . prototype = func . prototype ;
275
+
276
+ func . __raven_wrapper__ = wrapped ;
277
+ // Signal that this function has been wrapped already
278
+ // for both debugging and to prevent it to being wrapped twice
279
+ wrapped . __raven__ = true ;
280
+ wrapped . __inner__ = func ;
281
+
286
282
return wrapped ;
287
283
} ,
288
284
@@ -624,9 +620,9 @@ Raven.prototype = {
624
620
* @returns {Function }
625
621
* @private
626
622
*/
627
- _wrapEventHandlerForBreadcrumbs : function ( evtName , fn ) {
623
+ _breadcrumbEventHandler : function ( evtName ) {
628
624
var self = this ;
629
- function wrapped ( evt ) {
625
+ return function ( evt ) {
630
626
// It's possible this handler might trigger multiple times for the same
631
627
// event (e.g. event propagation through node ancestors). Ignore if we've
632
628
// already captured the event.
@@ -642,16 +638,7 @@ Raven.prototype = {
642
638
target : htmlElementAsString ( elem )
643
639
}
644
640
} ) ;
645
- if ( fn ) return fn . apply ( this , arguments ) ;
646
641
} ;
647
-
648
- if ( fn ) {
649
- this . _imitate ( wrapped , fn ) ;
650
- fn . __raven_breadcrumb__ = wrapped ;
651
-
652
- }
653
-
654
- return wrapped ;
655
642
} ,
656
643
657
644
/**
@@ -701,7 +688,7 @@ Raven.prototype = {
701
688
// Capture breadcrubms from any click that is unhandled / bubbled up all the way
702
689
// to the document. Do this before we instrument addEventListener.
703
690
if ( this . _hasDocument ) {
704
- document . addEventListener ( 'click' , self . _wrapEventHandlerForBreadcrumbs ( 'click' ) ) ;
691
+ document . addEventListener ( 'click' , self . _breadcrumbEventHandler ( 'click' ) ) ;
705
692
706
693
}
707
694
@@ -720,19 +707,17 @@ Raven.prototype = {
720
707
// can sometimes get 'Permission denied to access property "handle Event'
721
708
}
722
709
710
+
723
711
// TODO: more than just click
712
+ var before ;
724
713
if ( ( global === 'EventTarget' || global === 'Node' ) && evt === 'click' ) {
725
- fn = self . _wrapEventHandlerForBreadcrumbs ( evt , fn ) ;
714
+ before = self . _breadcrumbEventHandler ( evt , fn ) ;
726
715
}
727
- return orig . call ( this , evt , self . wrap ( fn ) , capture , secure ) ;
716
+ return orig . call ( this , evt , self . wrap ( fn , undefined , before ) , capture , secure ) ;
728
717
} ;
729
718
} ) ;
730
719
fill ( proto , 'removeEventListener' , function ( orig ) {
731
720
return function ( evt , fn , capture , secure ) {
732
- // from the original function, get the breadcrumb wrapper
733
- // from the breadcrumb wrapper, get the raven wrapper (try/catch)
734
- // i.e. fn => breadcrumb_wrapper(fn) => raven_wrapper(breadcrumb_wrapper(fn))
735
- fn = fn && ( fn . __raven_breadcrumb__ ? fn . __raven_breadcrumb__ : fn ) ;
736
721
fn = fn && ( fn . __raven_wrapper__ ? fn . __raven_wrapper__ : fn ) ;
737
722
return orig . call ( this , evt , fn , capture , secure ) ;
738
723
} ;
0 commit comments