(node: ReactiveNode)
| 214 | * Called by implementations when a producer's signal is read. |
| 215 | */ |
| 216 | export function producerAccessed(node: ReactiveNode): void { |
| 217 | if (inNotificationPhase[0]) { |
| 218 | throw new Error( |
| 219 | typeof ngDevMode !== 'undefined' && ngDevMode |
| 220 | ? `Assertion error: signal read during notification phase` |
| 221 | : '', |
| 222 | ); |
| 223 | } |
| 224 | |
| 225 | const consumer = activeConsumer[0]; |
| 226 | if (consumer === null) { |
| 227 | // Accessed outside of a reactive context, so nothing to record. |
| 228 | return; |
| 229 | } |
| 230 | |
| 231 | consumer.consumerOnSignalRead(node); |
| 232 | |
| 233 | const prevProducerLink = consumer.producersTail; |
| 234 | |
| 235 | // If the last producer we accessed is the same as the current one, we can skip adding a new |
| 236 | // link |
| 237 | if (prevProducerLink !== undefined && prevProducerLink.producer === node) { |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | let nextProducerLink: ReactiveLink | undefined = undefined; |
| 242 | const isRecomputing = consumer.recomputing; |
| 243 | if (isRecomputing) { |
| 244 | // If we're incrementally rebuilding the producers list, we want to check if the next producer |
| 245 | // in the list is the same as the one we're trying to add. |
| 246 | |
| 247 | // If the previous producer is defined, then the next producer is just the one that follows it. |
| 248 | // Otherwise, we should check the head of the producers list (the first node that we accessed the last time this consumer was run). |
| 249 | nextProducerLink = |
| 250 | prevProducerLink !== undefined ? prevProducerLink.nextProducer : consumer.producers; |
| 251 | if (nextProducerLink !== undefined && nextProducerLink.producer === node) { |
| 252 | // If the next producer is the same as the one we're trying to add, we can just update the |
| 253 | // last read version, update the tail of the producers list of this rerun, and return. |
| 254 | consumer.producersTail = nextProducerLink; |
| 255 | nextProducerLink.lastReadVersion = node.version; |
| 256 | nextProducerLink.knownValidAtEpoch = epoch; |
| 257 | return; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | const prevConsumerLink = node.consumersTail; |
| 262 | |
| 263 | // If the producer we're accessing already has a link to this consumer, we can skip adding a new |
| 264 | // link. This can short circuit the creation of a new link in the case where the consumer reads alternating ReactiveNodes |
| 265 | if ( |
| 266 | prevConsumerLink !== undefined && |
| 267 | prevConsumerLink.consumer === consumer && |
| 268 | (!isRecomputing || prevConsumerLink.knownValidAtEpoch === epoch) |
| 269 | ) { |
| 270 | return; |
| 271 | } |
| 272 | |
| 273 | // If we got here, it means that we need to create a new link between the producer and the consumer. |
no test coverage detected