All files / src/app/core/utils/script-loader script-loader.service.ts

28.57% Statements 8/28
54.54% Branches 6/11
16.66% Functions 1/6
26.92% Lines 7/26

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 51 52 53 54 55 56 57 58 59 60 617x 7x 7x               7x 27x     27x   27x                                                                                        
import { DOCUMENT } from '@angular/common';
import { Inject, Injectable, Renderer2, RendererFactory2 } from '@angular/core';
import { Observable, Observer } from 'rxjs';
 
interface ScriptType {
  src: string;
  loaded: boolean;
}
 
@Injectable({ providedIn: 'root' })
export class ScriptLoaderService {
  private registeredScripts: ScriptType[] = [];
  private renderer: Renderer2;
 
  constructor(private rendererFactory: RendererFactory2, @Inject(DOCUMENT) private document: Document) {
    // Get an instance of Renderer2
    this.renderer = this.rendererFactory.createRenderer(undefined, undefined);
  }
 
  /**
   * load a script, if it has not already been loaded
   *
   * @param url  script url, e.g. https://pptest.payengine.de/bridge/1.0/payengine.min.js
   */
 
  load(url: string): Observable<ScriptType> {
    return new Observable<ScriptType>((observer: Observer<ScriptType>) => {
      let script = this.registeredScripts.find(s => s.src === url);
      Iif (!script) {
        script = { src: url, loaded: false };
        this.registeredScripts.push(script);
      }
 
      // Complete if already loaded
      if (script?.loaded) {
        observer.next(script);
        observer.complete();
      } else {
        // Load the script
        const scriptElement = this.renderer.createElement('script');
        scriptElement.type = 'text/javascript';
        scriptElement.src = script.src;
        scriptElement.async = true;
 
        scriptElement.onload = () => {
          script.loaded = true;
          observer.next(script);
          observer.complete();
        };
 
        scriptElement.onerror = () => {
          observer.error(`Could not load script ${script.src}`);
        };
 
        // insert script as html body child
        this.renderer.appendChild(this.document.body, scriptElement);
      }
    });
  }
}