-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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 throttling #25801
base: dev
Are you sure you want to change the base?
Fix throttling #25801
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
export default function throttle<Args extends unknown[]>(fn: (...args: Args) => Promise<void>, wait: number): (...args: Args) => Promise<void> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't test it, but this looks to be messing with context, and also the return type is no longer generic as before. And also include a link to the source. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for catching that. While my tests pass and it's working on my system, there are some TypeScript issues to address. I tried implementing your suggestion but ran into problems with the any type. We'd need to enable noImplicitThis to use ThisType. I'm not very experienced with some TypeScript features, so there might be a simple solution I'm missing. I have a partial solution, but it's not perfect: export default function throttle<T extends (...arguments_: unknown[]) => unknown>(function_: T, wait: number): T {
if (typeof function_ !== 'function') {
throw new TypeError(`Expected the first argument to be a \`function\`, got \`${typeof function_}\`.`);
}
// TODO: Add `wait` validation too in the next major version.
let timeoutId: NodeJS.Timeout;
let lastCallTime = 0;
return function throttled(...arguments_) {
clearTimeout(timeoutId);
const now = Date.now();
const timeSinceLastCall = now - lastCallTime;
const delayForNextCall = wait - timeSinceLastCall;
if (delayForNextCall <= 0) {
lastCallTime = now;
function_.apply(this, arguments_);
} else {
timeoutId = setTimeout(() => {
lastCallTime = Date.now();
function_.apply(this, arguments_);
}, delayForNextCall);
}
} as T;
} This still relies on runtime type checking. While we could use runtime type checking, I prefer not to since:
You're right about adding attribution. If you know a better way to make the type checker happy, I'm interested. While my current TypeScript implementation works, I'm not sure if changing the function's context could cause problems. |
||
let timeoutId: ReturnType<typeof setTimeout>; | ||
let lastCallTime = 0; | ||
|
||
return (...args: Args) => { | ||
clearTimeout(timeoutId); | ||
const now = Date.now(); | ||
const timeSinceLastCall = now - lastCallTime; | ||
const delayForNextCall = wait - timeSinceLastCall; | ||
|
||
if (delayForNextCall <= 0) { | ||
lastCallTime = now; | ||
return fn(...args); | ||
} | ||
|
||
return new Promise<void>((resolve) => { | ||
timeoutId = setTimeout(() => { | ||
lastCallTime = Date.now(); | ||
resolve(fn(...args)); | ||
}, delayForNextCall); | ||
}); | ||
}; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will keep stale values in memory indefinitely (until Z2M shutdown). Better to invalidate the throttler when the option changes for a device.
Can't we just set the option as "restart mandatory" though (cleaner for such a feature)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for review! I am again running my message in ChatGPT, please tell me if unclear.
I considered the memory issue, but here's why I think it's okay:
2*throttleValue
using setTimeout, but this adds complexity for minimal benefit.That's not user-friendly. Plus, as we've seen, restarting Home Assistant on HAOS doesn't restart Zigbee2MQTT, which confuses users.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm also in favour of this. The restart button will automatically show up in the frontend. Settings
requiresRestart
true
will enable this:zigbee2mqtt/lib/util/settings.schema.json
Line 57 in 060ae99