Skip to content

Commit 144413f

Browse files
authored
fix: Correctly detach event listeners (#2737)
1 parent 5226fe3 commit 144413f

File tree

2 files changed

+6
-14
lines changed

2 files changed

+6
-14
lines changed

packages/browser/src/integrations/trycatch.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -185,25 +185,18 @@ export class TryCatch implements Integration {
185185
* our wrapped version of `addEventListener`, which internally calls `wrap` helper.
186186
* This helper "wraps" whole callback inside a try/catch statement, and attached appropriate metadata to it,
187187
* in order for us to make a distinction between wrapped/non-wrapped functions possible.
188-
* If a function has `__sentry__` property, it means that it was wrapped, and it has additional property
189-
* of `__sentry__original__`, holding the handler. And this original handler, has a reversed link,
190-
* with `__sentry_wrapped__` property, which holds the wrapped version.
188+
* If a function was wrapped, it has additional property of `__sentry_wrapped__`, holding the handler.
191189
*
192190
* When someone adds a handler prior to initialization, and then do it again, but after,
193191
* then we have to detach both of them. Otherwise, if we'd detach only wrapped one, it'd be impossible
194192
* to get rid of the initial handler and it'd stick there forever.
195-
* In case of second scenario, `__sentry_original__` refers to initial handler, and passed function
196-
* is a wrapped version.
197193
*/
198-
const callback = (fn as any) as WrappedFunction;
199194
try {
200-
if (callback && callback.__sentry__) {
201-
original.call(this, eventName, callback.__sentry_original__, options);
202-
}
195+
original.call(this, eventName, ((fn as unknown) as WrappedFunction).__sentry_wrapped__, options);
203196
} catch (e) {
204-
// ignore, accessing __sentry__ will throw in some Selenium environments
197+
// ignore, accessing __sentry_wrapped__ will throw in some Selenium environments
205198
}
206-
return original.call(this, eventName, callback, options);
199+
return original.call(this, eventName, fn, options);
207200
};
208201
});
209202
}

packages/utils/src/instrument.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -400,13 +400,12 @@ function instrumentDOM(): void {
400400
fn: EventListenerOrEventListenerObject,
401401
options?: boolean | EventListenerOptions,
402402
): () => void {
403-
let callback = fn as WrappedFunction;
404403
try {
405-
callback = callback && (callback.__sentry_wrapped__ || callback);
404+
original.call(this, eventName, ((fn as unknown) as WrappedFunction).__sentry_wrapped__, options);
406405
} catch (e) {
407406
// ignore, accessing __sentry_wrapped__ will throw in some Selenium environments
408407
}
409-
return original.call(this, eventName, callback, options);
408+
return original.call(this, eventName, fn, options);
410409
};
411410
});
412411
});

0 commit comments

Comments
 (0)