import { Http } from "./http"; import { Event } from "../../event/core/event"; import { CookieUtils } from "./cookie-utils"; /** * Queue of pending events. * * @export * @abstract * @class EventQueue */ export abstract class EventQueue { /** * Enqueue a pending event. * * @static * @template T Event type. * @param {T} event Event to enqueue. * @memberof EventQueue */ public static enqueue(event: T): void { const domain = CookieUtils.getDomain(); CookieUtils.set( window._RecSys.Bundle.Properties.event.queue.key, btoa(unescape(encodeURIComponent(JSON.stringify(event)))), { domain, }, ); } /** * Remove the pending event from queue. * * @static * @memberof EventQueue */ public static dequeue(): void { const domain = CookieUtils.getDomain(); CookieUtils.set(window._RecSys.Bundle.Properties.event.queue.key, "", { domain, }); } /** * Resend pending event. * * @static * @return {Promise} The return of the request. * @memberof EventQueue */ public static resend(): Promise { const pending = CookieUtils.get(window._RecSys.Bundle.Properties.event.queue.key); if (!pending) { return null; } EventQueue.dequeue(); return Http.post( `${window._RecSys.Bundle.Properties.event.url}/${window._RecSys.Bundle.Configuration.store.slug}/${window._RecSys.Bundle.Properties.event.endpoint.event}`, JSON.parse(decodeURIComponent(escape(atob(pending)))), ); } }