UNPKG

@braze/web-sdk

Version:

Braze SDK for web sites and other JS platforms.

189 lines (188 loc) 4.68 kB
import { logger as N } 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 a } from "../util/math.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 ir extends t { constructor(t, s, e, i) { super(), (this.B = t), (this.S = s), (this.C = e), (this.T = i), (this.De = []), (this.qe = 0), (this.B = t), (this.S = s), (this.C = e), (this.T = i), (this.ze = null), (this.Ue = new u()), (this.R = 10), (this.I = null), (this.q = null), r.F(this.Ue); } U(t) { var s; if ( (null === (s = this.B) || void 0 === s ? void 0 : s.ke()) && null != t && t.feature_flags ) { this.De = []; for (const s of t.feature_flags) { const t = at(s); t && this.De.push(t); } (this.qe = new Date().getTime()), this.Be(), this.Ue.X(this.De); } } Me() { let t = {}; this.C && (t = this.C.ft(s.gt.Xe)); const e = {}; for (const s in t) { const i = ht(t[s]); i && (e[i.id] = i); } return e; } _e() { var t; return ( (null === (t = this.C) || void 0 === t ? void 0 : t.ft(s.gt.Ge)) || {} ); } He(t) { this.C && this.C.Bt(s.gt.Ge, t); } qt(t) { return this.Ue.Dt(t); } refreshFeatureFlags(t, s, e = !1, i = !0) { const r = () => { "function" == typeof s && s(), this.Ue.X(this.De); }; if (!this.Je(e)) return ( !this.ze && this.B && (this.ze = this.B.Ke(() => { this.refreshFeatureFlags(t, s); })), void r() ); const o = this.S; if (!o) return void r(); i && this.G(); const n = o.H({}, !0), u = o.J(n, h.O.Le); let f = !1; o.V( n, (i = -1) => { const o = this.S; if (!o) return void r(); const c = new Date().valueOf(); h.W(this.C, h.O.Le, c), -1 !== i && u.push(["X-Braze-Req-Tokens-Remaining", i.toString()]), l.Y({ url: `${o.Z()}/feature_flags/sync`, headers: u, data: n, tt: (s) => { if (!o.st(n, s, u)) return (f = !0), void r(); o.it(), this.U(s), (f = !1), "function" == typeof t && t(); }, error: (t) => { o.et(t, "retrieving feature flags"), (f = !0), r(); }, nt: (i, r) => { let n; if (f) { let t = this.I; (null == t || t < 1e3 * this.R) && (t = 1e3 * this.R), (n = Math.min(3e5, a(1e3 * this.R, 3 * t))); } o.rt( r, () => { this.refreshFeatureFlags(t, s, e, !0); }, h.O.Le, (t) => this.ot(t), () => this.G(), n, ); }, }); }, h.O.Le, s, ); } G() { null != this.q && (clearTimeout(this.q), (this.q = null)); } ot(t) { this.G(), (this.q = t); } Je(t) { if (!this.B) return !1; if (!t) { const t = this.B.Oe(); if (null == t) return !1; let s = !1; if (!isNaN(t)) { if (-1 === t) return N.info("Feature flag refreshes not allowed"), !1; s = new Date().getTime() >= (this.qe || 0) + 1e3 * t; } if (!s) return ( N.info(`Feature flag refreshes were rate limited to ${t} seconds`), !1 ); } return this.B.ke(); } Qe() { var t; return ( (null === (t = this.C) || void 0 === t ? void 0 : t.ft(s.gt.Ve)) || null ); } We() { var t, e; null === (t = this.C) || void 0 === t || t.Bt(s.gt.Ve, null === (e = this.T) || void 0 === e ? void 0 : e.St()); } Ye() { var t; const s = null === (t = this.T) || void 0 === t ? void 0 : t.St(), e = this.Qe(); return null == e || s === e; } Be() { if (!this.C) return; const t = {}; for (const s of this.De) { const e = s.bt(); t[s.id] = e; } this.C.Bt(s.gt.Xe, t), this.C.Bt(s.gt.Ze, this.qe), this.We(); } changeUser() { this.G(); } clearData() { this.G(); } }