sandai-react
Version:
React components and utilities for the Sandai 3D AI Characters.
78 lines • 3.05 kB
TypeScript
import { LoadStateServerMessage } from "../types";
/**
* Manages the loading state of an iframe and communicates with it via postMessage.
*/
export declare class LoadManager {
private _iframe;
private _iframeUrl;
private _loadListener;
private _interactedListener;
private _skipLoadCheck;
loadState: LoadStateServerMessage["state"];
/**
* Creates an instance of LoadManager.
*
* @param {HTMLIFrameElement} iframe - The target iframe element to manage.
*/
constructor(iframe: typeof this._iframe, skipLoadCheck?: boolean);
private _isLoadStateServerMessage;
private _processData;
/**
* Initializes the LoadManager by listening for a "loadState" message from the iframe.
* Resolves when the iframe reports that it's in a "ready" state.
*
* @async
* @returns {Promise<{ready: boolean, state: LoadStateServerMessage["state"] | "timeout", hasInteractedPromise: Promise<boolean>}>}
*
* [loadState]
*
* A promise that resolves with `{ready: true, state: 'ready'}` when the iframe is ready.
* If there is no answer after 10 seconds, it will resolve with `{ready: false, state: 'timeout'}`.
* If there is a loadError, it resolves with `{ready: false, state: 'loadError'}`
*
* [interactionState]
* Also gives you a promise once the user interacts with the document. This promise needs to be fulfilled
* before audio can be played through the iframe.
* If a loadError occurs, the Promise will reject.
* If a timeout occurs, the Promise will still behave like normal.
*
*/
init(): Promise<{
ready: boolean;
state: LoadStateServerMessage["state"] | "timeout";
hasInteracted: LoadStateServerMessage["hasInteracted"];
hasInteractedPromise: Promise<boolean>;
}>;
/**
* Sends a message to the iframe.
*
* @private
* @param {*} data - The data payload to be sent to the iframe.
* @throws {Error} Throws an error if the iframe does not have a `contentWindow`.
*/
_sendMessage(data: any): void;
/**
* Stores all registered message event listeners for later removal.
*
* @private
*/
private _listeners;
/**
* Listens for messages from the iframe and invokes the provided callback when a message is received.
* Only messages from the iframe's origin are processed.
*
* The listener is stored internally so it can be removed later via `destroy()`.
*
* @private
* @param {(data: any) => void} callback - The callback function to invoke with the message data.
*/
private _listenForMessage;
destroyLoadStateListeners(): void;
destroyInteractionStateListeners(): void;
/**
* Cleans up all registered message event listeners previously added by `_listenForMessage`.
* Call this method when the instance is being destroyed or no longer needs to listen to messages.
*/
destroy(): void;
}
//# sourceMappingURL=LoadManager.d.ts.map