aboutsummaryrefslogtreecommitdiffhomepage
path: root/desktop/sources/scripts/clock.js
blob: e1c0fda6977d58e2119b0c3ff6f587a00e9033e0 (plain)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
'use strict'

class Clock {
  constructor (bpm, callback) {
    this.bpm = 0
    this.callback = () => {}
    this.timer = null
    this.running = false
    this.setBpm(bpm)
  }

  setCallback (callback) {
    this.callback = callback
  }

  canSetBpm () {
    return true
  }

  getBpm () {
    return this.bpm
  }

  setBpm (bpm) {
    this.bpm = bpm
    this.reset()
  }

  reset () {
    if (this.timer) {
      clearInterval(this.timer)
    }

    if (this.running) {
      this.timer = setInterval(() => { this.callback() }, (60000 / this.bpm) / 4)
    }
  }

  setRunning (running) {
    this.running = running
    this.reset()
  }

  start () {
    this.setRunning(true)
  }

  stop () {
    this.setRunning(false)
  }

  toString () {
    return `${this.bpm}`
  }
}

module.exports = Clock