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

Add debounce method directly to Pin #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/PHPi/Traits/EventEmitterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@ public function once($event, callable $listener)

$this->on($event, $onceListener);
}

/**
* Similar to on but with debounce
* @param $event
* @param callable $listener
* @param float $debounce
*/
public function onDebounce($event, callable $listener,$debounce=0.5){
//Do it like this so it can be hidden from userspace
$this->once($event, function () use($event,$listener,$debounce) {
$listener();
//Re-add the press event after the debounce period
$this->getBoard()->getLoop()->addTimer($debounce, function () use($event,$listener){
$this->onDebounce($event,$listener);
});
});
}

public function removeListener($event, callable $listener)
{
Expand Down Expand Up @@ -105,4 +122,4 @@ public function eventListenerRemoved($event_name)
}


}
}