Description
Goal of this component is to recieve array of strings where every element is one state
and to append them in html while showing the loading spinner.
Even if states change quickly, this component will keep every state for at least miliseconds
that are defines in minDelay variable.
Implements
Example
Methods
|
Public
appendState
|
appendState(state: string)
|
|
|
Parameters :
| Name |
Type |
Optional |
| state |
string
|
No
|
|
|
Public
clearStates
|
clearStates()
|
|
|
|
|
|
Public
replaceLastState
|
replaceLastState(state: string)
|
|
|
Parameters :
| Name |
Type |
Optional |
| state |
string
|
No
|
|
|
Private
startStater
|
startStater()
|
|
|
|
|
|
Private
minDelay
|
Type : number
|
Default value : 1000
|
|
|
|
Public
processedStates
|
Type : string[]
|
Default value : []
|
|
|
|
Private
stateInterval
|
Type : any
|
|
|
|
Public
staterInProgress
|
Type : boolean
|
Default value : false
|
|
|
|
|
|
Public
statesList
|
Type : string[]
|
Default value : []
|
|
|
import { Component, OnInit, ViewEncapsulation } from '@angular/core'
/**
* Goal of this component is to recieve array of strings where every element is one state
* and to append them in html while showing the loading spinner.
* Even if states change quickly, this component will keep every state for at least miliseconds
* that are defines in `minDelay` variable.
*/
@Component({
selector: 'app-upload-stater',
templateUrl: './upload-stater.component.html',
styleUrls: ['./upload-stater.component.scss'],
encapsulation: ViewEncapsulation.None,
standalone: false
})
export class UploadStaterComponent implements OnInit {
public statesList: string[] = [] //States appended to be displayed
public processedStates: string[] = [] //States that has been displayed and processed
/**
* Stater config
*/
public staterInProgress: boolean = false
private stateInterval: any
private minDelay: number = 1000
constructor() {}
ngOnInit(): void {}
public appendState(state: string) {
if (state === '{finish}') {
/**
* Stop the stater progress
*/
this.staterInProgress = false
clearInterval(this.stateInterval)
return
}
this.statesList.push(state)
this.processedStates.push(state)
// We are disabling delaying functionality for now
// if (!this.staterInProgress) {
// this.startStater()
// }
}
public replaceLastState(state: string) {
if (this.statesList.length > 0) {
this.statesList.pop()
this.statesList.push(state)
} else {
this.processedStates[this.processedStates.length - 1] = state
}
}
public clearStates() {
this.processedStates = []
}
private startStater() {
this.staterInProgress = true
//First run
if (this.statesList.length > 0) {
this.processedStates.push(this.statesList.shift() || '')
}
this.stateInterval = setInterval(() => {
if (this.statesList.length > 0) {
this.processedStates.push(this.statesList.shift() || '')
}
}, this.minDelay)
}
}
<div>
@for (state of processedStates; track state; let i = $index) {
<p class="m-0">
{{ state }}
</p>
}
</div>
<div class="text-center">
<span class="spinner spinner-sm vertical-align-middle">Loading...</span>
</div>
Legend
Html element with directive