UNPKG

@braze/web-sdk

Version:

Braze SDK for web sites and other JS platforms.

137 lines (136 loc) 3.84 kB
import { logger as E } from "../../shared-lib/index.js"; import { DUST_SHARED_WORKER_CODE as ri } from "./dust-shared-worker-code.js"; export function isSharedWorkerSupported() { return "undefined" != typeof SharedWorker; } export class DustWorkerBridge { constructor(i) { (this.Wr = null), (this.Lr = null), (this.isConnected = !1), (this.Ir = null), (this.Mr = null), (this.Be = i.Be), (this.Pe = i.Pe), (this.He = i.He), (this.Je = i.Je); } initialize() { if (!isSharedWorkerSupported()) return ( E.info("SharedWorker not supported, will use direct connection"), !1 ); try { const i = new Blob([ri], { type: "application/javascript" }); return ( (this.Lr = URL.createObjectURL(i)), (this.Wr = new SharedWorker(this.Lr, { name: "braze-dust-worker" })), (this.Wr.port.onmessage = (i) => { this.Ur(i.data); }), (this.Wr.port.onmessageerror = () => { E.warn("Message error from real-time messaging worker"); }), (this.Wr.onerror = (i) => { var e; E.error( `Real-time messaging worker error: ${i.message || "unknown error"}`, ), null === (e = this.Je) || void 0 === e || e.call(this, "SharedWorker error"); }), this.Wr.port.start(), this.Vr(), this.Er(), E.info("Real-time messaging worker initialized"), !0 ); } catch (i) { return ( E.error( `Failed to create real-time messaging worker: ${ i instanceof Error ? i.message : String(i) }`, ), this._r(), !1 ); } } Ur(i) { var e, t, s; switch (i.type) { case "connected": E.info("Real-time messaging connection established via SharedWorker"), (this.isConnected = !0), null === (e = this.Pe) || void 0 === e || e.call(this); break; case "disconnected": E.info("Real-time messaging connection closed via SharedWorker"), (this.isConnected = !1), null === (t = this.He) || void 0 === t || t.call(this); break; case "message": this.Be(i.data); break; case "error": E.error(`Real-time messaging error: ${i.error}`), null === (s = this.Je) || void 0 === s || s.call(this, i.error); } } Vr() { this.Ir = window.setInterval(() => { this.Wr && this.Gr({ type: "ping" }); }, 3e4); } Jr() { null !== this.Ir && (window.clearInterval(this.Ir), (this.Ir = null)); } Er() { (this.Mr = () => { "visible" === document.visibilityState && this.Wr && this.Gr({ type: "tab_active" }); }), document.addEventListener("visibilitychange", this.Mr); } Kr() { this.Mr && (document.removeEventListener("visibilitychange", this.Mr), (this.Mr = null)); } Gr(i) { this.Wr && this.Wr.port.postMessage(i); } connect(i) { this.Wr ? (this.Gr({ type: "connect", zn: i }), E.info("Connecting to real-time messaging")) : E.error("Cannot connect: real-time messaging worker not initialized"); } disconnect() { this.Wr && (this.Gr({ type: "disconnect" }), (this.isConnected = !1), E.info("Disconnecting from real-time messaging")); } cn() { return this.isConnected; } isInitialized() { return null !== this.Wr; } _r() { this.Jr(), this.Kr(), this.Wr && (this.Wr.port.close(), (this.Wr = null)), this.Lr && (URL.revokeObjectURL(this.Lr), (this.Lr = null)), (this.isConnected = !1); } destroy() { this.disconnect(), this._r(), E.info("Real-time messaging worker destroyed"); } }