Skip to content

Commit 591516c

Browse files
danielolaru91tjshiu
authored andcommitted
perf(cdk-experimental/column-resize): add debounce to column header hover to prevent excessive handler rendering (#30709)
Improve the user experience and performance of the `cdk-experimental` column resize feature by adding a `debounceTime` operator to the `headerCellHoveredDistinct` observable. Previously, the hover event triggered the handler immediately, which could result in unwanted handlers showing up when the user quickly moved their cursor over column headers. This change ensures that the handlers only appear if the user pauses (hovers for 300ms) over the column header, preventing handlers from rendering during fast cursor movements. (cherry picked from commit 6fd833d)
1 parent cd79b1c commit 591516c

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/cdk-experimental/column-resize/event-dispatcher.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import {Injectable, NgZone, inject} from '@angular/core';
1010
import {combineLatest, MonoTypeOperatorFunction, Observable, Subject} from 'rxjs';
11-
import {distinctUntilChanged, map, share, skip, startWith} from 'rxjs/operators';
11+
import {distinctUntilChanged, map, share, skip, startWith, debounceTime} from 'rxjs/operators';
1212

1313
import {_closest} from '../popover-edit';
1414

@@ -33,7 +33,11 @@ export class HeaderRowEventDispatcher {
3333
readonly overlayHandleActiveForCell = new Subject<Element | null>();
3434

3535
/** Distinct and shared version of headerCellHovered. */
36-
readonly headerCellHoveredDistinct = this.headerCellHovered.pipe(distinctUntilChanged(), share());
36+
readonly headerCellHoveredDistinct = this.headerCellHovered.pipe(
37+
distinctUntilChanged(),
38+
debounceTime(200),
39+
share(),
40+
);
3741

3842
/**
3943
* Emits the header that is currently hovered or hosting an active resize event (with active

src/material-experimental/column-resize/column-resize.spec.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
Injectable,
1010
ViewChild,
1111
} from '@angular/core';
12-
import {ComponentFixture, TestBed, fakeAsync, flush} from '@angular/core/testing';
12+
import {ComponentFixture, TestBed, fakeAsync, flush, tick} from '@angular/core/testing';
1313
import {MatTableModule} from '@angular/material/table';
1414
import {BehaviorSubject, Observable, ReplaySubject} from 'rxjs';
1515
import {dispatchKeyboardEvent} from '../../cdk/testing/private';
@@ -406,6 +406,7 @@ describe('Material Popover Edit', () => {
406406

407407
component.triggerHoverState();
408408
fixture.detectChanges();
409+
tick(200);
409410

410411
expect(
411412
component.getOverlayThumbElement(0).classList.contains('mat-column-resize-overlay-thumb'),
@@ -435,6 +436,7 @@ describe('Material Popover Edit', () => {
435436
component.completeResizeWithMouseInProgress(0);
436437
component.endHoverState();
437438
fixture.detectChanges();
439+
tick(200);
438440
flush();
439441

440442
expect(component.getOverlayThumbElement(0)).toBeUndefined();
@@ -448,6 +450,7 @@ describe('Material Popover Edit', () => {
448450

449451
component.triggerHoverState();
450452
fixture.detectChanges();
453+
tick(200);
451454
component.beginColumnResizeWithMouse(1);
452455

453456
const initialThumbPosition = component.getOverlayThumbPosition(1);
@@ -497,6 +500,7 @@ describe('Material Popover Edit', () => {
497500

498501
component.triggerHoverState();
499502
fixture.detectChanges();
503+
tick(200);
500504
component.beginColumnResizeWithMouse(1);
501505

502506
const initialThumbPosition = component.getOverlayThumbPosition(1);
@@ -532,6 +536,7 @@ describe('Material Popover Edit', () => {
532536

533537
component.triggerHoverState();
534538
fixture.detectChanges();
539+
tick(200);
535540
component.beginColumnResizeWithMouse(1, 2);
536541

537542
const initialPosition = component.getOverlayThumbPosition(1);
@@ -549,6 +554,7 @@ describe('Material Popover Edit', () => {
549554

550555
component.triggerHoverState();
551556
fixture.detectChanges();
557+
tick(200);
552558
component.beginColumnResizeWithMouse(1);
553559

554560
const initialThumbPosition = component.getOverlayThumbPosition(1);
@@ -586,6 +592,7 @@ describe('Material Popover Edit', () => {
586592

587593
component.triggerHoverState();
588594
fixture.detectChanges();
595+
tick(200);
589596

590597
expect(resize).toBe(null);
591598

@@ -607,6 +614,7 @@ describe('Material Popover Edit', () => {
607614

608615
component.triggerHoverState();
609616
fixture.detectChanges();
617+
tick(200);
610618
component.beginColumnResizeWithMouse(0);
611619

612620
component.updateResizeWithMouseInProgress(5);
@@ -669,6 +677,7 @@ describe('Material Popover Edit', () => {
669677

670678
component.triggerHoverState();
671679
fixture.detectChanges();
680+
tick(200);
672681

673682
component.resizeColumnWithMouse(1, 5);
674683
fixture.detectChanges();
@@ -694,6 +703,7 @@ describe('Material Popover Edit', () => {
694703

695704
component.triggerHoverState();
696705
fixture.detectChanges();
706+
tick(200);
697707

698708
component.resizeColumnWithMouse(1, 5);
699709
fixture.detectChanges();

0 commit comments

Comments
 (0)