|
| 1 | +/** |
| 2 | + * @module ember-clock |
| 3 | + */ |
| 4 | +import Service from '@ember/service'; |
| 5 | +import { tracked } from '@glimmer/tracking'; |
| 6 | +import { cancel, later } from '@ember/runloop'; |
| 7 | +import type { Timer } from '@ember/runloop'; |
| 8 | +import type Owner from '@ember/owner'; |
| 9 | +import { registerDestructor } from '@ember/destroyable'; |
| 10 | + |
| 11 | +const Interval = 1000; |
| 12 | + |
| 13 | +/** |
| 14 | + * ## ClockService |
| 15 | + * |
| 16 | + * The clock synchronizes to the local host's system clock and can be used to |
| 17 | + * display the time or to update time sensitive properties. |
| 18 | + * |
| 19 | + * @class ClockService |
| 20 | + * @namespace EmberClock |
| 21 | + */ |
| 22 | +export default class ClockService extends Service { |
| 23 | + disabled: boolean = false; |
| 24 | + |
| 25 | + /** |
| 26 | + * @property hour |
| 27 | + */ |
| 28 | + @tracked hour: number = 0; |
| 29 | + |
| 30 | + /** |
| 31 | + * @property minute |
| 32 | + */ |
| 33 | + @tracked minute: number = 0; |
| 34 | + |
| 35 | + /** |
| 36 | + * @property second |
| 37 | + */ |
| 38 | + @tracked second: number = 0; |
| 39 | + |
| 40 | + /** |
| 41 | + * Stores the next tick, so that it can be cancelled and the clock stopped. |
| 42 | + * @property nextTick |
| 43 | + * @private |
| 44 | + */ |
| 45 | + @tracked nextTick?: Timer; |
| 46 | + |
| 47 | + /** |
| 48 | + * @property isTicking |
| 49 | + * @readonly |
| 50 | + */ |
| 51 | + get isTicking() { |
| 52 | + return Boolean(this.nextTick); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Call `start()` |
| 57 | + * @method constructor |
| 58 | + */ |
| 59 | + constructor(owner: Owner) { |
| 60 | + super(owner); |
| 61 | + this.start(); |
| 62 | + registerDestructor(this, () => this.stop()); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Start the clock |
| 67 | + * |
| 68 | + * @method start |
| 69 | + * @private |
| 70 | + */ |
| 71 | + start() { |
| 72 | + this.tick(); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Stop the clock |
| 77 | + * |
| 78 | + * @method stop |
| 79 | + * @private |
| 80 | + */ |
| 81 | + stop() { |
| 82 | + cancel(this.nextTick); |
| 83 | + this.nextTick = undefined; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Set the time to the current time. |
| 88 | + * @method setTime |
| 89 | + * @private |
| 90 | + */ |
| 91 | + setTime() { |
| 92 | + const now = new Date(); |
| 93 | + this.second = now.getSeconds(); |
| 94 | + this.minute = now.getMinutes(); |
| 95 | + this.hour = now.getHours(); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Ticks the clock |
| 100 | + * @method tick |
| 101 | + * @private |
| 102 | + */ |
| 103 | + tick() { |
| 104 | + this.setTime(); |
| 105 | + if (this.disabled) { |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + this.nextTick = later(this, this.tick, Interval); |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +declare module '@ember/service' { |
| 114 | + interface Registry { |
| 115 | + clock: ClockService; |
| 116 | + } |
| 117 | +} |
0 commit comments