{"version":3,"file":"monitor.mjs","names":[],"sources":["../../src/entries/monitor.ts"],"sourcesContent":["import { getDebugMode } from './util'\nimport type { Display } from './util'\n\n/** An isolated monitor, as returned by {@link createMonitor}. */\nexport interface Monitor {\n  /**\n   * TypeScript method decorator that routes a class method through {@link Monitor.monitor}, so any\n   * error it throws is caught and reported instead of propagating to the caller. Apply it as\n   * `@monitored` on the method (it replaces the method's descriptor value with the monitored\n   * wrapper).\n   *\n   * When to use: prefer this for **class methods** that are entry points from outside the SDK\n   * (public API methods, lifecycle callbacks) where an internal error must never reach the host\n   * application. For standalone functions or inline blocks, use {@link Monitor.monitor} or\n   * {@link Monitor.callMonitored} instead.\n   */\n  monitored: <T extends (...params: any[]) => unknown>(\n    _: any,\n    __: string,\n    descriptor: TypedPropertyDescriptor<T>\n  ) => void\n\n  /**\n   * Wraps a function so that, when called, any thrown error is caught and reported (via the error\n   * callback) instead of propagating. The wrapper keeps the same signature as the input function.\n   *\n   * When to use: prefer this when you need a **reusable monitored callback** to hand to something\n   * that invokes it later, possibly multiple times — an event listener, `setTimeout`, an observable\n   * subscription. For a one-shot inline block, use {@link Monitor.callMonitored} instead.\n   *\n   * @param fn - The function to wrap.\n   * @returns A function with the same signature that never throws (errors are collected instead).\n   * @example\n   * ```ts\n   * element.addEventListener(\n   *   'click',\n   *   monitor((event) => {\n   *     // handler errors are collected instead of surfacing to the page\n   *   })\n   * )\n   * ```\n   */\n  monitor: <T extends (...args: any[]) => unknown>(fn: T) => T\n\n  /**\n   * Invokes a function with error handling: returns its result, or reports the error (via the error\n   * callback) and returns `undefined` if it throws.\n   *\n   * When to use: prefer this for a **one-off inline block** you want to run immediately under error\n   * protection. If you instead need a callback to pass elsewhere and reuse, wrap it once with\n   * {@link Monitor.monitor}.\n   *\n   * @param fn - The function to invoke.\n   * @param context - `this` value to invoke `fn` with (optional for context-free functions).\n   * @param args - Arguments to invoke `fn` with (optional for context-free functions).\n   * @returns The result of `fn`, or `undefined` if it threw.\n   * @example\n   * ```ts\n   * callMonitored(() => {\n   *   const stackTrace = computeStackTrace(error)\n   *   reportStackTrace(stackTrace)\n   * })\n   * ```\n   */\n  callMonitored: {\n    <T extends (...args: any[]) => unknown>(\n      fn: T,\n      context: ThisParameterType<T>,\n      args: Parameters<T>\n    ): ReturnType<T> | undefined\n    <T extends (this: void) => unknown>(fn: T): ReturnType<T> | undefined\n  }\n\n  /**\n   * Reports an error directly: logs it to the console when debug mode is enabled, then forwards it\n   * to the error callback. Used internally by {@link Monitor.monitor}/{@link Monitor.callMonitored},\n   * but can also be called to report an error caught elsewhere.\n   *\n   * When to use: prefer this when you **already hold an error value** and only need to route it to\n   * telemetry — e.g. a promise rejection, which `monitor`/`callMonitored` do not catch (they only\n   * handle synchronous throws).\n   *\n   * @param e - The error to report.\n   * @example\n   * ```ts\n   * // route a promise rejection to telemetry\n   * doAsyncThing().catch(monitorError)\n   * ```\n   */\n  monitorError: (e: unknown) => void\n}\n\n/**\n * Creates an isolated monitor with its own error-collection callback and display.\n *\n * Each consumer (SDK) should create its own monitor so that error-collection callbacks do not\n * clobber each other when several SDKs share the same `@openobserve/js-core/monitor` module instance.\n *\n * @param display - {@link Display} used for debug logging (see `createDisplay` in\n * `@openobserve/js-core/util`). Lets the consumer control the log prefix and console binding. Debug\n * output is only emitted when debug mode is enabled (see `setDebugMode`/`getDebugMode`).\n * @param onMonitorErrorCollected - Callback invoked with each error caught by the monitor (e.g. to\n * forward it to telemetry). Fixed for the lifetime of the monitor.\n * @returns A {@link Monitor}.\n */\nexport function createMonitor(display: Display, onMonitorErrorCollected: (error: unknown) => void): Monitor {\n  function monitored<T extends (...params: any[]) => unknown>(\n    _: any,\n    __: string,\n    descriptor: TypedPropertyDescriptor<T>\n  ) {\n    descriptor.value = monitor(descriptor.value!)\n  }\n\n  function monitor<T extends (...args: any[]) => unknown>(fn: T): T {\n    return function (this: ThisParameterType<T>, ...args: Parameters<T>) {\n      return callMonitored(fn, this, args)\n    } as unknown as T // consider output type has input type\n  }\n\n  function callMonitored<T extends (...args: any[]) => unknown>(\n    fn: T,\n    context: ThisParameterType<T>,\n    args: Parameters<T>\n  ): ReturnType<T> | undefined\n  function callMonitored<T extends (this: void) => unknown>(fn: T): ReturnType<T> | undefined\n  function callMonitored<T extends (...args: any[]) => unknown>(\n    fn: T,\n    context?: any,\n    args?: any\n  ): ReturnType<T> | undefined {\n    try {\n      return fn.apply(context, args) as ReturnType<T>\n    } catch (e) {\n      monitorError(e)\n    }\n  }\n\n  function monitorError(e: unknown) {\n    const displayIfDebugEnabled = (e: unknown) => {\n      if (getDebugMode()) {\n        display.error('[MONITOR]', e)\n      }\n    }\n\n    displayIfDebugEnabled(e)\n    try {\n      onMonitorErrorCollected(e)\n    } catch (e) {\n      displayIfDebugEnabled(e)\n    }\n  }\n\n  return {\n    monitored,\n    monitor,\n    callMonitored,\n    monitorError,\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;;AAyGA,SAAgB,cAAc,SAAkB,yBAA4D;CAC1G,SAAS,UACP,GACA,IACA,YACA;EACA,WAAW,QAAQ,QAAQ,WAAW,KAAM;CAC9C;CAEA,SAAS,QAA+C,IAAU;EAChE,OAAO,SAAsC,GAAG,MAAqB;GACnE,OAAO,cAAc,IAAI,MAAM,IAAI;EACrC;CACF;CAQA,SAAS,cACP,IACA,SACA,MAC2B;EAC3B,IAAI;GACF,OAAO,GAAG,MAAM,SAAS,IAAI;EAC/B,SAAS,GAAG;GACV,aAAa,CAAC;EAChB;CACF;CAEA,SAAS,aAAa,GAAY;EAChC,MAAM,yBAAyB,MAAe;GAC5C,IAAI,aAAa,GACf,QAAQ,MAAM,aAAa,CAAC;EAEhC;EAEA,sBAAsB,CAAC;EACvB,IAAI;GACF,wBAAwB,CAAC;EAC3B,SAAS,GAAG;GACV,sBAAsB,CAAC;EACzB;CACF;CAEA,OAAO;EACL;EACA;EACA;EACA;CACF;AACF"}