Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 1 | /** |
| 2 | * Created by obarda on 7/7/2016. |
| 3 | */ |
| 4 | |
| 5 | 'use strict'; |
| 6 | import IIntervalService = angular.IIntervalService; |
| 7 | |
| 8 | export class ProgressService { |
| 9 | |
| 10 | public progresses:any = {}; |
| 11 | |
| 12 | static '$inject' = ['$interval']; |
| 13 | |
| 14 | constructor(protected $interval:any) { |
| 15 | } |
| 16 | |
| 17 | private totalProgress:number = 90; |
| 18 | private startProgress:number = 10; |
| 19 | private onePercentIntervalSeconds:number = 5; |
| 20 | private createComponentInterval; |
| 21 | |
| 22 | public setProgressValue(name:string, value:number):void { |
| 23 | if (!this.progresses[name]) { |
| 24 | this.progresses[name] = {}; |
| 25 | } |
| 26 | this.progresses[name].value = value; |
| 27 | } |
| 28 | |
| 29 | public getProgressValue(name:string):number { |
| 30 | if (this.progresses[name]) { |
| 31 | return this.progresses[name].value; |
| 32 | } |
| 33 | return 0; |
| 34 | } |
| 35 | |
| 36 | public deleteProgressValue(name:string):void { |
| 37 | this.stopCreateComponentInterval(); |
| 38 | delete this.progresses[name]; |
| 39 | } |
| 40 | |
| 41 | |
| 42 | private stopCreateComponentInterval = ():void => { |
| 43 | this.$interval.cancel(this.createComponentInterval); |
| 44 | }; |
| 45 | |
| 46 | |
| 47 | public initCreateComponentProgress = (componentId:string):void => { |
| 48 | let progressValue:number = this.startProgress; |
| 49 | if (!this.getProgressValue(componentId)) { |
| 50 | this.stopCreateComponentInterval(); |
| 51 | this.setProgressValue(componentId, this.startProgress); |
| 52 | this.createComponentInterval = this.$interval(():void => { |
| 53 | //TODO replace getProgressMockData to real data after BE provide the API |
| 54 | let progressValue = this.getProgressMockData(componentId); |
| 55 | if (progressValue <= this.totalProgress) { |
| 56 | this.setProgressValue(componentId, progressValue); |
| 57 | } else { |
| 58 | /** |
| 59 | * Currently the progress is not really checking against the BE. |
| 60 | * So the progress can pass 100. So the workaround for now, in case we pass 90 (totalProgress) |
| 61 | * stop the interval, so the progress will be kept at 90 until the promise will return value and set |
| 62 | * the progress to 100. |
| 63 | */ |
| 64 | this.deleteProgressValue(componentId); |
| 65 | } |
| 66 | }, this.onePercentIntervalSeconds * 1000); |
| 67 | } |
| 68 | |
| 69 | }; |
| 70 | |
| 71 | |
| 72 | private getProgressMockData = (id:string):number => { |
| 73 | let progressValue = this.getProgressValue(id); |
| 74 | if (progressValue > 0) { |
| 75 | progressValue = progressValue + 1; |
| 76 | } |
| 77 | //if not finish always stay on 90% |
| 78 | if (progressValue > 90) { |
| 79 | progressValue = 90; |
| 80 | } |
| 81 | |
| 82 | return progressValue; |
| 83 | } |
| 84 | |
| 85 | } |