-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdivider.js
39 lines (35 loc) · 865 Bytes
/
divider.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
/** A simple divider. */
export default class Divider {
/**
* @param {function(this: THIS)} output Function to call at zero.
* @param {THIS=} opt_thisArg
* @template THIS
*/
constructor(output, opt_thisArg) {
/** @private @const {function(this: THIS)} */
this.output_ = output;
/** @private @const {THIS|undefined} */
this.this_ = opt_thisArg;
/** @private {number} */
this.counter_ = 0;
/** @type {number} */
this.period = 0; // TODO(sdh): make this a property?
}
/** @return {number} */
get counter() {
return this.counter_;
}
/** Reloads the divider. */
reload() {
this.counter_ = this.period;
}
/** Clocks the counter. */
clock() {
if (this.counter_ == 0) {
this.counter_ = this.period;
this.output_.call(this.this_);
} else {
this.counter_--;
}
}
}