UNPKG

ez-shp-storefront

Version:

A helper function collection for Shopify storefront.

42 lines (41 loc) 1.38 kB
import { EventBus } from "./eventBus"; export class Fetch { constructor() { const coreFetch = window.fetch; XMLHttpRequest.prototype['realSend'] = XMLHttpRequest.prototype.send; const updatePaths = ["change", "update", "add"]; window.fetch = async (url, data) => { if (updatePaths.find((updateUrl) => url.includes(updateUrl))) { const response = await coreFetch(url, data); this.getEventBus().emitEventListeners(`Ez_CART_CHANGE`, { url }); return response; } return coreFetch(url, data); }; const newSend = function (vData) { this.realSend(vData); const url = this._url; if (updatePaths.find((updateUrl) => String(url).includes(updateUrl))) { this.getEventBus().emitEventListeners(`Ez_CART_CHANGE`, { url }); } }; XMLHttpRequest.prototype.send = newSend; } static getInstance() { if (!this.instance) { this.instance = new Fetch(); } return this.instance; } setEventBus(eventBus) { this.eventBus = eventBus; return this; } getEventBus() { if (!this.eventBus) { this.eventBus = EventBus.getInstance(); } return this.eventBus; } } Fetch.instance = null;