UNPKG

@braze/web-sdk

Version:

Braze SDK for web sites and other JS platforms.

315 lines (314 loc) 9.7 kB
import t from "../common/base-provider.js"; import { newInAppMessageFromJson as pt } from "../InAppMessage/in-app-message-factory.js"; import Ie from "../models/braze-event.js"; import { isArray as g, isEqual as ni, validateValueIsFromEnum as ta, } from "../util/code-utils.js"; import { STORAGE_KEYS as s } from "../managers/storage-manager.js"; import Jt from "../InAppMessage/models/templated-in-app-message.js"; import gt from "./models/trigger.js"; import ot from "./models/trigger-events.js"; import { logger as E } from "../../shared-lib/index.js"; export default class vr extends t { constructor(t, i, s, e, r) { super(), (this.tg = t), (this.Rs = i), (this.C = s), (this.Ru = e), (this.ig = r), (this.tg = t), (this.Rs = i), (this.C = s), (this.Ru = e), (this.ig = r), (this.sg = []), (this.eg = []), (this.hg = null), (this.ng = {}), (this.og = {}), (this.triggers = []), (this.lg = 0), this.ag(), this.gg(); } fg() { if (this.C) { (this.hg = this.C.Rt(s.Tt.oS) || this.hg), (this.ng = this.C.Rt(s.Tt.nS) || this.ng), (this.og = this.C.Rt(s.Tt.rS) || this.og); for (let t = 0; t < this.triggers.length; t++) { const i = this.triggers[t]; i.id && null != this.og[i.id] && i.md(this.og[i.id]); } } } ag() { if (!this.C) return; this.lg = this.C.Rt(s.Tt.sS) || 0; const t = this.C.Rt(s.Tt.eS) || [], i = []; for (let s = 0; s < t.length; s++) i.push(gt._u(t[s])); (this.triggers = i), this.fg(); } gg() { const t = this, i = function (i, s, e, r, h) { return function () { t.cg(i, s, e, r, h); }; }, e = {}; for (const t of this.triggers) t.id && (e[t.id] = t); let r = !1; for (let t = 0; t < this.triggers.length; t++) { const s = this.triggers[t]; if (s.id && null != this.ng[s.id]) { const t = this.ng[s.id], h = []; for (let r = 0; r < t.length; r++) { const n = t[r], o = s.gd(n.ma || 0); if (o > 0) { let t, r; h.push(n), null != n.ug && (t = n.ug), null != n.dg && Ie.hS(n.dg) && (r = Ie._u(n.dg)); const l = []; if (n.pg && g(n.pg)) for (let t = 0; t < n.pg.length; t++) { const i = e[n.pg[t]]; null != i && l.push(i); } this.eg.push(window.setTimeout(i(s, n.ma || 0, t, r, l), o)); } } this.ng[s.id].length > h.length && ((this.ng[s.id] = h), (r = !0), 0 === this.ng[s.id].length && delete this.ng[s.id]); } } r && this.C && this.C.It(s.Tt.nS, this.ng); } mg() { if (!this.C) return; const t = []; for (let i = 0; i < this.triggers.length; i++) t.push(this.triggers[i].qt()); (this.lg = new Date().valueOf()), this.C.It(s.Tt.eS, t), this.C.It(s.Tt.sS, this.lg); } bg() { if (!this.C) return; (this.C.Rt(s.Tt.sS) || 0) > this.lg ? this.ag() : this.fg(); } I(t) { let i = !1; if (null != t && t.triggers) { this.ig.fa(), this.fg(); const e = {}, r = {}; this.triggers = []; for (let s = 0; s < t.triggers.length; s++) { const h = gt.fromJson(t.triggers[s]); if (h) { h.id && null != this.og[h.id] && (h.md(this.og[h.id]), (e[h.id] = this.og[h.id])), h.id && null != this.ng[h.id] && (r[h.id] = this.ng[h.id]); for (let t = 0; t < h.ed.length; t++) if (h.ed[t].Vc(ot.Ls, null)) { i = !0; break; } this.triggers.push(h); } } ni(this.og, e) || ((this.og = e), this.C && this.C.It(s.Tt.rS, this.og)), ni(this.ng, r) || ((this.ng = r), this.C && this.C.It(s.Tt.nS, this.ng)), this.mg(), i && (E.info("Trigger with test condition found, firing test."), this.Ee(ot.Ls)), this.Ee(ot.OPEN); const h = this.sg; let n; this.sg = []; for (let t = 0; t < h.length; t++) (n = Array.prototype.slice.call(h[t])), this.Ee(...n); } } cg(t, i, s, e, r) { const h = (e) => { this.fg(); const r = new Date().valueOf(); t.fd(i) || (!1 === navigator.onLine && t.type === gt.la.oa && e.imageUrl ? E.info( `Not showing ${t.type} trigger action ${t.id} due to offline state.`, ) : t.dd(r) && this.wg(t, r, s) ? 0 === this.Rs.Le() ? E.info( `Not displaying trigger ${t.id} because neither automaticallyShowInAppMessages() nor subscribeToInAppMessage() were called.`, ) : (this.Rs.A([e]), this.yg(t, r)) : E.info( `Not displaying trigger ${t.id} because display time fell outside of the acceptable time window.`, )); }, n = () => { this.fg(); const h = r.pop(); if (null != h) if ((this.Tg(h, i, s, e, r), h.fd(i))) { let t = `Server aborted in-app message display, but the timeout on fallback trigger ${h.id} has already elapsed.`; r.length > 0 && (t += " Continuing to fall back."), E.info(t), n(); } else { E.info( `Server aborted in-app message display. Falling back to lower priority ${h.type} trigger action ${t.id}.`, ); const n = 1e3 * h.hd - (new Date().valueOf() - i); n > 0 ? this.eg.push( window.setTimeout(() => { this.cg(h, i, s, e, r); }, n), ) : this.cg(h, i, s, e, r); } }; let o, l, a; switch (t.type) { case gt.la.oa: if (((o = pt(t.data)), null == o)) { E.error( `Could not parse trigger data for trigger ${t.id}, ignoring.`, ); break; } if (((l = this.ig.Qn(o)), l)) { E.error(l), n(); break; } h(o); break; case gt.la.pd: if (((a = Jt.fromJson(t.data, h, n, i, t.ca || 0)), null == a)) { E.error( `Could not parse trigger data for trigger ${t.id}, ignoring.`, ); break; } this.ig.Xo(a, s, e); break; default: E.error(`Trigger ${t.id} was of unexpected type ${t.type}, ignoring.`); } } Ee(t, i = null, s) { if (!ta(ot, t, "Cannot fire trigger action.", "TriggerEvents")) return; if (this.Ru && this.Ru.Ic()) return ( E.info( "Trigger sync is currently in progress, awaiting sync completion before firing trigger event.", ), void this.sg.push(arguments) ); this.bg(); const e = new Date().valueOf(), r = e - (this.hg || 0); let h = !0, n = !0; const o = []; for (let s = 0; s < this.triggers.length; s++) { const r = this.triggers[s], l = e + 1e3 * r.hd; if ( r.dd(l) && (null == r.startTime || r.startTime.valueOf() <= e) && (null == r.endTime || r.endTime.valueOf() >= e) ) { let s = !1; for (let e = 0; e < r.ed.length; e++) if (r.ed[e].Vc(t, i)) { s = !0; break; } s && ((h = !1), this.wg(r, l, t) && ((n = !1), o.push(r))); } } if (h) return void E.info( `Trigger event ${t} did not match any trigger conditions.`, ); if (n) return void E.info( `Ignoring ${t} trigger event because a trigger was displayed ${ r / 1e3 }s ago.`, ); o.sort((t, i) => t.priority - i.priority); const l = o.pop(); null != l && (E.info( `Firing ${l.type} trigger action ${l.id} from trigger event ${t}.`, ), this.Tg(l, e, t, s, o), 0 === l.hd ? this.cg(l, e, t, s, o) : this.eg.push( window.setTimeout(() => { this.cg(l, e, t, s, o); }, 1e3 * l.hd), )); } changeUser(t = !1) { if (((this.triggers = []), this.C && this.C.Qt(s.Tt.eS), !t)) { (this.sg = []), (this.hg = null), (this.og = {}), (this.ng = {}); for (let t = 0; t < this.eg.length; t++) clearTimeout(this.eg[t]); (this.eg = []), this.C && (this.C.Qt(s.Tt.oS), this.C.Qt(s.Tt.rS), this.C.Qt(s.Tt.nS)); } } clearData() { (this.triggers = []), (this.hg = null), (this.og = {}), (this.ng = {}); for (let t = 0; t < this.eg.length; t++) clearTimeout(this.eg[t]); this.eg = []; } wg(t, i, s) { if (null == this.hg) return !0; if (s === ot.Ls) return ( E.info( "Ignoring minimum interval between trigger because it is a test type.", ), !0 ); let e = t.ad; return null == e && (e = this.tg), i - this.hg >= 1e3 * e; } Tg(t, i, e, r, h) { this.fg(), t.id && (this.ng[t.id] = this.ng[t.id] || []); const n = {}; let o; (n.ma = i), (n.ug = e), null != r && (o = r.qt()), (n.dg = o); const l = []; for (const t of h) t.id && l.push(t.id); (n.pg = l), t.id && this.ng[t.id].push(n), this.C && this.C.It(s.Tt.nS, this.ng); } yg(t, i) { this.fg(), t.md(i), (this.hg = i), t.id && (this.og[t.id] = i), this.C && (this.C.It(s.Tt.oS, i), this.C.It(s.Tt.rS, this.og)); } }