Skip to content

refactor(core): Split consumerBefore/AfterComputation #62549

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

rockymeza
Copy link

@rockymeza rockymeza commented Jul 9, 2025

This makes it possible to batch effects, where we can "reopen" consumers during initial render and then finalize them after we are finally done adding all the effects to a batch:

// pseudo-code
function createBatch() {
  const effect = // ... create effect node
  resetConsumerBeforeComputation(effect);
  return effect;
}

function appendEffect(effectBatch, updater) {
  if (value is a signal) {
    const prevConsumer = setActiveConsumer(effectBatch.node);
    const output = value();
    setActiveConsumer(prevConsumer);
    effectBatch.push({ signal, updater });
    return output;
  }
}

function finalizeBatch(effectBatch) {
  if (effectBatch.length > 0) {
    consumerAfterComputationInternal(effectBatch.node);
  }
}

const effectBatch = createBatchEffectNode();
appendEffect(signal1, (newValue) => /* something */);
appendEffect(signal2, (newValue) => /* something different */);
finalizeBatch(effectBatch);

I didn't know what to call the new function, so I just called it consumerAfterComputationInternal, which I think should discourage its usage, but I'm definitely open to a better name.

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • angular.dev application / infrastructure changes
  • Other... Please describe:

What is the current behavior?

consumerAfterComputation resets the active consumer to the previous consumer and finalizes the current consumer.

It is currently impossible to finalize the current consumer without resetting the previous consumer.

Issue Number: N/A

What is the new behavior?

Split out the finalization code so that you can finalize a consumer separately from resetting the consumer. This is useful for batching APIs where we subscribe multiple signals to a consumer in different code paths before finalizing it.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

This is a refactor of consumerAfterComputation which splits it into two functions to allow a batching API. It is technically an API change though, since it's introducing a new function. Does that still count as refactor?

@pullapprove pullapprove bot requested review from AndrewKushnir and iteriani July 9, 2025 15:48
@pullapprove pullapprove bot added the requires: TGP This PR requires a passing TGP before merging is allowed label Jul 9, 2025
@angular-robot angular-robot bot added the area: core Issues related to the framework runtime label Jul 9, 2025
@ngbot ngbot bot added this to the Backlog milestone Jul 9, 2025
@rockymeza rockymeza changed the title refactor(core): Split consumerAfterComputation refactor(core): Split consumerBefore/AfterComputation Jul 11, 2025
@rockymeza rockymeza force-pushed the split-after branch 2 times, most recently from d09c1b3 to 5379b97 Compare July 11, 2025 21:33
This makes it possible to batch effects, where we can "reopen" consumers
during initial render and then finalize them after we are finally done
adding all the effects to a batch:

```
function createBatch() {
  const effect = // ... create effect node
  resetConsumerBeforeComputation(effect);
  return effect;
}

// pseudo-code
function appendEffect(effectBatch, updater) {
  if (value is a signal) {
    const prevConsumer = setActiveConsumer(effectBatch.node);
    const output = value();
    setActiveConsumer(prevConsumer);
    effectBatch.push({ signal, updater });
    return output;
  }
}

function finalizeBatch(effectBatch) {
  if (effectBatch.length > 0) {
    finalizeConsumerAfterComputation(effectBatch.node);
  }
}

const effectBatch = createBatchEffectNode();
appendEffect(signal1, (newValue) => /* something */);
appendEffect(signal2, (newValue) => /* something different */);
finalizeBatch(effectBatch);
```
@AndrewKushnir AndrewKushnir requested review from alxhub and pkozlowski-opensource and removed request for AndrewKushnir July 14, 2025 15:28
@rockymeza
Copy link
Author

@iteriani
Copy link
Contributor

LGTM

reviewed-for: primitives

However, get a review for signal specific review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: core Issues related to the framework runtime requires: TGP This PR requires a passing TGP before merging is allowed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants