import { LitElement, PropertyValues } from "lit"; import { state } from "lit/decorators.js"; import { ContextConsumer, provide } from "@lit/context"; import { whenParentsDefined } from "../utils/dom"; import { WeavyContext, type WeavyType } from "../contexts/weavy-context"; import type { UserType } from "../types/users.types"; import { QueryController } from "../controllers/query-controller"; import { getApiOptions } from "../data/api"; import { UserContext } from "../contexts/user-context"; import { ContextIdContext, type ContextIdType } from "../contexts/context-id-context"; import { v4 as uuid_v4 } from "uuid"; import type { WeavyComponentActionProps, WeavyComponentContextProps } from "../types/component.types"; import { WyActionEventDetailType, WyActionEventType } from "../types/action.events"; import { NamedEvent } from "../types/generic.types"; /** * Base class for exposed/public weavy components. This class provides common external properties and internal data provided as contexts for sub components. * * @fires ready - When the component has rendered. */ export class WeavyComponent extends LitElement implements WeavyComponentContextProps, WeavyComponentActionProps { // CONTEXT CONSUMERS /** @internal */ protected weavyContextConsumer?: ContextConsumer<{ __context__: WeavyType }, this>; /** * The consumed weavy context. */ @state() weavy: WeavyType | undefined; // CONTEXT PROVIDERS /** * Contextual guid that is unique for the client context. * * @type {ContextIdType} */ @provide({ context: ContextIdContext }) @state() contextId: ContextIdType = uuid_v4(); /** * The current user. * * @type {UserType | undefined} */ @provide({ context: UserContext }) @state() user: UserType | undefined; // PROPERTIES // PROMISES // TODO: Switch to Promise.withResolvers() when allowed by typescript // Promise.withResolvers() is available in ES2024, that needs to be set in TSConfig /** * 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. * * @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. * * @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: WeavyComponent) => 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); } // INTERNAL PROPERTIES #userQuery = new QueryController(this); override connectedCallback(): void { super.connectedCallback(); // if (this.agentUser) { // this.requestUpdate("agentUser"); // } if (this.contextId) { this.requestUpdate("contextId"); } if (this.user) { this.requestUpdate("user"); } if (this.weavy) { this.requestUpdate("weavy"); } } protected override async scheduleUpdate(): Promise { await whenParentsDefined(this); await super.scheduleUpdate(); } protected override async willUpdate(changedProperties: PropertyValues): Promise { super.willUpdate(changedProperties); this.weavyContextConsumer ??= new ContextConsumer(this, { context: WeavyContext, subscribe: true }); if (this.weavyContextConsumer?.value && this.weavy !== this.weavyContextConsumer?.value) { this.weavy = this.weavyContextConsumer?.value; } if (changedProperties.has("weavy") && this.weavy) { await this.#userQuery.trackQuery(getApiOptions(this.weavy, ["user"])); } if (!this.#userQuery.result?.isPending) { if (this.user && this.#userQuery.result.data && this.user.id !== this.#userQuery.result.data.id) { console.warn("User mismatch, resetting"); void this.weavy?.reset(); } this.user = this.#userQuery.result?.data; } // Promises 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); } } protected override firstUpdated(changedProperties: PropertyValues): void { super.firstUpdated(changedProperties); this.#resolveReady?.(this); this.dispatchEvent( new CustomEvent("ready", { bubbles: true, composed: true, }), ); } }