UNPKG

@braze/web-sdk

Version:

Braze SDK for web sites and other JS platforms.

347 lines (346 loc) 9.54 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 b, 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.j = s), (this.h = i), (this.Ts = e), (this.B = h), (this.Ss = t), (this.j = s), (this.h = i), (this.Ts = e), (this.B = h), (this.Rs = new f()), r.S(this.Rs), (this.Us = 0), (this.$s = 0), (this.cards = []), this.Ps(); const n = et._s.Xs; new et(n, b).Qs(n.Ws.Vs, (t) => { this.Ys(t); }), (this.Zs = null), (this.T = null), (this.fi = null), (this.di = null), (this.pi = 10); } vi() { return this.Zs; } Ci(t) { this.Zs = t; } Et() { return this.T; } Lt(t) { this.T = t; } Ps() { if (!this.j) return; const t = this.j.St(s.It.bi) || [], i = []; for (let s = 0; s < t.length; s++) { const e = it(t[s]); null != e && i.push(e); } (this.cards = this.wi(this.gi(i, !1))), (this.Us = this.j.St(s.It.ji) || this.Us), (this.$s = this.j.St(s.It.yi) || this.$s); } Ri(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.ni(e)) for (let t = 0; t < r.length; t++) if (e.id === r[t].id) { r.splice(t, 1); break; } } } (this.cards = this.wi(this.gi(r, i))), this.Ui(), (this.Us = e), (this.$s = h), this.j && (this.j.Pt(s.It.ji, this.Us), this.j.Pt(s.It.yi, this.$s)); } q(t) { if (this.ki() && null != t && t.cards) { this.j && this.j.Pt(s.It.zi, r.qi()); const i = t.full_sync; i || this.Ps(), this.Ri(t.cards, i, t.last_full_sync_at, t.last_card_updated_at), this.Rs.A(this.xi(!0)); } } Fi(t) { this.Li(), (this.fi = t); } Ys(t) { var s; if (!this.ki()) return; this.Ps(); 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.ni(e)) for (let t = 0; t < i.length; t++) if (e.id === i[t].id) { i.splice(t, 1); break; } } } (this.cards = this.wi(this.gi(i, !1))), this.Ui(), this.Rs.A(this.xi(!0)); } gi(t, i) { let e = {}, h = {}, r = {}; this.j && ((e = this.j.St(s.It.Zt) || {}), (h = this.j.St(s.It.ps) || {}), (r = this.j.St(s.It.fs) || {})); 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.j && (this.j.Pt(s.It.Zt, n), this.j.Pt(s.It.ps, o), this.j.Pt(s.It.fs, l)), t ); } wi(t) { const i = [], e = new Date(); let h = {}; this.j && (h = this.j.St(s.It.fs) || {}); let r = !1; for (let s = 0; s < t.length; s++) { const n = t[s].url; if (!this.Ts && n && tt(n)) { b.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.j && this.j.Pt(s.It.fs, h), i; } Ui() { var t; const i = []; for (let t = 0; t < this.cards.length; t++) i.push(this.cards[t].qt()); null === (t = this.j) || void 0 === t || t.Pt(s.It.bi, i); } Li() { this.fi && (clearTimeout(this.fi), (this.fi = null)); } lr(t, i, e = "sdk") { var n; const o = this.B, u = this.j; if (!o || !u) return void ("function" == typeof i && i()); if (("client" === e && (h.Ji(u, h.it.Mi), this.Li()), !this.ki())) return void ( null === (n = this.h) || void 0 === n || n.$i(() => { this.lr(t, i, "client"); }) ); const f = o.Z({}, !0); u.St(s.It.zi) !== r.qi() && this.Bi(), (f.last_full_sync_at = this.Us), (f.last_card_updated_at = this.$s); const p = o.tt(f, h.it.Mi, e); let v = !1; o.et( f, (s = -1) => { if (this.j) { const t = new Date().valueOf(); h.nt(this.j, h.it.Mi, 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.q(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.di; (null == e || e < t) && (e = t), (u = Math.min(i, a(t, e * s))); } o.yt( e, () => { this.lr(t, i); }, h.it.Mi, (t) => this.Fi(t), () => this.Li(), u, ); }, }); }, h.it.Mi, i, ); } xi(t) { t || this.Ps(); const i = this.wi(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.$s || 0, this.Us || 0); return ( 0 === e && (e = void 0), this.j && this.j.St(s.It.yi) === this.$s && void 0 === e && (e = this.$s), new ContentCards(i, Y(e)) ); } Kt(t) { return this.Rs.Ut(t); } Bi() { (this.Us = 0), (this.$s = 0), this.j && (this.j.Vt(s.It.ji), this.j.Vt(s.It.yi)); } changeUser(t) { t || ((this.cards = []), this.Rs.A(new ContentCards(this.cards.slice(), null)), this.j && (this.j.Vt(s.It.bi), this.j.Vt(s.It.Zt), this.j.Vt(s.It.ps), this.j.Vt(s.It.fs))), this.Li(), this.Bi(); } clearData(t) { (this.Us = 0), (this.$s = 0), (this.cards = []), this.Rs.A(new ContentCards(this.cards.slice(), null)), t && this.j && (this.j.Vt(s.It.bi), this.j.Vt(s.It.Zt), this.j.Vt(s.It.ps), this.j.Vt(s.It.fs), this.j.Vt(s.It.ji), this.j.Vt(s.It.yi)), this.Li(); } ki() { return !!this.h && (!!this.h.Pi() || (0 !== this.h.Qt() && this.Xi(), !1)); } Xi() { this.Rs.A(new ContentCards([], new Date())), this.j && this.j.Vt(s.It.bi); } }