-
-
Notifications
You must be signed in to change notification settings - Fork 77
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
Protect against improper use #8
Comments
I don't think it's worth the bloat. You can use the Lodash's |
# See - sindresorhus/debounce#8. The debounced function has a strict context comparison that breaks when applied in the resize listener
I believe context protection leads to code bloating when I want to provide a single function to the For example: const onResize = debounce(() => {...}, 10);
window.addEventListener('resize', onResize);
const resizeObserver = new ResizeObserver(onResize); The code above will lead to the Also, I cannot use There may be an option to ignore the context mismatch error, something like UPD: The solution I applied for now is to wrap the import libDebounce from 'debounce';
export const debounce: typeof libDebounce = (function_, wait = 10, options) => {
const fn = libDebounce(function_, wait, options);
const boundFn = fn.bind(undefined);
Object.getOwnPropertyNames(fn).forEach(
prop => Object.defineProperty(boundFn, prop, Object.getOwnPropertyDescriptor(fn, prop))
);
const proto = Object.getPrototypeOf(fn);
Object.setPrototypeOf(boundFn, proto);
return boundFn;
} UPD2: Created a PR that looses the context equality check strictness by adding the prototype equality check - #43 |
I've seen people use
debounce
to do this:This can lead to really confusing problems if they create multiple instances of
MyClass
.In this case:
This leads to the method only ever being called on
c2
with arguments"b"
. It is almost certainly not what the user intended.I think it's fairly easy to catch that misuse here: index.js#L41
I think the solution is to compare
context
to previously set values. If it changes, then throwHappy to create a PR if anyone is still maintaining this.
The text was updated successfully, but these errors were encountered: