Skip to content

fix(cdk): list-template-manager should always emit latest value #1460

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 4 commits into
base: main
Choose a base branch
from
Open
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
36 changes: 28 additions & 8 deletions libs/cdk/template/spec/list-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
} from '@rx-angular/cdk/template';
import { RxStrategyProvider } from '@rx-angular/cdk/render-strategies';
import { mockConsole } from '@test-helpers';
import { ReplaySubject } from 'rxjs';
import { ReplaySubject, Subject } from 'rxjs';

@Component({
selector: 'rx-angular-error-test',
Expand Down Expand Up @@ -63,6 +63,7 @@ class ListTemplateManagerSpecComponent implements AfterViewInit {
values$ = new ReplaySubject<number[]>(1);

latestRenderedValue: any;
latestRenderedValue$ = new Subject<number>();

constructor(
private iterableDiffers: IterableDiffers,
Expand Down Expand Up @@ -97,6 +98,7 @@ class ListTemplateManagerSpecComponent implements AfterViewInit {
});
this.listManager.render(this.values$).subscribe((v: any) => {
this.latestRenderedValue = v;
this.latestRenderedValue$.next(v);
});
}
}
Expand Down Expand Up @@ -124,13 +126,7 @@ const customErrorHandler: ErrorHandler = {
};

let fixtureComponent: any;
let componentInstance: {
listManager: RxListManager<any>;
errorHandler: ErrorHandler;
templateRef: TemplateRef<any>;
values$: ReplaySubject<any[]>;
latestRenderedValue: any;
};
let componentInstance: ListTemplateManagerSpecComponent;
let componentNativeElement: any;
const setupListManagerComponent = (): void => {
TestBed.configureTestingModule({
Expand Down Expand Up @@ -184,6 +180,30 @@ describe('list-manager', () => {
expect(componentContent).toEqual('1');
});

it('should report last rendered value', () => {
fixtureComponent.detectChanges();
componentInstance.values$.next([1]);
fixtureComponent.detectChanges();
expect(componentInstance.latestRenderedValue).toEqual([1]);
});

it('should report last rendered value without any changes', () => {
let renderedValueUpdate = 0;
let renderedValue;
componentInstance.latestRenderedValue$.subscribe((value) => {
renderedValueUpdate++;
renderedValue = value;
});
const valuesToRender = [1];
fixtureComponent.detectChanges();
componentInstance.values$.next(valuesToRender);
fixtureComponent.detectChanges();
componentInstance.values$.next(valuesToRender);
fixtureComponent.detectChanges();
expect(renderedValue).toEqual([1]);
expect(renderedValueUpdate).toBe(2);
});

describe('exception handling', () => {
it('should capture errors with errorHandler', () => {
fixtureComponent.detectChanges();
Expand Down
15 changes: 11 additions & 4 deletions libs/cdk/template/src/lib/list-template-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ import {
TemplateRef,
TrackByFunction,
} from '@angular/core';
import { combineLatest, MonoTypeOperatorFunction, Observable, of } from 'rxjs';
import {
combineLatest,
MonoTypeOperatorFunction,
NEVER,
Observable,
of,
} from 'rxjs';
import {
catchError,
distinctUntilChanged,
Expand Down Expand Up @@ -119,8 +125,9 @@ export function createListTemplateManager<
let changes: IterableChanges<T>;
if (differ) {
if (partiallyFinished) {
const currentIterable = [];
for (let i = 0, ilen = viewContainerRef.length; i < ilen; i++) {
const length = viewContainerRef.length;
const currentIterable = new Array<T>(viewContainerRef.length);
for (let i = 0; i < length; i++) {
const viewRef = <EmbeddedViewRef<C>>viewContainerRef.get(i);
currentIterable[i] = viewRef.context.$implicit;
}
Expand All @@ -137,7 +144,7 @@ export function createListTemplateManager<
// Cancel old renders
switchMap(({ changes, iterable, strategy }) => {
if (!changes) {
return of([]);
return of(iterable);
}
const values = iterable || [];
// TODO: we might want to treat other iterables in a more performant way than Array.from()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ErrorHandler } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { RxStrategyProvider } from '@rx-angular/cdk/render-strategies';
import { take } from 'rxjs/operators';
import { ForModule } from '../for.module';
import { createTestComponent, TestComponent } from './fixtures';

Expand Down Expand Up @@ -48,7 +49,7 @@ describe('rxFor strategies', () => {
])('Strategy: %p', (strategy) => {
it('should render with given strategy', (done) => {
component.strategy = strategy;
component.renderedValue$.subscribe((v) => {
component.renderedValue$.pipe(take(1)).subscribe((v) => {
expect(v).toEqual([1, 2]);
expect(nativeElement.textContent).toBe('1;2;');
done();
Expand All @@ -57,7 +58,7 @@ describe('rxFor strategies', () => {
});
it('should not affect primary strategy', (done) => {
component.strategy = strategy;
component.renderedValue$.subscribe((v) => {
component.renderedValue$.pipe(take(1)).subscribe((v) => {
expect(v).toEqual([1, 2]);
expect(nativeElement.textContent).toBe('1;2;');
expect(strategyProvider.primaryStrategy).toBe(primaryStrategy);
Expand Down