Skip to content

Commit

Permalink
fix(module:image): unsubscribe old src (#7102)
Browse files Browse the repository at this point in the history
Co-authored-by: jiangchunlin <[email protected]>
  • Loading branch information
qugemingzizhenmafan and jiangchunlin authored Jan 14, 2022
1 parent b12f43a commit 87a3e27
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 5 deletions.
11 changes: 8 additions & 3 deletions components/image/image.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ export class NzImageDirective implements OnInit, OnChanges, OnDestroy {

dir?: Direction;
backLoadImage!: HTMLImageElement;
private status: ImageStatusType = 'normal';
status: ImageStatusType = 'normal';
private backLoadDestroy$: Subject<void> = new Subject();
private destroy$: Subject<void> = new Subject();

get previewable(): boolean {
Expand Down Expand Up @@ -129,6 +130,10 @@ export class NzImageDirective implements OnInit, OnChanges, OnDestroy {
this.backLoadImage.srcset = this.nzSrcset;
this.status = 'loading';

// unsubscribe last backLoad
this.backLoadDestroy$.next();
this.backLoadDestroy$.complete();
this.backLoadDestroy$ = new Subject();
if (this.backLoadImage.complete) {
this.status = 'normal';
this.getElement().nativeElement.src = this.nzSrc;
Expand All @@ -145,15 +150,15 @@ export class NzImageDirective implements OnInit, OnChanges, OnDestroy {
// The `nz-image` directive can be destroyed before the `load` or `error` event is dispatched,
// so there's no sense to keep capturing `this`.
fromEvent(this.backLoadImage, 'load')
.pipe(takeUntil(this.destroy$))
.pipe(takeUntil(this.backLoadDestroy$), takeUntil(this.destroy$))
.subscribe(() => {
this.status = 'normal';
this.getElement().nativeElement.src = this.nzSrc;
this.getElement().nativeElement.srcset = this.nzSrcset;
});

fromEvent(this.backLoadImage, 'error')
.pipe(takeUntil(this.destroy$))
.pipe(takeUntil(this.backLoadDestroy$), takeUntil(this.destroy$))
.subscribe(() => {
this.status = 'error';
if (this.nzFallback) {
Expand Down
78 changes: 76 additions & 2 deletions components/image/image.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,74 @@ import {
NzImageService
} from 'ng-zorro-antd/image';

const SRC = 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png`';
const SRC = 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png';
const QUICK_SRC =
'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4KPHN2ZyB3aWR0aD0iMTA2cHgiIGhlaWdodD0iMTIwcHgiIHZpZXdCb3g9IjAgMCAxMDYgMTIwIiB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiPgogICAgPGRlZnM+CiAgICAgICAgPGxpbmVhckdyYWRpZW50IHgxPSI2OC4xMjc5ODcyJSIgeTE9Ii0zNS42OTA1NzM3JSIgeDI9IjMwLjQ0MDA5MTQlIiB5Mj0iMTE0Ljk0MjY3OSUiIGlkPSJsaW5lYXJHcmFkaWVudC0xIj4KICAgICAgICAgICAgPHN0b3Agc3RvcC1jb2xvcj0iI0ZBOEU3RCIgb2Zmc2V0PSIwJSI+PC9zdG9wPgogICAgICAgICAgICA8c3RvcCBzdG9wLWNvbG9yPSIjRjc0QTVDIiBvZmZzZXQ9IjUxLjI2MzUxOTElIj48L3N0b3A+CiAgICAgICAgICAgIDxzdG9wIHN0b3AtY29sb3I9IiNGNTFEMkMiIG9mZnNldD0iMTAwJSI+PC9zdG9wPgogICAgICAgIDwvbGluZWFyR3JhZGllbnQ+CiAgICAgICAgPGxpbmVhckdyYWRpZW50IHgxPSI2OC4xMjc5ODcyJSIgeTE9Ii0zNS42OTA1NzM3JSIgeDI9Ijc0LjUzNjM5MTQlIiB5Mj0iMTYyLjUxMTc1NSUiIGlkPSJsaW5lYXJHcmFkaWVudC0yIj4KICAgICAgICAgICAgPHN0b3Agc3RvcC1jb2xvcj0i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xMDYuNDUgWiIgaWQ9IlBhdGgiIGZpbGw9InVybCgjbGluZWFyR3JhZGllbnQtNCkiIGZpbGwtcnVsZT0ibm9uemVybyI+PC9wYXRoPgogICAgICAgICAgICA8L2c+CiAgICAgICAgPC9nPgogICAgPC9nPgo8L3N2Zz4=';
const PLACEHOLDER =
'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png?x-oss-process=image/blur,r_50,s_50/quality,q_1/resize,m_mfit,h_200,w_200';
const FALLBACK =
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMIAAADDCAYAAADQvc6UAAABRWlDQ1BJQ0MgUHJvZmlsZQAAKJFjYGASSSwoyGFhYGDIzSspCnJ3UoiIjFJgf8LAwSDCIMogwMCcmFxc4BgQ4ANUwgCjUcG3awyMIPqyLsis7PPOq3QdDFcvjV3jOD1boQVTPQrgSkktTgbSf4A4LbmgqISBgTEFyFYuLykAsTuAbJEioKOA7DkgdjqEvQHEToKwj4DVhAQ5A9k3gGyB5IxEoBmML4BsnSQk8XQkNtReEOBxcfXxUQg1Mjc0dyHgXNJBSWpFCYh2zi+oLMpMzyhRcASGUqqCZ16yno6CkYGRAQMDKMwhqj/fAIcloxgHQqxAjIHBEugw5sUIsSQpBobtQPdLciLEVJYzMPBHMDBsayhILEqEO4DxG0txmrERhM29nYGBddr//5/DGRjYNRkY/l7////39v///y4Dmn+LgeHANwDrkl1AuO+pmgAAADhlWElmTU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAAAAAAqACAAQAAAABAAAAwqADAAQAAAABAAAAwwAAAAD9b/HnAAAHlklEQVR4Ae3dP3PTWBSGcbGzM6GCKqlIBRV0dHRJFarQ0eUT8LH4BnRU0NHR0UEFVdIlFRV7TzRksomPY8uykTk/zewQfKw/9znv4yvJynLv4uLiV2dBoDiBf4qP3/ARuCRABEFAoBEgghggQAQZQKAnYEaQBAQaASKIAQJEkAEEegJmBElAoBEgghggQAQZQKAnYEaQBAQa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cjYqhQgQoVCzDTUnQIScjUohAkQo1GxDzQkQIWejUogAEQo121BzAkTI2agUIkCEQs021JwAEXI2KoUIEKFQsw01J0CEnI1KIQJEKNRsQ80JECFno1KIABEKNdtQcwJEyNmoFCJAhELNNtScABFyNiqFCBChULMNNSdAhJyNSiEC/wGgKKC4YMA4TAAAAABJRU5ErkJggg==';

describe('Basics', () => {
let fixture: ComponentFixture<TestImageBasicsComponent>;
let context: TestImageBasicsComponent;
let debugElement: DebugElement;

beforeEach(fakeAsync(() => {
TestBed.configureTestingModule({
imports: [NzImageModule, TestImageModule, NoopAnimationsModule],
providers: [{ provide: Overlay, useClass: Overlay }]
});
TestBed.compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(TestImageBasicsComponent);
fixture.detectChanges();
context = fixture.componentInstance;
debugElement = fixture.debugElement;
});

it('should only latest src work', fakeAsync(() => {
const ERROR_SRC = 'error.png';
context.src = ERROR_SRC;
context.placeholder = null;
const image = debugElement.nativeElement.querySelector('img');
fixture.detectChanges();
const oldBackLoadImage = context.nzImage.backLoadImage;
context.src = SRC;
fixture.detectChanges();
tick(1000);
context.nzImage.backLoadImage.dispatchEvent(new Event('load'));
fixture.detectChanges();
expect(image.src).toBe(SRC);
tick(1000);
oldBackLoadImage.dispatchEvent(new ErrorEvent('error'));
fixture.detectChanges();
expect(image.src).toBe(SRC);
expect(context.nzImage.status).toBe('normal');
}));

it('should keep placeholder when latest src is loading', fakeAsync(() => {
context.src = SRC;
context.placeholder = PLACEHOLDER;
const image = debugElement.nativeElement.querySelector('img');
fixture.detectChanges();
const oldBackLoadImage = context.nzImage.backLoadImage;
const SECOND_SRC = 'https://test.com/SECOND_SRC.png';
context.src = SECOND_SRC;
fixture.detectChanges();
tick(1000);
oldBackLoadImage.dispatchEvent(new Event('load'));
fixture.detectChanges();
expect(image.src).toBe(PLACEHOLDER);
tick(1000);
context.nzImage.backLoadImage.dispatchEvent(new Event('load'));
fixture.detectChanges();
expect(image.src).toBe(SECOND_SRC);
}));
});

describe('Placeholder', () => {
let fixture: ComponentFixture<TestImagePlaceholderComponent>;
let context: TestImagePlaceholderComponent;
Expand Down Expand Up @@ -466,6 +526,15 @@ describe('Preview', () => {
});
});

@Component({
template: ` <img nz-image [nzSrc]="src" [nzPlaceholder]="placeholder" /> `
})
export class TestImageBasicsComponent {
@ViewChild(NzImageDirective) nzImage!: NzImageDirective;
src = '';
placeholder: string | null = '';
}

@Component({
template: ` <img nz-image [nzSrc]="src" [nzPlaceholder]="placeholder" [nzDisablePreview]="disablePreview" /> `
})
Expand Down Expand Up @@ -511,7 +580,12 @@ export class TestImagePreviewGroupComponent {
}
}

const TEST_COMPONENTS = [TestImageFallbackComponent, TestImagePlaceholderComponent, TestImagePreviewGroupComponent];
const TEST_COMPONENTS = [
TestImageBasicsComponent,
TestImageFallbackComponent,
TestImagePlaceholderComponent,
TestImagePreviewGroupComponent
];

@NgModule({
imports: [NzImageModule],
Expand Down

0 comments on commit 87a3e27

Please sign in to comment.