@tanstack/query-core 5.83.1 → 5.87.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (163) hide show
  1. package/build/legacy/{hydration-Cvr-9VdO.d.ts → hydration-BYonJkjc.d.ts} +8 -6
  2. package/build/legacy/{hydration-CADtEOkK.d.cts → hydration-_hO-y142.d.cts} +8 -6
  3. package/build/legacy/hydration.d.cts +1 -1
  4. package/build/legacy/hydration.d.ts +1 -1
  5. package/build/legacy/index.cjs +14 -12
  6. package/build/legacy/index.cjs.map +1 -1
  7. package/build/legacy/index.d.cts +5 -4
  8. package/build/legacy/index.d.ts +5 -4
  9. package/build/legacy/index.js +26 -23
  10. package/build/legacy/index.js.map +1 -1
  11. package/build/legacy/infiniteQueryBehavior.d.cts +1 -1
  12. package/build/legacy/infiniteQueryBehavior.d.ts +1 -1
  13. package/build/legacy/infiniteQueryObserver.d.cts +1 -1
  14. package/build/legacy/infiniteQueryObserver.d.ts +1 -1
  15. package/build/legacy/mutation.d.cts +1 -1
  16. package/build/legacy/mutation.d.ts +1 -1
  17. package/build/legacy/mutationCache.d.cts +1 -1
  18. package/build/legacy/mutationCache.d.ts +1 -1
  19. package/build/legacy/mutationObserver.d.cts +1 -1
  20. package/build/legacy/mutationObserver.d.ts +1 -1
  21. package/build/legacy/notifyManager.cjs +2 -1
  22. package/build/legacy/notifyManager.cjs.map +1 -1
  23. package/build/legacy/notifyManager.js +2 -1
  24. package/build/legacy/notifyManager.js.map +1 -1
  25. package/build/legacy/queriesObserver.cjs +19 -13
  26. package/build/legacy/queriesObserver.cjs.map +1 -1
  27. package/build/legacy/queriesObserver.d.cts +1 -1
  28. package/build/legacy/queriesObserver.d.ts +1 -1
  29. package/build/legacy/queriesObserver.js +20 -14
  30. package/build/legacy/queriesObserver.js.map +1 -1
  31. package/build/legacy/query.cjs +78 -61
  32. package/build/legacy/query.cjs.map +1 -1
  33. package/build/legacy/query.d.cts +1 -1
  34. package/build/legacy/query.d.ts +1 -1
  35. package/build/legacy/query.js +79 -62
  36. package/build/legacy/query.js.map +1 -1
  37. package/build/legacy/queryCache.d.cts +1 -1
  38. package/build/legacy/queryCache.d.ts +1 -1
  39. package/build/legacy/queryClient.d.cts +1 -1
  40. package/build/legacy/queryClient.d.ts +1 -1
  41. package/build/legacy/queryObserver.cjs +12 -9
  42. package/build/legacy/queryObserver.cjs.map +1 -1
  43. package/build/legacy/queryObserver.d.cts +1 -1
  44. package/build/legacy/queryObserver.d.ts +1 -1
  45. package/build/legacy/queryObserver.js +12 -9
  46. package/build/legacy/queryObserver.js.map +1 -1
  47. package/build/legacy/removable.cjs +3 -2
  48. package/build/legacy/removable.cjs.map +1 -1
  49. package/build/legacy/removable.js +3 -2
  50. package/build/legacy/removable.js.map +1 -1
  51. package/build/legacy/retryer.cjs +12 -16
  52. package/build/legacy/retryer.cjs.map +1 -1
  53. package/build/legacy/retryer.d.cts +1 -1
  54. package/build/legacy/retryer.d.ts +1 -1
  55. package/build/legacy/retryer.js +12 -16
  56. package/build/legacy/retryer.js.map +1 -1
  57. package/build/legacy/streamedQuery.cjs +7 -8
  58. package/build/legacy/streamedQuery.cjs.map +1 -1
  59. package/build/legacy/streamedQuery.d.cts +18 -10
  60. package/build/legacy/streamedQuery.d.ts +18 -10
  61. package/build/legacy/streamedQuery.js +7 -8
  62. package/build/legacy/streamedQuery.js.map +1 -1
  63. package/build/legacy/timeoutManager.cjs +110 -0
  64. package/build/legacy/timeoutManager.cjs.map +1 -0
  65. package/build/legacy/timeoutManager.d.cts +58 -0
  66. package/build/legacy/timeoutManager.d.ts +58 -0
  67. package/build/legacy/timeoutManager.js +81 -0
  68. package/build/legacy/timeoutManager.js.map +1 -0
  69. package/build/legacy/types.d.cts +1 -1
  70. package/build/legacy/types.d.ts +1 -1
  71. package/build/legacy/utils.cjs +26 -22
  72. package/build/legacy/utils.cjs.map +1 -1
  73. package/build/legacy/utils.d.cts +1 -1
  74. package/build/legacy/utils.d.ts +1 -1
  75. package/build/legacy/utils.js +26 -22
  76. package/build/legacy/utils.js.map +1 -1
  77. package/build/modern/{hydration-Cvr-9VdO.d.ts → hydration-BYonJkjc.d.ts} +8 -6
  78. package/build/modern/{hydration-CADtEOkK.d.cts → hydration-_hO-y142.d.cts} +8 -6
  79. package/build/modern/hydration.d.cts +1 -1
  80. package/build/modern/hydration.d.ts +1 -1
  81. package/build/modern/index.cjs +14 -12
  82. package/build/modern/index.cjs.map +1 -1
  83. package/build/modern/index.d.cts +5 -4
  84. package/build/modern/index.d.ts +5 -4
  85. package/build/modern/index.js +26 -23
  86. package/build/modern/index.js.map +1 -1
  87. package/build/modern/infiniteQueryBehavior.d.cts +1 -1
  88. package/build/modern/infiniteQueryBehavior.d.ts +1 -1
  89. package/build/modern/infiniteQueryObserver.d.cts +1 -1
  90. package/build/modern/infiniteQueryObserver.d.ts +1 -1
  91. package/build/modern/mutation.d.cts +1 -1
  92. package/build/modern/mutation.d.ts +1 -1
  93. package/build/modern/mutationCache.d.cts +1 -1
  94. package/build/modern/mutationCache.d.ts +1 -1
  95. package/build/modern/mutationObserver.d.cts +1 -1
  96. package/build/modern/mutationObserver.d.ts +1 -1
  97. package/build/modern/notifyManager.cjs +2 -1
  98. package/build/modern/notifyManager.cjs.map +1 -1
  99. package/build/modern/notifyManager.js +2 -1
  100. package/build/modern/notifyManager.js.map +1 -1
  101. package/build/modern/queriesObserver.cjs +19 -13
  102. package/build/modern/queriesObserver.cjs.map +1 -1
  103. package/build/modern/queriesObserver.d.cts +1 -1
  104. package/build/modern/queriesObserver.d.ts +1 -1
  105. package/build/modern/queriesObserver.js +20 -14
  106. package/build/modern/queriesObserver.js.map +1 -1
  107. package/build/modern/query.cjs +71 -52
  108. package/build/modern/query.cjs.map +1 -1
  109. package/build/modern/query.d.cts +1 -1
  110. package/build/modern/query.d.ts +1 -1
  111. package/build/modern/query.js +72 -53
  112. package/build/modern/query.js.map +1 -1
  113. package/build/modern/queryCache.d.cts +1 -1
  114. package/build/modern/queryCache.d.ts +1 -1
  115. package/build/modern/queryClient.d.cts +1 -1
  116. package/build/modern/queryClient.d.ts +1 -1
  117. package/build/modern/queryObserver.cjs +12 -9
  118. package/build/modern/queryObserver.cjs.map +1 -1
  119. package/build/modern/queryObserver.d.cts +1 -1
  120. package/build/modern/queryObserver.d.ts +1 -1
  121. package/build/modern/queryObserver.js +12 -9
  122. package/build/modern/queryObserver.js.map +1 -1
  123. package/build/modern/removable.cjs +3 -2
  124. package/build/modern/removable.cjs.map +1 -1
  125. package/build/modern/removable.js +3 -2
  126. package/build/modern/removable.js.map +1 -1
  127. package/build/modern/retryer.cjs +12 -14
  128. package/build/modern/retryer.cjs.map +1 -1
  129. package/build/modern/retryer.d.cts +1 -1
  130. package/build/modern/retryer.d.ts +1 -1
  131. package/build/modern/retryer.js +12 -14
  132. package/build/modern/retryer.js.map +1 -1
  133. package/build/modern/streamedQuery.cjs +7 -8
  134. package/build/modern/streamedQuery.cjs.map +1 -1
  135. package/build/modern/streamedQuery.d.cts +18 -10
  136. package/build/modern/streamedQuery.d.ts +18 -10
  137. package/build/modern/streamedQuery.js +7 -8
  138. package/build/modern/streamedQuery.js.map +1 -1
  139. package/build/modern/timeoutManager.cjs +98 -0
  140. package/build/modern/timeoutManager.cjs.map +1 -0
  141. package/build/modern/timeoutManager.d.cts +58 -0
  142. package/build/modern/timeoutManager.d.ts +58 -0
  143. package/build/modern/timeoutManager.js +70 -0
  144. package/build/modern/timeoutManager.js.map +1 -0
  145. package/build/modern/types.d.cts +1 -1
  146. package/build/modern/types.d.ts +1 -1
  147. package/build/modern/utils.cjs +26 -22
  148. package/build/modern/utils.cjs.map +1 -1
  149. package/build/modern/utils.d.cts +1 -1
  150. package/build/modern/utils.d.ts +1 -1
  151. package/build/modern/utils.js +26 -22
  152. package/build/modern/utils.js.map +1 -1
  153. package/package.json +1 -1
  154. package/src/index.ts +32 -27
  155. package/src/notifyManager.ts +3 -1
  156. package/src/queriesObserver.ts +24 -15
  157. package/src/query.ts +96 -69
  158. package/src/queryObserver.ts +19 -11
  159. package/src/removable.ts +5 -3
  160. package/src/retryer.ts +20 -17
  161. package/src/streamedQuery.ts +47 -23
  162. package/src/timeoutManager.ts +135 -0
  163. package/src/utils.ts +39 -31
