import { Observable, Subject } from 'rxjs';
import { share } from 'rxjs/operators';

export class FileDownloadItem {
  downloadTime: number;
  downloadingProgress$: Observable<number>;

  private downloadingProgress: Subject<number>;
  private downloadStartTime: number;

  constructor() {}

  downloadStart(): Subject<number> {
    this.downloadStartTime = new Date().getTime();
    this.downloadingProgress = new Subject<number>();
    this.downloadingProgress$ = this.downloadingProgress
      .asObservable()
      .pipe(share());
    return this.downloadingProgress;
  }

  downloadComplete() {
    const endTime = new Date().getTime();
    this.downloadTime = endTime - this.downloadStartTime;
    this.downloadingProgress.complete();
  }
}
