@stripchatdev/ext-helper 0.1.43 → 0.1.45

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.
@@ -0,0 +1,84 @@
1
+ const w = (t) => t === "request" || t === "update", g = (t) => t === "snapshot", h = (t, e) => !(t == null || typeof t != "object" || Array.isArray(t) || !("channel" in t) || t.channel !== e || !("type" in t) || !("data" in t)), S = (t, e) => h(t, e) ? w(t.type) : !1, C = (t, e) => h(t, e) ? g(t.type) : !1, p = (t) => t != null && typeof t == "object" && !Array.isArray(t), q = (t, e) => async (n, s) => {
2
+ await t.makeRequest("v1.ext.whisper.local", {
3
+ data: {
4
+ channel: e,
5
+ data: s,
6
+ type: n
7
+ }
8
+ });
9
+ }, M = (t, e) => async (n, s) => {
10
+ await t.makeRequest("v1.ext.whisper.local", {
11
+ data: {
12
+ channel: e,
13
+ data: s,
14
+ type: n
15
+ }
16
+ });
17
+ }, l = /* @__PURE__ */ new Set(), d = /* @__PURE__ */ new Map(), V = ({
18
+ channel: t,
19
+ extHelper: e,
20
+ initialState: n
21
+ }) => {
22
+ if (l.has(t))
23
+ throw new Error(`A state host already exists for channel "${t}".`);
24
+ l.add(t);
25
+ let s = n;
26
+ const a = M(e, t), c = async (i) => {
27
+ s = { ...s, ...i }, await a("snapshot", s);
28
+ }, o = (i) => {
29
+ if (S(i, t)) {
30
+ if (i.type === "request") {
31
+ a("snapshot", s).catch(() => {
32
+ });
33
+ return;
34
+ }
35
+ if (i.type === "update" && p(i.data)) {
36
+ c(i.data).catch(() => {
37
+ });
38
+ return;
39
+ }
40
+ }
41
+ }, u = () => {
42
+ l.delete(t), e.unsubscribe("v1.ext.whispered.local", o);
43
+ };
44
+ return e.subscribe("v1.ext.whispered.local", o), {
45
+ unsubscribe: u,
46
+ getState: () => s,
47
+ setState: c
48
+ };
49
+ }, x = ({
50
+ channel: t,
51
+ extHelper: e
52
+ }) => {
53
+ const n = d.get(t);
54
+ if (n)
55
+ return n;
56
+ let s = {
57
+ isLoading: !0,
58
+ state: null
59
+ };
60
+ const a = /* @__PURE__ */ new Set(), c = q(e, t), o = () => a.forEach((r) => r()), u = async (r, b) => {
61
+ try {
62
+ await c(r, b);
63
+ } catch (y) {
64
+ throw r === "request" && s.isLoading && (s = { ...s, isLoading: !1 }, o()), y;
65
+ }
66
+ }, i = (r) => {
67
+ !C(r, t) || !p(r.data) || (s = {
68
+ isLoading: !1,
69
+ state: r.data
70
+ }, o());
71
+ }, f = {
72
+ getSnapshot: () => s,
73
+ setState: (r) => u("update", r),
74
+ subscribe: (r) => (a.add(r), a.size === 1 && (e.subscribe("v1.ext.whispered.local", i), u("request", null).catch(() => {
75
+ })), () => {
76
+ a.delete(r), a.size === 0 && (e.unsubscribe("v1.ext.whispered.local", i), d.get(t) && d.delete(t));
77
+ })
78
+ };
79
+ return d.set(t, f), f;
80
+ };
81
+ export {
82
+ x as createSlotStateClient,
83
+ V as createSlotStateHost
84
+ };