@formant/data-sdk 1.17.0 → 1.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -851,8 +851,6 @@ function filterDataByTime(e, t, n) {
851
851
  )
852
852
  })).filter(({ points: l }) => l.length > 0);
853
853
  }
854
- function fork(e) {
855
- }
856
854
  class StoreCache {
857
855
  constructor({
858
856
  capacity: t,
@@ -861,20 +859,21 @@ class StoreCache {
861
859
  be(this, "entries", /* @__PURE__ */ new Map());
862
860
  be(this, "metadata", /* @__PURE__ */ new Map());
863
861
  be(this, "capacity");
864
- be(this, "timeout");
865
- this.capacity = t || 1e4, this.timeout = n || duration.minute;
862
+ be(this, "staleIntervalMs");
863
+ this.capacity = t || 1e4, this.staleIntervalMs = n || duration.minute;
866
864
  }
867
865
  get(t, n) {
868
- const r = this.keyToCacheKey(t), o = this.entries.get(r), l = this.metadata.get(r);
869
- return (o === void 0 || l && (l == null ? void 0 : l.expiration.getTime()) < Date.now()) && !(l != null && l.generating) && n && this.generate(t, n()), o === void 0 && l && l.lastValue !== void 0 ? l.lastValue : o;
866
+ const r = this.keyToCacheKey(t);
867
+ return this.isStale(r) && !this.isGenerating(r) && n && this.generate(t, n), this.entries.get(r);
870
868
  }
871
869
  set(t, n) {
872
870
  const r = this.keyToCacheKey(t);
873
871
  this.metadata.set(r, {
874
872
  generating: !1,
875
- expiration: new Date(Date.now() + this.timeout),
876
- lastValue: n
877
- }), this.entries.set(r, n), this.metadata.size > this.capacity && this.deleteOldestEntry();
873
+ staleAt: performance.now() + this.staleIntervalMs
874
+ });
875
+ const o = this.entries.get(r);
876
+ JSON.stringify(o) === JSON.stringify(n) || (this.entries.set(r, n), this.enforceMaxSize());
878
877
  }
879
878
  clear() {
880
879
  this.entries.clear(), [...this.metadata.values()].forEach((t) => t.generating = !1);
@@ -885,26 +884,34 @@ class StoreCache {
885
884
  keyToCacheKey(t) {
886
885
  return JSON.stringify(t);
887
886
  }
888
- deleteOldestEntry() {
889
- if (this.metadata.size < 1)
890
- return;
891
- const [t] = [...this.metadata.entries()].reduce(
892
- ([n, r], [o, l]) => l.expiration.getTime() < r.expiration.getTime() ? [o, l] : [n, r]
893
- );
894
- this.clearKey(t);
887
+ enforceMaxSize() {
888
+ for (; this.metadata.size > this.capacity && this.metadata.size > 0; ) {
889
+ const [t] = [...this.metadata.entries()].reduce(
890
+ ([n, r], [o, l]) => l.staleAt < r.staleAt ? [o, l] : [n, r]
891
+ );
892
+ this.clearKey(t);
893
+ }
894
+ }
895
+ isStale(t) {
896
+ const n = this.metadata.get(t);
897
+ return n ? (n == null ? void 0 : n.staleAt) < performance.now() : !0;
898
+ }
899
+ isGenerating(t) {
900
+ const n = this.metadata.get(t);
901
+ return n ? n.generating : !1;
895
902
  }
896
903
  generate(t, n) {
897
- const r = this.keyToCacheKey(t), o = this.metadata.get(r) || {};
904
+ const r = this.keyToCacheKey(t), o = this.metadata.get(r) || {}, l = n().then((u) => {
905
+ const s = this.metadata.get(r);
906
+ return (s == null ? void 0 : s.generating) !== l || this.set(t, u), u;
907
+ }).catch((u) => {
908
+ throw this.metadata.delete(r), u;
909
+ });
898
910
  this.metadata.set(r, {
899
911
  ...o,
900
- generating: !0,
901
- expiration: new Date(Date.now() + this.timeout)
902
- }), setTimeout(() => {
903
- n.then((l) => {
904
- const u = this.metadata.get(r);
905
- !(u != null && u.generating) || this.set(t, l);
906
- });
907
- }, 0);
912
+ generating: l,
913
+ staleAt: performance.now() + this.staleIntervalMs
914
+ });
908
915
  }
909
916
  }
910
917
  async function queryTelemetry(e) {
@@ -24961,6 +24968,8 @@ class KeyValue {
24961
24968
  function stringToArrayBuffer(e) {
24962
24969
  return Uint8Array.from(base64Exports.decode(e), (t) => t.charCodeAt(0));
24963
24970
  }
24971
+ function fork(e) {
24972
+ }
24964
24973
  function browser() {
24965
24974
  const { userAgent: e } = navigator;
24966
24975
  return e ? e.includes("Firefox/") ? "Firefox" : e.includes("Edg/") ? "Edge" : e.includes("Chrome/") ? "Chrome" : e.includes("Safari/") ? "Safari" : e.includes("MSIE/") || e.includes("Trident/") ? "IE" : "Other" : "Other";