weak_ref.d-DWHPG08n.d.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /**
  2. * @license Angular v19.2.13
  3. * (c) 2010-2025 Google LLC. https://angular.io/
  4. * License: MIT
  5. */
  6. /**
  7. * A comparison function which can determine if two values are equal.
  8. */
  9. type ValueEqualityFn<T> = (a: T, b: T) => boolean;
  10. /**
  11. * The default equality function used for `signal` and `computed`, which uses referential equality.
  12. */
  13. declare function defaultEquals<T>(a: T, b: T): boolean;
  14. type Version = number & {
  15. __brand: 'Version';
  16. };
  17. /**
  18. * Symbol used to tell `Signal`s apart from other functions.
  19. *
  20. * This can be used to auto-unwrap signals in various cases, or to auto-wrap non-signal values.
  21. */
  22. declare const SIGNAL: unique symbol;
  23. declare function setActiveConsumer(consumer: ReactiveNode | null): ReactiveNode | null;
  24. declare function getActiveConsumer(): ReactiveNode | null;
  25. declare function isInNotificationPhase(): boolean;
  26. interface Reactive {
  27. [SIGNAL]: ReactiveNode;
  28. }
  29. declare function isReactive(value: unknown): value is Reactive;
  30. declare const REACTIVE_NODE: ReactiveNode;
  31. /**
  32. * A producer and/or consumer which participates in the reactive graph.
  33. *
  34. * Producer `ReactiveNode`s which are accessed when a consumer `ReactiveNode` is the
  35. * `activeConsumer` are tracked as dependencies of that consumer.
  36. *
  37. * Certain consumers are also tracked as "live" consumers and create edges in the other direction,
  38. * from producer to consumer. These edges are used to propagate change notifications when a
  39. * producer's value is updated.
  40. *
  41. * A `ReactiveNode` may be both a producer and consumer.
  42. */
  43. interface ReactiveNode {
  44. /**
  45. * Version of the value that this node produces.
  46. *
  47. * This is incremented whenever a new value is produced by this node which is not equal to the
  48. * previous value (by whatever definition of equality is in use).
  49. */
  50. version: Version;
  51. /**
  52. * Epoch at which this node is verified to be clean.
  53. *
  54. * This allows skipping of some polling operations in the case where no signals have been set
  55. * since this node was last read.
  56. */
  57. lastCleanEpoch: Version;
  58. /**
  59. * Whether this node (in its consumer capacity) is dirty.
  60. *
  61. * Only live consumers become dirty, when receiving a change notification from a dependency
  62. * producer.
  63. */
  64. dirty: boolean;
  65. /**
  66. * Producers which are dependencies of this consumer.
  67. *
  68. * Uses the same indices as the `producerLastReadVersion` and `producerIndexOfThis` arrays.
  69. */
  70. producerNode: ReactiveNode[] | undefined;
  71. /**
  72. * `Version` of the value last read by a given producer.
  73. *
  74. * Uses the same indices as the `producerNode` and `producerIndexOfThis` arrays.
  75. */
  76. producerLastReadVersion: Version[] | undefined;
  77. /**
  78. * Index of `this` (consumer) in each producer's `liveConsumers` array.
  79. *
  80. * This value is only meaningful if this node is live (`liveConsumers.length > 0`). Otherwise
  81. * these indices are stale.
  82. *
  83. * Uses the same indices as the `producerNode` and `producerLastReadVersion` arrays.
  84. */
  85. producerIndexOfThis: number[] | undefined;
  86. /**
  87. * Index into the producer arrays that the next dependency of this node as a consumer will use.
  88. *
  89. * This index is zeroed before this node as a consumer begins executing. When a producer is read,
  90. * it gets inserted into the producers arrays at this index. There may be an existing dependency
  91. * in this location which may or may not match the incoming producer, depending on whether the
  92. * same producers were read in the same order as the last computation.
  93. */
  94. nextProducerIndex: number;
  95. /**
  96. * Array of consumers of this producer that are "live" (they require push notifications).
  97. *
  98. * `liveConsumerNode.length` is effectively our reference count for this node.
  99. */
  100. liveConsumerNode: ReactiveNode[] | undefined;
  101. /**
  102. * Index of `this` (producer) in each consumer's `producerNode` array.
  103. *
  104. * Uses the same indices as the `liveConsumerNode` array.
  105. */
  106. liveConsumerIndexOfThis: number[] | undefined;
  107. /**
  108. * Whether writes to signals are allowed when this consumer is the `activeConsumer`.
  109. *
  110. * This is used to enforce guardrails such as preventing writes to writable signals in the
  111. * computation function of computed signals, which is supposed to be pure.
  112. */
  113. consumerAllowSignalWrites: boolean;
  114. readonly consumerIsAlwaysLive: boolean;
  115. /**
  116. * Tracks whether producers need to recompute their value independently of the reactive graph (for
  117. * example, if no initial value has been computed).
  118. */
  119. producerMustRecompute(node: unknown): boolean;
  120. producerRecomputeValue(node: unknown): void;
  121. consumerMarkedDirty(node: unknown): void;
  122. /**
  123. * Called when a signal is read within this consumer.
  124. */
  125. consumerOnSignalRead(node: unknown): void;
  126. /**
  127. * A debug name for the reactive node. Used in Angular DevTools to identify the node.
  128. */
  129. debugName?: string;
  130. /**
  131. * Kind of node. Example: 'signal', 'computed', 'input', 'effect'.
  132. *
  133. * ReactiveNode has this as 'unknown' by default, but derived node types should override this to
  134. * make available the kind of signal that particular instance of a ReactiveNode represents.
  135. *
  136. * Used in Angular DevTools to identify the kind of signal.
  137. */
  138. kind: string;
  139. }
  140. /**
  141. * Called by implementations when a producer's signal is read.
  142. */
  143. declare function producerAccessed(node: ReactiveNode): void;
  144. /**
  145. * Increment the global epoch counter.
  146. *
  147. * Called by source producers (that is, not computeds) whenever their values change.
  148. */
  149. declare function producerIncrementEpoch(): void;
  150. /**
  151. * Ensure this producer's `version` is up-to-date.
  152. */
  153. declare function producerUpdateValueVersion(node: ReactiveNode): void;
  154. /**
  155. * Propagate a dirty notification to live consumers of this producer.
  156. */
  157. declare function producerNotifyConsumers(node: ReactiveNode): void;
  158. /**
  159. * Whether this `ReactiveNode` in its producer capacity is currently allowed to initiate updates,
  160. * based on the current consumer context.
  161. */
  162. declare function producerUpdatesAllowed(): boolean;
  163. declare function consumerMarkDirty(node: ReactiveNode): void;
  164. declare function producerMarkClean(node: ReactiveNode): void;
  165. /**
  166. * Prepare this consumer to run a computation in its reactive context.
  167. *
  168. * Must be called by subclasses which represent reactive computations, before those computations
  169. * begin.
  170. */
  171. declare function consumerBeforeComputation(node: ReactiveNode | null): ReactiveNode | null;
  172. /**
  173. * Finalize this consumer's state after a reactive computation has run.
  174. *
  175. * Must be called by subclasses which represent reactive computations, after those computations
  176. * have finished.
  177. */
  178. declare function consumerAfterComputation(node: ReactiveNode | null, prevConsumer: ReactiveNode | null): void;
  179. /**
  180. * Determine whether this consumer has any dependencies which have changed since the last time
  181. * they were read.
  182. */
  183. declare function consumerPollProducersForChange(node: ReactiveNode): boolean;
  184. /**
  185. * Disconnect this consumer from the graph.
  186. */
  187. declare function consumerDestroy(node: ReactiveNode): void;
  188. interface SignalNode<T> extends ReactiveNode {
  189. value: T;
  190. equal: ValueEqualityFn<T>;
  191. }
  192. type SignalBaseGetter<T> = (() => T) & {
  193. readonly [SIGNAL]: unknown;
  194. };
  195. interface SignalGetter<T> extends SignalBaseGetter<T> {
  196. readonly [SIGNAL]: SignalNode<T>;
  197. }
  198. /**
  199. * Create a `Signal` that can be set or updated directly.
  200. */
  201. declare function createSignal<T>(initialValue: T, equal?: ValueEqualityFn<T>): SignalGetter<T>;
  202. declare function setPostSignalSetFn(fn: (() => void) | null): (() => void) | null;
  203. declare function signalSetFn<T>(node: SignalNode<T>, newValue: T): void;
  204. declare function signalUpdateFn<T>(node: SignalNode<T>, updater: (value: T) => T): void;
  205. declare function runPostSignalSetFn(): void;
  206. declare const SIGNAL_NODE: SignalNode<unknown>;
  207. declare function setAlternateWeakRefImpl(impl: unknown): void;
  208. export { REACTIVE_NODE, SIGNAL, SIGNAL_NODE, consumerAfterComputation, consumerBeforeComputation, consumerDestroy, consumerMarkDirty, consumerPollProducersForChange, createSignal, defaultEquals, getActiveConsumer, isInNotificationPhase, isReactive, producerAccessed, producerIncrementEpoch, producerMarkClean, producerNotifyConsumers, producerUpdateValueVersion, producerUpdatesAllowed, runPostSignalSetFn, setActiveConsumer, setAlternateWeakRefImpl, setPostSignalSetFn, signalSetFn, signalUpdateFn };
  209. export type { Reactive, ReactiveNode, SignalGetter, SignalNode, ValueEqualityFn };