@braze/web-sdk
Version:
Braze SDK for web sites and other JS platforms.
337 lines (336 loc) • 8.62 kB
JavaScript
import t from "../common/base-provider.js";
import { STORAGE_KEYS as s } from "../managers/storage-manager.js";
import {
newBannerFromJson as n,
newBannerFromSerializedValue as o,
} from "./banner-factory.js";
import h from "../util/request-header-utils.js";
import l from "../util/net.js";
import { randomInclusive as a } from "../util/math.js";
import {
DISMISSALS_CACHE_SIZE_DEFAULT as u,
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 f from "../managers/subscription-manager.js";
import r from "../managers/braze-instance.js";
import v from "../common/event-logger.js";
import { EventTypes as p } from "../../shared-lib/event-types.js";
import { isArray as g } from "../util/code-utils.js";
export default class e extends t {
constructor(t, s, i, e) {
super(),
(this.h = t),
(this.B = s),
(this.C = i),
(this.j = e),
(this.banners = {}),
(this.h = t),
(this.B = s),
(this.C = i),
(this.j = e),
(this.D = 10),
(this.N = null),
(this.F = null),
(this.R = new f()),
r.S(this.R),
(this.T = null),
(this.q = null);
}
I(t) {
if (this.P() && (this._(t), null != t && t.banners)) {
const s = t.request_time,
i = "number" != typeof s || isNaN(s) ? null : s,
e = this.k(),
r = this.L(),
o = this.$();
this.banners = {};
const h = t.banners;
for (const t in h) {
const s = this.K(t, i, o),
l = h[t];
let a = null;
if (
(null != l && null != l.banner && (a = l.banner),
this.U(t, a, i, s, e, r))
) {
if (s) {
const s = e[t];
s && (this.banners[t] = s);
}
continue;
}
let u = null;
null != a && (u = n(a)), u && (this.banners[t] = u);
}
this.W(), this.R.A(this.banners);
}
}
U(t, s, i, e, n, r) {
return !this.M(t, s, n) && null != i && (!!e || this.X(s, r, i));
}
K(t, s, i) {
if (null == s) return !1;
const e = i[t];
return "number" == typeof e && !isNaN(e) && s < e;
}
M(t, s, i) {
if (!s) return !1;
const e = s.stable_key;
if ("string" != typeof e || 0 === e.length) return !1;
const n = i[t],
r = null == n ? void 0 : n.G;
return "string" == typeof r && 0 !== r.length && r !== e;
}
X(t, s, i) {
if (!t) return !1;
const e = t.stable_key;
if ("string" != typeof e || 0 === e.length) return !1;
for (const t of s) {
if (t.stable_key !== e) continue;
const s = t.dismissal_time;
if ("number" == typeof s && !isNaN(s) && s >= i) return !0;
}
return !1;
}
_(t) {
var s;
const i =
null === (s = null == t ? void 0 : t.dismissals) || void 0 === s
? void 0
: s.acknowledged;
if (!i || !g(i) || 0 === i.length) return;
const e = this.L();
if (0 === e.length) return;
const n = {};
for (const t of i)
t.banner_id &&
t.dismissal_time &&
(n[this.H(t.banner_id, t.dismissal_time)] = !0);
const r = e.filter((t) => !n[this.H(t.banner_id, t.dismissal_time)]);
r.length !== e.length && this.J(r);
}
H(t, s) {
return `${t}:${s}`;
}
O(t, s, i, e = !0) {
var n;
const r = () => {
"function" == typeof i && i();
};
if (!this.P())
return void (
null === (n = this.h) ||
void 0 === n ||
n.V(() => {
this.O(t, s, i);
})
);
const o = this.B;
if (!o) return void r();
e && this.Y();
const u = o.Z({}, !0);
u.time_ms = new Date().valueOf();
const f = [];
for (const s of t) f.push({ id: s });
u.placements = f;
const v = this.L().map((t) => ({
banner_id: t.banner_id,
dismissal_time: t.dismissal_time,
}));
u.pending_dismissals = v;
const p = o.tt(u, h.it.st);
let g = !1;
o.et(
u,
(e = -1) => {
const n = this.B;
if (!n) return void r();
const o = new Date().valueOf();
h.nt(this.C, h.it.st, o),
-1 !== e && p.push(["X-Braze-Req-Tokens-Remaining", e.toString()]);
const f = u.time_ms;
null == f || "number" != typeof f || isNaN(f) || this.rt(t, f),
l.ot({
url: `${n.ht()}/banners/sync`,
headers: p,
data: u,
lt: (t) => {
if (!n.ut(u, t, p)) return (g = !0), void r();
n.ct(), this.I(t), (g = !1), "function" == typeof s && s();
},
error: (t) => {
n.dt(t, "retrieving banners"), (g = !0), r();
},
ft: (e, r) => {
var o, l, u;
let f;
if (((this.q = t), g)) {
const t =
(null === (o = this.h) || void 0 === o ? void 0 : o.vt()) ||
d,
s =
(null === (l = this.h) || void 0 === l ? void 0 : l.gt()) ||
m,
i =
(null === (u = this.h) || void 0 === u ? void 0 : u.bt()) ||
c;
let e = this.N;
(null == e || e < t) && (e = t), (f = Math.min(i, a(t, e * s)));
}
n.yt(
r,
() => {
this.O(t, s, i, !1);
},
h.it.st,
(t) => this.Bt(t),
() => this.Y(),
f,
);
},
});
},
h.it.st,
i,
);
}
Ct() {
return this.q;
}
jt(t, s) {
const i = { id: t.id };
s && (i.bid = s);
return v.wt(p.Dt, i).lt;
}
Nt(t) {
if (!t.G) return !1;
const s = this.L();
if (s.some((s) => s.banner_id === t.id && s.stable_key === t.G)) return !1;
const i = { id: t.id },
e = this.k(),
n = e[t.placementId];
n &&
n.G === t.G &&
(delete e[t.placementId],
(this.banners = e),
this.W(),
this.R.A(this.banners)),
s.push({
banner_id: t.id,
dismissal_time: new Date().valueOf(),
stable_key: t.G,
}),
this.J(s);
return v.wt(p.Ft, i).lt;
}
Y() {
null != this.F && (clearTimeout(this.F), (this.F = null));
}
Bt(t) {
this.Y(), (this.F = t);
}
k() {
let t = {};
this.C && (t = this.C.Rt(s.Tt.St));
const i = {};
for (const s in t) {
let e = null;
null != t[s] && (e = o(t[s])), e && (i[e.placementId] = e);
}
return i;
}
W() {
var t;
if (!this.C) return;
const i = {};
for (const s in this.banners) {
const e =
(null === (t = this.banners[s]) || void 0 === t ? void 0 : t.qt()) ||
null;
i[s] = e;
}
this.C.It(s.Tt.St, i), this.Pt();
}
Pt() {
var t, i;
null === (t = this.C) ||
void 0 === t ||
t.It(s.Tt._t, null === (i = this.j) || void 0 === i ? void 0 : i.kt());
}
Et() {
var t;
return (
(null === (t = this.C) || void 0 === t ? void 0 : t.Rt(s.Tt._t)) || null
);
}
Lt() {
return this.T;
}
$t(t) {
this.T = t;
}
xt() {
var t;
const s = null === (t = this.j) || void 0 === t ? void 0 : t.kt(),
i = this.Et();
return null == i || s === i;
}
zt(t) {
return this.R.Kt(t);
}
Ut() {
var t;
return (
(null === (t = this.C) || void 0 === t ? void 0 : t.Rt(s.Tt.Wt)) || {}
);
}
At(t) {
this.C && this.C.It(s.Tt.Wt, t);
}
L() {
var t;
return (
(null === (t = this.C) || void 0 === t ? void 0 : t.Rt(s.Tt.Mt)) || []
);
}
J(t) {
var i;
if (!this.C) return;
const e = null === (i = this.h) || void 0 === i ? void 0 : i.Xt(),
n = null != e ? e : u,
r = t.length <= n ? t : t.slice(-n);
this.C.It(s.Tt.Mt, r);
}
$() {
var t;
return (
(null === (t = this.C) || void 0 === t ? void 0 : t.Rt(s.Tt.Gt)) || {}
);
}
rt(t, i) {
if (!this.C) return;
const e = this.$();
for (const s of t) e[s] = i;
this.C.It(s.Tt.Gt, e);
}
changeUser() {
this.Ht(), this.Y();
}
clearData() {
this.Y();
}
P() {
return !!this.h && (!!this.h.Jt() || (0 !== this.h.Ot() && this.Ht(), !1));
}
Ht() {
(this.banners = {}),
this.C &&
(this.C.Qt(s.Tt.St),
this.C.Qt(s.Tt.Wt),
this.C.Qt(s.Tt.Mt),
this.C.Qt(s.Tt.Gt)),
this.R.A({});
}
}