UNPKG

@braze/web-sdk

Version:

Braze SDK for web sites and other JS platforms.

80 lines (79 loc) 2.38 kB
import { STORAGE_KEYS as s } from "../managers/storage-manager.js"; import Ie from "../models/braze-event.js"; import _t from "../models/identifier.js"; import { convertMsToSeconds as X } from "../util/date-utils.js"; import { logger as E, Guid as V, EventTypes as p, } from "../../shared-lib/index.js"; import { DEFAULT_MINIMUM_SESSION_TIMEOUT_IN_MS as ts } from "./constants.js"; export default class Xt { constructor(s, t, i, e) { (this.C = s), (this.Ss = t), (this.h = i), (this.tm = e), (this.C = s), (this.Ss = t), (this.h = i); const n = this.h.gc(); (this.im = n ? 1e3 * n : ts), (null == e || isNaN(e)) && (e = 1800), e < this.im / 1e3 && E.info( "Specified session timeout of " + e + "s is too small, using the minimum session timeout of " + this.im / 1e3 + "s instead.", ), (this.tm = Math.max(e, this.im / 1e3)); } nm(s, t) { return new Ie(this.Ss.getUserId(), p.hm, s, t.Tu, { d: X(s - t.lm) }); } al() { return this.C.$u(s.Ou.um); } kt() { const t = this.C.$u(s.Ou.um); return null == t ? null : t.Tu; } am() { const t = new Date().valueOf(), i = this.h.hc(); if (null == i) return !1; const e = this.C.Rt(s.Tt.dm), n = null == e || t - e > 1e3 * i; return n && this.C.It(s.Tt.dm, t), n; } ul(s, t) { return null == t || null == t.fm || (!(s - t.lm < this.im) && t.fm < s); } el() { const t = new Date().valueOf(), i = t + 1e3 * this.tm, e = this.C.$u(s.Ou.um); if (this.ul(t, e)) { let n = "Generating session start event with time " + t; if (null != e) { let s = e.pm; s - e.lm < this.im && (s = e.lm + this.im), this.C.gm(this.nm(s, e)), (n += " (old session ended " + s + ")"); } (n += ". Will expire " + i.valueOf()), E.info(n); const r = new _t(V.de(), i); this.C.gm(new Ie(this.Ss.getUserId(), p.wm, t, r.Tu)), this.C.Iu(s.Ou.um, r); return null == this.C.Rt(s.Tt.dm) && this.C.It(s.Tt.dm, t), r.Tu; } if (null != e) return (e.pm = t), (e.fm = i), this.C.Iu(s.Ou.um, e), e.Tu; } Sm() { const t = this.C.$u(s.Ou.um); null != t && (this.C.jm(s.Ou.um), this.C.gm(this.nm(new Date().valueOf(), t))); } }