UNPKG

@dannyfranca/any-chat

Version:

Universal, extensible and improved JS API for Chats, like TawkTo, Jivochat, and others.

63 lines (62 loc) 2.5 kB
import EventMap from "../types/EventMap"; import EventCallback from "../types/EventCallback"; import EventHandler from "./EventHandler"; export default abstract class ChatBase<ChatApi> extends EventHandler { abstract _eventMap: EventMap; abstract _loader(): Promise<ChatApi> | ChatApi; protected _api: ChatApi | undefined; private _eventsMapped; private _loaded; /** * Method to init chat loading process, must be executed in Driver class. Can be overridden only in a last * resort, but its really not recommended. * @returns {Promise<void>} */ protected init(): Promise<void>; /** * Inform class that its loaded and ready to work. */ protected ready(): void; /** * Quick check that returns true if chat is loaded. * @returns {boolean} */ readonly loaded: boolean; /** * Returns a Promise that resolves on chat load, if already loaded, returns a resolved promise * @returns {Promise<void>} */ toLoad(): Promise<void>; /** * Turn ChatBase into a thenable, use like a regular promise. Resolves on chat load. * @param resolve * @param reject * @returns {Promise<void>} */ then(resolve?: (chat?: this) => any, reject?: (chat?: this) => any): Promise<any>; /** * Helper to build drivers that needs to monitor some global event or value. * * Keep testing function given in "test" argument until it returns a truthy value. After that, resolves a * promise with value returned in function given in "then" argument. If "then" callback generates an error, * Promise is rejected with same exception. * * @param {() => any} test * @param {() => any} then * @param {number} testInterval * @param {number} limit * @returns {Promise<any>} */ protected waitFor(test: () => any, then: () => any, testInterval?: number, limit?: number): Promise<any>; /** * Set events from _eventsMapped to Chat's global JS API. */ private mapApiEvents; /** * Method to attach each event to Chat's global JS API. Can be overridden in Driver class to change its behavior. * Default simply stores callback method is a global key, most chats JS APIs works that way. * @param {string} event * @param {EventCallback<any>} callback */ protected attachEvent(event: string, callback: EventCallback<any>): void; }