Skip to content

fix(browser): Call removeEventListener twice only when necessary #3016

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

Merged
merged 2 commits into from
Oct 30, 2020
Merged
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
10 changes: 7 additions & 3 deletions packages/browser/src/integrations/trycatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export class TryCatch implements Integration {
});

fill(proto, 'removeEventListener', function(
original: () => void,
originalRemoveEventListener: () => void,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): (this: any, eventName: string, fn: EventListenerObject, options?: boolean | EventListenerOptions) => () => void {
return function(
Expand All @@ -230,12 +230,16 @@ export class TryCatch implements Integration {
* then we have to detach both of them. Otherwise, if we'd detach only wrapped one, it'd be impossible
* to get rid of the initial handler and it'd stick there forever.
*/
const wrappedEventHandler = (fn as unknown) as WrappedFunction;
try {
original.call(this, eventName, ((fn as unknown) as WrappedFunction).__sentry_wrapped__, options);
const originalEventHandler = wrappedEventHandler?.__sentry_wrapped__;
if (originalEventHandler) {
originalRemoveEventListener.call(this, eventName, originalEventHandler, options);
}
} catch (e) {
// ignore, accessing __sentry_wrapped__ will throw in some Selenium environments
}
return original.call(this, eventName, fn, options);
return originalRemoveEventListener.call(this, eventName, wrappedEventHandler, options);
};
});
}
Expand Down