UNPKG

@empathyco/x-components

Version:
114 lines (112 loc) 2.77 kB
/** * Default implementation for {@link XAPI}. * * @public */ class BaseXAPI { constructor() { /** * Flag to check if the initialization was already done. * * @internal */ this.isXInitialized = false; } /** * Tracks that a product was added to cart from PDP. * * @param productId - The product id that was added to cart. */ addProductToCart(productId) { void this.bus?.emit('UserClickedPDPAddToCart', productId); } /** * Setter for the XBus. * * @param bus - The XBus received to emit events. * * @internal */ setBus(bus) { this.bus = bus; } /** * Setter for the callback to call in the init method. * * @param initCallback - The callback to call. */ setInitCallback(initCallback) { this.initCallback = initCallback; } /** * Setter for the callback to modify the snippet config. * * @param snippetCallback - The callback to call. * * @internal */ setSnippetConfigCallback(snippetCallback) { this.snippetCallback = snippetCallback; } /** * Sets or updates the snippet config getter. * * @param snippetConfigGetter - A function that returns the snippet config. * * @internal */ setSnippetConfigGetter(snippetConfigGetter) { this.getSnippetConfig = snippetConfigGetter; } /** * Sets or updates the snippet config. * * @param config - A part or all the snippet config. * * @public */ setSnippetConfig(config) { this?.snippetCallback(config); } /** * Searches the query parameter as user query. * * @param query - Query to be searched. * * @public */ search(query) { if (query) { void this.bus?.emit('UserAcceptedAQuery', query); } void this.bus?.emit('UserClickedOpenX', undefined); } /** * Initializes the Application passing the {@link SnippetConfig}. * * @param config - The config coming from the customer snippet. * * @returns A promise that will be resolved once x components are initialized. * * @public */ async init(config) { if (!this.isXInitialized) { this.isXInitialized = true; await this?.initCallback(config); } else { console.warn('We know X is awesome, but you only need to initialize it once.'); } } /** * Closes the Application. * * @public */ close() { void this.bus?.emit('UserClickedCloseX', undefined); } } export { BaseXAPI }; //# sourceMappingURL=base-api.js.map