feat(docs-infra): add debouncing in storing scroll position (#28368)
There is no debouncing when we store the scroll Position. Currently, we have a message in the console after a while: >Throttling history state changes to prevent the browser from hanging see: https://bugs.chromium.org/p/chromium/issues/detail?id=786211 for more informations PR Close #28368
This commit is contained in:
parent
d491a20f3b
commit
76e40eed35
@ -169,13 +169,6 @@ describe('AppComponent', () => {
|
|||||||
|
|
||||||
expect(component.tocMaxHeight).toMatch(/^\d+\.\d{2}$/);
|
expect(component.tocMaxHeight).toMatch(/^\d+\.\d{2}$/);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should update `scrollService.updateScrollPositonInHistory()`', () => {
|
|
||||||
const scrollService = fixture.debugElement.injector.get<ScrollService>(ScrollService);
|
|
||||||
spyOn(scrollService, 'updateScrollPositionInHistory');
|
|
||||||
component.onScroll();
|
|
||||||
expect(scrollService.updateScrollPositionInHistory).toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('SideNav', () => {
|
describe('SideNav', () => {
|
||||||
|
@ -339,9 +339,6 @@ export class AppComponent implements OnInit {
|
|||||||
// Dynamically change height of table of contents container
|
// Dynamically change height of table of contents container
|
||||||
@HostListener('window:scroll')
|
@HostListener('window:scroll')
|
||||||
onScroll() {
|
onScroll() {
|
||||||
|
|
||||||
this.scrollService.updateScrollPositionInHistory();
|
|
||||||
|
|
||||||
if (!this.tocMaxHeightOffset) {
|
if (!this.tocMaxHeightOffset) {
|
||||||
// Must wait until `mat-toolbar` is measurable.
|
// Must wait until `mat-toolbar` is measurable.
|
||||||
const el = this.hostElement.nativeElement as Element;
|
const el = this.hostElement.nativeElement as Element;
|
||||||
|
@ -34,11 +34,6 @@ describe('ScrollService', () => {
|
|||||||
'viewportScroller',
|
'viewportScroller',
|
||||||
['getScrollPosition', 'scrollToPosition']);
|
['getScrollPosition', 'scrollToPosition']);
|
||||||
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
spyOn(window, 'scrollBy');
|
|
||||||
});
|
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
injector = ReflectiveInjector.resolveAndCreate([
|
injector = ReflectiveInjector.resolveAndCreate([
|
||||||
ScrollService,
|
ScrollService,
|
||||||
@ -52,8 +47,24 @@ describe('ScrollService', () => {
|
|||||||
document = injector.get(DOCUMENT);
|
document = injector.get(DOCUMENT);
|
||||||
scrollService = injector.get(ScrollService);
|
scrollService = injector.get(ScrollService);
|
||||||
location = injector.get(Location);
|
location = injector.get(Location);
|
||||||
|
|
||||||
|
spyOn(window, 'scrollBy');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should debounce `updateScrollPositonInHistory()` after 500ms', fakeAsync(() => {
|
||||||
|
const updateScrollPositionInHistorySpy = spyOn(scrollService, 'updateScrollPositionInHistory');
|
||||||
|
|
||||||
|
window.dispatchEvent(new Event('scroll'));
|
||||||
|
tick(249);
|
||||||
|
window.dispatchEvent(new Event('scroll'));
|
||||||
|
tick(249);
|
||||||
|
window.dispatchEvent(new Event('scroll'));
|
||||||
|
tick(249);
|
||||||
|
expect(updateScrollPositionInHistorySpy).not.toHaveBeenCalled();
|
||||||
|
tick(1);
|
||||||
|
expect(updateScrollPositionInHistorySpy).toHaveBeenCalledTimes(1);
|
||||||
|
}));
|
||||||
|
|
||||||
it('should set `scrollRestoration` to `manual` if supported', () => {
|
it('should set `scrollRestoration` to `manual` if supported', () => {
|
||||||
if (scrollService.supportManualScrollRestoration) {
|
if (scrollService.supportManualScrollRestoration) {
|
||||||
expect(window.history.scrollRestoration).toBe('manual');
|
expect(window.history.scrollRestoration).toBe('manual');
|
||||||
|
@ -2,6 +2,7 @@ import { Injectable, Inject } from '@angular/core';
|
|||||||
import { Location, PlatformLocation, ViewportScroller } from '@angular/common';
|
import { Location, PlatformLocation, ViewportScroller } from '@angular/common';
|
||||||
import { DOCUMENT } from '@angular/common';
|
import { DOCUMENT } from '@angular/common';
|
||||||
import { fromEvent } from 'rxjs';
|
import { fromEvent } from 'rxjs';
|
||||||
|
import { debounceTime } from 'rxjs/operators';
|
||||||
|
|
||||||
export const topMargin = 16;
|
export const topMargin = 16;
|
||||||
/**
|
/**
|
||||||
@ -45,6 +46,9 @@ export class ScrollService {
|
|||||||
// On resize, the toolbar might change height, so "invalidate" the top offset.
|
// On resize, the toolbar might change height, so "invalidate" the top offset.
|
||||||
fromEvent(window, 'resize').subscribe(() => this._topOffset = null);
|
fromEvent(window, 'resize').subscribe(() => this._topOffset = null);
|
||||||
|
|
||||||
|
fromEvent(window, 'scroll')
|
||||||
|
.pipe(debounceTime(250)).subscribe(() => this.updateScrollPositionInHistory());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this.supportManualScrollRestoration = !!window && !!window.scrollTo && 'scrollX' in window
|
this.supportManualScrollRestoration = !!window && !!window.scrollTo && 'scrollX' in window
|
||||||
&& 'scrollY' in window && !!history && !!history.scrollRestoration;
|
&& 'scrollY' in window && !!history && !!history.scrollRestoration;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user