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

fix(component): untrack subscription in ngrxPush pipe #3918

Merged
merged 2 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions modules/component/spec/push/push.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
import { ChangeDetectorRef, Component, ErrorHandler } from '@angular/core';
import {
ChangeDetectorRef,
Component,
ErrorHandler,
computed,
signal,
} from '@angular/core';
import {
ComponentFixture,
fakeAsync,
flushMicrotasks,
TestBed,
waitForAsync,
} from '@angular/core/testing';
import { BehaviorSubject, delay, EMPTY, NEVER, of, throwError } from 'rxjs';
import {
BehaviorSubject,
delay,
EMPTY,
NEVER,
Observable,
of,
throwError,
} from 'rxjs';
import { PushPipe } from '../../src/push/push.pipe';
import { MockChangeDetectorRef, MockErrorHandler } from '../fixtures/fixtures';
import { stripSpaces, wrapWithSpace } from '../helpers';
Expand Down Expand Up @@ -157,6 +171,29 @@ describe('PushPipe', () => {
expect(pushPipe.transform(100)).toBe(100);
expect(pushPipe.transform('ngrx')).toBe('ngrx');
});

it('should not track signal reads in subscriptions', () => {
const trigger = signal(false);

const obs = new Observable(() => {
// Whenever `obs` is subscribed, synchronously read `trigger`.
trigger();
});

let trackCount = 0;
const tracker = computed(() => {
// Subscribe to `obs` within this `computed`. If the subscription side effect runs
// within the computed, then changes to `trigger` will invalidate this computed.
pushPipe.transform(obs);

// The computed returns how many times it's run.
return ++trackCount;
});

expect(tracker()).toBe(1);
trigger.set(true);
expect(tracker()).toBe(1);
});
});
});

Expand Down
31 changes: 17 additions & 14 deletions modules/component/src/core/render-event/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
fromPotentialObservable,
PotentialObservableResult,
} from '../potential-observable';
import { untracked } from '@angular/core';

export interface RenderEventManager<PO> {
nextPotentialObservable(potentialObservable: PO): void;
Expand Down Expand Up @@ -46,20 +47,22 @@ function switchMapToRenderEvent<PO>(): (

return new Observable<RenderEvent<PotentialObservableResult<PO>>>(
(subscriber) => {
const subscription = observable$.subscribe({
next(value) {
subscriber.next({ type: 'next', value, reset, synchronous });
reset = false;
},
error(error) {
subscriber.next({ type: 'error', error, reset, synchronous });
reset = false;
},
complete() {
subscriber.next({ type: 'complete', reset, synchronous });
reset = false;
},
});
const subscription = untracked(() =>
observable$.subscribe({
next(value) {
subscriber.next({ type: 'next', value, reset, synchronous });
reset = false;
},
error(error) {
subscriber.next({ type: 'error', error, reset, synchronous });
reset = false;
},
complete() {
subscriber.next({ type: 'complete', reset, synchronous });
reset = false;
},
})
);

if (reset) {
subscriber.next({ type: 'suspense', reset, synchronous: true });
Expand Down