From b58adf2a467c9f4ce15e8a9090b3d6fb5caed855 Mon Sep 17 00:00:00 2001 From: JC Franco Date: Wed, 15 Jan 2025 10:10:48 -0800 Subject: [PATCH] refactor(loadable): deprecate obsolete helpers --- .../calcite-components/src/utils/loadable.ts | 84 +++++++------------ 1 file changed, 31 insertions(+), 53 deletions(-) diff --git a/packages/calcite-components/src/utils/loadable.ts b/packages/calcite-components/src/utils/loadable.ts index ed0132c82f8..3f072448caf 100644 --- a/packages/calcite-components/src/utils/loadable.ts +++ b/packages/calcite-components/src/utils/loadable.ts @@ -5,8 +5,7 @@ import { isBrowser } from "./browser"; /** * This helper adds support for knowing when a component has been loaded. * - * Related issue: https://github.com/Esri/calcite-design-system/issues/5369 - * Could be related to Stencil.js issue: https://github.com/ionic-team/stencil/issues/3580 + * @deprecated this interface is no longer needed, and you can use LitElement.componentOnReady instead * * Implementing * @@ -21,11 +20,11 @@ import { isBrowser } from "./browser"; * // * //-------------------------------------------------------------------------- * - * componentWillLoad(): void { + * load(): void { * setUpLoadableComponent(this); * } * - * componentDidLoad(): void { + * loaded(): void { * setComponentLoaded(this); * } * @@ -40,101 +39,80 @@ import { isBrowser } from "./browser"; * } * ``` */ -export interface LoadableComponent extends LitElement { - /** - * Stencil lifecycle method. - * https://stenciljs.com/docs/component-lifecycle#componentwillload - * - * Called once just after the component is first connected to the DOM. Since this method is only called once, it's a good place to load data asynchronously and to setup the state without triggering extra re-renders. - */ - load: () => Promise | void; - - /** - * Stencil lifecycle method. - * https://stenciljs.com/docs/component-lifecycle#componentdidload - * - * Called once just after the component is fully loaded and the first render() occurs. - */ - loaded: () => Promise | void; -} - -const resolveMap = new WeakMap) => void>(); - -const promiseMap = new WeakMap>(); +// eslint-disable-next-line @typescript-eslint/no-empty-object-type -- this interface is deprecated, and we allow it to be empty for incremental migration +export interface LoadableComponent {} /** * This helper util sets up the component for the ability to know when the component has been loaded. * - * This should be used in the `componentWillLoad` lifecycle hook. + * This should be used in the `load` lifecycle hook. * - * ``` - * componentWillLoad(): void { + * @deprecated this method is no longer needed, and you can use LitElement.componentOnReady instead + * + * @example + * load(): void { * setUpLoadableComponent(this); * } - * ``` * - * @param component + * @param _component */ -export function setUpLoadableComponent(component: LoadableComponent): void { - promiseMap.set(component, new Promise((resolve) => resolveMap.set(component, resolve))); +export function setUpLoadableComponent(_component: LoadableComponent): void { + // intentionally empty } /** * This helper util lets the loadable component know that it is now loaded. * - * This should be used in the `componentDidLoad` lifecycle hook. + * This should be used in the `loaded` lifecycle hook. * - * ``` - * componentDidLoad(): void { + * @deprecated this method is no longer needed, and you can use LitElement.componentOnReady instead + * + * @example + * loaded(): void { * setComponentLoaded(this); * } - * ``` * - * @param component + * @param _component */ -export function setComponentLoaded(component: LoadableComponent): void { - resolveMap.get(component)(); +export function setComponentLoaded(_component: LoadableComponent): void { + // intentionally empty } /** - * This helper util can be used to ensure a component has been loaded (The "componentDidLoad" Stencil lifecycle method has been called). - * - * Requires requires `LoadableComponent` to be implemented. + * This helper util can be used to ensure a component has been loaded (The "componentOnReady" lifecycle method has been called). * * A component developer can await this method before proceeding with any logic that requires a component to be loaded first. * - * ``` + * @deprecated use LitElement.componentOnReady instead + * + * @example * async myMethod(): Promise { * await componentLoaded(this); * } - * ``` * * @param component * @returns Promise */ -export function componentLoaded(component: LoadableComponent): Promise { - return promiseMap.get(component); +export async function componentLoaded(component: LitElement): Promise { + await component.componentOnReady(); } /** - * This helper util can be used to ensuring the component is loaded and rendered by the browser (The "componentDidLoad" Stencil lifecycle method has been called and any internal elements are focusable). - * - * Requires `LoadableComponent` to be implemented. + * This helper util can be used to ensuring the component is loaded and rendered by the browser (The "componentOnReady" lifecycle method has been called and any internal elements are focusable). * * A component developer can await this method before proceeding with any logic that requires a component to be loaded first and then an internal element be focused. * - * ``` + * @example * async setFocus(): Promise { * await componentFocusable(this); * this.internalElement?.focus(); * } - * ``` * * @param component * @returns Promise */ -export async function componentFocusable(component: LoadableComponent): Promise { - await componentLoaded(component); +export async function componentFocusable(component: LitElement): Promise { + await component.componentOnReady(); if (!isBrowser()) { return;