版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

165 lines
6.1 KiB

  1. import { RouteParams, LocationQuery, RouteRecordName, RouteLocationNormalizedLoaded, Router } from 'vue-router';
  2. import { Ref, UnwrapRef } from 'vue';
  3. /**
  4. * `DataLoaderEntry` groups all of the properties that can be relied on by the data fetching guard. Any extended loader
  5. * should implement this interface. Each loaders has their own set of entries attached to an app instance.
  6. */
  7. interface DataLoaderEntry<T = unknown, isLazy = boolean> {
  8. /**
  9. * When was the data loaded in ms (Date.now()).
  10. * @internal
  11. */
  12. when: number;
  13. /**
  14. * Location's params that were used to load the data.
  15. */
  16. params: Partial<RouteParams>;
  17. /**
  18. * Location's query that was used to load the data.
  19. */
  20. query: Partial<LocationQuery>;
  21. /**
  22. * Location's hash that was used to load the data.
  23. */
  24. hash: string | null;
  25. /**
  26. * Other data loaders that depend on this one. This is used to invalidate the data when a dependency is invalidated.
  27. */
  28. children: Set<DataLoaderEntry>;
  29. /**
  30. * Whether there is an ongoing request.
  31. */
  32. pending: Ref<boolean>;
  33. /**
  34. * Error if there was an error.
  35. */
  36. error: Ref<any>;
  37. /**
  38. * Is the entry ready with data. This is set to `true` the first time the entry is updated with data.
  39. */
  40. isReady: boolean;
  41. /**
  42. * Data stored in the entry.
  43. */
  44. data: false extends isLazy ? Ref<UnwrapRef<T>> : Ref<UnwrapRef<T> | undefined>;
  45. }
  46. /**
  47. * Stop and invalidate the scope used for data. Note this will make any application stop working. It should be used only
  48. * if there is a need to manually stop a running application without stopping the process.
  49. */
  50. declare function stopScope(): void;
  51. interface DefineLoaderOptions<isLazy extends boolean = boolean> {
  52. /**
  53. * How long should we wait to consider the fetched data expired. Amount in ms. Defaults to 5 minutes. A value of 0
  54. * means no cache while a value of `Infinity` means cache forever.
  55. */
  56. cacheTime?: number;
  57. /**
  58. * Whether the data should be lazy loaded without blocking the navigation or not. Defaults to false. When set to true
  59. * or a function, the loader will no longer block the navigation and the returned composable can be called even
  60. * without having the data ready. This also means that the data will be available as one single `ref()` named `data`
  61. * instead of all the individual properties returned by the loader.
  62. */
  63. lazy?: isLazy;
  64. /**
  65. * SSR Key to store the data in an object that can be serialized later to the HTML page.
  66. */
  67. key?: string;
  68. }
  69. /**
  70. * Loader function that can be passed to `defineLoader()`.
  71. */
  72. interface DefineLoaderFn<T> {
  73. (route: RouteLocationNormalizedLoaded): T extends Promise<any> ? T : Promise<T>;
  74. }
  75. /**
  76. * Creates a data loader composables that can be exported by pages to attach the data loading to a route. This returns a
  77. * composable that can be used in any component.
  78. *
  79. * @experimental
  80. * Still under development and subject to change. See https://github.com/vuejs/rfcs/discussions/460
  81. *
  82. * @param name - optional name of the route to narrow down the type of the `to` argument
  83. * @param loader - loader function that returns the data to be loaded
  84. * @param options - options to configure the loader
  85. */
  86. declare function defineLoader<P extends Promise<any>, isLazy extends boolean = false>(name: RouteRecordName, loader: DefineLoaderFn<P>, options?: DefineLoaderOptions<isLazy>): DataLoader<Awaited<P>, isLazy>;
  87. /**
  88. * Creates a data loader composables that can be exported by pages to attach the data loading to a route. This returns a
  89. * composable that can be used in any component.
  90. *
  91. * @experimental
  92. * Still under development and subject to change. See https://github.com/vuejs/rfcs/discussions/460
  93. *
  94. * @param loader - loader function that returns the data to be loaded
  95. * @param options - options to configure the loader
  96. */
  97. declare function defineLoader<P extends Promise<any>, isLazy extends boolean = false>(loader: DefineLoaderFn<P>, options?: DefineLoaderOptions<isLazy>): DataLoader<Awaited<P>, isLazy>;
  98. type _PromiseMerged<T> = T & Promise<T>;
  99. declare const IsLoader: unique symbol;
  100. /**
  101. * Composable returned by `defineDataLoader()`.
  102. */
  103. interface DataLoader<T, isLazy extends boolean = boolean> {
  104. (): _PromiseMerged<_DataLoaderResult<T, isLazy>>;
  105. [IsLoader]: true;
  106. /**
  107. * Internal context for the loader.
  108. * @internal
  109. */
  110. _: _DataLoaderInternals<T>;
  111. }
  112. /**
  113. * Holds internal state of a loader. Used by the data fetching navigation guard.
  114. *
  115. * @internal
  116. */
  117. interface _DataLoaderInternals<T> {
  118. /**
  119. * Loads the data from the cache if possible, otherwise loads it from the loader and awaits it.
  120. */
  121. load: (route: RouteLocationNormalizedLoaded, router: Router, parent?: DataLoaderEntry, initialRootData?: Record<string, unknown>) => Promise<void>;
  122. /**
  123. * The data loaded by the loader associated with the router instance. As one router instance can only be used for one
  124. * app, it ensures the cache is not shared among requests.
  125. */
  126. entries: WeakMap<Router, DataLoaderEntry<T>>;
  127. /**
  128. * Resolved options for the loader.
  129. */
  130. options: Required<DefineLoaderOptions>;
  131. }
  132. /**
  133. * Return value of a loader composable defined with `defineDataLoader()`.
  134. */
  135. interface _DataLoaderResult<T = unknown, isLazy = boolean> {
  136. /**
  137. * Whether there is an ongoing request.
  138. */
  139. pending: Ref<boolean>;
  140. /**
  141. * Error if there was an error.
  142. */
  143. error: Ref<any>;
  144. /**
  145. * Refresh the data. Returns a promise that resolves when the data is refreshed.
  146. */
  147. refresh: () => Promise<void>;
  148. /**
  149. * Invalidates the data so it is reloaded on the next request.
  150. */
  151. invalidate: () => void;
  152. /**
  153. * Get the promise of the current loader if there is one, returns a falsy value otherwise.
  154. */
  155. pendingLoad: () => Promise<void> | undefined | null;
  156. /**
  157. * Data returned by the loader. If the data loader is lazy, it will be undefined until the first load.
  158. */
  159. data: false extends isLazy ? Ref<UnwrapRef<T>> : Ref<UnwrapRef<T> | undefined>;
  160. }
  161. export { DefineLoaderOptions as D, DataLoader as a, defineLoader as d, stopScope as s };