Centralize and optimize ResizeObserver usage
This commit addresses failures in end-to-end tests that occurred due to
`ResizeObserver` loop limit exceptions.
These errors were triggered by Vue dependency upgrades in the commit
aae5434451.
The errors had the following message:
> `ResizeObserver loop completed with undelivered notifications`
This error happens when there are too many observations and the observer
is not able to deliver all observations within a single animation frame.
See: WICG/resize-observer#38
his commit resolves the issue by controlling how many observations are
delivered per animation frame and limiting it to only one.
It improves performance by reducing layout trashing, improving frame
rates, and managing resources more effectively.
Changes:
- Introduce an animation frame control to manage observations more
efficiently.
- Centralized `ResizeObserver` management within the `UseResizeObserver`
hook to improve consistency and reuse across the application.
This commit is contained in:
@@ -0,0 +1,133 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import {
|
||||
useAnimationFrameLimiter, type AnimationFrameId, type AnimationFrameRequestCallback,
|
||||
type CancelAnimationFrameFunction, type RegisterTeardownCallbackFunction,
|
||||
type RequestAnimationFrameFunction,
|
||||
} from '@/presentation/components/Shared/Hooks/Resize/UseAnimationFrameLimiter';
|
||||
import { expectExists } from '@tests/shared/Assertions/ExpectExists';
|
||||
|
||||
describe('useAnimationFrameLimiter', () => {
|
||||
describe('resetNextFrame', () => {
|
||||
it('schedules the callback in the next animation frame', () => {
|
||||
// arrange
|
||||
const expectedCallback = () => {};
|
||||
let scheduledCallback: AnimationFrameRequestCallback | undefined;
|
||||
const requestAnimationFrame: RequestAnimationFrameFunction = (callback) => {
|
||||
scheduledCallback = callback;
|
||||
return 0;
|
||||
};
|
||||
const context = new TestContext()
|
||||
.withRequestAnimationFrameFunction(requestAnimationFrame);
|
||||
// act
|
||||
const { resetNextFrame } = context.useAnimationFrameLimiter();
|
||||
resetNextFrame(expectedCallback);
|
||||
// assert
|
||||
expect(scheduledCallback).to.equal(expectedCallback);
|
||||
});
|
||||
it('cancels the existing animation frame before scheduling a new one', () => {
|
||||
// arrange
|
||||
const expectedCancelledAnimationFrameId: AnimationFrameId = 5;
|
||||
let actualCancelledAnimationFrameId: AnimationFrameId | undefined;
|
||||
const requestAnimationFrame: RequestAnimationFrameFunction = () => {
|
||||
return expectedCancelledAnimationFrameId;
|
||||
};
|
||||
const cancelAnimationFrame: CancelAnimationFrameFunction = (animationFrameId) => {
|
||||
actualCancelledAnimationFrameId = animationFrameId;
|
||||
};
|
||||
const context = new TestContext()
|
||||
.withRequestAnimationFrameFunction(requestAnimationFrame)
|
||||
.withCancelAnimationFrame(cancelAnimationFrame);
|
||||
// act
|
||||
const { resetNextFrame } = context.useAnimationFrameLimiter();
|
||||
resetNextFrame(() => {}); // Nothing to cancel in first call
|
||||
resetNextFrame(() => {});
|
||||
// assert
|
||||
expect(actualCancelledAnimationFrameId).to.equal(expectedCancelledAnimationFrameId);
|
||||
});
|
||||
});
|
||||
describe('cancelNextFrame', () => {
|
||||
it('cancels the scheduled animation frame if one exists', () => {
|
||||
// arrange
|
||||
const expectedCancelledAnimationFrameId: AnimationFrameId = 5;
|
||||
let actualCancelledAnimationFrameId: AnimationFrameId | undefined;
|
||||
const requestAnimationFrame: RequestAnimationFrameFunction = () => {
|
||||
return expectedCancelledAnimationFrameId;
|
||||
};
|
||||
const cancelAnimationFrame: CancelAnimationFrameFunction = (animationFrameId) => {
|
||||
actualCancelledAnimationFrameId = animationFrameId;
|
||||
};
|
||||
const context = new TestContext()
|
||||
.withRequestAnimationFrameFunction(requestAnimationFrame)
|
||||
.withCancelAnimationFrame(cancelAnimationFrame);
|
||||
// act
|
||||
const { resetNextFrame, cancelNextFrame } = context.useAnimationFrameLimiter();
|
||||
resetNextFrame(() => {}); // Schedule the initial one
|
||||
cancelNextFrame();
|
||||
// assert
|
||||
expect(actualCancelledAnimationFrameId).to.equal(expectedCancelledAnimationFrameId);
|
||||
});
|
||||
});
|
||||
it('automatically cancels the animation frame on cleanup', () => {
|
||||
// arrange
|
||||
let actualCancelledAnimationFrameId: AnimationFrameId | undefined;
|
||||
let actualCleanupCallback: (() => void) | undefined;
|
||||
const onTeardownCallback: RegisterTeardownCallbackFunction = (cleanupCallback) => {
|
||||
actualCleanupCallback = cleanupCallback;
|
||||
};
|
||||
const expectedCancelledAnimationFrameId: AnimationFrameId = 5;
|
||||
const requestAnimationFrame: RequestAnimationFrameFunction = () => {
|
||||
return expectedCancelledAnimationFrameId;
|
||||
};
|
||||
const cancelAnimationFrame: CancelAnimationFrameFunction = (animationFrameId) => {
|
||||
actualCancelledAnimationFrameId = animationFrameId;
|
||||
};
|
||||
const testContext = new TestContext()
|
||||
.withOnTeardownCallback(onTeardownCallback)
|
||||
.withRequestAnimationFrameFunction(requestAnimationFrame)
|
||||
.withCancelAnimationFrame(cancelAnimationFrame);
|
||||
// act
|
||||
const { resetNextFrame } = testContext.useAnimationFrameLimiter();
|
||||
resetNextFrame(() => {}); // Schedule the initial one
|
||||
// assert
|
||||
expectExists(actualCleanupCallback);
|
||||
actualCleanupCallback();
|
||||
expect(actualCancelledAnimationFrameId).to.equal(expectedCancelledAnimationFrameId);
|
||||
});
|
||||
});
|
||||
|
||||
class TestContext {
|
||||
private cancelAnimationFrame: CancelAnimationFrameFunction = () => {};
|
||||
|
||||
private requestAnimationFrameFunction: RequestAnimationFrameFunction = () => Math.random();
|
||||
|
||||
private onTeardownCallback: RegisterTeardownCallbackFunction = () => {};
|
||||
|
||||
public withRequestAnimationFrameFunction(
|
||||
requestAnimationFrameFunction: RequestAnimationFrameFunction,
|
||||
): this {
|
||||
this.requestAnimationFrameFunction = requestAnimationFrameFunction;
|
||||
return this;
|
||||
}
|
||||
|
||||
public withCancelAnimationFrame(
|
||||
cancelAnimationFrame: CancelAnimationFrameFunction,
|
||||
): this {
|
||||
this.cancelAnimationFrame = cancelAnimationFrame;
|
||||
return this;
|
||||
}
|
||||
|
||||
public withOnTeardownCallback(
|
||||
registerCleanupCallback: RegisterTeardownCallbackFunction,
|
||||
): this {
|
||||
this.onTeardownCallback = registerCleanupCallback;
|
||||
return this;
|
||||
}
|
||||
|
||||
public useAnimationFrameLimiter(): ReturnType<typeof useAnimationFrameLimiter> {
|
||||
return useAnimationFrameLimiter(
|
||||
this.cancelAnimationFrame,
|
||||
this.requestAnimationFrameFunction,
|
||||
this.onTeardownCallback,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
import { shallowRef } from 'vue';
|
||||
import { useResizeObserver, type LifecycleHookRegistration, type ObservedElementReference } from '@/presentation/components/Shared/Hooks/Resize/UseResizeObserver';
|
||||
import { flushPromiseResolutionQueue } from '@tests/unit/shared/PromiseInspection';
|
||||
import type { AnimationFrameLimiter } from '@/presentation/components/Shared/Hooks/Resize/UseAnimationFrameLimiter';
|
||||
import { ThrottleStub } from '@tests/unit/shared/Stubs/ThrottleStub';
|
||||
import { expectExists } from '@tests/shared/Assertions/ExpectExists';
|
||||
|
||||
describe('UseResizeObserver', () => {
|
||||
it('registers observer once mounted', async () => {
|
||||
// arrange
|
||||
let registeredElement: Element | null = null;
|
||||
const expectedElement = document.createElement('div');
|
||||
const resizeObserverStub = createResizeObserverStub();
|
||||
resizeObserverStub.observe = (element) => {
|
||||
registeredElement = element;
|
||||
};
|
||||
// act
|
||||
new TestContext()
|
||||
.withObservedElementRef(shallowRef(expectedElement))
|
||||
.withResizeObserver(resizeObserverStub)
|
||||
.useResizeObserver();
|
||||
await flushPromiseResolutionQueue();
|
||||
// assert
|
||||
expect(registeredElement).to.equal(expectedElement);
|
||||
});
|
||||
it('disposes observer once unmounted', async () => {
|
||||
// arrange
|
||||
let isObserverDisconnected = false;
|
||||
const resizeObserverStub = createResizeObserverStub();
|
||||
resizeObserverStub.disconnect = () => {
|
||||
isObserverDisconnected = true;
|
||||
};
|
||||
let teardownCallback: (() => void) | undefined;
|
||||
// act
|
||||
new TestContext()
|
||||
.withResizeObserver(resizeObserverStub)
|
||||
.withOnTeardown((callback) => {
|
||||
teardownCallback = callback;
|
||||
})
|
||||
.useResizeObserver();
|
||||
await flushPromiseResolutionQueue();
|
||||
expectExists(teardownCallback);
|
||||
teardownCallback();
|
||||
// assert
|
||||
expect(isObserverDisconnected).to.equal(true);
|
||||
});
|
||||
});
|
||||
|
||||
function createResizeObserverStub(): ResizeObserver {
|
||||
return {
|
||||
disconnect: () => {},
|
||||
observe: () => {},
|
||||
unobserve: () => {},
|
||||
};
|
||||
}
|
||||
|
||||
function createFrameLimiterStub(): AnimationFrameLimiter {
|
||||
return {
|
||||
cancelNextFrame: () => {},
|
||||
resetNextFrame: (callback) => { callback(); },
|
||||
};
|
||||
}
|
||||
|
||||
class TestContext {
|
||||
private resizeObserver: ResizeObserver = createResizeObserverStub();
|
||||
|
||||
private observedElementRef: ObservedElementReference = shallowRef(document.createElement('div'));
|
||||
|
||||
private onSetup: LifecycleHookRegistration = (callback) => { callback(); };
|
||||
|
||||
private onTeardown: LifecycleHookRegistration = () => { };
|
||||
|
||||
public withResizeObserver(resizeObserver: ResizeObserver): this {
|
||||
this.resizeObserver = resizeObserver;
|
||||
return this;
|
||||
}
|
||||
|
||||
public withObservedElementRef(observedElementRef: ObservedElementReference): this {
|
||||
this.observedElementRef = observedElementRef;
|
||||
return this;
|
||||
}
|
||||
|
||||
public withOnSetup(onSetup: LifecycleHookRegistration): this {
|
||||
this.onSetup = onSetup;
|
||||
return this;
|
||||
}
|
||||
|
||||
public withOnTeardown(onTeardown: LifecycleHookRegistration): this {
|
||||
this.onTeardown = onTeardown;
|
||||
return this;
|
||||
}
|
||||
|
||||
public useResizeObserver() {
|
||||
return useResizeObserver(
|
||||
...this.buildParameters(),
|
||||
);
|
||||
}
|
||||
|
||||
private buildParameters(): Parameters<typeof useResizeObserver> {
|
||||
return [
|
||||
{
|
||||
observedElementRef: this.observedElementRef,
|
||||
throttleInMs: 50,
|
||||
observeCallback: () => {},
|
||||
},
|
||||
() => ({
|
||||
resizeObserverReady: Promise.resolve(() => this.resizeObserver),
|
||||
}),
|
||||
() => createFrameLimiterStub(),
|
||||
new ThrottleStub()
|
||||
.withImmediateExecution(true)
|
||||
.func,
|
||||
this.onSetup,
|
||||
this.onTeardown,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user