| 29 | } |
| 30 | |
| 31 | export class ProgressBar extends EventEmitter implements ProgressBarState { |
| 32 | private _current: number = 0 |
| 33 | public get current() { return this._current } |
| 34 | public set current(value: number) { |
| 35 | this._current = Math.max(value, 0) |
| 36 | this.checkCompleteEvent() |
| 37 | } |
| 38 | |
| 39 | private _total: number = 0 |
| 40 | public get total() { return this._total } |
| 41 | public set total(value: number) { |
| 42 | this._total = Math.max(value, 0) |
| 43 | this.checkCompleteEvent() |
| 44 | } |
| 45 | |
| 46 | private _completeEventEmitted = false |
| 47 | private get completeEventEmitted() { return this._completeEventEmitted } |
| 48 | private set completeEventEmitted(value: boolean) { |
| 49 | if (this.completeEventEmitted && !value) this.emit(Events.REACTIVATED) |
| 50 | this._completeEventEmitted = value |
| 51 | } |
| 52 | |
| 53 | public title: string |
| 54 | public readonly startTime = Date.now(); |
| 55 | |
| 56 | eventNames(): Array<string> { |
| 57 | return Object.values(Events); |
| 58 | } |
| 59 | |
| 60 | constructor({ current, total, title }: { current?: number, total?: number, title?: string } = {}) { |
| 61 | super() |
| 62 | this.current = current ?? this.current; |
| 63 | this.total = total ?? this.total |
| 64 | this.title = title ?? '' |
| 65 | this.checkCompleteEvent() |
| 66 | } |
| 67 | |
| 68 | private checkCompleteEvent() { |
| 69 | if (this.completeEventEmitted && !this.isComplete()) { |
| 70 | this.completeEventEmitted = false |
| 71 | } |
| 72 | |
| 73 | if (this.isComplete() && this.current > 0 && !this.completeEventEmitted) { |
| 74 | this.completeEventEmitted = true |
| 75 | this.emit(Events.COMPLETED) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | public tick(value: number) { |
| 80 | const incValue = (this.current + value) > this.total |
| 81 | ? Math.min(this.total - this.current, value) |
| 82 | : value |
| 83 | |
| 84 | this.current += incValue |
| 85 | this.emit(Events.TICK, incValue) |
| 86 | } |
| 87 | |
| 88 | public rate(): number { |
nothing calls this directly
no outgoing calls
no test coverage detected