@braze/web-sdk
Version:
Braze SDK for web sites and other JS platforms.
203 lines (202 loc) • 5.22 kB
JavaScript
import { logger as E } from "../../shared-lib/index.js";
import t from "../common/base-provider.js";
import r from "../managers/braze-instance.js";
import { STORAGE_KEYS as s } from "../managers/storage-manager.js";
import f from "../managers/subscription-manager.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 l from "../util/net.js";
import {
newFeatureFlagFromJson as at,
newFeatureFlagFromSerializedValue as ht,
} from "./feature-flag-factory.js";
import h from "../util/request-header-utils.js";
export default class ar extends t {
constructor(t, s, i, e) {
super(),
(this.h = t),
(this.B = s),
(this.C = i),
(this.j = e),
(this.Xr = []),
(this.Yr = 0),
(this.h = t),
(this.B = s),
(this.C = i),
(this.j = e),
(this.Zr = null),
(this.ho = new f()),
(this.D = 10),
(this.N = null),
(this.F = null),
r.S(this.ho);
}
I(t) {
var s;
if (
(null === (s = this.h) || void 0 === s ? void 0 : s.lo()) &&
null != t &&
t.feature_flags
) {
this.Xr = [];
for (const s of t.feature_flags) {
const t = at(s);
t && this.Xr.push(t);
}
(this.Yr = new Date().getTime()), this.do(), this.ho.A(this.Xr);
}
}
vo() {
let t = {};
this.C && (t = this.C.Rt(s.Tt.Fo));
const i = {};
for (const s in t) {
const e = ht(t[s]);
e && (i[e.id] = e);
}
return i;
}
po() {
var t;
return (
(null === (t = this.C) || void 0 === t ? void 0 : t.Rt(s.Tt.jo)) || {}
);
}
wo(t) {
this.C && this.C.It(s.Tt.jo, t);
}
zt(t) {
return this.ho.Kt(t);
}
refreshFeatureFlags(t, s, i = !1, e = !0) {
const r = () => {
"function" == typeof s && s(), this.ho.A(this.Xr);
};
if (!this.yo(i))
return (
!this.Zr &&
this.h &&
(this.Zr = this.h.bo(() => {
this.refreshFeatureFlags(t, s);
})),
void r()
);
const o = this.B;
if (!o) return void r();
e && this.Y();
const n = o.Z({}, !0),
u = o.tt(n, h.it.Co);
let f = !1;
o.et(
n,
(e = -1) => {
const o = this.B;
if (!o) return void r();
const v = new Date().valueOf();
h.nt(this.C, h.it.Co, v),
-1 !== e && u.push(["X-Braze-Req-Tokens-Remaining", e.toString()]),
l.ot({
url: `${o.ht()}/feature_flags/sync`,
headers: u,
data: n,
lt: (s) => {
if (!o.ut(n, s, u)) return (f = !0), void r();
o.ct(), this.I(s), (f = !1), "function" == typeof t && t();
},
error: (t) => {
o.dt(t, "retrieving feature flags"), (f = !0), r();
},
ft: (e, r) => {
var n, l, u;
let v;
if (f) {
const t =
(null === (n = this.h) || void 0 === n ? void 0 : n.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), (v = Math.min(i, a(t, e * s)));
}
o.yt(
r,
() => {
this.refreshFeatureFlags(t, s, i, !0);
},
h.it.Co,
(t) => this.Bt(t),
() => this.Y(),
v,
);
},
});
},
h.it.Co,
s,
);
}
Y() {
null != this.F && (clearTimeout(this.F), (this.F = null));
}
Bt(t) {
this.Y(), (this.F = t);
}
yo(t) {
if (!this.h) return !1;
if (!t) {
const t = this.h.Ro();
if (null == t) return !1;
let s = !1;
if (!isNaN(t)) {
if (-1 === t) return E.info("Feature flag refreshes not allowed"), !1;
s = new Date().getTime() >= (this.Yr || 0) + 1e3 * t;
}
if (!s)
return (
E.info(`Feature flag refreshes were rate limited to ${t} seconds`), !1
);
}
return this.h.lo();
}
To() {
var t;
return (
(null === (t = this.C) || void 0 === t ? void 0 : t.Rt(s.Tt.Do)) || null
);
}
Io() {
var t, i;
null === (t = this.C) ||
void 0 === t ||
t.It(s.Tt.Do, null === (i = this.j) || void 0 === i ? void 0 : i.kt());
}
So() {
var t;
const s = null === (t = this.j) || void 0 === t ? void 0 : t.kt(),
i = this.To();
return null == i || s === i;
}
do() {
if (!this.C) return;
const t = {};
for (const s of this.Xr) {
const i = s.qt();
t[s.id] = i;
}
this.C.It(s.Tt.Fo, t), this.C.It(s.Tt.qo, this.Yr), this.Io();
}
changeUser() {
this.Y();
}
clearData() {
this.Y();
}
}