{"version":3,"file":"inject-is-fetching.mjs","sources":["../src/inject-is-fetching.ts"],"sourcesContent":["import {\n  DestroyRef,\n  Injector,\n  NgZone,\n  assertInInjectionContext,\n  inject,\n  signal,\n} from '@angular/core'\nimport { QueryClient, notifyManager } from '@tanstack/query-core'\nimport type { QueryFilters } from '@tanstack/query-core'\nimport type { Signal } from '@angular/core'\n\nexport interface InjectIsFetchingOptions {\n  /**\n   * The `Injector` in which to create the isFetching signal.\n   *\n   * If this is not provided, the current injection context will be used instead (via `inject`).\n   */\n  injector?: Injector\n}\n\n/**\n * Injects a signal that tracks the number of queries that your application is loading or\n * fetching in the background.\n *\n * Can be used for app-wide loading indicators\n * @param filters - The filters to apply to the query.\n * @param options - Additional configuration\n * @returns signal with number of loading or fetching queries.\n */\nexport function injectIsFetching(\n  filters?: QueryFilters,\n  options?: InjectIsFetchingOptions,\n): Signal<number> {\n  !options?.injector && assertInInjectionContext(injectIsFetching)\n  const injector = options?.injector ?? inject(Injector)\n  const destroyRef = injector.get(DestroyRef)\n  const ngZone = injector.get(NgZone)\n  const queryClient = injector.get(QueryClient)\n\n  const cache = queryClient.getQueryCache()\n  // isFetching is the prev value initialized on mount *\n  let isFetching = queryClient.isFetching(filters)\n\n  const result = signal(isFetching)\n\n  const unsubscribe = ngZone.runOutsideAngular(() =>\n    cache.subscribe(\n      notifyManager.batchCalls(() => {\n        const newIsFetching = queryClient.isFetching(filters)\n        if (isFetching !== newIsFetching) {\n          // * and update with each change\n          isFetching = newIsFetching\n          ngZone.run(() => {\n            result.set(isFetching)\n          })\n        }\n      }),\n    ),\n  )\n\n  destroyRef.onDestroy(unsubscribe)\n\n  return result\n}\n"],"names":[],"mappings":";;AA8BO,SAAS,iBACd,SACA,SACgB;AAChB,IAAC,mCAAS,aAAY,yBAAyB,gBAAgB;AAC/D,QAAM,YAAW,mCAAS,aAAY,OAAO,QAAQ;AACrD,QAAM,aAAa,SAAS,IAAI,UAAU;AAC1C,QAAM,SAAS,SAAS,IAAI,MAAM;AAClC,QAAM,cAAc,SAAS,IAAI,WAAW;AAE5C,QAAM,QAAQ,YAAY,cAAA;AAE1B,MAAI,aAAa,YAAY,WAAW,OAAO;AAE/C,QAAM,SAAS,OAAO,UAAU;AAEhC,QAAM,cAAc,OAAO;AAAA,IAAkB,MAC3C,MAAM;AAAA,MACJ,cAAc,WAAW,MAAM;AAC7B,cAAM,gBAAgB,YAAY,WAAW,OAAO;AACpD,YAAI,eAAe,eAAe;AAEhC,uBAAa;AACb,iBAAO,IAAI,MAAM;AACf,mBAAO,IAAI,UAAU;AAAA,UACvB,CAAC;AAAA,QACH;AAAA,MACF,CAAC;AAAA,IAAA;AAAA,EACH;AAGF,aAAW,UAAU,WAAW;AAEhC,SAAO;AACT;"}