forked from revolunet/s63
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRotary.js
43 lines (39 loc) · 1.04 KB
/
Rotary.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"use strict";
var EventEmitter = require('events').EventEmitter;
const PULSE_VALUES = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
const PULSE_TIMEOUT = 500;
const COMPOSE_TIMEOUT = 2000;
class Rotary extends EventEmitter {
constructor() {
super()
this.value = '';
this.pulseCount = 0;
}
onPulse() {
if (this.pulseCount === 0) {
this.emit('compositionstart');
}
this.pulseCount++;
if (this.pulseTimeout) {
clearTimeout(this.pulseTimeout);
}
if (this.composeTimeout) {
clearTimeout(this.composeTimeout);
}
this.pulseTimeout = setTimeout(this.onPulseTimeout.bind(this), PULSE_TIMEOUT)
}
onPulseTimeout() {
// last pulse received - store number
const num = PULSE_VALUES[this.pulseCount - 1];
this.value += num;
this.pulseCount = 0;
this.composeTimeout = setTimeout(this.onComposeTimeout.bind(this), COMPOSE_TIMEOUT)
}
onComposeTimeout() {
// composition timeout
this.emit('compositionend', this.value);
this.value = '';
this.pulseCount = 0;
}
}
module.exports = Rotary