@braze/web-sdk
Version:
Braze SDK for web sites and other JS platforms.
315 lines (314 loc) • 9.7 kB
JavaScript
import t from "../common/base-provider.js";
import { newInAppMessageFromJson as ut } from "../InAppMessage/in-app-message-factory.js";
import ue from "../models/braze-event.js";
import {
isArray as D,
isEqual as ii,
validateValueIsFromEnum as ta,
} from "../util/code-utils.js";
import { STORAGE_KEYS as s } from "../managers/storage-manager.js";
import wt from "../InAppMessage/models/templated-in-app-message.js";
import pt from "./models/trigger.js";
import ot from "./models/trigger-events.js";
import { logger as E } from "../../shared-lib/index.js";
export default class gr extends t {
constructor(t, i, s, e, r) {
super(),
(this.tg = t),
(this.ws = i),
(this.j = s),
(this.Cn = e),
(this.ig = r),
(this.tg = t),
(this.ws = i),
(this.j = s),
(this.Cn = e),
(this.ig = r),
(this.sg = []),
(this.eg = []),
(this.hg = null),
(this.ng = {}),
(this.og = {}),
(this.triggers = []),
(this.lg = 0),
this.ag(),
this.gg();
}
fg() {
if (this.j) {
(this.hg = this.j.lt(s.ct.iE) || this.hg),
(this.ng = this.j.lt(s.ct.EE) || this.ng),
(this.og = this.j.lt(s.ct.aE) || this.og);
for (let t = 0; t < this.triggers.length; t++) {
const i = this.triggers[t];
i.id && null != this.og[i.id] && i.ad(this.og[i.id]);
}
}
}
ag() {
if (!this.j) return;
this.lg = this.j.lt(s.ct.nE) || 0;
const t = this.j.lt(s.ct.oE) || [],
i = [];
for (let s = 0; s < t.length; s++) i.push(pt.qn(t[s]));
(this.triggers = i), this.fg();
}
gg() {
const t = this,
i = function (i, s, e, r, h) {
return function () {
t.cg(i, s, e, r, h);
};
},
e = {};
for (const t of this.triggers) t.id && (e[t.id] = t);
let r = !1;
for (let t = 0; t < this.triggers.length; t++) {
const s = this.triggers[t];
if (s.id && null != this.ng[s.id]) {
const t = this.ng[s.id],
h = [];
for (let r = 0; r < t.length; r++) {
const n = t[r],
o = s.ud(n.Er || 0);
if (o > 0) {
let t, r;
h.push(n),
null != n.ug && (t = n.ug),
null != n.dg && ue.gE(n.dg) && (r = ue.qn(n.dg));
const l = [];
if (n.pg && D(n.pg))
for (let t = 0; t < n.pg.length; t++) {
const i = e[n.pg[t]];
null != i && l.push(i);
}
this.eg.push(window.setTimeout(i(s, n.Er || 0, t, r, l), o));
}
}
this.ng[s.id].length > h.length &&
((this.ng[s.id] = h),
(r = !0),
0 === this.ng[s.id].length && delete this.ng[s.id]);
}
}
r && this.j && this.j.ft(s.ct.EE, this.ng);
}
mg() {
if (!this.j) return;
const t = [];
for (let i = 0; i < this.triggers.length; i++)
t.push(this.triggers[i].dt());
(this.lg = new Date().valueOf()),
this.j.ft(s.ct.oE, t),
this.j.ft(s.ct.nE, this.lg);
}
bg() {
if (!this.j) return;
(this.j.lt(s.ct.nE) || 0) > this.lg ? this.ag() : this.fg();
}
N(t) {
let i = !1;
if (null != t && t.triggers) {
this.ig.Lr(), this.fg();
const e = {},
r = {};
this.triggers = [];
for (let s = 0; s < t.triggers.length; s++) {
const h = pt.fromJson(t.triggers[s]);
if (h) {
h.id &&
null != this.og[h.id] &&
(h.ad(this.og[h.id]), (e[h.id] = this.og[h.id])),
h.id && null != this.ng[h.id] && (r[h.id] = this.ng[h.id]);
for (let t = 0; t < h.sd.length; t++)
if (h.sd[t].kc(ot.qs, null)) {
i = !0;
break;
}
this.triggers.push(h);
}
}
ii(this.og, e) || ((this.og = e), this.j && this.j.ft(s.ct.aE, this.og)),
ii(this.ng, r) ||
((this.ng = r), this.j && this.j.ft(s.ct.EE, this.ng)),
this.mg(),
i &&
(E.info("Trigger with test condition found, firing test."),
this.ve(ot.qs)),
this.ve(ot.OPEN);
const h = this.sg;
let n;
this.sg = [];
for (let t = 0; t < h.length; t++)
(n = Array.prototype.slice.call(h[t])), this.ve(...n);
}
}
cg(t, i, s, e, r) {
const h = (e) => {
this.fg();
const r = new Date().valueOf();
t.dd(i) ||
(!1 === navigator.onLine && t.type === pt._r.Br && e.imageUrl
? E.info(
`Not showing ${t.type} trigger action ${t.id} due to offline state.`,
)
: t.nd(r) && this.wg(t, r, s)
? 0 === this.ws.sc()
? E.info(
`Not displaying trigger ${t.id} because neither automaticallyShowInAppMessages() nor subscribeToInAppMessage() were called.`,
)
: (this.ws.L([e]), this.yg(t, r))
: E.info(
`Not displaying trigger ${t.id} because display time fell outside of the acceptable time window.`,
));
},
n = () => {
this.fg();
const h = r.pop();
if (null != h)
if ((this.Tg(h, i, s, e, r), h.dd(i))) {
let t = `Server aborted in-app message display, but the timeout on fallback trigger ${h.id} has already elapsed.`;
r.length > 0 && (t += " Continuing to fall back."), E.info(t), n();
} else {
E.info(
`Server aborted in-app message display. Falling back to lower priority ${h.type} trigger action ${t.id}.`,
);
const n = 1e3 * h.ed - (new Date().valueOf() - i);
n > 0
? this.eg.push(
window.setTimeout(() => {
this.cg(h, i, s, e, r);
}, n),
)
: this.cg(h, i, s, e, r);
}
};
let o, l, a;
switch (t.type) {
case pt._r.Br:
if (((o = ut(t.data)), null == o)) {
E.error(
`Could not parse trigger data for trigger ${t.id}, ignoring.`,
);
break;
}
if (((l = this.ig.Ki(o)), l)) {
E.error(l), n();
break;
}
h(o);
break;
case pt._r.md:
if (((a = wt.fromJson(t.data, h, n, i, t.Gr || 0)), null == a)) {
E.error(
`Could not parse trigger data for trigger ${t.id}, ignoring.`,
);
break;
}
this.ig.Zi(a, s, e);
break;
default:
E.error(`Trigger ${t.id} was of unexpected type ${t.type}, ignoring.`);
}
}
ve(t, i = null, s) {
if (!ta(ot, t, "Cannot fire trigger action.", "TriggerEvents")) return;
if (this.Cn && this.Cn.Yl())
return (
E.info(
"Trigger sync is currently in progress, awaiting sync completion before firing trigger event.",
),
void this.sg.push(arguments)
);
this.bg();
const e = new Date().valueOf(),
r = e - (this.hg || 0);
let h = !0,
n = !0;
const o = [];
for (let s = 0; s < this.triggers.length; s++) {
const r = this.triggers[s],
l = e + 1e3 * r.ed;
if (
r.nd(l) &&
(null == r.startTime || r.startTime.valueOf() <= e) &&
(null == r.endTime || r.endTime.valueOf() >= e)
) {
let s = !1;
for (let e = 0; e < r.sd.length; e++)
if (r.sd[e].kc(t, i)) {
s = !0;
break;
}
s && ((h = !1), this.wg(r, l, t) && ((n = !1), o.push(r)));
}
}
if (h)
return void E.info(
`Trigger event ${t} did not match any trigger conditions.`,
);
if (n)
return void E.info(
`Ignoring ${t} trigger event because a trigger was displayed ${
r / 1e3
}s ago.`,
);
o.sort((t, i) => t.priority - i.priority);
const l = o.pop();
null != l &&
(E.info(
`Firing ${l.type} trigger action ${l.id} from trigger event ${t}.`,
),
this.Tg(l, e, t, s, o),
0 === l.ed
? this.cg(l, e, t, s, o)
: this.eg.push(
window.setTimeout(() => {
this.cg(l, e, t, s, o);
}, 1e3 * l.ed),
));
}
changeUser(t = !1) {
if (((this.triggers = []), this.j && this.j.Nt(s.ct.oE), !t)) {
(this.sg = []), (this.hg = null), (this.og = {}), (this.ng = {});
for (let t = 0; t < this.eg.length; t++) clearTimeout(this.eg[t]);
(this.eg = []),
this.j && (this.j.Nt(s.ct.iE), this.j.Nt(s.ct.aE), this.j.Nt(s.ct.EE));
}
}
clearData() {
(this.triggers = []), (this.hg = null), (this.og = {}), (this.ng = {});
for (let t = 0; t < this.eg.length; t++) clearTimeout(this.eg[t]);
this.eg = [];
}
wg(t, i, s) {
if (null == this.hg) return !0;
if (s === ot.qs)
return (
E.info(
"Ignoring minimum interval between trigger because it is a test type.",
),
!0
);
let e = t.hd;
return null == e && (e = this.tg), i - this.hg >= 1e3 * e;
}
Tg(t, i, e, r, h) {
this.fg(), t.id && (this.ng[t.id] = this.ng[t.id] || []);
const n = {};
let o;
(n.Er = i), (n.ug = e), null != r && (o = r.dt()), (n.dg = o);
const l = [];
for (const t of h) t.id && l.push(t.id);
(n.pg = l),
t.id && this.ng[t.id].push(n),
this.j && this.j.ft(s.ct.EE, this.ng);
}
yg(t, i) {
this.fg(),
t.ad(i),
(this.hg = i),
t.id && (this.og[t.id] = i),
this.j && (this.j.ft(s.ct.iE, i), this.j.ft(s.ct.aE, this.og));
}
}