{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * Pinia Colada Auto Refetch plugin.\n *\n * Automatically refreshes queries when they become stale.\n *\n * @module @pinia/colada-plugin-auto-refetch\n */\n\nimport type { PiniaColadaPlugin, UseQueryEntry, UseQueryOptions } from '@pinia/colada'\nimport { toValue } from 'vue'\nimport type { MaybeRefOrGetter } from 'vue'\n\n/**\n * Options for the auto-refetch plugin.\n */\nexport interface PiniaColadaAutoRefetchOptions {\n  /**\n   * Whether to enable auto refresh by default.\n   * @default false\n   */\n  autoRefetch?: MaybeRefOrGetter<boolean>\n}\n\n/**\n * To store timeouts in the entry extensions.\n * @internal\n */\nconst REFETCH_TIMEOUT_KEY = Symbol()\nexport { REFETCH_TIMEOUT_KEY as _REFETCH_TIMEOUT_KEY }\n\n/**\n * Plugin that automatically refreshes queries when they become stale\n */\nexport function PiniaColadaAutoRefetch(\n  options: PiniaColadaAutoRefetchOptions = {},\n): PiniaColadaPlugin {\n  const { autoRefetch = false } = options\n\n  return ({ queryCache }) => {\n    // Skip setting auto-refetch on the server\n    if (typeof document === 'undefined') return\n\n    function scheduleRefetch(\n      entry: UseQueryEntry<unknown, unknown, unknown>,\n      options: UseQueryOptions<unknown, unknown, unknown>,\n    ) {\n      if (!entry.active) return\n\n      // Always clear existing timeout first\n      clearTimeout(entry.ext[REFETCH_TIMEOUT_KEY])\n\n      // Schedule next refetch\n      const timeout = setTimeout(() => {\n        if (options) {\n          const entry: UseQueryEntry | undefined = queryCache.getEntries({\n            key: toValue(options.key),\n          })?.[0]\n          if (entry && entry.active) {\n            queryCache.refresh(entry).catch(console.error)\n          }\n        }\n      }, options.staleTime)\n\n      entry.ext[REFETCH_TIMEOUT_KEY] = timeout\n    }\n\n    queryCache.$onAction(({ name, args, after }) => {\n      /**\n       * Whether to schedule a refetch for the given entry\n       */\n      function shouldScheduleRefetch(options: UseQueryOptions<unknown, unknown, unknown>) {\n        const queryEnabled = toValue(options.autoRefetch) ?? autoRefetch\n        const staleTime = options.staleTime\n        return Boolean(queryEnabled && staleTime)\n      }\n\n      // Trigger a fetch on creation to enable auto-refetch on initial load\n      if (name === 'ensure') {\n        const [options] = args\n        after((entry) => {\n          if (!shouldScheduleRefetch(options)) return\n          scheduleRefetch(entry, options)\n        })\n      }\n\n      // Set up auto-refetch on every fetch\n      if (name === 'fetch') {\n        const [entry] = args\n\n        // Clear any existing timeout before scheduling a new one\n        clearTimeout(entry.ext[REFETCH_TIMEOUT_KEY])\n\n        after(async () => {\n          if (!entry.options) return\n          if (!shouldScheduleRefetch(entry.options)) return\n\n          scheduleRefetch(entry, entry.options)\n        })\n      }\n\n      // Clean up timeouts when entry is removed\n      if (name === 'remove') {\n        const [entry] = args\n        clearTimeout(entry.ext[REFETCH_TIMEOUT_KEY])\n      }\n    })\n  }\n}\n\n// Add types for the new option\ndeclare module '@pinia/colada' {\n  // eslint-disable-next-line unused-imports/no-unused-vars\n  interface UseQueryOptions<TData, TError, TDataInitial> extends PiniaColadaAutoRefetchOptions {}\n\n  interface UseQueryOptionsGlobal extends PiniaColadaAutoRefetchOptions {}\n\n  // eslint-disable-next-line unused-imports/no-unused-vars\n  interface UseQueryEntryExtensions<TData, TError, TDataInitial> {\n    /**\n     * Used to store the timeout for the auto-refetch plugin.\n     * @internal\n     */\n    [REFETCH_TIMEOUT_KEY]?: ReturnType<typeof setTimeout>\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AASA,iBAAwB;AAkBxB,IAAM,sBAAsB,OAAO;AAM5B,SAAS,uBACd,UAAyC,CAAC,GACvB;AACnB,QAAM,EAAE,cAAc,MAAM,IAAI;AAEhC,SAAO,CAAC,EAAE,WAAW,MAAM;AAEzB,QAAI,OAAO,aAAa,YAAa;AAErC,aAAS,gBACP,OACAA,UACA;AACA,UAAI,CAAC,MAAM,OAAQ;AAGnB,mBAAa,MAAM,IAAI,mBAAmB,CAAC;AAG3C,YAAM,UAAU,WAAW,MAAM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