PromucFlow_constructor/app/client/src/widgets/withLazyRender.tsx

67 lines
2.1 KiB
TypeScript
Raw Normal View History

import { useEffect, useRef, useState } from "react";
import React from "react";
import BaseWidget, { WidgetProps } from "./BaseWidget";
import { REQUEST_IDLE_CALLBACK_TIMEOUT } from "constants/AppConstants";
import { useSelector } from "react-redux";
import { selectFeatureFlags } from "selectors/usersSelectors";
export function withLazyRender(Widget: typeof BaseWidget) {
return function WrappedComponent(props: WidgetProps) {
const features = useSelector(selectFeatureFlags);
const [deferRender, setDeferRender] = useState(
!!features.LAZY_CANVAS_RENDERING,
);
const wrapperRef = useRef<HTMLDivElement>(null);
let idleCallbackId: number;
let observer: IntersectionObserver;
useEffect(() => {
if (wrapperRef.current && deferRender) {
/*
* For the hidden widgets, we are observing till it,
* 1. Scrolls into view, or
* 2. idleCallback is called (browser is either idle or timed out)
* which ever happens first
*/
observer = new IntersectionObserver(
(entries: IntersectionObserverEntry[]) => {
if (!!entries.find((entry) => entry.isIntersecting)) {
setDeferRender(false);
(window as any).cancelIdleCallback(idleCallbackId);
observer.disconnect();
} else if (!idleCallbackId) {
idleCallbackId = (window as any).requestIdleCallback(
() => {
setDeferRender(false);
observer.disconnect();
},
{
timeout: REQUEST_IDLE_CALLBACK_TIMEOUT.lowPriority,
},
);
}
},
{
root: null,
threshold: 0,
},
);
observer.observe(wrapperRef.current);
} else {
setDeferRender(false);
}
return () => {
(window as any).cancelIdleCallback(idleCallbackId);
chore: [lazy canvas rendering] check whether observer exist before disconnecting while cleaning up (#21171) Fixes #20558 Media > A video or a GIF is preferred. when using Loom, don’t embed because it looks like it’s a GIF. instead, just link to the video ## Type of change > Please delete options that are not relevant. - Bug fix (non-breaking change which fixes an issue) - New feature (non-breaking change which adds functionality) - Breaking change (fix or feature that would cause existing functionality to not work as expected) - Chore (housekeeping or task changes that don't impact user perception) - This change requires a documentation update ## How Has This Been Tested? > Please describe the tests that you ran to verify your changes. Provide instructions, so we can reproduce. > Please also list any relevant details for your test configuration. > Delete anything that is not important - Manual - Jest - Cypress ### Test Plan > Add Testsmith test cases links that relate to this PR ### Issues raised during DP testing > Link issues raised during DP testing for better visiblity and tracking (copy link from comments dropped on this PR) ## Checklist: ### Dev activity - [x] My code follows the style guidelines of this project - [x] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes - [ ] PR is being merged under a feature flag ### QA activity: - [ ] Test plan has been approved by relevant developers - [ ] Test plan has been peer reviewed by QA - [ ] Cypress test cases have been added and approved by either SDET or manual QA - [ ] Organized project review call with relevant stakeholders after Round 1/2 of QA - [ ] Added Test Plan Approved label after reveiwing all Cypress test
2023-03-09 04:48:23 +00:00
observer && observer.disconnect();
};
}, []);
return (
<Widget {...props} deferRender={deferRender} wrapperRef={wrapperRef} />
);
};
}