Skip to content

Commit

Permalink
Vendor throttle function, add to UI
Browse files Browse the repository at this point in the history
  • Loading branch information
gracianodias3 committed Jan 14, 2025
1 parent be17e69 commit 9be98ab
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 13 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ Zigbee2MQTT is made up of three modules, each developed in its own Github projec
### Developing

Zigbee2MQTT uses TypeScript (partially for now). Therefore after making changes to files in the `lib/` directory you need to recompile Zigbee2MQTT. This can be done by executing `pnpm run build`. For faster development instead of running `pnpm run build` you can run `pnpm run build-watch` in another terminal session, this will recompile as you change files.
In first time before building you need to run `pnpm install --include=dev`
Before submitting changes run `pnpm run test-with-coverage`, `pnpm run pretty:check` and `pnpm run eslint`

Before running any of the commands, you'll first need to run `pnpm install --include=dev`.
Before submitting changes run `pnpm run test:coverage`, `pnpm run pretty:check` and `pnpm run eslint`

## Supported devices

Expand Down
2 changes: 1 addition & 1 deletion lib/extension/receive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import assert from 'node:assert';
import bind from 'bind-decorator';
import debounce from 'debounce';
import stringify from 'json-stable-stringify-without-jsonify';
import throttle from 'throttleit';

import * as zhc from 'zigbee-herdsman-converters';

import logger from '../util/logger';
import * as settings from '../util/settings';
import throttle from '../util/throttler';
import utils from '../util/utils';
import Extension from './extension';

Expand Down
5 changes: 5 additions & 0 deletions lib/util/settings.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,11 @@
"title": "QoS",
"description": "QoS level for MQTT messages of this device"
},
"throttle": {
"type": "number",
"title": "Throttle",
"description": "The minimum time between payloads in seconds. Payloads received whilst the device is being throttled will be discarded"
},
"debounce": {
"type": "number",
"title": "Debounce",
Expand Down
23 changes: 23 additions & 0 deletions lib/util/throttler.ts
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> {
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);
});
};
}
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
"rimraf": "^6.0.1",
"semver": "^7.6.3",
"source-map-support": "^0.5.21",
"throttleit": "^2.1.0",
"winston": "^3.17.0",
"winston-syslog": "^2.7.1",
"winston-transport": "^4.9.0",
Expand Down
9 changes: 0 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9be98ab

Please sign in to comment.