@@ -1,3 +1,3 @@
1
- export { C as CancelledError, bC as RetryDelayValue, bB as RetryValue, bA as Retryer, bD as canFetch, bE as createRetryer, o as isCancelledError } from './hydration-CADtEOkK.cjs';
1
+ export { C as CancelledError, bC as RetryDelayValue, bB as RetryValue, bA as Retryer, bD as canFetch, bE as createRetryer, j as isCancelledError } from './hydration-_hO-y142.cjs';
2
2
  import './removable.cjs';
3
3
  import './subscribable.cjs';
@@ -1,3 +1,3 @@
1
- export { C as CancelledError, bC as RetryDelayValue, bB as RetryValue, bA as Retryer, bD as canFetch, bE as createRetryer, o as isCancelledError } from './hydration-Cvr-9VdO.js';
1
+ export { C as CancelledError, bC as RetryDelayValue, bB as RetryValue, bA as Retryer, bD as canFetch, bE as createRetryer, j as isCancelledError } from './hydration-BYonJkjc.js';
2
2
  import './removable.js';
3
3
  import './subscribable.js';
@@ -22,13 +22,14 @@ function isCancelledError(value) {
22
22
  function createRetryer(config) {
23
23
  let isRetryCancelled = false;
24
24
  let failureCount = 0;
25
- let isResolved = false;
26
25
  let continueFn;
27
26
  const thenable = pendingThenable();
27
+ const isResolved = () => thenable.status !== "pending";
28
28
  const cancel = (cancelOptions) => {
29
- if (!isResolved) {
30
- reject(new CancelledError(cancelOptions));
31
- config.abort?.();
29
+ if (!isResolved()) {
30
+ const error = new CancelledError(cancelOptions);
31
+ reject(error);
32
+ config.onCancel?.(error);
32
33
  }
33
34
  };
34
35
  const cancelRetry = () => {
@@ -40,17 +41,13 @@ function createRetryer(config) {
40
41
  const canContinue = () => focusManager.isFocused() && (config.networkMode === "always" || onlineManager.isOnline()) && config.canRun();
41
42
  const canStart = () => canFetch(config.networkMode) && config.canRun();
42
43
  const resolve = (value) => {
43
- if (!isResolved) {
44
- isResolved = true;
45
- config.onSuccess?.(value);
44
+ if (!isResolved()) {
46
45
  continueFn?.();
47
46
  thenable.resolve(value);
48
47
  }
49
48
  };
50
49
  const reject = (value) => {
51
- if (!isResolved) {
52
- isResolved = true;
53
- config.onError?.(value);
50
+ if (!isResolved()) {
54
51
  continueFn?.();
55
52
  thenable.reject(value);
56
53
  }
@@ -58,20 +55,20 @@ function createRetryer(config) {
58
55
  const pause = () => {
59
56
  return new Promise((continueResolve) => {
60
57
  continueFn = (value) => {
61
- if (isResolved || canContinue()) {
58
+ if (isResolved() || canContinue()) {
62
59
  continueResolve(value);
63
60
  }
64
61
  };
65
62
  config.onPause?.();
66
63
  }).then(() => {
67
64
  continueFn = void 0;
68
- if (!isResolved) {
65
+ if (!isResolved()) {
69
66
  config.onContinue?.();
70
67
  }
71
68
  });
72
69
  };
73
70
  const run = () => {
74
- if (isResolved) {
71
+ if (isResolved()) {
75
72
  return;
76
73
  }
77
74
  let promiseOrValue;
@@ -82,7 +79,7 @@ function createRetryer(config) {
82
79
  promiseOrValue = Promise.reject(error);
83
80
  }
84
81
  Promise.resolve(promiseOrValue).then(resolve).catch((error) => {
85
- if (isResolved) {
82
+ if (isResolved()) {
86
83
  return;
87
84
  }
88
85
  const retry = config.retry ?? (isServer ? 0 : 3);
@@ -108,6 +105,7 @@ function createRetryer(config) {
108
105
  };
109
106
  return {
110
107
  promise: thenable,
108
+ status: () => thenable.status,
111
109
  cancel,
112
110
  continue: () => {
113
111
  continueFn?.();
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/retryer.ts"],"sourcesContent":["import { focusManager } from './focusManager'\nimport { onlineManager } from './onlineManager'\nimport { pendingThenable } from './thenable'\nimport { isServer, sleep } from './utils'\nimport type { CancelOptions, DefaultError, NetworkMode } from './types'\n\n// TYPES\n\ninterface RetryerConfig<TData = unknown, TError = DefaultError> {\n fn: () => TData | Promise<TData>\n initialPromise?: Promise<TData>\n abort?: () => void\n onError?: (error: TError) => void\n onSuccess?: (data: TData) => void\n onFail?: (failureCount: number, error: TError) => void\n onPause?: () => void\n onContinue?: () => void\n retry?: RetryValue<TError>\n retryDelay?: RetryDelayValue<TError>\n networkMode: NetworkMode | undefined\n canRun: () => boolean\n}\n\nexport interface Retryer<TData = unknown> {\n promise: Promise<TData>\n cancel: (cancelOptions?: CancelOptions) => void\n continue: () => Promise<unknown>\n cancelRetry: () => void\n continueRetry: () => void\n canStart: () => boolean\n start: () => Promise<TData>\n}\n\nexport type RetryValue<TError> = boolean | number | ShouldRetryFunction<TError>\n\ntype ShouldRetryFunction<TError = DefaultError> = (\n failureCount: number,\n error: TError,\n) => boolean\n\nexport type RetryDelayValue<TError> = number | RetryDelayFunction<TError>\n\ntype RetryDelayFunction<TError = DefaultError> = (\n failureCount: number,\n error: TError,\n) => number\n\nfunction defaultRetryDelay(failureCount: number) {\n return Math.min(1000 * 2 ** failureCount, 30000)\n}\n\nexport function canFetch(networkMode: NetworkMode | undefined): boolean {\n return (networkMode ?? 'online') === 'online'\n ? onlineManager.isOnline()\n : true\n}\n\nexport class CancelledError extends Error {\n revert?: boolean\n silent?: boolean\n constructor(options?: CancelOptions) {\n super('CancelledError')\n this.revert = options?.revert\n this.silent = options?.silent\n }\n}\n\nexport function isCancelledError(value: any): value is CancelledError {\n return value instanceof CancelledError\n}\n\nexport function createRetryer<TData = unknown, TError = DefaultError>(\n config: RetryerConfig<TData, TError>,\n): Retryer<TData> {\n let isRetryCancelled = false\n let failureCount = 0\n let isResolved = false\n let continueFn: ((value?: unknown) => void) | undefined\n\n const thenable = pendingThenable<TData>()\n\n const cancel = (cancelOptions?: CancelOptions): void => {\n if (!isResolved) {\n reject(new CancelledError(cancelOptions))\n\n config.abort?.()\n }\n }\n const cancelRetry = () => {\n isRetryCancelled = true\n }\n\n const continueRetry = () => {\n isRetryCancelled = false\n }\n\n const canContinue = () =>\n focusManager.isFocused() &&\n (config.networkMode === 'always' || onlineManager.isOnline()) &&\n config.canRun()\n\n const canStart = () => canFetch(config.networkMode) && config.canRun()\n\n const resolve = (value: any) => {\n if (!isResolved) {\n isResolved = true\n config.onSuccess?.(value)\n continueFn?.()\n thenable.resolve(value)\n }\n }\n\n const reject = (value: any) => {\n if (!isResolved) {\n isResolved = true\n config.onError?.(value)\n continueFn?.()\n thenable.reject(value)\n }\n }\n\n const pause = () => {\n return new Promise((continueResolve) => {\n continueFn = (value) => {\n if (isResolved || canContinue()) {\n continueResolve(value)\n }\n }\n config.onPause?.()\n }).then(() => {\n continueFn = undefined\n if (!isResolved) {\n config.onContinue?.()\n }\n })\n }\n\n // Create loop function\n const run = () => {\n // Do nothing if already resolved\n if (isResolved) {\n return\n }\n\n let promiseOrValue: any\n\n // we can re-use config.initialPromise on the first call of run()\n const initialPromise =\n failureCount === 0 ? config.initialPromise : undefined\n\n // Execute query\n try {\n promiseOrValue = initialPromise ?? config.fn()\n } catch (error) {\n promiseOrValue = Promise.reject(error)\n }\n\n Promise.resolve(promiseOrValue)\n .then(resolve)\n .catch((error) => {\n // Stop if the fetch is already resolved\n if (isResolved) {\n return\n }\n\n // Do we need to retry the request?\n const retry = config.retry ?? (isServer ? 0 : 3)\n const retryDelay = config.retryDelay ?? defaultRetryDelay\n const delay =\n typeof retryDelay === 'function'\n ? retryDelay(failureCount, error)\n : retryDelay\n const shouldRetry =\n retry === true ||\n (typeof retry === 'number' && failureCount < retry) ||\n (typeof retry === 'function' && retry(failureCount, error))\n\n if (isRetryCancelled || !shouldRetry) {\n // We are done if the query does not need to be retried\n reject(error)\n return\n }\n\n failureCount++\n\n // Notify on fail\n config.onFail?.(failureCount, error)\n\n // Delay\n sleep(delay)\n // Pause if the document is not visible or when the device is offline\n .then(() => {\n return canContinue() ? undefined : pause()\n })\n .then(() => {\n if (isRetryCancelled) {\n reject(error)\n } else {\n run()\n }\n })\n })\n }\n\n return {\n promise: thenable,\n cancel,\n continue: () => {\n continueFn?.()\n return thenable\n },\n cancelRetry,\n continueRetry,\n canStart,\n start: () => {\n // Start loop\n if (canStart()) {\n run()\n } else {\n pause().then(run)\n }\n return thenable\n },\n }\n}\n"],"mappings":"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","names":[]}
1
+ {"version":3,"sources":["../../src/retryer.ts"],"sourcesContent":["import { focusManager } from './focusManager'\nimport { onlineManager } from './onlineManager'\nimport { pendingThenable } from './thenable'\nimport { isServer, sleep } from './utils'\nimport type { Thenable } from './thenable'\nimport type { CancelOptions, DefaultError, NetworkMode } from './types'\n\n// TYPES\n\ninterface RetryerConfig<TData = unknown, TError = DefaultError> {\n fn: () => TData | Promise<TData>\n initialPromise?: Promise<TData>\n onCancel?: (error: TError) => void\n onFail?: (failureCount: number, error: TError) => void\n onPause?: () => void\n onContinue?: () => void\n retry?: RetryValue<TError>\n retryDelay?: RetryDelayValue<TError>\n networkMode: NetworkMode | undefined\n canRun: () => boolean\n}\n\nexport interface Retryer<TData = unknown> {\n promise: Promise<TData>\n cancel: (cancelOptions?: CancelOptions) => void\n continue: () => Promise<unknown>\n cancelRetry: () => void\n continueRetry: () => void\n canStart: () => boolean\n start: () => Promise<TData>\n status: () => 'pending' | 'resolved' | 'rejected'\n}\n\nexport type RetryValue<TError> = boolean | number | ShouldRetryFunction<TError>\n\ntype ShouldRetryFunction<TError = DefaultError> = (\n failureCount: number,\n error: TError,\n) => boolean\n\nexport type RetryDelayValue<TError> = number | RetryDelayFunction<TError>\n\ntype RetryDelayFunction<TError = DefaultError> = (\n failureCount: number,\n error: TError,\n) => number\n\nfunction defaultRetryDelay(failureCount: number) {\n return Math.min(1000 * 2 ** failureCount, 30000)\n}\n\nexport function canFetch(networkMode: NetworkMode | undefined): boolean {\n return (networkMode ?? 'online') === 'online'\n ? onlineManager.isOnline()\n : true\n}\n\nexport class CancelledError extends Error {\n revert?: boolean\n silent?: boolean\n constructor(options?: CancelOptions) {\n super('CancelledError')\n this.revert = options?.revert\n this.silent = options?.silent\n }\n}\n\n/**\n * @deprecated Use instanceof `CancelledError` instead.\n */\nexport function isCancelledError(value: any): value is CancelledError {\n return value instanceof CancelledError\n}\n\nexport function createRetryer<TData = unknown, TError = DefaultError>(\n config: RetryerConfig<TData, TError>,\n): Retryer<TData> {\n let isRetryCancelled = false\n let failureCount = 0\n let continueFn: ((value?: unknown) => void) | undefined\n\n const thenable = pendingThenable<TData>()\n\n const isResolved = () =>\n (thenable.status as Thenable<TData>['status']) !== 'pending'\n\n const cancel = (cancelOptions?: CancelOptions): void => {\n if (!isResolved()) {\n const error = new CancelledError(cancelOptions) as TError\n reject(error)\n\n config.onCancel?.(error)\n }\n }\n const cancelRetry = () => {\n isRetryCancelled = true\n }\n\n const continueRetry = () => {\n isRetryCancelled = false\n }\n\n const canContinue = () =>\n focusManager.isFocused() &&\n (config.networkMode === 'always' || onlineManager.isOnline()) &&\n config.canRun()\n\n const canStart = () => canFetch(config.networkMode) && config.canRun()\n\n const resolve = (value: any) => {\n if (!isResolved()) {\n continueFn?.()\n thenable.resolve(value)\n }\n }\n\n const reject = (value: any) => {\n if (!isResolved()) {\n continueFn?.()\n thenable.reject(value)\n }\n }\n\n const pause = () => {\n return new Promise((continueResolve) => {\n continueFn = (value) => {\n if (isResolved() || canContinue()) {\n continueResolve(value)\n }\n }\n config.onPause?.()\n }).then(() => {\n continueFn = undefined\n if (!isResolved()) {\n config.onContinue?.()\n }\n })\n }\n\n // Create loop function\n const run = () => {\n // Do nothing if already resolved\n if (isResolved()) {\n return\n }\n\n let promiseOrValue: any\n\n // we can re-use config.initialPromise on the first call of run()\n const initialPromise =\n failureCount === 0 ? config.initialPromise : undefined\n\n // Execute query\n try {\n promiseOrValue = initialPromise ?? config.fn()\n } catch (error) {\n promiseOrValue = Promise.reject(error)\n }\n\n Promise.resolve(promiseOrValue)\n .then(resolve)\n .catch((error) => {\n // Stop if the fetch is already resolved\n if (isResolved()) {\n return\n }\n\n // Do we need to retry the request?\n const retry = config.retry ?? (isServer ? 0 : 3)\n const retryDelay = config.retryDelay ?? defaultRetryDelay\n const delay =\n typeof retryDelay === 'function'\n ? retryDelay(failureCount, error)\n : retryDelay\n const shouldRetry =\n retry === true ||\n (typeof retry === 'number' && failureCount < retry) ||\n (typeof retry === 'function' && retry(failureCount, error))\n\n if (isRetryCancelled || !shouldRetry) {\n // We are done if the query does not need to be retried\n reject(error)\n return\n }\n\n failureCount++\n\n // Notify on fail\n config.onFail?.(failureCount, error)\n\n // Delay\n sleep(delay)\n // Pause if the document is not visible or when the device is offline\n .then(() => {\n return canContinue() ? undefined : pause()\n })\n .then(() => {\n if (isRetryCancelled) {\n reject(error)\n } else {\n run()\n }\n })\n })\n }\n\n return {\n promise: thenable,\n status: () => thenable.status,\n cancel,\n continue: () => {\n continueFn?.()\n return thenable\n },\n cancelRetry,\n continueRetry,\n canStart,\n start: () => {\n // Start loop\n if (canStart()) {\n run()\n } else {\n pause().then(run)\n }\n return thenable\n },\n }\n}\n"],"mappings":"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","names":[]}
@@ -25,9 +25,10 @@ __export(streamedQuery_exports, {
25
25
  module.exports = __toCommonJS(streamedQuery_exports);
26
26
  var import_utils = require("./utils.cjs");
27
27
  function streamedQuery({
28
- queryFn,
28
+ streamFn,
29
29
  refetchMode = "reset",
30
- maxChunks
30
+ reducer = (items, chunk) => (0, import_utils.addToEnd)(items, chunk),
31
+ initialValue = []
31
32
  }) {
32
33
  return async (context) => {
33
34
  const query = context.client.getQueryCache().find({ queryKey: context.queryKey, exact: true });
@@ -40,8 +41,8 @@ function streamedQuery({
40
41
  fetchStatus: "fetching"
41
42
  });
42
43
  }
43
- let result = [];
44
- const stream = await queryFn(context);
44
+ let result = initialValue;
45
+ const stream = await streamFn(context);
45
46
  for await (const chunk of stream) {
46
47
  if (context.signal.aborted) {
47
48
  break;
@@ -49,12 +50,10 @@ function streamedQuery({
49
50
  if (!isRefetch || refetchMode !== "replace") {
50
51
  context.client.setQueryData(
51
52
  context.queryKey,
52
- (prev = []) => {
53
- return (0, import_utils.addToEnd)(prev, chunk, maxChunks);
54
- }
53
+ (prev) => reducer(prev === void 0 ? initialValue : prev, chunk)
55
54
  );
56
55
  }
57
- result = (0, import_utils.addToEnd)(result, chunk, maxChunks);
56
+ result = reducer(result, chunk);
58
57
  }
59
58
  if (isRefetch && refetchMode === "replace" && !context.signal.aborted) {
60
59
  context.client.setQueryData(context.queryKey, result);
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/streamedQuery.ts"],"sourcesContent":["import { addToEnd } from './utils'\nimport type { QueryFunction, QueryFunctionContext, QueryKey } from './types'\n\n/**\n * This is a helper function to create a query function that streams data from an AsyncIterable.\n * Data will be an Array of all the chunks received.\n * The query will be in a 'pending' state until the first chunk of data is received, but will go to 'success' after that.\n * The query will stay in fetchStatus 'fetching' until the stream ends.\n * @param queryFn - The function that returns an AsyncIterable to stream data from.\n * @param refetchMode - Defines how re-fetches are handled.\n * Defaults to `'reset'`, erases all data and puts the query back into `pending` state.\n * Set to `'append'` to append new data to the existing data.\n * Set to `'replace'` to write all data to the cache once the stream ends.\n * @param maxChunks - The maximum number of chunks to keep in the cache.\n * Defaults to `undefined`, meaning all chunks will be kept.\n * If `undefined` or `0`, the number of chunks is unlimited.\n * If the number of chunks exceeds this number, the oldest chunk will be removed.\n */\nexport function streamedQuery<\n TQueryFnData = unknown,\n TQueryKey extends QueryKey = QueryKey,\n>({\n queryFn,\n refetchMode = 'reset',\n maxChunks,\n}: {\n queryFn: (\n context: QueryFunctionContext<TQueryKey>,\n ) => AsyncIterable<TQueryFnData> | Promise<AsyncIterable<TQueryFnData>>\n refetchMode?: 'append' | 'reset' | 'replace'\n maxChunks?: number\n}): QueryFunction<Array<TQueryFnData>, TQueryKey> {\n return async (context) => {\n const query = context.client\n .getQueryCache()\n .find({ queryKey: context.queryKey, exact: true })\n const isRefetch = !!query && query.state.data !== undefined\n\n if (isRefetch && refetchMode === 'reset') {\n query.setState({\n status: 'pending',\n data: undefined,\n error: null,\n fetchStatus: 'fetching',\n })\n }\n\n let result: Array<TQueryFnData> = []\n const stream = await queryFn(context)\n\n for await (const chunk of stream) {\n if (context.signal.aborted) {\n break\n }\n\n // don't append to the cache directly when replace-refetching\n if (!isRefetch || refetchMode !== 'replace') {\n context.client.setQueryData<Array<TQueryFnData>>(\n context.queryKey,\n (prev = []) => {\n return addToEnd(prev, chunk, maxChunks)\n },\n )\n }\n result = addToEnd(result, chunk, maxChunks)\n }\n\n // finalize result: replace-refetching needs to write to the cache\n if (isRefetch && refetchMode === 'replace' && !context.signal.aborted) {\n context.client.setQueryData<Array<TQueryFnData>>(context.queryKey, result)\n }\n\n return context.client.getQueryData(context.queryKey)!\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAyB;AAkBlB,SAAS,cAGd;AAAA,EACA;AAAA,EACA,cAAc;AAAA,EACd;AACF,GAMkD;AAChD,SAAO,OAAO,YAAY;AACxB,UAAM,QAAQ,QAAQ,OACnB,cAAc,EACd,KAAK,EAAE,UAAU,QAAQ,UAAU,OAAO,KAAK,CAAC;AACnD,UAAM,YAAY,CAAC,CAAC,SAAS,MAAM,MAAM,SAAS;AAElD,QAAI,aAAa,gBAAgB,SAAS;AACxC,YAAM,SAAS;AAAA,QACb,QAAQ;AAAA,QACR,MAAM;AAAA,QACN,OAAO;AAAA,QACP,aAAa;AAAA,MACf,CAAC;AAAA,IACH;AAEA,QAAI,SAA8B,CAAC;AACnC,UAAM,SAAS,MAAM,QAAQ,OAAO;AAEpC,qBAAiB,SAAS,QAAQ;AAChC,UAAI,QAAQ,OAAO,SAAS;AAC1B;AAAA,MACF;AAGA,UAAI,CAAC,aAAa,gBAAgB,WAAW;AAC3C,gBAAQ,OAAO;AAAA,UACb,QAAQ;AAAA,UACR,CAAC,OAAO,CAAC,MAAM;AACb,uBAAO,uBAAS,MAAM,OAAO,SAAS;AAAA,UACxC;AAAA,QACF;AAAA,MACF;AACA,mBAAS,uBAAS,QAAQ,OAAO,SAAS;AAAA,IAC5C;AAGA,QAAI,aAAa,gBAAgB,aAAa,CAAC,QAAQ,OAAO,SAAS;AACrE,cAAQ,OAAO,aAAkC,QAAQ,UAAU,MAAM;AAAA,IAC3E;AAEA,WAAO,QAAQ,OAAO,aAAa,QAAQ,QAAQ;AAAA,EACrD;AACF;","names":[]}
1
+ {"version":3,"sources":["../../src/streamedQuery.ts"],"sourcesContent":["import { addToEnd } from './utils'\nimport type { QueryFunction, QueryFunctionContext, QueryKey } from './types'\n\ntype BaseStreamedQueryParams<TQueryFnData, TQueryKey extends QueryKey> = {\n streamFn: (\n context: QueryFunctionContext<TQueryKey>,\n ) => AsyncIterable<TQueryFnData> | Promise<AsyncIterable<TQueryFnData>>\n refetchMode?: 'append' | 'reset' | 'replace'\n}\n\ntype SimpleStreamedQueryParams<\n TQueryFnData,\n TQueryKey extends QueryKey,\n> = BaseStreamedQueryParams<TQueryFnData, TQueryKey> & {\n reducer?: never\n initialValue?: never\n}\n\ntype ReducibleStreamedQueryParams<\n TQueryFnData,\n TData,\n TQueryKey extends QueryKey,\n> = BaseStreamedQueryParams<TQueryFnData, TQueryKey> & {\n reducer: (acc: TData, chunk: TQueryFnData) => TData\n initialValue: TData\n}\n\ntype StreamedQueryParams<TQueryFnData, TData, TQueryKey extends QueryKey> =\n | SimpleStreamedQueryParams<TQueryFnData, TQueryKey>\n | ReducibleStreamedQueryParams<TQueryFnData, TData, TQueryKey>\n\n/**\n * This is a helper function to create a query function that streams data from an AsyncIterable.\n * Data will be an Array of all the chunks received.\n * The query will be in a 'pending' state until the first chunk of data is received, but will go to 'success' after that.\n * The query will stay in fetchStatus 'fetching' until the stream ends.\n * @param queryFn - The function that returns an AsyncIterable to stream data from.\n * @param refetchMode - Defines how re-fetches are handled.\n * Defaults to `'reset'`, erases all data and puts the query back into `pending` state.\n * Set to `'append'` to append new data to the existing data.\n * Set to `'replace'` to write all data to the cache once the stream ends.\n * @param reducer - A function to reduce the streamed chunks into the final data.\n * Defaults to a function that appends chunks to the end of the array.\n * @param initialValue - Initial value to be used while the first chunk is being fetched.\n */\nexport function streamedQuery<\n TQueryFnData = unknown,\n TData = Array<TQueryFnData>,\n TQueryKey extends QueryKey = QueryKey,\n>({\n streamFn,\n refetchMode = 'reset',\n reducer = (items, chunk) =>\n addToEnd(items as Array<TQueryFnData>, chunk) as TData,\n initialValue = [] as TData,\n}: StreamedQueryParams<TQueryFnData, TData, TQueryKey>): QueryFunction<\n TData,\n TQueryKey\n> {\n return async (context) => {\n const query = context.client\n .getQueryCache()\n .find({ queryKey: context.queryKey, exact: true })\n const isRefetch = !!query && query.state.data !== undefined\n if (isRefetch && refetchMode === 'reset') {\n query.setState({\n status: 'pending',\n data: undefined,\n error: null,\n fetchStatus: 'fetching',\n })\n }\n\n let result = initialValue\n\n const stream = await streamFn(context)\n\n for await (const chunk of stream) {\n if (context.signal.aborted) {\n break\n }\n\n // don't append to the cache directly when replace-refetching\n if (!isRefetch || refetchMode !== 'replace') {\n context.client.setQueryData<TData>(context.queryKey, (prev) =>\n reducer(prev === undefined ? initialValue : prev, chunk),\n )\n }\n result = reducer(result, chunk)\n }\n\n // finalize result: replace-refetching needs to write to the cache\n if (isRefetch && refetchMode === 'replace' && !context.signal.aborted) {\n context.client.setQueryData<TData>(context.queryKey, result)\n }\n\n return context.client.getQueryData(context.queryKey)!\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAyB;AA6ClB,SAAS,cAId;AAAA,EACA;AAAA,EACA,cAAc;AAAA,EACd,UAAU,CAAC,OAAO,cAChB,uBAAS,OAA8B,KAAK;AAAA,EAC9C,eAAe,CAAC;AAClB,GAGE;AACA,SAAO,OAAO,YAAY;AACxB,UAAM,QAAQ,QAAQ,OACnB,cAAc,EACd,KAAK,EAAE,UAAU,QAAQ,UAAU,OAAO,KAAK,CAAC;AACnD,UAAM,YAAY,CAAC,CAAC,SAAS,MAAM,MAAM,SAAS;AAClD,QAAI,aAAa,gBAAgB,SAAS;AACxC,YAAM,SAAS;AAAA,QACb,QAAQ;AAAA,QACR,MAAM;AAAA,QACN,OAAO;AAAA,QACP,aAAa;AAAA,MACf,CAAC;AAAA,IACH;AAEA,QAAI,SAAS;AAEb,UAAM,SAAS,MAAM,SAAS,OAAO;AAErC,qBAAiB,SAAS,QAAQ;AAChC,UAAI,QAAQ,OAAO,SAAS;AAC1B;AAAA,MACF;AAGA,UAAI,CAAC,aAAa,gBAAgB,WAAW;AAC3C,gBAAQ,OAAO;AAAA,UAAoB,QAAQ;AAAA,UAAU,CAAC,SACpD,QAAQ,SAAS,SAAY,eAAe,MAAM,KAAK;AAAA,QACzD;AAAA,MACF;AACA,eAAS,QAAQ,QAAQ,KAAK;AAAA,IAChC;AAGA,QAAI,aAAa,gBAAgB,aAAa,CAAC,QAAQ,OAAO,SAAS;AACrE,cAAQ,OAAO,aAAoB,QAAQ,UAAU,MAAM;AAAA,IAC7D;AAEA,WAAO,QAAQ,OAAO,aAAa,QAAQ,QAAQ;AAAA,EACrD;AACF;","names":[]}
@@ -1,7 +1,20 @@
1
- import { I as QueryKey, a1 as QueryFunctionContext, Y as QueryFunction } from './hydration-CADtEOkK.cjs';
1
+ import { I as QueryKey, a1 as QueryFunctionContext, Y as QueryFunction } from './hydration-_hO-y142.cjs';
2
2
  import './removable.cjs';
3
3
  import './subscribable.cjs';
4
4
 
5
+ type BaseStreamedQueryParams<TQueryFnData, TQueryKey extends QueryKey> = {
6
+ streamFn: (context: QueryFunctionContext<TQueryKey>) => AsyncIterable<TQueryFnData> | Promise<AsyncIterable<TQueryFnData>>;
7
+ refetchMode?: 'append' | 'reset' | 'replace';
8
+ };
9
+ type SimpleStreamedQueryParams<TQueryFnData, TQueryKey extends QueryKey> = BaseStreamedQueryParams<TQueryFnData, TQueryKey> & {
10
+ reducer?: never;
11
+ initialValue?: never;
12
+ };
13
+ type ReducibleStreamedQueryParams<TQueryFnData, TData, TQueryKey extends QueryKey> = BaseStreamedQueryParams<TQueryFnData, TQueryKey> & {
14
+ reducer: (acc: TData, chunk: TQueryFnData) => TData;
15
+ initialValue: TData;
16
+ };
17
+ type StreamedQueryParams<TQueryFnData, TData, TQueryKey extends QueryKey> = SimpleStreamedQueryParams<TQueryFnData, TQueryKey> | ReducibleStreamedQueryParams<TQueryFnData, TData, TQueryKey>;
5
18
  /**
6
19
  * This is a helper function to create a query function that streams data from an AsyncIterable.
7
20
  * Data will be an Array of all the chunks received.
@@ -12,15 +25,10 @@ import './subscribable.cjs';
12
25
  * Defaults to `'reset'`, erases all data and puts the query back into `pending` state.
13
26
  * Set to `'append'` to append new data to the existing data.
14
27
  * Set to `'replace'` to write all data to the cache once the stream ends.
15
- * @param maxChunks - The maximum number of chunks to keep in the cache.
16
- * Defaults to `undefined`, meaning all chunks will be kept.
17
- * If `undefined` or `0`, the number of chunks is unlimited.
18
- * If the number of chunks exceeds this number, the oldest chunk will be removed.
28
+ * @param reducer - A function to reduce the streamed chunks into the final data.
29
+ * Defaults to a function that appends chunks to the end of the array.
30
+ * @param initialValue - Initial value to be used while the first chunk is being fetched.
19
31
  */
20
- declare function streamedQuery<TQueryFnData = unknown, TQueryKey extends QueryKey = QueryKey>({ queryFn, refetchMode, maxChunks, }: {
21
- queryFn: (context: QueryFunctionContext<TQueryKey>) => AsyncIterable<TQueryFnData> | Promise<AsyncIterable<TQueryFnData>>;
22
- refetchMode?: 'append' | 'reset' | 'replace';
23
- maxChunks?: number;
24
- }): QueryFunction<Array<TQueryFnData>, TQueryKey>;
32
+ declare function streamedQuery<TQueryFnData = unknown, TData = Array<TQueryFnData>, TQueryKey extends QueryKey = QueryKey>({ streamFn, refetchMode, reducer, initialValue, }: StreamedQueryParams<TQueryFnData, TData, TQueryKey>): QueryFunction<TData, TQueryKey>;
25
33
 
26
34
  export { streamedQuery };
@@ -1,7 +1,20 @@
1
- import { I as QueryKey, a1 as QueryFunctionContext, Y as QueryFunction } from './hydration-Cvr-9VdO.js';
1
+ import { I as QueryKey, a1 as QueryFunctionContext, Y as QueryFunction } from './hydration-BYonJkjc.js';
2
2
  import './removable.js';
3
3
  import './subscribable.js';
4
4
 
5
+ type BaseStreamedQueryParams<TQueryFnData, TQueryKey extends QueryKey> = {
6
+ streamFn: (context: QueryFunctionContext<TQueryKey>) => AsyncIterable<TQueryFnData> | Promise<AsyncIterable<TQueryFnData>>;
7
+ refetchMode?: 'append' | 'reset' | 'replace';
8
+ };
9
+ type SimpleStreamedQueryParams<TQueryFnData, TQueryKey extends QueryKey> = BaseStreamedQueryParams<TQueryFnData, TQueryKey> & {
10
+ reducer?: never;
11
+ initialValue?: never;
12
+ };
13
+ type ReducibleStreamedQueryParams<TQueryFnData, TData, TQueryKey extends QueryKey> = BaseStreamedQueryParams<TQueryFnData, TQueryKey> & {
14
+ reducer: (acc: TData, chunk: TQueryFnData) => TData;
15
+ initialValue: TData;
16
+ };
17
+ type StreamedQueryParams<TQueryFnData, TData, TQueryKey extends QueryKey> = SimpleStreamedQueryParams<TQueryFnData, TQueryKey> | ReducibleStreamedQueryParams<TQueryFnData, TData, TQueryKey>;
5
18
  /**
6
19
  * This is a helper function to create a query function that streams data from an AsyncIterable.
7
20
  * Data will be an Array of all the chunks received.
@@ -12,15 +25,10 @@ import './subscribable.js';
12
25
  * Defaults to `'reset'`, erases all data and puts the query back into `pending` state.
13
26
  * Set to `'append'` to append new data to the existing data.
14
27
  * Set to `'replace'` to write all data to the cache once the stream ends.
15
- * @param maxChunks - The maximum number of chunks to keep in the cache.
16
- * Defaults to `undefined`, meaning all chunks will be kept.
17
- * If `undefined` or `0`, the number of chunks is unlimited.
18
- * If the number of chunks exceeds this number, the oldest chunk will be removed.
28
+ * @param reducer - A function to reduce the streamed chunks into the final data.
29
+ * Defaults to a function that appends chunks to the end of the array.
30
+ * @param initialValue - Initial value to be used while the first chunk is being fetched.
19
31
  */
20
- declare function streamedQuery<TQueryFnData = unknown, TQueryKey extends QueryKey = QueryKey>({ queryFn, refetchMode, maxChunks, }: {
21
- queryFn: (context: QueryFunctionContext<TQueryKey>) => AsyncIterable<TQueryFnData> | Promise<AsyncIterable<TQueryFnData>>;
22
- refetchMode?: 'append' | 'reset' | 'replace';
23
- maxChunks?: number;
24
- }): QueryFunction<Array<TQueryFnData>, TQueryKey>;
32
+ declare function streamedQuery<TQueryFnData = unknown, TData = Array<TQueryFnData>, TQueryKey extends QueryKey = QueryKey>({ streamFn, refetchMode, reducer, initialValue, }: StreamedQueryParams<TQueryFnData, TData, TQueryKey>): QueryFunction<TData, TQueryKey>;
25
33
 
26
34
  export { streamedQuery };
@@ -1,9 +1,10 @@
1
1
  // src/streamedQuery.ts
2
2
  import { addToEnd } from "./utils.js";
3
3
  function streamedQuery({
4
- queryFn,
4
+ streamFn,
5
5
  refetchMode = "reset",
6
- maxChunks
6
+ reducer = (items, chunk) => addToEnd(items, chunk),
7
+ initialValue = []
7
8
  }) {
8
9
  return async (context) => {
9
10
  const query = context.client.getQueryCache().find({ queryKey: context.queryKey, exact: true });
@@ -16,8 +17,8 @@ function streamedQuery({
16
17
  fetchStatus: "fetching"
17
18
  });
18
19
  }
19
- let result = [];
20
- const stream = await queryFn(context);
20
+ let result = initialValue;
21
+ const stream = await streamFn(context);
21
22
  for await (const chunk of stream) {
22
23
  if (context.signal.aborted) {
23
24
  break;
@@ -25,12 +26,10 @@ function streamedQuery({
25
26
  if (!isRefetch || refetchMode !== "replace") {
26
27
  context.client.setQueryData(
27
28
  context.queryKey,
28
- (prev = []) => {
29
- return addToEnd(prev, chunk, maxChunks);
30
- }
29
+ (prev) => reducer(prev === void 0 ? initialValue : prev, chunk)
31
30
  );
32
31
  }
33
- result = addToEnd(result, chunk, maxChunks);
32
+ result = reducer(result, chunk);
34
33
  }
35
34
  if (isRefetch && refetchMode === "replace" && !context.signal.aborted) {
36
35
  context.client.setQueryData(context.queryKey, result);
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/streamedQuery.ts"],"sourcesContent":["import { addToEnd } from './utils'\nimport type { QueryFunction, QueryFunctionContext, QueryKey } from './types'\n\n/**\n * This is a helper function to create a query function that streams data from an AsyncIterable.\n * Data will be an Array of all the chunks received.\n * The query will be in a 'pending' state until the first chunk of data is received, but will go to 'success' after that.\n * The query will stay in fetchStatus 'fetching' until the stream ends.\n * @param queryFn - The function that returns an AsyncIterable to stream data from.\n * @param refetchMode - Defines how re-fetches are handled.\n * Defaults to `'reset'`, erases all data and puts the query back into `pending` state.\n * Set to `'append'` to append new data to the existing data.\n * Set to `'replace'` to write all data to the cache once the stream ends.\n * @param maxChunks - The maximum number of chunks to keep in the cache.\n * Defaults to `undefined`, meaning all chunks will be kept.\n * If `undefined` or `0`, the number of chunks is unlimited.\n * If the number of chunks exceeds this number, the oldest chunk will be removed.\n */\nexport function streamedQuery<\n TQueryFnData = unknown,\n TQueryKey extends QueryKey = QueryKey,\n>({\n queryFn,\n refetchMode = 'reset',\n maxChunks,\n}: {\n queryFn: (\n context: QueryFunctionContext<TQueryKey>,\n ) => AsyncIterable<TQueryFnData> | Promise<AsyncIterable<TQueryFnData>>\n refetchMode?: 'append' | 'reset' | 'replace'\n maxChunks?: number\n}): QueryFunction<Array<TQueryFnData>, TQueryKey> {\n return async (context) => {\n const query = context.client\n .getQueryCache()\n .find({ queryKey: context.queryKey, exact: true })\n const isRefetch = !!query && query.state.data !== undefined\n\n if (isRefetch && refetchMode === 'reset') {\n query.setState({\n status: 'pending',\n data: undefined,\n error: null,\n fetchStatus: 'fetching',\n })\n }\n\n let result: Array<TQueryFnData> = []\n const stream = await queryFn(context)\n\n for await (const chunk of stream) {\n if (context.signal.aborted) {\n break\n }\n\n // don't append to the cache directly when replace-refetching\n if (!isRefetch || refetchMode !== 'replace') {\n context.client.setQueryData<Array<TQueryFnData>>(\n context.queryKey,\n (prev = []) => {\n return addToEnd(prev, chunk, maxChunks)\n },\n )\n }\n result = addToEnd(result, chunk, maxChunks)\n }\n\n // finalize result: replace-refetching needs to write to the cache\n if (isRefetch && refetchMode === 'replace' && !context.signal.aborted) {\n context.client.setQueryData<Array<TQueryFnData>>(context.queryKey, result)\n }\n\n return context.client.getQueryData(context.queryKey)!\n }\n}\n"],"mappings":";AAAA,SAAS,gBAAgB;AAkBlB,SAAS,cAGd;AAAA,EACA;AAAA,EACA,cAAc;AAAA,EACd;AACF,GAMkD;AAChD,SAAO,OAAO,YAAY;AACxB,UAAM,QAAQ,QAAQ,OACnB,cAAc,EACd,KAAK,EAAE,UAAU,QAAQ,UAAU,OAAO,KAAK,CAAC;AACnD,UAAM,YAAY,CAAC,CAAC,SAAS,MAAM,MAAM,SAAS;AAElD,QAAI,aAAa,gBAAgB,SAAS;AACxC,YAAM,SAAS;AAAA,QACb,QAAQ;AAAA,QACR,MAAM;AAAA,QACN,OAAO;AAAA,QACP,aAAa;AAAA,MACf,CAAC;AAAA,IACH;AAEA,QAAI,SAA8B,CAAC;AACnC,UAAM,SAAS,MAAM,QAAQ,OAAO;AAEpC,qBAAiB,SAAS,QAAQ;AAChC,UAAI,QAAQ,OAAO,SAAS;AAC1B;AAAA,MACF;AAGA,UAAI,CAAC,aAAa,gBAAgB,WAAW;AAC3C,gBAAQ,OAAO;AAAA,UACb,QAAQ;AAAA,UACR,CAAC,OAAO,CAAC,MAAM;AACb,mBAAO,SAAS,MAAM,OAAO,SAAS;AAAA,UACxC;AAAA,QACF;AAAA,MACF;AACA,eAAS,SAAS,QAAQ,OAAO,SAAS;AAAA,IAC5C;AAGA,QAAI,aAAa,gBAAgB,aAAa,CAAC,QAAQ,OAAO,SAAS;AACrE,cAAQ,OAAO,aAAkC,QAAQ,UAAU,MAAM;AAAA,IAC3E;AAEA,WAAO,QAAQ,OAAO,aAAa,QAAQ,QAAQ;AAAA,EACrD;AACF;","names":[]}
1
+ {"version":3,"sources":["../../src/streamedQuery.ts"],"sourcesContent":["import { addToEnd } from './utils'\nimport type { QueryFunction, QueryFunctionContext, QueryKey } from './types'\n\ntype BaseStreamedQueryParams<TQueryFnData, TQueryKey extends QueryKey> = {\n streamFn: (\n context: QueryFunctionContext<TQueryKey>,\n ) => AsyncIterable<TQueryFnData> | Promise<AsyncIterable<TQueryFnData>>\n refetchMode?: 'append' | 'reset' | 'replace'\n}\n\ntype SimpleStreamedQueryParams<\n TQueryFnData,\n TQueryKey extends QueryKey,\n> = BaseStreamedQueryParams<TQueryFnData, TQueryKey> & {\n reducer?: never\n initialValue?: never\n}\n\ntype ReducibleStreamedQueryParams<\n TQueryFnData,\n TData,\n TQueryKey extends QueryKey,\n> = BaseStreamedQueryParams<TQueryFnData, TQueryKey> & {\n reducer: (acc: TData, chunk: TQueryFnData) => TData\n initialValue: TData\n}\n\ntype StreamedQueryParams<TQueryFnData, TData, TQueryKey extends QueryKey> =\n | SimpleStreamedQueryParams<TQueryFnData, TQueryKey>\n | ReducibleStreamedQueryParams<TQueryFnData, TData, TQueryKey>\n\n/**\n * This is a helper function to create a query function that streams data from an AsyncIterable.\n * Data will be an Array of all the chunks received.\n * The query will be in a 'pending' state until the first chunk of data is received, but will go to 'success' after that.\n * The query will stay in fetchStatus 'fetching' until the stream ends.\n * @param queryFn - The function that returns an AsyncIterable to stream data from.\n * @param refetchMode - Defines how re-fetches are handled.\n * Defaults to `'reset'`, erases all data and puts the query back into `pending` state.\n * Set to `'append'` to append new data to the existing data.\n * Set to `'replace'` to write all data to the cache once the stream ends.\n * @param reducer - A function to reduce the streamed chunks into the final data.\n * Defaults to a function that appends chunks to the end of the array.\n * @param initialValue - Initial value to be used while the first chunk is being fetched.\n */\nexport function streamedQuery<\n TQueryFnData = unknown,\n TData = Array<TQueryFnData>,\n TQueryKey extends QueryKey = QueryKey,\n>({\n streamFn,\n refetchMode = 'reset',\n reducer = (items, chunk) =>\n addToEnd(items as Array<TQueryFnData>, chunk) as TData,\n initialValue = [] as TData,\n}: StreamedQueryParams<TQueryFnData, TData, TQueryKey>): QueryFunction<\n TData,\n TQueryKey\n> {\n return async (context) => {\n const query = context.client\n .getQueryCache()\n .find({ queryKey: context.queryKey, exact: true })\n const isRefetch = !!query && query.state.data !== undefined\n if (isRefetch && refetchMode === 'reset') {\n query.setState({\n status: 'pending',\n data: undefined,\n error: null,\n fetchStatus: 'fetching',\n })\n }\n\n let result = initialValue\n\n const stream = await streamFn(context)\n\n for await (const chunk of stream) {\n if (context.signal.aborted) {\n break\n }\n\n // don't append to the cache directly when replace-refetching\n if (!isRefetch || refetchMode !== 'replace') {\n context.client.setQueryData<TData>(context.queryKey, (prev) =>\n reducer(prev === undefined ? initialValue : prev, chunk),\n )\n }\n result = reducer(result, chunk)\n }\n\n // finalize result: replace-refetching needs to write to the cache\n if (isRefetch && refetchMode === 'replace' && !context.signal.aborted) {\n context.client.setQueryData<TData>(context.queryKey, result)\n }\n\n return context.client.getQueryData(context.queryKey)!\n }\n}\n"],"mappings":";AAAA,SAAS,gBAAgB;AA6ClB,SAAS,cAId;AAAA,EACA;AAAA,EACA,cAAc;AAAA,EACd,UAAU,CAAC,OAAO,UAChB,SAAS,OAA8B,KAAK;AAAA,EAC9C,eAAe,CAAC;AAClB,GAGE;AACA,SAAO,OAAO,YAAY;AACxB,UAAM,QAAQ,QAAQ,OACnB,cAAc,EACd,KAAK,EAAE,UAAU,QAAQ,UAAU,OAAO,KAAK,CAAC;AACnD,UAAM,YAAY,CAAC,CAAC,SAAS,MAAM,MAAM,SAAS;AAClD,QAAI,aAAa,gBAAgB,SAAS;AACxC,YAAM,SAAS;AAAA,QACb,QAAQ;AAAA,QACR,MAAM;AAAA,QACN,OAAO;AAAA,QACP,aAAa;AAAA,MACf,CAAC;AAAA,IACH;AAEA,QAAI,SAAS;AAEb,UAAM,SAAS,MAAM,SAAS,OAAO;AAErC,qBAAiB,SAAS,QAAQ;AAChC,UAAI,QAAQ,OAAO,SAAS;AAC1B;AAAA,MACF;AAGA,UAAI,CAAC,aAAa,gBAAgB,WAAW;AAC3C,gBAAQ,OAAO;AAAA,UAAoB,QAAQ;AAAA,UAAU,CAAC,SACpD,QAAQ,SAAS,SAAY,eAAe,MAAM,KAAK;AAAA,QACzD;AAAA,MACF;AACA,eAAS,QAAQ,QAAQ,KAAK;AAAA,IAChC;AAGA,QAAI,aAAa,gBAAgB,aAAa,CAAC,QAAQ,OAAO,SAAS;AACrE,cAAQ,OAAO,aAAoB,QAAQ,UAAU,MAAM;AAAA,IAC7D;AAEA,WAAO,QAAQ,OAAO,aAAa,QAAQ,QAAQ;AAAA,EACrD;AACF;","names":[]}
@@ -0,0 +1,98 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/timeoutManager.ts
21
+ var timeoutManager_exports = {};
22
+ __export(timeoutManager_exports, {
23
+ TimeoutManager: () => TimeoutManager,
24
+ defaultTimeoutProvider: () => defaultTimeoutProvider,
25
+ systemSetTimeoutZero: () => systemSetTimeoutZero,
26
+ timeoutManager: () => timeoutManager
27
+ });
28
+ module.exports = __toCommonJS(timeoutManager_exports);
29
+ var defaultTimeoutProvider = {
30
+ // We need the wrapper function syntax below instead of direct references to
31
+ // global setTimeout etc.
32
+ //
33
+ // BAD: `setTimeout: setTimeout`
34
+ // GOOD: `setTimeout: (cb, delay) => setTimeout(cb, delay)`
35
+ //
36
+ // If we use direct references here, then anything that wants to spy on or
37
+ // replace the global setTimeout (like tests) won't work since we'll already
38
+ // have a hard reference to the original implementation at the time when this
39
+ // file was imported.
40
+ setTimeout: (callback, delay) => setTimeout(callback, delay),
41
+ clearTimeout: (timeoutId) => clearTimeout(timeoutId),
42
+ setInterval: (callback, delay) => setInterval(callback, delay),
43
+ clearInterval: (intervalId) => clearInterval(intervalId)
44
+ };
45
+ var TimeoutManager = class {
46
+ // We cannot have TimeoutManager<T> as we must instantiate it with a concrete
47
+ // type at app boot; and if we leave that type, then any new timer provider
48
+ // would need to support ReturnType<typeof setTimeout>, which is infeasible.
49
+ //
50
+ // We settle for type safety for the TimeoutProvider type, and accept that
51
+ // this class is unsafe internally to allow for extension.
52
+ #provider = defaultTimeoutProvider;
53
+ #providerCalled = false;
54
+ setTimeoutProvider(provider) {
55
+ if (process.env.NODE_ENV !== "production") {
56
+ if (this.#providerCalled && provider !== this.#provider) {
57
+ console.error(
58
+ `[timeoutManager]: Switching provider after calls to previous provider might result in unexpected behavior.`,
59
+ { previous: this.#provider, provider }
60
+ );
61
+ }
62
+ }
63
+ this.#provider = provider;
64
+ if (process.env.NODE_ENV !== "production") {
65
+ this.#providerCalled = false;
66
+ }
67
+ }
68
+ setTimeout(callback, delay) {
69
+ if (process.env.NODE_ENV !== "production") {
70
+ this.#providerCalled = true;
71
+ }
72
+ return this.#provider.setTimeout(callback, delay);
73
+ }
74
+ clearTimeout(timeoutId) {
75
+ this.#provider.clearTimeout(timeoutId);
76
+ }
77
+ setInterval(callback, delay) {
78
+ if (process.env.NODE_ENV !== "production") {
79
+ this.#providerCalled = true;
80
+ }
81
+ return this.#provider.setInterval(callback, delay);
82
+ }
83
+ clearInterval(intervalId) {
84
+ this.#provider.clearInterval(intervalId);
85
+ }
86
+ };
87
+ var timeoutManager = new TimeoutManager();
88
+ function systemSetTimeoutZero(callback) {
89
+ setTimeout(callback, 0);
90
+ }
91
+ // Annotate the CommonJS export names for ESM import in node:
92
+ 0 && (module.exports = {
93
+ TimeoutManager,
94
+ defaultTimeoutProvider,
95
+ systemSetTimeoutZero,
96
+ timeoutManager
97
+ });
98
+ //# sourceMappingURL=timeoutManager.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/timeoutManager.ts"],"sourcesContent":["/**\n * {@link TimeoutManager} does not support passing arguments to the callback.\n *\n * `(_: void)` is the argument type inferred by TypeScript's default typings for\n * `setTimeout(cb, number)`.\n * If we don't accept a single void argument, then\n * `new Promise(resolve => timeoutManager.setTimeout(resolve, N))` is a type error.\n */\nexport type TimeoutCallback = (_: void) => void\n\n/**\n * Wrapping `setTimeout` is awkward from a typing perspective because platform\n * typings may extend the return type of `setTimeout`. For example, NodeJS\n * typings add `NodeJS.Timeout`; but a non-default `timeoutManager` may not be\n * able to return such a type.\n */\nexport type ManagedTimerId = number | { [Symbol.toPrimitive]: () => number }\n\n/**\n * Backend for timer functions.\n */\nexport type TimeoutProvider<TTimerId extends ManagedTimerId = ManagedTimerId> =\n {\n readonly setTimeout: (callback: TimeoutCallback, delay: number) => TTimerId\n readonly clearTimeout: (timeoutId: TTimerId | undefined) => void\n\n readonly setInterval: (callback: TimeoutCallback, delay: number) => TTimerId\n readonly clearInterval: (intervalId: TTimerId | undefined) => void\n }\n\nexport const defaultTimeoutProvider: TimeoutProvider<\n ReturnType<typeof setTimeout>\n> = {\n // We need the wrapper function syntax below instead of direct references to\n // global setTimeout etc.\n //\n // BAD: `setTimeout: setTimeout`\n // GOOD: `setTimeout: (cb, delay) => setTimeout(cb, delay)`\n //\n // If we use direct references here, then anything that wants to spy on or\n // replace the global setTimeout (like tests) won't work since we'll already\n // have a hard reference to the original implementation at the time when this\n // file was imported.\n setTimeout: (callback, delay) => setTimeout(callback, delay),\n clearTimeout: (timeoutId) => clearTimeout(timeoutId),\n\n setInterval: (callback, delay) => setInterval(callback, delay),\n clearInterval: (intervalId) => clearInterval(intervalId),\n}\n\n/**\n * Allows customization of how timeouts are created.\n *\n * @tanstack/query-core makes liberal use of timeouts to implement `staleTime`\n * and `gcTime`. The default TimeoutManager provider uses the platform's global\n * `setTimeout` implementation, which is known to have scalability issues with\n * thousands of timeouts on the event loop.\n *\n * If you hit this limitation, consider providing a custom TimeoutProvider that\n * coalesces timeouts.\n */\nexport class TimeoutManager implements Omit<TimeoutProvider, 'name'> {\n // We cannot have TimeoutManager<T> as we must instantiate it with a concrete\n // type at app boot; and if we leave that type, then any new timer provider\n // would need to support ReturnType<typeof setTimeout>, which is infeasible.\n //\n // We settle for type safety for the TimeoutProvider type, and accept that\n // this class is unsafe internally to allow for extension.\n #provider: TimeoutProvider<any> = defaultTimeoutProvider\n #providerCalled = false\n\n setTimeoutProvider<TTimerId extends ManagedTimerId>(\n provider: TimeoutProvider<TTimerId>,\n ): void {\n if (process.env.NODE_ENV !== 'production') {\n if (this.#providerCalled && provider !== this.#provider) {\n // After changing providers, `clearTimeout` will not work as expected for\n // timeouts from the previous provider.\n //\n // Since they may allocate the same timeout ID, clearTimeout may cancel an\n // arbitrary different timeout, or unexpected no-op.\n //\n // We could protect against this by mixing the timeout ID bits\n // deterministically with some per-provider bits.\n //\n // We could internally queue `setTimeout` calls to `TimeoutManager` until\n // some API call to set the initial provider.\n console.error(\n `[timeoutManager]: Switching provider after calls to previous provider might result in unexpected behavior.`,\n { previous: this.#provider, provider },\n )\n }\n }\n\n this.#provider = provider\n if (process.env.NODE_ENV !== 'production') {\n this.#providerCalled = false\n }\n }\n\n setTimeout(callback: TimeoutCallback, delay: number): ManagedTimerId {\n if (process.env.NODE_ENV !== 'production') {\n this.#providerCalled = true\n }\n return this.#provider.setTimeout(callback, delay)\n }\n\n clearTimeout(timeoutId: ManagedTimerId | undefined): void {\n this.#provider.clearTimeout(timeoutId)\n }\n\n setInterval(callback: TimeoutCallback, delay: number): ManagedTimerId {\n if (process.env.NODE_ENV !== 'production') {\n this.#providerCalled = true\n }\n return this.#provider.setInterval(callback, delay)\n }\n\n clearInterval(intervalId: ManagedTimerId | undefined): void {\n this.#provider.clearInterval(intervalId)\n }\n}\n\nexport const timeoutManager = new TimeoutManager()\n\n/**\n * In many cases code wants to delay to the next event loop tick; this is not\n * mediated by {@link timeoutManager}.\n *\n * This function is provided to make auditing the `tanstack/query-core` for\n * incorrect use of system `setTimeout` easier.\n */\nexport function systemSetTimeoutZero(callback: TimeoutCallback): void {\n setTimeout(callback, 0)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA8BO,IAAM,yBAET;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWF,YAAY,CAAC,UAAU,UAAU,WAAW,UAAU,KAAK;AAAA,EAC3D,cAAc,CAAC,cAAc,aAAa,SAAS;AAAA,EAEnD,aAAa,CAAC,UAAU,UAAU,YAAY,UAAU,KAAK;AAAA,EAC7D,eAAe,CAAC,eAAe,cAAc,UAAU;AACzD;AAaO,IAAM,iBAAN,MAA8D;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOnE,YAAkC;AAAA,EAClC,kBAAkB;AAAA,EAElB,mBACE,UACM;AACN,QAAI,QAAQ,IAAI,aAAa,cAAc;AACzC,UAAI,KAAK,mBAAmB,aAAa,KAAK,WAAW;AAYvD,gBAAQ;AAAA,UACN;AAAA,UACA,EAAE,UAAU,KAAK,WAAW,SAAS;AAAA,QACvC;AAAA,MACF;AAAA,IACF;AAEA,SAAK,YAAY;AACjB,QAAI,QAAQ,IAAI,aAAa,cAAc;AACzC,WAAK,kBAAkB;AAAA,IACzB;AAAA,EACF;AAAA,EAEA,WAAW,UAA2B,OAA+B;AACnE,QAAI,QAAQ,IAAI,aAAa,cAAc;AACzC,WAAK,kBAAkB;AAAA,IACzB;AACA,WAAO,KAAK,UAAU,WAAW,UAAU,KAAK;AAAA,EAClD;AAAA,EAEA,aAAa,WAA6C;AACxD,SAAK,UAAU,aAAa,SAAS;AAAA,EACvC;AAAA,EAEA,YAAY,UAA2B,OAA+B;AACpE,QAAI,QAAQ,IAAI,aAAa,cAAc;AACzC,WAAK,kBAAkB;AAAA,IACzB;AACA,WAAO,KAAK,UAAU,YAAY,UAAU,KAAK;AAAA,EACnD;AAAA,EAEA,cAAc,YAA8C;AAC1D,SAAK,UAAU,cAAc,UAAU;AAAA,EACzC;AACF;AAEO,IAAM,iBAAiB,IAAI,eAAe;AAS1C,SAAS,qBAAqB,UAAiC;AACpE,aAAW,UAAU,CAAC;AACxB;","names":[]}
@@ -0,0 +1,58 @@
1
+ /**
2
+ * {@link TimeoutManager} does not support passing arguments to the callback.
3
+ *
4
+ * `(_: void)` is the argument type inferred by TypeScript's default typings for
5
+ * `setTimeout(cb, number)`.
6
+ * If we don't accept a single void argument, then
7
+ * `new Promise(resolve => timeoutManager.setTimeout(resolve, N))` is a type error.
8
+ */
9
+ type TimeoutCallback = (_: void) => void;
10
+ /**
11
+ * Wrapping `setTimeout` is awkward from a typing perspective because platform
12
+ * typings may extend the return type of `setTimeout`. For example, NodeJS
13
+ * typings add `NodeJS.Timeout`; but a non-default `timeoutManager` may not be
14
+ * able to return such a type.
15
+ */
16
+ type ManagedTimerId = number | {
17
+ [Symbol.toPrimitive]: () => number;
18
+ };
19
+ /**
20
+ * Backend for timer functions.
21
+ */
22
+ type TimeoutProvider<TTimerId extends ManagedTimerId = ManagedTimerId> = {
23
+ readonly setTimeout: (callback: TimeoutCallback, delay: number) => TTimerId;
24
+ readonly clearTimeout: (timeoutId: TTimerId | undefined) => void;
25
+ readonly setInterval: (callback: TimeoutCallback, delay: number) => TTimerId;
26
+ readonly clearInterval: (intervalId: TTimerId | undefined) => void;
27
+ };
28
+ declare const defaultTimeoutProvider: TimeoutProvider<ReturnType<typeof setTimeout>>;
29
+ /**
30
+ * Allows customization of how timeouts are created.
31
+ *
32
+ * @tanstack/query-core makes liberal use of timeouts to implement `staleTime`
33
+ * and `gcTime`. The default TimeoutManager provider uses the platform's global
34
+ * `setTimeout` implementation, which is known to have scalability issues with
35
+ * thousands of timeouts on the event loop.
36
+ *
37
+ * If you hit this limitation, consider providing a custom TimeoutProvider that
38
+ * coalesces timeouts.
39
+ */
40
+ declare class TimeoutManager implements Omit<TimeoutProvider, 'name'> {
41
+ #private;
42
+ setTimeoutProvider<TTimerId extends ManagedTimerId>(provider: TimeoutProvider<TTimerId>): void;
43
+ setTimeout(callback: TimeoutCallback, delay: number): ManagedTimerId;
44
+ clearTimeout(timeoutId: ManagedTimerId | undefined): void;
45
+ setInterval(callback: TimeoutCallback, delay: number): ManagedTimerId;
46
+ clearInterval(intervalId: ManagedTimerId | undefined): void;
47
+ }
48
+ declare const timeoutManager: TimeoutManager;
49
+ /**
50
+ * In many cases code wants to delay to the next event loop tick; this is not
51
+ * mediated by {@link timeoutManager}.
52
+ *
53
+ * This function is provided to make auditing the `tanstack/query-core` for
54
+ * incorrect use of system `setTimeout` easier.
55
+ */
56
+ declare function systemSetTimeoutZero(callback: TimeoutCallback): void;
57
+
58
+ export { type ManagedTimerId, type TimeoutCallback, TimeoutManager, type TimeoutProvider, defaultTimeoutProvider, systemSetTimeoutZero, timeoutManager };
@@ -0,0 +1,58 @@
1
+ /**
2
+ * {@link TimeoutManager} does not support passing arguments to the callback.
3
+ *
4
+ * `(_: void)` is the argument type inferred by TypeScript's default typings for
5
+ * `setTimeout(cb, number)`.
6
+ * If we don't accept a single void argument, then
7
+ * `new Promise(resolve => timeoutManager.setTimeout(resolve, N))` is a type error.
8
+ */
9
+ type TimeoutCallback = (_: void) => void;
10
+ /**
11
+ * Wrapping `setTimeout` is awkward from a typing perspective because platform
12
+ * typings may extend the return type of `setTimeout`. For example, NodeJS
13
+ * typings add `NodeJS.Timeout`; but a non-default `timeoutManager` may not be
14
+ * able to return such a type.
15
+ */
16
+ type ManagedTimerId = number | {
17
+ [Symbol.toPrimitive]: () => number;
18
+ };
19
+ /**
20
+ * Backend for timer functions.
21
+ */
22
+ type TimeoutProvider<TTimerId extends ManagedTimerId = ManagedTimerId> = {
23
+ readonly setTimeout: (callback: TimeoutCallback, delay: number) => TTimerId;
24
+ readonly clearTimeout: (timeoutId: TTimerId | undefined) => void;
25
+ readonly setInterval: (callback: TimeoutCallback, delay: number) => TTimerId;
26
+ readonly clearInterval: (intervalId: TTimerId | undefined) => void;
27
+ };
28
+ declare const defaultTimeoutProvider: TimeoutProvider<ReturnType<typeof setTimeout>>;
29
+ /**
30
+ * Allows customization of how timeouts are created.
31
+ *
32
+ * @tanstack/query-core makes liberal use of timeouts to implement `staleTime`
33
+ * and `gcTime`. The default TimeoutManager provider uses the platform's global
34
+ * `setTimeout` implementation, which is known to have scalability issues with
35
+ * thousands of timeouts on the event loop.
36
+ *
37
+ * If you hit this limitation, consider providing a custom TimeoutProvider that
38
+ * coalesces timeouts.
39
+ */
40
+ declare class TimeoutManager implements Omit<TimeoutProvider, 'name'> {
41
+ #private;
42
+ setTimeoutProvider<TTimerId extends ManagedTimerId>(provider: TimeoutProvider<TTimerId>): void;
43
+ setTimeout(callback: TimeoutCallback, delay: number): ManagedTimerId;
44
+ clearTimeout(timeoutId: ManagedTimerId | undefined): void;
45
+ setInterval(callback: TimeoutCallback, delay: number): ManagedTimerId;
46
+ clearInterval(intervalId: ManagedTimerId | undefined): void;
47
+ }
48
+ declare const timeoutManager: TimeoutManager;
49
+ /**
50
+ * In many cases code wants to delay to the next event loop tick; this is not
51
+ * mediated by {@link timeoutManager}.
52
+ *
53
+ * This function is provided to make auditing the `tanstack/query-core` for
54
+ * incorrect use of system `setTimeout` easier.
55
+ */
56
+ declare function systemSetTimeoutZero(callback: TimeoutCallback): void;
57
+
58
+ export { type ManagedTimerId, type TimeoutCallback, TimeoutManager, type TimeoutProvider, defaultTimeoutProvider, systemSetTimeoutZero, timeoutManager };