Skip to content

refactor: replace with fromEventOutsideAngular in other places #8998

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 1 commit into
base: master
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
40 changes: 23 additions & 17 deletions components/affix/affix.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
DestroyRef,
ElementRef,
EventEmitter,
inject,
Input,
NgZone,
OnChanges,
OnDestroy,
OnInit,
Expand All @@ -25,14 +25,20 @@ import {
ViewChild,
ViewEncapsulation
} from '@angular/core';
import { fromEvent, merge, ReplaySubject, Subject, Subscription } from 'rxjs';
import { map, takeUntil, throttleTime } from 'rxjs/operators';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { merge, ReplaySubject, Subscription } from 'rxjs';
import { map, throttleTime } from 'rxjs/operators';

import { NzResizeObserver } from 'ng-zorro-antd/cdk/resize-observer';
import { NzConfigKey, NzConfigService, WithConfig } from 'ng-zorro-antd/core/config';
import { NzScrollService } from 'ng-zorro-antd/core/services';
import { NgStyleInterface } from 'ng-zorro-antd/core/types';
import { getStyleAsText, numberAttributeWithZeroFallback, shallowEqual } from 'ng-zorro-antd/core/util';
import {
fromEventOutsideAngular,
getStyleAsText,
numberAttributeWithZeroFallback,
shallowEqual
} from 'ng-zorro-antd/core/util';

import { AffixRespondEvents } from './respond-events';
import { getTargetRect, SimpleRect } from './utils';
Expand Down Expand Up @@ -78,7 +84,6 @@ export class NzAffixComponent implements AfterViewInit, OnChanges, OnDestroy, On
private placeholderStyle?: NgStyleInterface;
private positionChangeSubscription: Subscription = Subscription.EMPTY;
private offsetChanged$ = new ReplaySubject<void>(1);
private destroy$ = new Subject<boolean>();
private timeout?: ReturnType<typeof setTimeout>;
private document: Document = inject(DOCUMENT);

Expand All @@ -87,11 +92,12 @@ export class NzAffixComponent implements AfterViewInit, OnChanges, OnDestroy, On
return (typeof el === 'string' ? this.document.querySelector(el) : el) || window;
}

private destroyRef = inject(DestroyRef);

constructor(
el: ElementRef,
public nzConfigService: NzConfigService,
private scrollSrv: NzScrollService,
private ngZone: NgZone,
private platform: Platform,
private renderer: Renderer2,
private nzResizeObserver: NzResizeObserver,
Expand All @@ -103,7 +109,7 @@ export class NzAffixComponent implements AfterViewInit, OnChanges, OnDestroy, On
}

