{"version":3,"file":"idle-timeout.esm.mjs","names":[],"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    this.eventNames.forEach((eventName): void => {\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) {\n      return;\n    }\n\n    const remainingTime: number = this.startTime + this.options.timeout - new Date().getTime();\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    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    this.eventNames.forEach((eventName): void => {\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 = new Date().getTime();\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,IAAa,cAAb,MAAyB;;;;;;;CAgDvB,AAAO,YAAY,UAA2B,SAAuB;;EAxCrE,KAAU,gBAA+B;EAGzC,KAAU,SAAkB;EAG5B,KAAU,cAAuB;EAGjC,KAAU,YAAoB;EAG9B,KAAU,gBAAwB;EAGlC,KAAU,YAAoB;EAG9B,KAAU,YAAoB;EAG9B,KAAU,aAAuB;GAC/B;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;EACF;EAmIA,KAAU,eAAe,UAAuB;GAC9C,IAAI,KAAK,aACP;GAGF,IAAI,KAAK,gBAAgB,GACvB;GAGF,IAAI,MAA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