Skip to content

Instantly share code, notes, and snippets.

@AdamTReineke
Created September 25, 2014 01:42
Show Gist options
  • Save AdamTReineke/58c4696fb0bc07bd5247 to your computer and use it in GitHub Desktop.
Save AdamTReineke/58c4696fb0bc07bd5247 to your computer and use it in GitHub Desktop.
Instrument addEventListener to provide async callstacks for Internet Explorer 11's developer tools
Element.prototype._addEventListener = Element.prototype.addEventListener;
Element.prototype.addEventListener = function () {
var opID = Debug.msTraceAsyncOperationStarting("Element.prototype.addEventListener");
var eventThis = this;
// Replace the user's function with a version wrapped with the async instrumentation calls
arguments[1] = (function (userFunc) {
return function instrumentAddEventListener() {
// We call that the async op completed instantly because the async op was the click
// event that started this call.
Debug.msTraceAsyncOperationCompleted(opID, Debug.MS_ASYNC_OP_STATUS_SUCCESS);
// Mark callback starting, fire the callback, mark callback completed.
Debug.msTraceAsyncCallbackStarting(opID);
userFunc.apply(eventThis, arguments);
Debug.msTraceAsyncCallbackCompleted();
}
})(arguments[1]);
return Element.prototype._addEventListener.apply(this, arguments);
}
@AdamTReineke
Copy link
Author

@AdamTReineke
Copy link
Author

Also works in for Visual Studio. (Currently testing a Cordova app, running as a WWA, on WP8.1.) It may be causing a few extra things to show up on the stack though...

@AdamTReineke
Copy link
Author

The motivation is that in IE11, you only get async callstacks for XHR readyState == 4, setTimeout and setImmediate, IIRC. Also, YMMV. It is a definitely unsupported route to get async callstacks like this. VS seems to like this method a lot less than IE did.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment