| 15 | */ |
| 16 | |
| 17 | export default class DefaultTimer { |
| 18 | constructor() { |
| 19 | this.subscribers = []; |
| 20 | this.loopId = null; |
| 21 | } |
| 22 | |
| 23 | loop = time => { |
| 24 | if (this.loopId) { |
| 25 | this.subscribers.forEach(callback => { |
| 26 | callback(time); |
| 27 | }); |
| 28 | } |
| 29 | |
| 30 | this.loopId = requestAnimationFrame(this.loop); |
| 31 | }; |
| 32 | |
| 33 | start() { |
| 34 | if (!this.loopId) { |
| 35 | this.loop(); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | stop() { |
| 40 | if (this.loopId) { |
| 41 | cancelAnimationFrame(this.loopId); |
| 42 | this.loopId = null; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | subscribe(callback) { |
| 47 | if (this.subscribers.indexOf(callback) === -1) |
| 48 | this.subscribers.push(callback); |
| 49 | } |
| 50 | |
| 51 | unsubscribe(callback) { |
| 52 | this.subscribers = this.subscribers.filter(s => s !== callback) |
| 53 | } |
| 54 | } |
nothing calls this directly
no outgoing calls
no test coverage detected