sindresorhus / debounce

Delay function calls until a set time elapses after the last invocation
Other
798 stars 80 forks source link

Fix the class context validation #43

Closed oleksandr-danylchenko closed 2 months ago

oleksandr-danylchenko commented 2 months ago

Issue

Context equality protection is indeed useful when you want to protect the same function from running on two distinct instances, as described in #8. However, equality isn't always expected. Here's an example:

const onResize = debounce(() => {...});

window.addEventListener('resize', onResize);
const resizeObserver = new ResizeObserver(onResize);

In this case, I want to operate a single debounced function that will shared between the event listener and resize observer. However, when the function gets invoked, it receives different contexts that lead to the exception: image However, those contexts come from totally different classes and don't violate the issue described in #8.

Changes Made

Additionally to the context check, I also added the prototype equality check. That will help to dismiss false exceptions when the context differs between instances of unrelated classes.

sindresorhus commented 2 months ago

Can you add a test?

oleksandr-danylchenko commented 2 months ago

Can you add a test?

Yeah, I can. I'll provide them sometime during the weekend

oleksandr-danylchenko commented 2 months ago

UPD: Added the test in https://github.com/sindresorhus/debounce/pull/43/commits/2271ba1bf5c6629446b5437013ba2799263a8f05

sindresorhus commented 2 months ago

Thanks :)