@adyen/adyen-platform-experience-web
Version:

57 lines (56 loc) • 1.86 kB
JavaScript
import { createWatchListCurrentStateRecord as W, createWatchListSubscriptionEventCallbacks as b } from "./helpers.js";
import { UNSUBSCRIBE_TOKEN as d } from "./constants.js";
import { sameValue as R } from "../../../utils/value/compare.js";
import { getMappedValue as _ } from "../../../utils/collection/main.js";
import { struct as w } from "../../../utils/struct/main.js";
import { getter as S, enumerable as c } from "../../../utils/struct/property.js";
import { isFunction as C } from "../../../utils/value/is.js";
import { noop as y } from "../../../utils/common.js";
const M = (u) => u === d, V = (u) => {
let e;
const g = W(u), f = b(), n = /* @__PURE__ */ new Map(), a = /* @__PURE__ */ new WeakMap(), m = () => Object.freeze({ ...g }), i = () => n.size === 0, l = (t) => {
if (i()) return;
if (M(t)) {
const s = [];
return n.forEach((r, o) => {
s.push(o);
const h = a.get(o);
for (; r--; ) h == null || h();
}), s.forEach((r) => r(t)), !0;
}
const p = e;
e = m();
for (const s of Object.keys(e))
if (!R(e[s], p[s]))
return n.forEach((r, o) => o(e)), !0;
return !1;
}, E = (t) => {
if (!C(t)) return y;
const p = _(t, a, () => {
let r = () => {
const o = n.get(t) || 0;
o === 1 ? (r = void 0, n.delete(t), a.delete(t), i() && (e = void 0, f.idle())) : o > 1 && n.set(t, o - 1);
};
return () => {
r == null || r();
};
}), s = i();
return n.set(
t,
(n.get(t) || 0) + 1
), s && (e = m(), f.resume()), t(e), p;
};
return w({
idle: S(i),
on: c(f),
cancelSubscriptions: c(() => l(d)),
requestNotification: c(() => l()),
snapshot: S(() => e ?? m()),
subscribe: c(E)
});
};
export {
V as createWatchlist,
V as default,
M as isWatchlistUnsubscribeToken
};