All files / app/util async.ts

91.3% Statements 21/23
100% Branches 0/0
83.33% Functions 10/12
90.9% Lines 20/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 5177x     2x                 3x 3x 3x 3x 3x   3x     3x 3x 3x                       5x 5x 5x 5x 5x   5x     5x 5x 5x        
import { Observable } from 'rxjs';
 
export function delay(ms: number) {
  return new Promise(resolve => setTimeout(resolve, ms));
}
 
/**
 * Reads a File or Blob as a Data URL using FileReader and returns an Observable.
 * @param file The File or Blob to read.
 * @returns An Observable that emits the Data URL string upon successful completion.
 */
export function readFileAsDataURL(file: Blob): Observable<string> {
  return new Observable<string>((subscriber) => {
    const reader = new FileReader();
    reader.onload = () => {
      subscriber.next(reader.result as string);
      subscriber.complete();
    };
    reader.onerror = (error) => {
      subscriber.error(error);
    };
    reader.readAsDataURL(file);
    return () => {
      reader.abort();
    };
  });
}
 
 
/**
 * Reads a File or Blob as a string using FileReader and returns an Observable.
 * @param file The File or Blob to read.
 * @returns An Observable that emits the string upon successful completion.
 */
export function readFileAsString(file: Blob): Observable<string> {
  return new Observable<string>((subscriber) => {
    const reader = new FileReader();
    reader.onload = () => {
      subscriber.next(reader.result as string);
      subscriber.complete();
    };
    reader.onerror = (error) => {
      subscriber.error(error);
    };
    reader.readAsText(file);
    return () => {
      reader.abort();
    };
  });
}