import { LitElement, PropertyValueMap } from "lit"; import { state } from "lit/decorators.js"; import { consume } from "@lit/context"; import { WeavyContext, type WeavyType } from "../contexts/weavy-context"; import type { UserType } from "../types/users.types"; import { UserContext } from "../contexts/user-context"; import { ContextIdContext, type ContextIdType } from "../contexts/context-id-context"; import { WeavyComponentActionProps, WeavyComponentContextProps } from "../types/component.types"; import { WyActionEventDetailType, WyActionEventType } from "../types/action.events"; import { NamedEvent } from "../types/generic.types"; /** * Base class for internal weavy sub components. This class provides common properties and internal data consumed from contexts. * * @fires ready - When the component has rendered. */ export class WeavySubComponent extends LitElement implements WeavyComponentContextProps, WeavyComponentActionProps { // CONTEXT PROVIDERS /** * @internal */ @consume({ context: ContextIdContext, subscribe: true }) @state() contextId: ContextIdType | undefined; /** * @internal */ @consume({ context: UserContext, subscribe: true }) @state() user: UserType | undefined; /** * @internal */ @consume({ context: WeavyContext, subscribe: true }) @state() weavy: WeavyType | undefined; // PROMISES // TODO: Switch to Promise.withResolvers() when allowed by typescript /** * Resolves when a contextual id is available. * * @internal * @returns {Promise} */ async whenContextId() { return await this.#whenContextId; } #resolveContextId?: (contextId: ContextIdType) => void; #whenContextId = new Promise((r) => { this.#resolveContextId = r; }); /** * Resolves when current user data is available. * * @internal * @returns {Promise} */ async whenUser() { return await this.#whenUser; } #resolveUser?: (user: UserType) => void; #whenUser = new Promise((r) => { this.#resolveUser = r; }); /** * Resolves when a weavy context is available. * * @internal * @returns {Promise} */ async whenWeavy() { return await this.#whenWeavy; } #resolveWeavy?: (weavy: WeavyType) => void; #whenWeavy = new Promise((r) => { this.#resolveWeavy = r; }); /** * Resolves when the component has rendered first time. * * @returns {Promise} */ async whenReady() { return await this.#whenReady; } #resolveReady?: (component: WeavySubComponent) => void; #whenReady = new Promise((r) => { this.#resolveReady = r; }); /** * Dispatch a `wy-action` event on the component. * * @fires {WyActionEventType} wy-action - Emitted when an action takes place. */ dispatchAction(detail: WyActionEventDetailType): boolean { const eventAction: WyActionEventType = new (CustomEvent as NamedEvent)("wy-action", { detail, bubbles: true, composed: true, cancelable: true, }); return this.dispatchEvent(eventAction); } protected override willUpdate(changedProperties: PropertyValueMap): void { super.willUpdate(changedProperties); // if (changedProperties.has("agentUser") && this.agentUser) { // if (changedProperties.get("agentUser")) { // // reset promise // this.#whenAgentUser = new Promise((r) => { // this.#resolveAgentUser = r; // }); // } // this.#resolveAgentUser?.(this.agentUser); // } if (changedProperties.has("contextId") && this.contextId) { if (changedProperties.get("contextId")) { // reset promise this.#whenContextId = new Promise((r) => { this.#resolveContextId = r; }); } this.#resolveContextId?.(this.contextId); } if (changedProperties.has("user") && this.user) { if (changedProperties.get("user")) { // reset promise this.#whenUser = new Promise((r) => { this.#resolveUser = r; }); } this.#resolveUser?.(this.user); } if (changedProperties.has("weavy") && this.weavy) { if (changedProperties.get("weavy")) { // reset promise this.#whenWeavy = new Promise((r) => { this.#resolveWeavy = r; }); } this.#resolveWeavy?.(this.weavy); } } override connectedCallback(): void { super.connectedCallback(); if (this.contextId) { this.requestUpdate("contextId"); } if (this.user) { this.requestUpdate("user"); } if (this.weavy) { this.requestUpdate("weavy"); } } protected override firstUpdated(changedProperties: PropertyValueMap): void { super.firstUpdated(changedProperties); this.#resolveReady?.(this); this.dispatchEvent( new CustomEvent("ready", { bubbles: false, composed: false, }), ); } }