{"version":3,"file":"ngx-version-check.mjs","sources":["../../../projects/version-check/src/lib/version-check.service.ts","../../../projects/version-check/src/public-api.ts","../../../projects/version-check/src/ngx-version-check.ts"],"sourcesContent":["/**\n * Author: Henrik Peinar\n * https://blog.nodeswat.com/automagic-reload-for-clients-after-deploy-with-angular-4-8440c9fdd96c\n */\nimport { Injectable } from '@angular/core'\nimport { HttpClient } from '@angular/common/http'\nimport { interval, Subscription } from 'rxjs'\n\nexport interface IVersionCheck {\n  /** (Optional) The notification method to call from the client if there is a new version available. */\n  notification?: any\n\n  /** (Required) The frequency in milliseconds (defaults to 30 minutes). */\n  frequency: number\n}\n\n@Injectable({\n  providedIn: 'root'\n})\nexport class VersionCheckService {\n  // These will be replaced by the post-build.js script\n  private currentHash = '{{POST_BUILD_ENTERS_HASH_HERE}}'\n  private version = '{{POST_BUILD_ENTERS_VERSION_HERE}}'\n\n  // Private properties\n  private newVersionAvailable: boolean = false\n  private versionCheckInterval: Subscription\n\n  constructor(private http: HttpClient) {}\n\n  /** Will do the call and check if the hash has changed or not. */\n  public checkVersion(notification: any) {\n    // Timestamp these requests to invalidate caches\n    this.http.get(`version.json?t=${new Date().getTime()}`).subscribe(\n      (response: any) => {\n        this.newVersionAvailable = this.hasHashChanged(this.currentHash, response.hash)\n\n        // Stop checking for a new version if a new version is already available\n        if (this.newVersionAvailable) {\n          this.stopVersionChecking()\n\n          // Call the consuming client's notification method if one exists\n          if (notification) notification()\n        }\n      },\n      err => {\n        console.error(err, 'Error checking version')\n      }\n    )\n  }\n\n  /**\n   * Starts the version check interval for the specified frequency.\n   * @param config The configuration parameters for the notification function and version check frequency.\n   */\n  public startVersionChecking(config: IVersionCheck = { notification: null, frequency: 1800000 }) {\n    this.versionCheckInterval = interval(config.frequency).subscribe(() => {\n      this.checkVersion(config.notification)\n    })\n  }\n\n  /** Stops the version check interval. */\n  public stopVersionChecking() {\n    this.versionCheckInterval.unsubscribe()\n  }\n\n  /**\n   * Checks if hash has changed.\n   * This file has the JS hash, if it is a different one than in the version.json\n   * we are dealing with version change\n   * @param currentHash The current hash of the application.\n   * @param newHash The new application hash from the version.json file.\n   * @returns Boolean value determining if the hash has changed between the application and version.json file.\n   */\n  private hasHashChanged(currentHash, newHash): boolean {\n    if (!currentHash || currentHash === '{{POST_BUILD_ENTERS_HASH_HERE}}') {\n      return false\n    }\n\n    return currentHash !== newHash\n  }\n\n  /** The current build hash of the application */\n  get Hash(): string {\n    return this.currentHash\n  }\n\n  /** The current version number of the application */\n  get Version(): string {\n    return this.version\n  }\n\n  /** Flag showing if a new version of the application is available. */\n  get NewVersionAvailable(): boolean {\n    return this.newVersionAvailable\n  }\n}\n","/*\n * Public API Surface of version-check\n */\n\nexport * from './lib/version-check.service'\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;;;AAAA;;;AAGG;MAgBU,mBAAmB,CAAA;AAS9B,IAAA,WAAA,CAAoB,IAAgB,EAAA;QAAhB,IAAI,CAAA,IAAA,GAAJ,IAAI,CAAY;;QAP5B,IAAW,CAAA,WAAA,GAAG,iCAAiC,CAAA;QAC/C,IAAO,CAAA,OAAA,GAAG,oCAAoC,CAAA;;QAG9C,IAAmB,CAAA,mBAAA,GAAY,KAAK,CAAA;KAGJ;;AAGjC,IAAA,YAAY,CAAC,YAAiB,EAAA;;QAEnC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAkB,eAAA,EAAA,IAAI,IAAI,EAAE,CAAC,OAAO,EAAE,CAAA,CAAE,CAAC,CAAC,SAAS,CAC/D,CAAC,QAAa,KAAI;AAChB,YAAA,IAAI,CAAC,mBAAmB,GAAG,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,WAAW,EAAE,QAAQ,CAAC,IAAI,CAAC,CAAA;;YAG/E,IAAI,IAAI,CAAC,mBAAmB,EAAE;gBAC5B,IAAI,CAAC,mBAAmB,EAAE,CAAA;;AAG1B,gBAAA,IAAI,YAAY;AAAE,oBAAA,YAAY,E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