ngOnInit(): void {
this.directionality.change?.pipe(takeUntil(this.destroy$)).subscribe((direction: Direction) => {
this.directionality.change?.pipe(takeUntilDestroyed(this.destroyRef)).subscribe((direction: Direction) => {
this.dir = direction;
this.registerListeners();
this.updatePosition({} as Event);
Expand Down Expand Up @@ -139,23 +145,23 @@ export class NzAffixComponent implements AfterViewInit, OnChanges, OnDestroy, On

this.removeListeners();
const el = this.target === window ? this.document.body : (this.target as Element);
this.positionChangeSubscription = this.ngZone.runOutsideAngular(() =>
merge(
...Object.keys(AffixRespondEvents).map(evName => fromEvent(this.target, evName)),
this.offsetChanged$.pipe(map(() => ({}))),
this.nzResizeObserver.observe(el)
this.positionChangeSubscription = merge(
...Object.keys(AffixRespondEvents).map(evName => fromEventOutsideAngular(this.target, evName)),
this.offsetChanged$.pipe(map(() => ({}))),
this.nzResizeObserver.observe(el)
)
.pipe(
throttleTime(NZ_AFFIX_DEFAULT_SCROLL_TIME, undefined, { trailing: true }),
takeUntilDestroyed(this.destroyRef)
)
.pipe(throttleTime(NZ_AFFIX_DEFAULT_SCROLL_TIME, undefined, { trailing: true }), takeUntil(this.destroy$))
.subscribe(e => this.updatePosition(e as Event))
);
.subscribe(e => this.updatePosition(e as Event));

this.timeout = setTimeout(() => this.updatePosition({} as Event));
}

private removeListeners(): void {
clearTimeout(this.timeout);
this.positionChangeSubscription.unsubscribe();
this.destroy$.next(true);
this.destroy$.complete();
}

getOffset(element: Element, target: Element | Window | undefined): SimpleRect {
Expand Down
42 changes: 19 additions & 23 deletions components/image/image.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ import { Direction, Directionality } from '@angular/cdk/bidi';
import { DOCUMENT } from '@angular/common';
import {
ChangeDetectorRef,
DestroyRef,
Directive,
ElementRef,
Input,
OnChanges,
OnDestroy,
OnInit,
SimpleChanges,
booleanAttribute,
inject
} from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { Subject, fromEvent } from 'rxjs';
import { takeUntil } from 'rxjs/operators';

Expand All @@ -39,7 +40,7 @@ export type NzImageScaleStep = number;
'(click)': 'onPreview()'
}
})
export class NzImageDirective implements OnInit, OnChanges, OnDestroy {
export class NzImageDirective implements OnInit, OnChanges {
readonly _nzModuleName: NzConfigKey = NZ_CONFIG_MODULE_NAME;

@Input() nzSrc = '';
Expand All @@ -53,9 +54,9 @@ export class NzImageDirective implements OnInit, OnChanges, OnDestroy {
backLoadImage!: HTMLImageElement;
status: ImageStatusType = 'normal';
private backLoadDestroy$ = new Subject<void>();
private destroy$ = new Subject<void>();
private document: Document = inject(DOCUMENT);
private parentGroup = inject(NzImageGroupComponent, { optional: true });
private destroyRef = inject(DestroyRef);

get previewable(): boolean {
return !this.nzDisablePreview && this.status !== 'error';
Expand All @@ -75,19 +76,14 @@ export class NzImageDirective implements OnInit, OnChanges, OnDestroy {
this.parentGroup.addImage(this);
}
if (this.directionality) {
this.directionality.change?.pipe(takeUntil(this.destroy$)).subscribe((direction: Direction) => {
this.directionality.change?.pipe(takeUntilDestroyed(this.destroyRef)).subscribe((direction: Direction) => {
this.dir = direction;
this.cdr.detectChanges();
});
this.dir = this.directionality.value;
}
}

ngOnDestroy(): void {
this.destroy$.next();
this.destroy$.complete();
}

onPreview(): void {
if (!this.previewable) {
return;
Expand Down Expand Up @@ -146,40 +142,40 @@ export class NzImageDirective implements OnInit, OnChanges, OnDestroy {
this.backLoadImage.srcset = this.nzSrcset;
this.status = 'loading';

const element = this.elementRef.nativeElement;

// 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;
this.getElement().nativeElement.srcset = this.nzSrcset;
element.src = this.nzSrc;
element.srcset = this.nzSrcset;
} else {
if (this.nzPlaceholder) {
this.getElement().nativeElement.src = this.nzPlaceholder;
this.getElement().nativeElement.srcset = '';
element.src = this.nzPlaceholder;
element.srcset = '';
} else {
this.getElement().nativeElement.src = this.nzSrc;
this.getElement().nativeElement.srcset = this.nzSrcset;
element.src = this.nzSrc;
element.srcset = this.nzSrcset;
}

// 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.backLoadDestroy$), takeUntil(this.destroy$))
.pipe(takeUntil(this.backLoadDestroy$), takeUntilDestroyed(this.destroyRef))
.subscribe(() => {
this.status = 'normal';
this.getElement().nativeElement.src = this.nzSrc;
this.getElement().nativeElement.srcset = this.nzSrcset;
element.src = this.nzSrc;
element.srcset = this.nzSrcset;
});

fromEvent(this.backLoadImage, 'error')
.pipe(takeUntil(this.backLoadDestroy$), takeUntil(this.destroy$))
.pipe(takeUntil(this.backLoadDestroy$), takeUntilDestroyed(this.destroyRef))
.subscribe(() => {
this.status = 'error';
if (this.nzFallback) {
this.getElement().nativeElement.src = this.nzFallback;
this.getElement().nativeElement.srcset = '';
element.src = this.nzFallback;
element.srcset = '';
}
});
}
Expand Down
54 changes: 27 additions & 27 deletions components/table/src/table/table-inner-scroll.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
AfterViewInit,
ChangeDetectionStrategy,
Component,
DestroyRef,
ElementRef,
inject,
Input,
NgZone,
OnChanges,
Expand All @@ -22,8 +24,9 @@
ViewChild,
ViewEncapsulation
} from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { merge, Subject } from 'rxjs';
import { delay, filter, startWith, switchMap, takeUntil } from 'rxjs/operators';
import { delay, filter, startWith, switchMap } from 'rxjs/operators';

import { NzResizeService } from 'ng-zorro-antd/core/services';
import { NzSafeAny } from 'ng-zorro-antd/core/types';
Expand Down Expand Up @@ -134,7 +137,6 @@
@Input() noDataVirtualHeight = '182px';
private data$ = new Subject<void>();
private scroll$ = new Subject<void>();
private destroy$ = new Subject<void>();

setScrollPositionClassName(clear: boolean = false): void {
const { scrollWidth, scrollLeft, clientWidth } = this.tableBodyElement.nativeElement;
Expand All @@ -155,6 +157,8 @@
}
}

private destroyRef = inject(DestroyRef);

constructor(
private renderer: Renderer2,
private ngZone: NgZone,
Expand Down Expand Up @@ -187,36 +191,32 @@

ngAfterViewInit(): void {
if (this.platform.isBrowser) {
this.ngZone.runOutsideAngular(() => {
const scrollEvent$ = this.scroll$.pipe(
startWith(null),
delay(0),
switchMap(() =>
fromEventOutsideAngular<MouseEvent>(this.tableBodyElement.nativeElement, 'scroll').pipe(startWith(true))
),
takeUntil(this.destroy$)
);
const resize$ = this.resizeService.subscribe().pipe(takeUntil(this.destroy$));
const data$ = this.data$.pipe(takeUntil(this.destroy$));
const setClassName$ = merge(scrollEvent$, resize$, data$, this.scroll$).pipe(
startWith(true),
delay(0),
takeUntil(this.destroy$)
);
setClassName$.subscribe(() => this.setScrollPositionClassName());
scrollEvent$.pipe(filter(() => !!this.scrollY)).subscribe(() => {
this.tableHeaderElement.nativeElement.scrollLeft = this.tableBodyElement.nativeElement.scrollLeft;
if (this.tableFootElement) {
this.tableFootElement.nativeElement.scrollLeft = this.tableBodyElement.nativeElement.scrollLeft;
}
});
const scrollEvent$ = this.scroll$.pipe(
startWith(null),
delay(0),
switchMap(() =>
fromEventOutsideAngular<MouseEvent>(this.tableBodyElement.nativeElement, 'scroll').pipe(startWith(true))
),
takeUntilDestroyed(this.destroyRef)
);
const resize$ = this.resizeService.subscribe().pipe(takeUntilDestroyed(this.destroyRef));
const data$ = this.data$.pipe(takeUntilDestroyed(this.destroyRef));
const setClassName$ = merge(scrollEvent$, resize$, data$, this.scroll$).pipe(
startWith(true),
delay(0),
takeUntilDestroyed(this.destroyRef)
);
setClassName$.subscribe(() => this.setScrollPositionClassName());
scrollEvent$.pipe(filter(() => !!this.scrollY)).subscribe(() => {
this.tableHeaderElement.nativeElement.scrollLeft = this.tableBodyElement.nativeElement.scrollLeft;
if (this.tableFootElement) {
this.tableFootElement.nativeElement.scrollLeft = this.tableBodyElement.nativeElement.scrollLeft;

Check warning on line 213 in components/table/src/table/table-inner-scroll.component.ts

View check run for this annotation

Codecov / codecov/patch

components/table/src/table/table-inner-scroll.component.ts#L213

Added line #L213 was not covered by tests
}
});
}
}

ngOnDestroy(): void {
this.setScrollPositionClassName(true);
this.destroy$.next();
this.destroy$.complete();
}
}
Loading
Loading