{"version":3,"file":"idle-timeout.esm.min.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,EAAb,KAAyB,CA6CvB,YACE,EACA,EACA,WAxCF,KAAU,cAA+B,KAGzC,KAAU,OAAkB,GAG5B,KAAU,UAAoB,EAG9B,KAAU,cAAwB,EAGlC,KAAU,UAAoB,GAG9B,KAAU,UAAoB,GAG9B,KAAU,WAAuB,CAC/B,iBACA,YACA,YACA,aACA,gBACA,gBACA,UACA,YACA,aACA,QACD,CA+GD,KAAU,YAAe,GAAuB,CAC1C,UAAK,cAAgB,GAIzB,IAAI,EAAM,OAAS,YAAa,CAC9B,GAAM,CAAE,QAAO,SAAU,EACzB,GACG,IAAU,IAAA,IAAa,IAAU,IAAA,IACjC,IAAU,KAAK,WAAa,IAAU,KAAK,UAE5C,OAGF,KAAK,UAAY,EACjB,KAAK,UAAY,EAGnB,KAAK,cAAc,GArHnB,KAAK,SAAW,EAChB,KAAK,QAAU,CACb,SAAA,EAAA,GAAA,KAAA,IAAA,GAAS,EAAS,UAAA,KAAW,SAAS,KAApB,EAClB,MAAA,EAAA,GAAA,KAAA,IAAA,GAAM,EAAS,OAAA,KAAQ,GAAR,EACf,SAAA,EAAA,GAAA,KAAA,IAAA,GAAS,EAAS,UAAA,KAAW,GAAK,IAAO,EAAvB,E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