Skip to content

refactor(core): Synchronously emit the current signal value in toObservable#49894

Closed
atscott wants to merge 1 commit intoangular:mainfrom
atscott:toObservable
Closed

refactor(core): Synchronously emit the current signal value in toObservable#49894
atscott wants to merge 1 commit intoangular:mainfrom
atscott:toObservable

Conversation

@atscott
Copy link
Contributor

@atscott atscott commented Apr 17, 2023

As described in
#49681 (reply in thread), if an Observable created from a signal with toObservable is subscribed to in a template, it will initially have null as the value. Immediately after the template is done executing, effects are flushed and this results in the AsyncPipe getting a new value before the checkNoChanges pass, resulting in ExpressionChanged error.

template: '{{obs$ | async}}'
...
obs$ = toObservable(signal(0));

Instead, this commit updates the inner effect of toObservable to not be created until the next microtask.

@atscott atscott added the target: rc This PR is targeted for the next release-candidate label Apr 17, 2023
@atscott atscott requested a review from alxhub April 17, 2023 20:10
@jessicajaniuk jessicajaniuk added area: core Issues related to the framework runtime cross-cutting: signals labels Apr 17, 2023
@ngbot ngbot bot modified the milestone: Backlog Apr 17, 2023
@jessicajaniuk jessicajaniuk added the action: review The PR is still awaiting reviews from at least one requested reviewer label Apr 17, 2023
@atscott atscott added 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 action: merge The PR is ready for merge by the caretaker labels Apr 17, 2023
@atscott
Copy link
Contributor Author

atscott commented Apr 17, 2023

Adding blocked label since we have two possible options here that we should discuss as a team: Promise.resolve vs ReplaySubject.

…rvable

As described in
angular#49681 (reply in thread),
if an `Observable` created from a signal with `toObservable` is
subscribed to in a template, it will initially have `null` as the value.
Immediately after the template is done executing, effects are flushed
and this results in the `AsyncPipe` getting a new value before the
`checkNoChanges` pass, resulting in `ExpressionChanged` error.

```
template: '{{obs$ | async}}'
...
obs$ = toObservable(signal(0));
```

Instead, this commit updates the `toObservable` to synchronously emit
the initial value to the Observable stream.

Side note here: We don't exactly encourage this pattern. Instead of
using `AsyncPipe`, the template should just read signals.
@atscott atscott changed the title refactor(core): Avoid creating inner effect in toObservable until next tick refactor(core): Synchronously emit the current signal value in toObservable Apr 24, 2023
@atscott atscott added the action: merge The PR is ready for merge by the caretaker label Apr 24, 2023
@dylhunn
Copy link
Contributor

dylhunn commented Apr 25, 2023

This PR was merged into the repository by commit 02a539c.

dylhunn pushed a commit that referenced this pull request Apr 25, 2023
…rvable (#49894)

As described in
#49681 (reply in thread),
if an `Observable` created from a signal with `toObservable` is
subscribed to in a template, it will initially have `null` as the value.
Immediately after the template is done executing, effects are flushed
and this results in the `AsyncPipe` getting a new value before the
`checkNoChanges` pass, resulting in `ExpressionChanged` error.

```
template: '{{obs$ | async}}'
...
obs$ = toObservable(signal(0));
```

Instead, this commit updates the `toObservable` to synchronously emit
the initial value to the Observable stream.

Side note here: We don't exactly encourage this pattern. Instead of
using `AsyncPipe`, the template should just read signals.

PR Close #49894
@dylhunn dylhunn closed this in 02a539c Apr 25, 2023
@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 May 26, 2023
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 cross-cutting: signals target: rc This PR is targeted for the next release-candidate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants