Skip to content
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

Fix the class context validation #43

Conversation

oleksandr-danylchenko
Copy link
Contributor

@oleksandr-danylchenko oleksandr-danylchenko commented Aug 30, 2024

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
Copy link
Owner

Can you add a test?

@oleksandr-danylchenko

This comment was marked as resolved.

@oleksandr-danylchenko
Copy link
Contributor Author

UPD: Added the test in 2271ba1

@sindresorhus sindresorhus merged commit 205fd8f into sindresorhus:main Sep 9, 2024
2 checks passed
@sindresorhus sindresorhus changed the title Narrowed the context comparison to the prototype match Fix the class context validation Sep 9, 2024
@sindresorhus
Copy link
Owner

Thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants