@braze/web-sdk
Version:
Braze SDK for web sites and other JS platforms.
189 lines (188 loc) • 4.67 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 u from "../managers/subscription-manager.js";
import { randomInclusive as l } from "../util/math.js";
import a 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 nr extends t {
constructor(t, s, e, i) {
super(),
(this.h = t),
(this.B = s),
(this.j = e),
(this.C = i),
(this.je = []),
(this.ye = 0),
(this.h = t),
(this.B = s),
(this.j = e),
(this.C = i),
(this.Ce = null),
(this.Re = new u()),
(this.S = 10),
(this.T = null),
(this.I = null),
r.q(this.Re);
}
N(t) {
var s;
if (
(null === (s = this.h) || void 0 === s ? void 0 : s.Te()) &&
null != t &&
t.feature_flags
) {
this.je = [];
for (const s of t.feature_flags) {
const t = at(s);
t && this.je.push(t);
}
(this.ye = new Date().getTime()), this.De(), this.Re.L(this.je);
}
}
Ie() {
let t = {};
this.j && (t = this.j.lt(s.ct.Se));
const e = {};
for (const s in t) {
const i = ht(t[s]);
i && (e[i.id] = i);
}
return e;
}
qe() {
var t;
return (
(null === (t = this.j) || void 0 === t ? void 0 : t.lt(s.ct.Ne)) || {}
);
}
ze(t) {
this.j && this.j.ft(s.ct.Ne, t);
}
St(t) {
return this.Re.wt(t);
}
refreshFeatureFlags(t, s, e = !1, i = !0) {
const r = () => {
"function" == typeof s && s(), this.Re.L(this.je);
};
if (!this.Ue(e))
return (
!this.Ce &&
this.h &&
(this.Ce = this.h.$e(() => {
this.refreshFeatureFlags(t, s);
})),
void r()
);
const o = this.B;
if (!o) return void r();
i && this.X();
const n = o.$({}, !0),
u = o.A(n, h.H.ke);
let f = !1;
o.J(
n,
(i = -1) => {
const o = this.B;
if (!o) return void r();
const c = new Date().valueOf();
h.K(this.j, h.H.ke, c),
-1 !== i && u.push(["X-Braze-Req-Tokens-Remaining", i.toString()]),
a.O({
url: `${o.V()}/feature_flags/sync`,
headers: u,
data: n,
W: (s) => {
if (!o.Y(n, s, u)) return (f = !0), void r();
o.Z(), this.N(s), (f = !1), "function" == typeof t && t();
},
error: (t) => {
o._(t, "retrieving feature flags"), (f = !0), r();
},
tt: (i, r) => {
let a;
if (f) {
let t = this.T;
(null == t || t < 1e3 * this.S) && (t = 1e3 * this.S),
(a = Math.min(3e5, l(1e3 * this.S, 3 * t)));
}
o.st(
r,
() => {
this.refreshFeatureFlags(t, s, e, !0);
},
h.H.ke,
(t) => this.it(t),
() => this.X(),
a,
);
},
});
},
h.H.ke,
s,
);
}
X() {
null != this.I && (clearTimeout(this.I), (this.I = null));
}
it(t) {
this.X(), (this.I = t);
}
Ue(t) {
if (!this.h) return !1;
if (!t) {
const t = this.h.Be();
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.ye || 0) + 1e3 * t;
}
if (!s)
return (
E.info(`Feature flag refreshes were rate limited to ${t} seconds`), !1
);
}
return this.h.Te();
}
Me() {
var t;
return (
(null === (t = this.j) || void 0 === t ? void 0 : t.lt(s.ct.Xe)) || null
);
}
_e() {
var t, e;
null === (t = this.j) ||
void 0 === t ||
t.ft(s.ct.Xe, null === (e = this.C) || void 0 === e ? void 0 : e.bt());
}
Ae() {
var t;
const s = null === (t = this.C) || void 0 === t ? void 0 : t.bt(),
e = this.Me();
return null == e || s === e;
}
De() {
if (!this.j) return;
const t = {};
for (const s of this.je) {
const e = s.dt();
t[s.id] = e;
}
this.j.ft(s.ct.Se, t), this.j.ft(s.ct.Ee, this.ye), this._e();
}
changeUser() {
this.X();
}
clearData() {
this.X();
}
}