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