{"version":3,"file":"idle-timeout.esm.min.mjs","names":["IdleTimeout","_options$element","_options$loop","_options$timeout","event","pageX","pageY","callback","options","element","eventName","remainingTime","value","idleTimeout"],"sources":["../src/IdleTimeout.ts","../src/index.ts"],"sourcesContent":["import { Options, TimeoutCallback, UserOptions } from './types';\n\n/** Creates an idle timeout instance. */\nexport class IdleTimeout {\n  /** The callback function to invoke when the timeout is complete. */\n  protected callback: TimeoutCallback;\n\n  /** The merged configuration options for the timeout. */\n  protected options: Options;\n\n  /** The timeout handle to reset the timeout. */\n  protected timeoutHandle: number | null = null;\n\n  /** Whether the timeout is idle. */\n  protected isIdle: boolean = false;\n\n  /** Whether the timeout instance has been destroyed. */\n  protected isDestroyed: boolean = false;\n\n  /** The start time of the timeout in milliseconds. */\n  protected startTime: number = 0;\n\n  /** The remaining time of the timeout in milliseconds. */\n  protected remainingTime: number = 0;\n\n  /** The last X-axis position on the page. */\n  protected lastPageX: number = -1;\n\n  /** The last Y-axis position on the page. */\n  protected lastPageY: number = -1;\n\n  /** The input event names. */\n  protected eventNames: string[] = [\n    'DOMMouseScroll',\n    'mousedown',\n    'mousemove',\n    'mousewheel',\n    'MSPointerDown',\n    'MSPointerMove',\n    'keydown',\n    'touchmove',\n    'touchstart',\n    'wheel'\n  ];\n\n  /**\n   * The idle timeout constructor.\n   * @param {Function} callback The callback function to invoke when the timeout is complete.\n   * @param {object} [options] The configuration options for the timeout.\n   * @returns {void}\n   */\n  public constructor(callback: TimeoutCallback, options?: UserOptions) {\n    this.callback = callback;\n    this.options = {\n      element: options?.element ?? document.body,\n      loop: options?.loop ?? false,\n      timeout: options?.timeout ?? 60 * 1000 * 5 // 5 minutes\n    };\n\n    const element = this.options.element;\n\n    for (const eventName of this.eventNames) {\n      element.addEventListener(eventName, this.handleEvent);\n    }\n\n    this.resetTimeout();\n  }\n\n  /**\n   * Pause the timeout.\n   * @returns {void}\n   */\n  public pause(): void {\n    if (this.isDestroyed || this.remainingTime > 0) {\n      return;\n    }\n\n    const remainingTime: number = this.startTime + this.options.timeout - Date.now();\n    if (remainingTime <= 0) {\n      return;\n    }\n\n    this.remainingTime = remainingTime;\n\n    if (this.timeoutHandle) {\n      window.clearTimeout(this.timeoutHandle);\n      this.timeoutHandle = null;\n    }\n  }\n\n  /**\n   * Resume the paused timeout.\n   * @returns {void}\n   */\n  public resume(): void {\n    if (this.isDestroyed) {\n      return;\n    }\n\n    if (this.remainingTime <= 0) {\n      return;\n    }\n\n    this.resetTimeout();\n    // Preserve elapsed active time so a later pause uses the remaining countdown.\n    this.startTime -= this.options.timeout - this.remainingTime;\n    this.remainingTime = 0;\n  }\n\n  /**\n   * Reset the timeout.\n   * @returns {void}\n   */\n  public reset(): void {\n    if (this.isDestroyed) {\n      return;\n    }\n\n    this.isIdle = false;\n    this.remainingTime = 0;\n    this.resetTimeout();\n  }\n\n  /**\n   * Destroy the instance.\n   * @returns {void}\n   */\n  public destroy(): void {\n    if (this.isDestroyed) {\n      return;\n    }\n\n    const element = this.options.element;\n\n    for (const eventName of this.eventNames) {\n      element.removeEventListener(eventName, this.handleEvent);\n    }\n\n    if (this.timeoutHandle) {\n      window.clearTimeout(this.timeoutHandle);\n      this.timeoutHandle = null;\n    }\n\n    this.isDestroyed = true;\n  }\n\n  /**\n   * Reset the timeout function.\n   * @returns {void}\n   */\n  protected resetTimeout(): void {\n    if (this.isDestroyed) {\n      return;\n    }\n\n    if (this.timeoutHandle) {\n      window.clearTimeout(this.timeoutHandle);\n      this.timeoutHandle = null;\n    }\n\n    if (this.isIdle && !this.options.loop) {\n      return;\n    }\n\n    this.timeoutHandle = window.setTimeout((): void => {\n      this.handleTimeout();\n    }, this.remainingTime || this.options.timeout);\n\n    this.startTime = Date.now();\n  }\n\n  /**\n   * Handle the input events.\n   * @param {Event} event The input event.\n   * @returns {void}\n   */\n  protected handleEvent = (event: Event): void => {\n    if (this.isDestroyed) {\n      return;\n    }\n\n    if (this.remainingTime > 0) {\n      return;\n    }\n\n    if (event.type === 'mousemove') {\n      if (!(event instanceof MouseEvent)) {\n        return;\n      }\n\n      const { pageX, pageY } = event;\n      if (\n        (pageX === undefined && pageY === undefined) ||\n        (pageX === this.lastPageX && pageY === this.lastPageY)\n      ) {\n        return;\n      }\n\n      this.lastPageX = pageX;\n      this.lastPageY = pageY;\n    }\n\n    this.resetTimeout();\n  };\n\n  /**\n   * Handle the completed timeout.\n   * @returns {void}\n   */\n  protected handleTimeout(): void {\n    if (this.isDestroyed) {\n      return;\n    }\n\n    this.isIdle = true;\n    this.resetTimeout();\n\n    this.callback(this.options.element, this.options.timeout);\n  }\n\n  /** Gets whether the timeout is idle. */\n  public get idle(): boolean {\n    return this.isIdle;\n  }\n\n  /**\n   * Sets whether the timeout should restart on completion.\n   * @param {boolean} value Whether the timeout should restart on completion.\n   */\n  public set loop(value: boolean) {\n    this.options.loop = value;\n  }\n\n  /**\n   * Sets the idle timeout in milliseconds.\n   * @param {number} value The idle timeout in milliseconds.\n   */\n  public set timeout(value: number) {\n    this.options.timeout = value;\n    this.remainingTime = 0;\n    this.resetTimeout();\n  }\n\n  /**\n   * Sets whether the timeout is idle.\n   * @param {boolean} value Whether the timeout is idle.\n   */\n  public set idle(value: boolean) {\n    if (this.isDestroyed) {\n      return;\n    }\n\n    if (value) {\n      this.handleTimeout();\n    } else {\n      this.reset();\n    }\n  }\n}\n","import { IdleTimeout } from './IdleTimeout';\nimport { TimeoutCallback, UserOptions } from './types';\n\nconst idleTimeout = (callback: TimeoutCallback, options?: UserOptions): IdleTimeout =>\n  new IdleTimeout(callback, options);\n\nexport default idleTimeout;\n"],"mappings":";AAGA,IAAaA,EAAb,KAAyB,CAgDvB,YAAmB,EAA2B,EAAuB,WAxCrE,KAAU,cAA+B,KAGzC,KAAU,OAAkB,GAG5B,KAAU,YAAuB,GAGjC,KAAU,UAAoB,EAG9B,KAAU,cAAwB,EAGlC,KAAU,UAAoB,GAG9B,KAAU,UAAoB,GAG9B,KAAU,WAAuB,CAC/B,iBACA,YACA,YACA,aACA,g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