{"version":3,"file":"idle-timeout.esm.mjs","names":[],"sources":["../src/IdleTimeout.ts","../src/index.ts"],"sourcesContent":["import { Options, 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: (element: HTMLElement, timeout?: number) => void;\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  /** 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(\n    callback: (element: HTMLElement, timeout?: number) => void,\n    options?: UserOptions\n  ) {\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    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.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    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    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    }\n  }\n\n  /**\n   * Reset the timeout function.\n   * @returns {void}\n   */\n  protected resetTimeout(): void {\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.remainingTime > 0) {\n      return;\n    }\n\n    if (event.type === 'mousemove') {\n      const { pageX, pageY } = event as MouseEvent;\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    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 wether the timeout should restart on completion.\n   * @param {boolean} value Wether 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  }\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 (value) {\n      this.handleTimeout();\n    } else {\n      this.reset();\n    }\n  }\n}\n","import { IdleTimeout } from './IdleTimeout';\nimport { UserOptions } from './types';\n\nconst idleTimeout = (\n  callback: (element: HTMLElement, timeout?: number) => void,\n  options?: UserOptions\n): IdleTimeout => new IdleTimeout(callback, options);\n\nexport default idleTimeout;\n"],"mappings":";;;AAGA,IAAa,cAAb,MAAyB;;;;;;;CA6CvB,AAAO,YACL,UACA,SACA;;EAxCF,KAAU,gBAA+B;EAGzC,KAAU,SAAkB;EAG5B,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;GACD;EA+GD,KAAU,eAAe,UAAuB;AAC9C,OAAI,KAAK,gBAAgB,EACvB;AAGF,OAAI,MAAM,SAAS,aAAa;IAC9B,MAAM,EAAE,OAAO,UAAU;AACzB,QACG,UAAU,UAAa,UAAU,UACjC,UAAU,KAAK,aAAa,UAAU,KAAK,UAE5C;AAGF,SAAK,YAAY;AACjB,SAAK,YAAY;;AAGnB,QAAK,cAAc;;AArHnB,OAAK,WAAW;AAChB,OAAK,UAAU;GACb,+EAAS,QAAS,sEAAW,SAAS;GACtC,yEAAM,QAAS,6DAAQ;GACvB,+EAAS,QAAS,sEAAW,KAAK,MAAO;GAC1C;EAED,MAAM,UAAU,KAAK,QAAQ;AAE7B,OAAK,WAAW,SAAS,cAAoB;AAC3C,WAAQ,iBAAiB,WAAW,KAAK,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