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

feat(button): add a debounce time option on v2 #1690

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 12 additions & 7 deletions lib/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,6 @@ class Button extends Emitter {
last: null
};

// Create a debounce boundary on event triggers
// this avoids button events firing on
// press noise and false positives
const trigger = Fn.debounce(key => {
aliases[key].forEach(type => this.emit(type));
}, 7);

let pinValue = typeof options === "object" ? options.pin : options;

Board.Component.call(
Expand All @@ -210,6 +203,11 @@ class Button extends Emitter {

this.pulldown = options.pulldown || options.isPulldown || false;

// `debounceTime` is a duration (milliseconds)
// to prevent button events firing on
// press noise and false positives
this.debounceTime = options.debounceTime || 7;

// Turns out some button circuits will send
// 0 for up and 1 for down, and some the inverse,
// so we can invert our function with this option.
Expand Down Expand Up @@ -275,6 +273,13 @@ class Button extends Emitter {
}
});

// Create a debounce boundary on event triggers
// this avoids button events firing on
// press noise and false positives
const trigger = Fn.debounce(key => {
aliases[key].forEach(type => this.emit(type));
}, this.debounceTime);

/* istanbul ignore else */
if (typeof this.initialize === "function") {
this.initialize(options, data => {
Expand Down