UNPKG

@braze/web-sdk

Version:

Braze SDK for web sites and other JS platforms.

152 lines (151 loc) 4.26 kB
import { logger as b } 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.Rr = null), (this.isConnected = !1), (this.Mr = null), (this.Lr = null), (this.Pe = i.Pe), (this.He = i.He), (this.Le = i.Le), (this.Oe = i.Oe); } initialize() { if (!isSharedWorkerSupported()) return ( b.info("SharedWorker not supported, will use direct connection"), !1 ); try { const i = new Blob([ri], { type: "application/javascript" }); return ( (this.Rr = URL.createObjectURL(i)), (this.Wr = new SharedWorker(this.Rr, { name: "braze-dust-worker" })), (this.Wr.port.onmessage = (i) => { this.Ir(i.data); }), (this.Wr.port.onmessageerror = () => { b.warn("Message error from real-time messaging worker"); }), (this.Wr.onerror = (i) => { var e; b.error( `Real-time messaging worker error: ${i.message || "unknown error"}`, ), null === (e = this.Oe) || void 0 === e || e.call(this, "SharedWorker error"); }), this.Wr.port.start(), this.Ur(), this.Vr(), b.info("Real-time messaging worker initialized"), !0 ); } catch (i) { return ( b.error( `Failed to create real-time messaging worker: ${ i instanceof Error ? i.message : String(i) }`, ), this._r(), !1 ); } } Ir(i) { var e, t, s; switch (i.type) { case "connected": b.info("Real-time messaging connection established via SharedWorker"), (this.isConnected = !0), null === (e = this.He) || void 0 === e || e.call(this); break; case "disconnected": b.info("Real-time messaging connection closed via SharedWorker"), (this.isConnected = !1), null === (t = this.Le) || void 0 === t || t.call(this); break; case "message": { const e = i.data, t = { type: e.type }; null != e.body && (t.body = e.body), this.Pe(t); break; } case "error": b.error(`Real-time messaging error: ${i.error}`), null === (s = this.Oe) || void 0 === s || s.call(this, i.error); } } Ur() { this.Mr = window.setInterval(() => { this.Wr && this.Gr({ type: "ping" }); }, 3e4); } Jr() { null !== this.Mr && (window.clearInterval(this.Mr), (this.Mr = null)); } Vr() { (this.Lr = () => { "visible" === document.visibilityState && this.Wr && this.Gr({ type: "tab_active" }); }), document.addEventListener("visibilitychange", this.Lr); } Kr() { this.Lr && (document.removeEventListener("visibilitychange", this.Lr), (this.Lr = null)); } Gr(i) { this.Wr && this.Wr.port.postMessage(i); } connect(i) { this.Wr ? (this.Gr({ type: "connect", config: { mite: i.mite, dustHost: i.dustHost, auth: i.auth, backoff: { minSleepMs: i.backoff.minSleepMs, maxSleepMs: i.backoff.maxSleepMs, scaleFactor: i.backoff.scaleFactor, }, }, }), b.info("Connecting to real-time messaging")) : b.error("Cannot connect: real-time messaging worker not initialized"); } disconnect() { this.Wr && (this.Gr({ type: "disconnect" }), (this.isConnected = !1), b.info("Disconnecting from real-time messaging")); } Ke() { return this.isConnected; } isInitialized() { return null !== this.Wr; } _r() { this.Jr(), this.Kr(), this.Wr && (this.Wr.port.close(), (this.Wr = null)), this.Rr && (URL.revokeObjectURL(this.Rr), (this.Rr = null)), (this.isConnected = !1); } destroy() { this.disconnect(), this._r(), b.info("Real-time messaging worker destroyed"); } }