api.d.d.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /**
  2. * @license Angular v20.1.0
  3. * (c) 2010-2025 Google LLC. https://angular.io/
  4. * License: MIT
  5. */
  6. import { OutputRef, OutputRefSubscription, DestroyRef, Signal, WritableSignal, ValueEqualityFn, Injector } from './chrome_dev_tools_performance.d.js';
  7. /**
  8. * An `OutputEmitterRef` is created by the `output()` function and can be
  9. * used to emit values to consumers of your directive or component.
  10. *
  11. * Consumers of your directive/component can bind to the output and
  12. * subscribe to changes via the bound event syntax. For example:
  13. *
  14. * ```html
  15. * <my-comp (valueChange)="processNewValue($event)" />
  16. * ```
  17. *
  18. * @publicAPI
  19. */
  20. declare class OutputEmitterRef<T> implements OutputRef<T> {
  21. private destroyed;
  22. private listeners;
  23. private errorHandler;
  24. constructor();
  25. subscribe(callback: (value: T) => void): OutputRefSubscription;
  26. /** Emits a new value to the output. */
  27. emit(value: T): void;
  28. }
  29. /** Gets the owning `DestroyRef` for the given output. */
  30. declare function getOutputDestroyRef(ref: OutputRef<unknown>): DestroyRef | undefined;
  31. /**
  32. * Options for declaring an output.
  33. *
  34. * @publicApi 19.0
  35. */
  36. interface OutputOptions {
  37. alias?: string;
  38. }
  39. /**
  40. * The `output` function allows declaration of Angular outputs in
  41. * directives and components.
  42. *
  43. * You can use outputs to emit values to parent directives and component.
  44. * Parents can subscribe to changes via:
  45. *
  46. * - template event bindings. For example, `(myOutput)="doSomething($event)"`
  47. * - programmatic subscription by using `OutputRef#subscribe`.
  48. *
  49. * @usageNotes
  50. *
  51. * To use `output()`, import the function from `@angular/core`.
  52. *
  53. * ```ts
  54. * import {output} from '@angular/core';
  55. * ```
  56. *
  57. * Inside your component, introduce a new class member and initialize
  58. * it with a call to `output`.
  59. *
  60. * ```ts
  61. * @Directive({
  62. * ...
  63. * })
  64. * export class MyDir {
  65. * nameChange = output<string>(); // OutputEmitterRef<string>
  66. * onClick = output(); // OutputEmitterRef<void>
  67. * }
  68. * ```
  69. *
  70. * You can emit values to consumers of your directive, by using
  71. * the `emit` method from `OutputEmitterRef`.
  72. *
  73. * ```ts
  74. * updateName(newName: string): void {
  75. * this.nameChange.emit(newName);
  76. * }
  77. * ```
  78. * @initializerApiFunction {"showTypesInSignaturePreview": true}
  79. * @publicApi 19.0
  80. */
  81. declare function output<T = void>(opts?: OutputOptions): OutputEmitterRef<T>;
  82. /**
  83. * String value capturing the status of a `Resource`.
  84. *
  85. * Possible statuses are:
  86. *
  87. * `idle` - The resource has no valid request and will not perform any loading. `value()` will be
  88. * `undefined`.
  89. *
  90. * `loading` - The resource is currently loading a new value as a result of a change in its reactive
  91. * dependencies. `value()` will be `undefined`.
  92. *
  93. * `reloading` - The resource is currently reloading a fresh value for the same reactive
  94. * dependencies. `value()` will continue to return the previously fetched value during the reloading
  95. * operation.
  96. *
  97. * `error` - Loading failed with an error. `value()` will be `undefined`.
  98. *
  99. * `resolved` - Loading has completed and the resource has the value returned from the loader.
  100. *
  101. * `local` - The resource's value was set locally via `.set()` or `.update()`.
  102. *
  103. * @experimental
  104. */
  105. type ResourceStatus = 'idle' | 'error' | 'loading' | 'reloading' | 'resolved' | 'local';
  106. /**
  107. * A Resource is an asynchronous dependency (for example, the results of an API call) that is
  108. * managed and delivered through signals.
  109. *
  110. * The usual way of creating a `Resource` is through the `resource` function, but various other APIs
  111. * may present `Resource` instances to describe their own concepts.
  112. *
  113. * @experimental
  114. */
  115. interface Resource<T> {
  116. /**
  117. * The current value of the `Resource`, or throws an error if the resource is in an error state.
  118. */
  119. readonly value: Signal<T>;
  120. /**
  121. * The current status of the `Resource`, which describes what the resource is currently doing and
  122. * what can be expected of its `value`.
  123. */
  124. readonly status: Signal<ResourceStatus>;
  125. /**
  126. * When in the `error` state, this returns the last known error from the `Resource`.
  127. */
  128. readonly error: Signal<Error | undefined>;
  129. /**
  130. * Whether this resource is loading a new value (or reloading the existing one).
  131. */
  132. readonly isLoading: Signal<boolean>;
  133. /**
  134. * Whether this resource has a valid current value.
  135. *
  136. * This function is reactive.
  137. */
  138. hasValue(): this is Resource<Exclude<T, undefined>>;
  139. }
  140. /**
  141. * A `Resource` with a mutable value.
  142. *
  143. * Overwriting the value of a resource sets it to the 'local' state.
  144. *
  145. * @experimental
  146. */
  147. interface WritableResource<T> extends Resource<T> {
  148. readonly value: WritableSignal<T>;
  149. hasValue(): this is WritableResource<Exclude<T, undefined>>;
  150. /**
  151. * Convenience wrapper for `value.set`.
  152. */
  153. set(value: T): void;
  154. /**
  155. * Convenience wrapper for `value.update`.
  156. */
  157. update(updater: (value: T) => T): void;
  158. asReadonly(): Resource<T>;
  159. /**
  160. * Instructs the resource to re-load any asynchronous dependency it may have.
  161. *
  162. * Note that the resource will not enter its reloading state until the actual backend request is
  163. * made.
  164. *
  165. * @returns true if a reload was initiated, false if a reload was unnecessary or unsupported
  166. */
  167. reload(): boolean;
  168. }
  169. /**
  170. * A `WritableResource` created through the `resource` function.
  171. *
  172. * @experimental
  173. */
  174. interface ResourceRef<T> extends WritableResource<T> {
  175. hasValue(): this is ResourceRef<Exclude<T, undefined>>;
  176. /**
  177. * Manually destroy the resource, which cancels pending requests and returns it to `idle` state.
  178. */
  179. destroy(): void;
  180. }
  181. /**
  182. * Parameter to a `ResourceLoader` which gives the request and other options for the current loading
  183. * operation.
  184. *
  185. * @experimental
  186. */
  187. interface ResourceLoaderParams<R> {
  188. params: NoInfer<Exclude<R, undefined>>;
  189. abortSignal: AbortSignal;
  190. previous: {
  191. status: ResourceStatus;
  192. };
  193. }
  194. /**
  195. * Loading function for a `Resource`.
  196. *
  197. * @experimental
  198. */
  199. type ResourceLoader<T, R> = (param: ResourceLoaderParams<R>) => PromiseLike<T>;
  200. /**
  201. * Streaming loader for a `Resource`.
  202. *
  203. * @experimental
  204. */
  205. type ResourceStreamingLoader<T, R> = (param: ResourceLoaderParams<R>) => PromiseLike<Signal<ResourceStreamItem<T>>>;
  206. /**
  207. * Options to the `resource` function, for creating a resource.
  208. *
  209. * @experimental
  210. */
  211. interface BaseResourceOptions<T, R> {
  212. /**
  213. * A reactive function which determines the request to be made. Whenever the request changes, the
  214. * loader will be triggered to fetch a new value for the resource.
  215. *
  216. * If a request function isn't provided, the loader won't rerun unless the resource is reloaded.
  217. */
  218. params?: () => R;
  219. /**
  220. * The value which will be returned from the resource when a server value is unavailable, such as
  221. * when the resource is still loading.
  222. */
  223. defaultValue?: NoInfer<T>;
  224. /**
  225. * Equality function used to compare the return value of the loader.
  226. */
  227. equal?: ValueEqualityFn<T>;
  228. /**
  229. * Overrides the `Injector` used by `resource`.
  230. */
  231. injector?: Injector;
  232. }
  233. /**
  234. * Options to the `resource` function, for creating a resource.
  235. *
  236. * @experimental
  237. */
  238. interface PromiseResourceOptions<T, R> extends BaseResourceOptions<T, R> {
  239. /**
  240. * Loading function which returns a `Promise` of the resource's value for a given request.
  241. */
  242. loader: ResourceLoader<T, R>;
  243. /**
  244. * Cannot specify `stream` and `loader` at the same time.
  245. */
  246. stream?: never;
  247. }
  248. /**
  249. * Options to the `resource` function, for creating a resource.
  250. *
  251. * @experimental
  252. */
  253. interface StreamingResourceOptions<T, R> extends BaseResourceOptions<T, R> {
  254. /**
  255. * Loading function which returns a `Promise` of a signal of the resource's value for a given
  256. * request, which can change over time as new values are received from a stream.
  257. */
  258. stream: ResourceStreamingLoader<T, R>;
  259. /**
  260. * Cannot specify `stream` and `loader` at the same time.
  261. */
  262. loader?: never;
  263. }
  264. /**
  265. * @experimental
  266. */
  267. type ResourceOptions<T, R> = PromiseResourceOptions<T, R> | StreamingResourceOptions<T, R>;
  268. /**
  269. * @experimental
  270. */
  271. type ResourceStreamItem<T> = {
  272. value: T;
  273. } | {
  274. error: Error;
  275. };
  276. export { OutputEmitterRef, getOutputDestroyRef, output };
  277. export type { BaseResourceOptions, OutputOptions, PromiseResourceOptions, Resource, ResourceLoader, ResourceLoaderParams, ResourceOptions, ResourceRef, ResourceStatus, ResourceStreamItem, ResourceStreamingLoader, StreamingResourceOptions, WritableResource };