Scheduling change detection on error and complete and aborting on teardown #199
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
When using
rxLet
directive and binding an observable to it, internally, change detection is scheduled whenever an observable notification (next/error/complete) is sent. There's a possibility (a slight, but...) that a notification is sent and before the actual change detection is triggered, the component is destroyed and stops existing. In other words - change detection is triggered on the non-existing component. That may result with errors or unexpected behaviors.The other problem this PR solves is triggering change detection on error and complete notifications by using
RenderStrategy#detectChanges
method insteadRenderStrategy#scheduleCD
. By using the second one we gain some rendering optimizations specific for the provided rendering strategy.How to reproduce?
The best way to actually see it is to change
libs/template/src/lib/core/render-aware/render-aware_creator.ts
, lines 88 and 92 (master
branch) and usescheduleCD()
method instead ofdetectChanged()
.Tests start to fail on assertions from the
detectChanges
function from the core Angular. This is because Angular is not happy with triggering change detection on component that doesn't actually exist anymore. Tests are so fast, that they destroy a component under test before the final change detection happens.Solution
Solution is to define some custom teardown logic for an observable from the view and abort scheduled CD there. This is done with a custom
finalizeWithScheduledCD
operator.