@braze/web-sdk
Version:
Braze SDK for web sites and other JS platforms.
205 lines (204 loc) • 4.89 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 u from "../managers/subscription-manager.js";
import r from "../managers/braze-instance.js";
import c from "../common/event-logger.js";
import { EventTypes as d } from "../../shared-lib/event-types.js";
export default class e extends t {
constructor(t, s, i, e) {
super(),
(this.B = t),
(this.S = s),
(this.C = i),
(this.T = e),
(this.banners = {}),
(this.B = t),
(this.S = s),
(this.C = i),
(this.T = e),
(this.R = 10),
(this.I = null),
(this.q = null),
(this.D = new u()),
r.F(this.D),
(this.N = null),
(this.k = null);
}
U(t) {
if (this.L() && null != t && t.banners) {
this.banners = {};
const s = t.banners;
for (const t in s) {
const i = s[t];
let e = null;
null != i && null != i.banner && (e = n(i.banner)),
e && (this.banners[t] = e);
}
this.M(), this.D.X(this.banners);
}
}
$(t, s, i, e = !0) {
var n;
const r = () => {
"function" == typeof i && i();
};
if (!this.L())
return void (
null === (n = this.B) ||
void 0 === n ||
n.A(() => {
this.$(t, s, i);
})
);
const o = this.S;
if (!o) return void r();
e && this.G();
const u = o.H({}, !0),
c = [];
for (const s of t) c.push({ id: s });
u.placements = c;
const d = o.J(u, h.O.K);
let m = !1;
o.V(
u,
(e = -1) => {
const n = this.S;
if (!n) return void r();
const o = new Date().valueOf();
h.W(this.C, h.O.K, o),
-1 !== e && d.push(["X-Braze-Req-Tokens-Remaining", e.toString()]),
l.Y({
url: `${n.Z()}/banners/sync`,
headers: d,
data: u,
tt: (t) => {
if (!n.st(u, t, d)) return (m = !0), void r();
n.it(), this.U(t), (m = !1), "function" == typeof s && s();
},
error: (t) => {
n.et(t, "retrieving banners"), (m = !0), r();
},
nt: (e, r) => {
let o;
if (((this.k = t), m)) {
let t = this.I;
(null == t || t < 1e3 * this.R) && (t = 1e3 * this.R),
(o = Math.min(3e5, a(1e3 * this.R, 3 * t)));
}
n.rt(
r,
() => {
this.$(t, s, i, !1);
},
h.O.K,
(t) => this.ot(t),
() => this.G(),
o,
);
},
});
},
h.O.K,
i,
);
}
ht() {
return this.k;
}
lt(t, s) {
const i = { id: t.id };
s && (i.bid = s);
return c.ut(d.ct, i).tt;
}
G() {
null != this.q && (clearTimeout(this.q), (this.q = null));
}
ot(t) {
this.G(), (this.q = t);
}
dt() {
let t = {};
this.C && (t = this.C.ft(s.gt.vt));
const i = {};
for (const s in t) {
let e = null;
null != t[s] && (e = o(t[s])), e && (i[e.placementId] = e);
}
return i;
}
M() {
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.bt()) ||
null;
i[s] = e;
}
this.C.Bt(s.gt.vt, i), this.jt();
}
jt() {
var t, i;
null === (t = this.C) ||
void 0 === t ||
t.Bt(s.gt.yt, null === (i = this.T) || void 0 === i ? void 0 : i.St());
}
wt() {
var t;
return (
(null === (t = this.C) || void 0 === t ? void 0 : t.ft(s.gt.yt)) || null
);
}
Ct() {
return this.N;
}
Tt(t) {
this.N = t;
}
Rt() {
var t;
const s = null === (t = this.T) || void 0 === t ? void 0 : t.St(),
i = this.wt();
return null != i && s === i;
}
It() {
var t;
const s = null === (t = this.T) || void 0 === t ? void 0 : t.St(),
i = this.wt();
return null == i || s === i;
}
qt(t) {
return this.D.Dt(t);
}
Ft() {
var t;
return (
(null === (t = this.C) || void 0 === t ? void 0 : t.ft(s.gt.Nt)) || {}
);
}
kt(t) {
this.C && this.C.Bt(s.gt.Nt, t);
}
changeUser() {
this.xt(), this.G();
}
clearData() {
this.G();
}
L() {
return !!this.B && (!!this.B.zt() || (0 !== this.B.Et() && this.xt(), !1));
}
xt() {
(this.banners = {}),
this.C && (this.C.Ut(s.gt.vt), this.C.Ut(s.gt.Nt)),
this.D.X({});
}
}