| 134 | } |
| 135 | |
| 136 | export class SimpleTimer implements ITimer { |
| 137 | public readonly ready = true |
| 138 | private _lasttime: number = 0 |
| 139 | private _time: number = 0 |
| 140 | private _timescale: number = 1 |
| 141 | private _state: ITimerState = 'Paused' |
| 142 | private _onReadyCb: ITimerReadyCallback = NULL_FN |
| 143 | private _onTickCb: ITimerTickCallback = NULL_FN |
| 144 | private _onStateChangeCb: ITimerStateChangeCallback = NULL_FN |
| 145 | |
| 146 | public constructor( |
| 147 | private _ticker: ITicker, |
| 148 | private _duration: number = Infinity |
| 149 | ) { } |
| 150 | |
| 151 | public get duration(): number { return this._duration } |
| 152 | |
| 153 | public get timescale(): number { return this._timescale } |
| 154 | public set timescale(timescale: number) { |
| 155 | if (timescale <= TIMESCALE_MIN || timescale > TIMESCALE_MAX) { |
| 156 | throw new Error(`timescale must be between ${TIMESCALE_MIN} and ${TIMESCALE_MAX}`) |
| 157 | } |
| 158 | this._timescale = timescale |
| 159 | } |
| 160 | |
| 161 | public get time(): number { return this._time } |
| 162 | public set time(time: number) { |
| 163 | if (time < 0) { time = 0 } |
| 164 | if (time === this._time) { return } |
| 165 | if (time > this._duration) { |
| 166 | this._time = this._duration |
| 167 | this.stop() |
| 168 | } else { |
| 169 | this._time = time |
| 170 | } |
| 171 | this._lasttime = this._ticker.now() |
| 172 | } |
| 173 | |
| 174 | private _setState(state: ITimerState): void { |
| 175 | if (this._state !== state) { |
| 176 | this._state = state |
| 177 | this._onStateChangeCb(this._state) |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | public get state(): ITimerState { return this._state } |
| 182 | public get progress(): number { return this.time / this._duration } |
| 183 | |
| 184 | public onReady(cb: ITimerReadyCallback): ITimer { |
| 185 | this._onReadyCb = cb |
| 186 | this._onReadyCb() // This is intended to make initialization process the same as MediaTimer |
| 187 | return this |
| 188 | } |
| 189 | public onTick(cb: ITimerTickCallback): ITimer { |
| 190 | this._onTickCb = cb |
| 191 | return this |
| 192 | } |
| 193 | public onStateChange(cb: ITimerStateChangeCallback): ITimer { |
nothing calls this directly
no outgoing calls
no test coverage detected