2023-11-23 00:43:22 -05:00
|
|
|
/*
|
|
|
|
* Vencord, a Discord client mod
|
|
|
|
* Copyright (c) 2023 Vendicated and contributors
|
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { makeLazy } from "./lazy";
|
|
|
|
|
|
|
|
const NoopComponent = () => null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A lazy component. The factory method is called on first render.
|
|
|
|
* @param factory Function returning a Component
|
|
|
|
* @param attempts How many times to try to get the component before giving up
|
|
|
|
* @returns Result of factory function
|
|
|
|
*/
|
|
|
|
export function LazyComponent<T extends object = any>(factory: () => React.ComponentType<T>, attempts = 5) {
|
|
|
|
const get = makeLazy(factory, attempts);
|
2023-11-24 19:32:21 -05:00
|
|
|
const LazyComponent = (props: T) => {
|
2023-11-23 00:43:22 -05:00
|
|
|
const Component = get() ?? NoopComponent;
|
|
|
|
return <Component {...props} />;
|
|
|
|
};
|
2023-11-24 19:32:21 -05:00
|
|
|
|
|
|
|
LazyComponent.$$get = get;
|
|
|
|
|
|
|
|
return LazyComponent;
|
2023-11-23 00:43:22 -05:00
|
|
|
}
|