UNPKG

@braze/web-sdk

Version:

Braze SDK for web sites and other JS platforms.

347 lines (346 loc) 9.52 kB
import l from "../util/net.js"; import t from "../common/base-provider.js"; import r from "../managers/braze-instance.js"; import ContentCards from "./content-cards.js"; import { dateFromUnixTimestamp as Y } from "../util/date-utils.js"; import { isURIJavascriptOrData as tt } from "../util/url-utils.js"; import { newCardFromContentCardsJson as st, newCardFromSerializedValue as it, } from "../Card/util/card-factory.js"; import { STORAGE_KEYS as s } from "../managers/storage-manager.js"; import f from "../managers/subscription-manager.js"; import h from "../util/request-header-utils.js"; import { randomInclusive as a } from "../util/math.js"; import { REQUEST_BACKOFF_MAX_SLEEP_MS_DEFAULT as c, REQUEST_BACKOFF_MIN_SLEEP_MS_DEFAULT as d, REQUEST_BACKOFF_SCALE_FACTOR_DEFAULT as m, } from "../common/constants.js"; import { logger as E, IndexedDBAdapter as et } from "../../shared-lib/index.js"; export default class rr extends t { constructor(t, s, i, e, h) { super(), (this.Ss = t), (this.C = s), (this.h = i), (this.Ts = e), (this.B = h), (this.Ss = t), (this.C = s), (this.h = i), (this.Ts = e), (this.B = h), (this.Rs = new f()), r.S(this.Rs), (this.Us = 0), (this.Ns = 0), (this.cards = []), this.Ms(); const n = et.Ps.$s; new et(n, E).Xs(n.Os._s, (t) => { this.Qs(t); }), (this.Vs = null), (this.T = null), (this.Ws = null), (this.Ys = null), (this.Zs = 10); } fi() { return this.Vs; } vi(t) { this.Vs = t; } Lt() { return this.T; } $t(t) { this.T = t; } Ms() { if (!this.C) return; const t = this.C.Rt(s.Tt.Ci) || [], i = []; for (let s = 0; s < t.length; s++) { const e = it(t[s]); null != e && i.push(e); } (this.cards = this.bi(this.wi(i, !1))), (this.Us = this.C.Rt(s.Tt.gi) || this.Us), (this.Ns = this.C.Rt(s.Tt.ji) || this.Ns); } yi(t, i = !1, e = 0, h = 0) { let r; if (i) { r = []; for (const t of this.cards) t.test && r.push(t); } else r = this.cards.slice(); for (let s = 0; s < t.length; s++) { const e = t[s]; let h = null; for (let t = 0; t < this.cards.length; t++) if (e.id === this.cards[t].id) { h = this.cards[t]; break; } if (i) { const t = st(e); null != h && h.viewed && t && (t.viewed = !0), null != t && r.push(t); } else if (null == h) { const t = st(e); null != t && r.push(t); } else { if (!h.ri(e)) for (let t = 0; t < r.length; t++) if (e.id === r[t].id) { r.splice(t, 1); break; } } } (this.cards = this.bi(this.wi(r, i))), this.Ri(), (this.Us = e), (this.Ns = h), this.C && (this.C.It(s.Tt.gi, this.Us), this.C.It(s.Tt.ji, this.Ns)); } I(t) { if (this.Ui() && null != t && t.cards) { this.C && this.C.It(s.Tt.ki, r.zi()); const i = t.full_sync; i || this.Ms(), this.yi(t.cards, i, t.last_full_sync_at, t.last_card_updated_at), this.Rs.A(this.qi(!0)); } } xi(t) { this.Fi(), (this.Ws = t); } Qs(t) { var s; if (!this.Ui()) return; this.Ms(); const i = this.cards.slice(); let e = null; e = null === (s = this.Ss) || void 0 === s ? void 0 : s.getUserId(); for (let s = 0; s < t.length; s++) if (e === t[s].userId || (null == e && null == t[s].userId)) { const e = t[s].card; let h = null; for (let t = 0; t < this.cards.length; t++) if (e.id === this.cards[t].id) { h = this.cards[t]; break; } if (null == h) { const t = st(e); null != t && i.push(t); } else { if (!h.ri(e)) for (let t = 0; t < i.length; t++) if (e.id === i[t].id) { i.splice(t, 1); break; } } } (this.cards = this.bi(this.wi(i, !1))), this.Ri(), this.Rs.A(this.qi(!0)); } wi(t, i) { let e = {}, h = {}, r = {}; this.C && ((e = this.C.Rt(s.Tt.ns) || {}), (h = this.C.Rt(s.Tt.hs) || {}), (r = this.C.Rt(s.Tt.us) || {})); const n = {}, o = {}, l = {}; for (let s = 0; s < t.length; s++) { const i = t[s].id; i && (e[i] && ((t[s].clicked = !0), (n[i] = !0)), h[i] && ((t[s].viewed = !0), (o[i] = !0)), r[i] && ((t[s].dismissed = !0), (l[i] = !0))); } return ( i && this.C && (this.C.It(s.Tt.ns, n), this.C.It(s.Tt.hs, o), this.C.It(s.Tt.us, l)), t ); } bi(t) { const i = [], e = new Date(); let h = {}; this.C && (h = this.C.Rt(s.Tt.us) || {}); let r = !1; for (let s = 0; s < t.length; s++) { const n = t[s].url; if (!this.Ts && n && tt(n)) { E.error( `Card with url ${n} will not be displayed because Javascript URLs are disabled. Use the "allowUserSuppliedJavascript" option for braze.initialize to enable this card.`, ); continue; } const o = t[s].expiresAt; let l = !0; if ((null != o && (l = o >= e), (l = l && !t[s].dismissed), l)) i.push(t[s]); else { const i = t[s].id; i && (h[i] = !0), (r = !0); } } return r && this.C && this.C.It(s.Tt.us, h), i; } Ri() { var t; const i = []; for (let t = 0; t < this.cards.length; t++) i.push(this.cards[t].qt()); null === (t = this.C) || void 0 === t || t.It(s.Tt.Ci, i); } Fi() { this.Ws && (clearTimeout(this.Ws), (this.Ws = null)); } lr(t, i, e = !1) { var n; const o = this.B, u = this.C; if (!o || !u) return void ("function" == typeof i && i()); if ((e && (h.Li(u, h.it.Ji), this.Fi()), !this.Ui())) return void ( null === (n = this.h) || void 0 === n || n.Mi(() => { this.lr(t, i, !0); }) ); const f = o.Z({}, !0); u.Rt(s.Tt.ki) !== r.zi() && this.$i(), (f.last_full_sync_at = this.Us), (f.last_card_updated_at = this.Ns); const p = o.tt(f, h.it.Ji, e); let v = !1; o.et( f, (s = -1) => { if (this.C) { const t = new Date().valueOf(); h.nt(this.C, h.it.Ji, t); } -1 !== s && p.push(["X-Braze-Req-Tokens-Remaining", s.toString()]), l.ot({ url: `${o.ht()}/content_cards/sync`, data: f, headers: p, lt: (s) => { if (!o.ut(f, s, p)) return (v = !0), void ("function" == typeof i && i()); o.ct(), this.I(s), (v = !1), "function" == typeof t && t(); }, error: (t) => { o.dt(t, "retrieving content cards"), (v = !0), "function" == typeof i && i(); }, ft: (s, e) => { var r, n, l; let u; if (v) { const t = (null === (r = this.h) || void 0 === r ? void 0 : r.vt()) || d, s = (null === (n = this.h) || void 0 === n ? void 0 : n.gt()) || m, i = (null === (l = this.h) || void 0 === l ? void 0 : l.bt()) || c; let e = this.Ys; (null == e || e < t) && (e = t), (u = Math.min(i, a(t, e * s))); } o.yt( e, () => { this.lr(t, i, !1); }, h.it.Ji, (t) => this.xi(t), () => this.Fi(), u, ); }, }); }, h.it.Ji, i, ); } qi(t) { t || this.Ms(); const i = this.bi(this.cards); i.sort((t, s) => t.pinned && !s.pinned ? -1 : s.pinned && !t.pinned ? 1 : t.updated && s.updated && t.updated > s.updated ? -1 : t.updated && s.updated && s.updated > t.updated ? 1 : 0, ); let e = Math.max(this.Ns || 0, this.Us || 0); return ( 0 === e && (e = void 0), this.C && this.C.Rt(s.Tt.ji) === this.Ns && void 0 === e && (e = this.Ns), new ContentCards(i, Y(e)) ); } zt(t) { return this.Rs.Kt(t); } $i() { (this.Us = 0), (this.Ns = 0), this.C && (this.C.Qt(s.Tt.gi), this.C.Qt(s.Tt.ji)); } changeUser(t) { t || ((this.cards = []), this.Rs.A(new ContentCards(this.cards.slice(), null)), this.C && (this.C.Qt(s.Tt.Ci), this.C.Qt(s.Tt.ns), this.C.Qt(s.Tt.hs), this.C.Qt(s.Tt.us))), this.Fi(), this.$i(); } clearData(t) { (this.Us = 0), (this.Ns = 0), (this.cards = []), this.Rs.A(new ContentCards(this.cards.slice(), null)), t && this.C && (this.C.Qt(s.Tt.Ci), this.C.Qt(s.Tt.ns), this.C.Qt(s.Tt.hs), this.C.Qt(s.Tt.us), this.C.Qt(s.Tt.gi), this.C.Qt(s.Tt.ji)), this.Fi(); } Ui() { return !!this.h && (!!this.h.Bi() || (0 !== this.h.Ot() && this.Pi(), !1)); } Pi() { this.Rs.A(new ContentCards([], new Date())), this.C && this.C.Qt(s.Tt.Ci); } }