@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
59 lines (58 loc) • 1.62 kB
JavaScript
/*!
* All material copyright ESRI, All Rights Reserved, unless otherwise specified.
* See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
* v1.5.0-next.4
*/
const resolveMap = new WeakMap();
const promiseMap = new WeakMap();
/**
* 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.
*
* ```
* componentWillLoad(): void {
* setUpLoadableComponent(this);
* }
* ```
*
* @param component
*/
export function setUpLoadableComponent(component) {
promiseMap.set(component, new Promise((resolve) => resolveMap.set(component, resolve)));
}
/**
* This helper util lets the loadable component know that it is now loaded.
*
* This should be used in the `componentDidLoad` lifecycle hook.
*
* ```
* componentDidLoad(): void {
* setComponentLoaded(this);
* }
* ```
*
* @param component
*/
export function setComponentLoaded(component) {
resolveMap.get(component)();
}
/**
* This helper util can be used to ensure a component has been loaded (The "componentDidLoad" stencil lifecycle method has been called).
*
* Requires "setUpLoadableComponent" and "setComponentLoaded" to be called first.
*
* A component developer can await this method before proceeding with any logic that requires a component to be loaded first.
*
* ```
* async setFocus(): Promise<void> {
* await componentLoaded(this);
* }
* ```
*
* @param component
* @returns Promise<void>
*/
export function componentLoaded(component) {
return promiseMap.get(component);
}