button-events
Advanced tools
Comparing version 1.0.2 to 1.1.0
65
index.js
@@ -10,3 +10,7 @@ | ||
const STATE_DOUBLE_CLICKED = 5; | ||
const STATE_RELEASE_WAIT = 6; | ||
const STATE_DOUBLE_CLICKED_PRESSED = 6; | ||
const STATE_TRIPLE_CLICKED = 7; | ||
const STATE_TRIPLE_CLICKED_PRESSED = 8; | ||
const STATE_QUADRUPLE_CLICKED = 9; | ||
const STATE_RELEASE_WAIT = 10; | ||
@@ -23,2 +27,6 @@ // low level button state events | ||
const EVENT_BUTTON_DOUBLE_CLICKED = 'double_clicked'; | ||
const EVENT_BUTTON_DOUBLE_CLICKED_PRESSED = 'double_clicked_pressed'; | ||
const EVENT_BUTTON_TRIPLE_CLICKED = 'triple_clicked'; | ||
const EVENT_BUTTON_TRIPLE_CLICKED_PRESSED = 'triple_clicked_pressed'; | ||
const EVENT_BUTTON_QUADRUPLE_CLICKED = 'quadruple_clicked'; | ||
const EVENT_BUTTON_RELEASED = 'released'; | ||
@@ -97,2 +105,3 @@ | ||
if (this.lastValue) { | ||
clearTimeout(this.emitTimer); | ||
// debounced button press | ||
@@ -104,9 +113,15 @@ this.emit(EVENT_BUTTON_PRESS); | ||
// transition from a clicked state to clicked and pressed | ||
clearTimeout(this.emitTimer); | ||
this.buttonState = STATE_CLICKED_PRESSED; | ||
break; | ||
case STATE_DOUBLE_CLICKED: | ||
this.buttonState = STATE_DOUBLE_CLICKED_PRESSED; | ||
break; | ||
case STATE_TRIPLE_CLICKED: | ||
this.buttonState = STATE_TRIPLE_CLICKED_PRESSED; | ||
break; | ||
default: | ||
// begin the pressed state | ||
clearTimeout(this.emitTimer); | ||
this.buttonState = STATE_PRESSED; | ||
@@ -134,2 +149,18 @@ } | ||
this.buttonState = STATE_DOUBLE_CLICKED; | ||
// delay event to allow for further state transition | ||
this.emitTimer = setTimeout(this.emitState.bind(this), this.Config.timing.clicked); | ||
break; | ||
case STATE_DOUBLE_CLICKED_PRESSED: | ||
// transition from double clicked and pressed to triple clicked | ||
clearTimeout(this.emitTimer); | ||
this.buttonState = STATE_TRIPLE_CLICKED; | ||
// delay event to allow for further state transition | ||
this.emitTimer = setTimeout(this.emitState.bind(this), this.Config.timing.clicked); | ||
break; | ||
case STATE_TRIPLE_CLICKED_PRESSED: | ||
// transition from triple clicked and pressed to quadruple clicked | ||
clearTimeout(this.emitTimer); | ||
this.buttonState = STATE_QUADRUPLE_CLICKED; | ||
// no further transitions | ||
@@ -179,2 +210,30 @@ this.emitState(); | ||
case STATE_DOUBLE_CLICKED_PRESSED: | ||
// emit event and transition to release wait | ||
this.emit(EVENT_BUTTON_DOUBLE_CLICKED_PRESSED); | ||
this.emit(EVENT_BUTTON_STATUS, EVENT_BUTTON_DOUBLE_CLICKED_PRESSED); | ||
this.buttonState = STATE_RELEASE_WAIT; | ||
break; | ||
case STATE_TRIPLE_CLICKED: | ||
// emit event and transition to idle | ||
this.emit(EVENT_BUTTON_TRIPLE_CLICKED); | ||
this.emit(EVENT_BUTTON_STATUS, EVENT_BUTTON_TRIPLE_CLICKED); | ||
this.buttonState = STATE_IDLE; | ||
break; | ||
case STATE_TRIPLE_CLICKED_PRESSED: | ||
// emit event and transition to release wait | ||
this.emit(EVENT_BUTTON_TRIPLE_CLICKED_PRESSED); | ||
this.emit(EVENT_BUTTON_STATUS, EVENT_BUTTON_TRIPLE_CLICKED_PRESSED); | ||
this.buttonState = STATE_RELEASE_WAIT; | ||
break; | ||
case STATE_QUADRUPLE_CLICKED: | ||
// emit event and transition to idle | ||
this.emit(EVENT_BUTTON_QUADRUPLE_CLICKED); | ||
this.emit(EVENT_BUTTON_STATUS, EVENT_BUTTON_QUADRUPLE_CLICKED); | ||
this.buttonState = STATE_IDLE; | ||
break; | ||
case STATE_RELEASE_WAIT: | ||
@@ -181,0 +240,0 @@ // emit event and transition to idle |
{ | ||
"name": "button-events", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Button event module used to debounce a digital input signal and produce button action events.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "node test.js" | ||
}, | ||
@@ -9,0 +9,0 @@ "repository": { |
# Button Events | ||
Button event module used to generate simple button events, i.e. *'clicked'*, *'double_clicked'* | ||
from complex user interactions with a button input. | ||
Button event module used to generate simple button events, i.e. *'clicked'*, | ||
*'double_clicked'*, etc., from complex user interactions with a button input. | ||
@@ -12,3 +12,3 @@ The module is used to create class instances each with a method that accepts a binary input | ||
high level button event types, i.e. *'clicked'*, *'double_clicked'*, *'pressed'*, *'released'*, | ||
*'clicked_pressed'*. | ||
*'clicked_pressed'*, etc. | ||
@@ -170,2 +170,6 @@ | ||
- double_clicked | ||
- double_clicked_pressed | ||
- triple_clicked | ||
- triple_clicked_pressed | ||
- quadruple_clicked | ||
- released | ||
@@ -228,2 +232,19 @@ | ||
## double_clicked_pressed | ||
If a double clicked is followed with pressing the button again then the | ||
*double_clicked_pressed* event will be emitted. | ||
## triple_clicked | ||
The triple clicked event follows a double_clicked_pressed. | ||
## triple_clicked_pressed | ||
A press following the triple clicked event results in tirple_clicked_pressed. | ||
## quadruple_clicked | ||
A quadruple_clicked event follows the triple_clicked_pressed event. | ||
## released | ||
@@ -230,0 +251,0 @@ When one of the pressed type events is generated the button is placed in a state where |
65
test.js
const ButtonEvents = require('./index.js'); | ||
let passed; | ||
let watch = () => passed = true; | ||
let Noisy = true; | ||
@@ -33,9 +36,21 @@ | ||
async function testSequence() { | ||
await click(); | ||
await test('Test clicked event...', click, 'clicked'); | ||
await test('Test double_clicked event...', doubleClicked, 'double_clicked'); | ||
await test('Test triple_clicked event...', tripleClicked, 'triple_clicked'); | ||
await test('Test quadple_clicked event...', quadrupleClicked, 'quadruple_clicked'); | ||
await test('Test press event..', pressRelease, 'pressed'); | ||
await test('Test released event..', pressRelease, 'released'); | ||
await test('Test clicked_pressed...', clickedPressedRelease, 'clicked_pressed'); | ||
await test('Test double_clicked_pressed...', doubleClickedPressedRelease, 'double_clicked_pressed'); | ||
await test('Test triple_clicked_pressed...', tripleClickedPressedRelease, 'triple_clicked_pressed'); | ||
} | ||
async function test (msg, method, event) { | ||
passed = false; | ||
be.on(event, watch); | ||
console.log(msg); | ||
await method(); | ||
await sleep(500); | ||
await doubleClicked(); | ||
await sleep(500); | ||
await pressRelease(); | ||
await sleep(500); | ||
await clickPressRelease(); | ||
be.removeListener(event, watch); | ||
if (!passed) console.log('Test failed.'); | ||
} | ||
@@ -55,2 +70,20 @@ | ||
async function tripleClicked () { | ||
await click(); | ||
await sleep(100); | ||
await click(); | ||
await sleep(100); | ||
await click(); | ||
} | ||
async function quadrupleClicked () { | ||
await click(); | ||
await sleep(100); | ||
await click(); | ||
await sleep(100); | ||
await click(); | ||
await sleep(100); | ||
await click(); | ||
} | ||
async function pressRelease () { | ||
@@ -62,5 +95,19 @@ await press(); | ||
async function clickPressRelease () { | ||
await click(); | ||
await sleep(100); | ||
async function clickedPressedRelease () { | ||
return await clickedPressedCountRelease(1); | ||
} | ||
async function doubleClickedPressedRelease () { | ||
return await clickedPressedCountRelease(2); | ||
} | ||
async function tripleClickedPressedRelease () { | ||
return await clickedPressedCountRelease(3); | ||
} | ||
async function clickedPressedCountRelease (clicks) { | ||
for (let i =0; i < clicks; i++) { | ||
await click(); | ||
await sleep(100); | ||
} | ||
await pressRelease(); | ||
@@ -67,0 +114,0 @@ } |
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
21920
335
1
294