Skip to content
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

revert: fix(core): allow toSignal in reactive contexts #52049

Closed
wants to merge 2 commits into from

Conversation

devversion
Copy link
Member

We recently landed a change that allows toSignal to be called from within reactive contexts (e.g. effect/computed). After more thorough investigatio and consideration with the team, we feel like allowing toSignal to be called in such contexts is encouraging non-ideal / hard-to-notice code patterns.

e.g. a new subscription to an observable is made every time toSignal is invoked. There is no caching done here. Additionally, multiple new subscriptions can trigger unintended side-effects- that may slow down the app, result in incorrect/unexpected behavior or perform unnecessary work.

Users should instead move the toSignal call outside of the computed or effect and then read the signal values from within their computed. e.g.

computed(() => {
  const smth = toSignal(coldObservable$)
  return smth() + 2;
}

--> should instead be:

const smth = toSignal(coldObsverable$);
computed(() => smth() + 2);

In cases where a new subscription for each invocation is actually intended, a manual subscription can be made. That way it's also much more obvious to users that they are triggering side-effects every time, or causing new subscriptions.

@angular-robot angular-robot bot added the detected: feature PR contains a feature commit label Oct 5, 2023
@devversion devversion changed the title revert: do not allow toSignal in reactive contexts revert(core): allow toSignal in reactive contexts Oct 5, 2023
@devversion devversion changed the title revert(core): allow toSignal in reactive contexts revert: fix(core): allow toSignal in reactive contexts Oct 5, 2023
…text

Some functions or code should never run inside reactive contexts. A
function to assert that will help putting guard rails in place.
Revert (with improvements of): dcf18dc

We recently landed a change that allows `toSignal` to be called
from within reactive contexts (e.g. `effect`/`computed`). After
more thorough investigatio and consideration with the team, we
feel like allowing `toSignal` to be called in such contexts is
encouraging non-ideal / hard-to-notice code patterns.

e.g. a new subscription to an observable is made every time `toSignal`
is invoked. There is no caching done here. Additionally, multiple new
subscriptions can trigger unintended side-effects- that may slow down
the app, result in incorrect/unexpected behavior or perform unnecessary
work.

Users should instead move the `toSignal` call outside of the `computed`
or `effect` and then read the signal values from within their `computed`. e.g.

```ts
computed(() => {
  const smth = toSignal(coldObservable$)
  return smth() + 2;
}
```

--> should instead be:

```ts
const smth = toSignal(coldObsverable$);
computed(() => smth() + 2);
```

In cases where a new subscription for each invocation is actually intended, a manual
subscription can be made. That way it's also much more obvious to users
that they are triggering side-effects every time, or causing new
subscriptions.
@devversion devversion added action: review The PR is still awaiting reviews from at least one requested reviewer target: major This PR is targeted for the next major release and removed target: major This PR is targeted for the next major release labels Oct 5, 2023
@devversion devversion marked this pull request as ready for review October 5, 2023 13:03
@devversion devversion added this to the v17-candidates milestone Oct 5, 2023
@ngbot ngbot bot removed this from the v17-candidates milestone Oct 5, 2023
Copy link
Member

@pkozlowski-opensource pkozlowski-opensource left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Reviewed-for: public-api
Reviewed-for: fw-core

@pkozlowski-opensource pkozlowski-opensource added the area: core Issues related to the framework runtime label Oct 5, 2023
@ngbot ngbot bot added this to the Backlog milestone Oct 5, 2023
@devversion devversion added target: major This PR is targeted for the next major release action: merge The PR is ready for merge by the caretaker and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Oct 5, 2023
Copy link
Contributor

@jessicajaniuk jessicajaniuk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reviewed-for: public-api

@devversion devversion added the merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note label Oct 5, 2023
@devversion
Copy link
Member Author

Caretaker note: I cannot remove pending reviewers. please ignore. PR has sufficient approvals

@atscott
Copy link
Contributor

atscott commented Oct 5, 2023

This PR was merged into the repository by commit ced66d4.

@atscott atscott closed this in 4427e1e Oct 5, 2023
atscott pushed a commit that referenced this pull request Oct 5, 2023
Revert (with improvements of): dcf18dc

We recently landed a change that allows `toSignal` to be called
from within reactive contexts (e.g. `effect`/`computed`). After
more thorough investigatio and consideration with the team, we
feel like allowing `toSignal` to be called in such contexts is
encouraging non-ideal / hard-to-notice code patterns.

e.g. a new subscription to an observable is made every time `toSignal`
is invoked. There is no caching done here. Additionally, multiple new
subscriptions can trigger unintended side-effects- that may slow down
the app, result in incorrect/unexpected behavior or perform unnecessary
work.

Users should instead move the `toSignal` call outside of the `computed`
or `effect` and then read the signal values from within their `computed`. e.g.

```ts
computed(() => {
  const smth = toSignal(coldObservable$)
  return smth() + 2;
}
```

--> should instead be:

```ts
const smth = toSignal(coldObsverable$);
computed(() => smth() + 2);
```

In cases where a new subscription for each invocation is actually intended, a manual
subscription can be made. That way it's also much more obvious to users
that they are triggering side-effects every time, or causing new
subscriptions.

PR Close #52049
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Nov 6, 2023
ChellappanRajan pushed a commit to ChellappanRajan/angular that referenced this pull request Jan 23, 2024
…text (angular#52049)

Some functions or code should never run inside reactive contexts. A
function to assert that will help putting guard rails in place.

PR Close angular#52049
ChellappanRajan pushed a commit to ChellappanRajan/angular that referenced this pull request Jan 23, 2024
Revert (with improvements of): dcf18dc

We recently landed a change that allows `toSignal` to be called
from within reactive contexts (e.g. `effect`/`computed`). After
more thorough investigatio and consideration with the team, we
feel like allowing `toSignal` to be called in such contexts is
encouraging non-ideal / hard-to-notice code patterns.

e.g. a new subscription to an observable is made every time `toSignal`
is invoked. There is no caching done here. Additionally, multiple new
subscriptions can trigger unintended side-effects- that may slow down
the app, result in incorrect/unexpected behavior or perform unnecessary
work.

Users should instead move the `toSignal` call outside of the `computed`
or `effect` and then read the signal values from within their `computed`. e.g.

```ts
computed(() => {
  const smth = toSignal(coldObservable$)
  return smth() + 2;
}
```

--> should instead be:

```ts
const smth = toSignal(coldObsverable$);
computed(() => smth() + 2);
```

In cases where a new subscription for each invocation is actually intended, a manual
subscription can be made. That way it's also much more obvious to users
that they are triggering side-effects every time, or causing new
subscriptions.

PR Close angular#52049
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker area: core Issues related to the framework runtime detected: feature PR contains a feature commit merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note target: major This PR is targeted for the next major release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants