/* Copyright 2022 Adobe. All rights reserved. This file is licensed to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import type { Extension, GuestEmitter, NamedEvent, Emits, GuestApis, } from "@adobe/uix-core"; import type { CapabilitySpec } from "./port.js"; import { Emitter, quietConsole } from "@adobe/uix-core"; import { Port, PortOptions } from "./port.js"; import { debugHost } from "./debug-host.js"; import { addMetrics } from "./metrics.js"; import { compareExtensions } from "./utils/compareExtensions.js"; /** * Dictionary of {@link Port} objects by extension ID. * @public */ export type PortMap = Map; /** @public */ export type HostEvent< Type extends string = string, Detail = Record > = NamedEvent & { host: Host }>; /** @public */ type HostGuestEvent = HostEvent< `guest${Type}`, { guest: Port } >; /** * All guests requested by host have been loaded and connected. * @public */ export type HostEventLoadAllGuests = HostEvent< "loadallguests", { failed: Port[]; loaded: Port[] } >; /** * Shared context has been set or updated; all guests receive this event too. * @public */ export type HostEventContextChange = HostEvent< "contextchange", { context: SharedContextValues } >; /** * An error has occurred during loading or unloading of guests. * @public */ export type HostEventError = HostEvent<"error", { error: Error }>; /** @public */ export type HostEvents = | HostGuestEvent<"beforeload"> | HostGuestEvent<"load"> | HostGuestEvent<"beforeunload"> | HostGuestEvent<"unload"> | HostEvent<"beforeunload"> | HostEvent<"unload"> | HostEventLoadAllGuests | HostEventContextChange | HostEventError; /** @public */ export type InstalledExtensions = Record< Extension["id"], Extension["url"] | Extension >; /** @public */ export type ExtensionsArray = Array<[string, Extension | string]>; /** @public */ export type ExtensionsProvider = () => Promise; /** * Values for shared context. Must be a plain object, serializable to JSON. * @public */ export type SharedContextValues = Record; /** @public */ export interface HostConfig { /** * Human-readable "slug" name of the extensible area--often an entire app. * This string serves as a namespace for extension points within the area. */ hostName: string; /** * A DOM element _outside_ of the React root. This is necessary to preserve * the lifetime of the iframes which are running extension objects; if they * live inside the React root, then React could unexpectedly re-render the * iframe tags themselves at any time, causing a reload of the frame. */ runtimeContainer?: HTMLElement; /** * Copiously log lifecycle events. */ debug?: boolean; /** * Default options to use for every guest Port. * * If `config.debug` is true, then the guest options will have `debug: true` * unless `debug: false` is explicitly passed in `guestOptions`. */ guestOptions?: PortOptions; /** * A read-only dictionary of values that the host will supply to all the * guests. */ sharedContext?: SharedContextValues; /** * Disables built-in metrics of UIX SDK */ disableMetrics?: boolean; } /** * Callback to use to filter the list returned from {@link Host.(getLoadedGuests:2)} * @public */ type GuestFilter = (item: GuestEmitter) => boolean; const passAllGuests = () => true; /** * Manager object for connecting to {@link @adobe/uix-guest#GuestServer | * GuestServers} and {@link @adobe/uix-guest#GuestUI | GuestUIs}, providing and * receiving their APIs, and providing them to the app for interacting with UI. * * @remarks * The Host object is the main connection manager for all UIX Guests. * Making an app extensible requires creating a Host object. * * The extensible app using the Hostis responsible for providing a list of * extension references to the Host object. Use {@link * createExtensionRegistryProvider} for that purpose. Once you have retrieved a * list of extensions available to the host app, pass it to {@link Host.load}. * * When a Host creates a Guest, it must create an `