@blogmcp/sdk 0.10.1 → 0.12.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.
Files changed (59) hide show
  1. package/dist/community/channels.d.ts +3 -2
  2. package/dist/community/client.d.ts +130 -3
  3. package/dist/community/client.js +232 -161
  4. package/dist/community/forum.d.ts +82 -0
  5. package/dist/community/forum.js +96 -0
  6. package/dist/community/index.d.ts +14 -7
  7. package/dist/community/index.js +185 -121
  8. package/dist/community/links.d.ts +38 -0
  9. package/dist/community/links.js +98 -71
  10. package/dist/community/messages.cs.d.ts +164 -3
  11. package/dist/community/messages.cs.js +184 -3
  12. package/dist/community/messages.d.ts +19 -1
  13. package/dist/community/messages.en.d.ts +154 -3
  14. package/dist/community/messages.en.js +167 -3
  15. package/dist/community/messages.js +37 -28
  16. package/dist/community/outcome.d.ts +8 -3
  17. package/dist/community/outcome.js +61 -20
  18. package/dist/community/poll.d.ts +28 -4
  19. package/dist/community/poll.js +70 -47
  20. package/dist/community/realtime/index.d.ts +19 -3
  21. package/dist/community/realtime/index.js +175 -145
  22. package/dist/community/routes.d.ts +36 -1
  23. package/dist/community/routes.js +51 -21
  24. package/dist/community/server/bff.d.ts +25 -2
  25. package/dist/community/server/bff.js +135 -118
  26. package/dist/community/server/host-routes.d.ts +3 -0
  27. package/dist/community/server/host-routes.js +17 -0
  28. package/dist/community/server/index.d.ts +5 -1
  29. package/dist/community/shapes.d.ts +649 -7
  30. package/dist/community/shapes.js +92 -61
  31. package/dist/community/store.d.ts +27 -0
  32. package/dist/community/store.js +84 -80
  33. package/dist/community/topic.d.ts +173 -0
  34. package/dist/community/topic.js +225 -0
  35. package/dist/nuxt/composables/useAsyncKb.d.ts +10 -4
  36. package/dist/packages/mdx-render-vue/dist/render/gallery/GalleryLightbox.vue.js +2 -2
  37. package/dist/packages/mdx-render-vue/dist/render/tripmap/TripMapDefault.vue.js +36 -36
  38. package/package.json +3 -3
  39. package/src/community/channels.ts +3 -2
  40. package/src/community/client.ts +212 -7
  41. package/src/community/forum.ts +209 -0
  42. package/src/community/index.ts +146 -2
  43. package/src/community/links.ts +64 -4
  44. package/src/community/messages.cs.ts +185 -3
  45. package/src/community/messages.en.ts +168 -3
  46. package/src/community/messages.ts +25 -2
  47. package/src/community/outcome.ts +47 -1
  48. package/src/community/poll.ts +79 -17
  49. package/src/community/realtime/index.ts +124 -48
  50. package/src/community/routes.ts +42 -1
  51. package/src/community/server/bff.ts +89 -30
  52. package/src/community/server/host-routes.ts +20 -0
  53. package/src/community/server/index.ts +5 -1
  54. package/src/community/shapes.ts +677 -6
  55. package/src/community/store.ts +25 -7
  56. package/src/community/topic.ts +521 -0
  57. package/src/core/markdown.ts +1 -1
  58. package/src/nuxt/composables/useAsyncKb.ts +11 -4
  59. package/src/nuxt/nuxt-imports.d.ts +2 -2
@@ -1,70 +1,93 @@
1
- import { COMMUNITY_HEAD_ARTICLES_MAX as p } from "./routes.js";
2
- const C = 2e4, I = 5 * 6e4, w = 6e4, O = (a, o) => a.lastSeq === o.lastSeq && a.count === o.count && a.lastChangeAt === o.lastChangeAt;
3
- function F(a, o, n) {
4
- const l = Math.max(1e3, n.intervalMs ?? C), g = [...new Set((Array.isArray(o) ? o : []).filter((e) => typeof e == "string" && e !== ""))].slice(
5
- 0,
6
- p
7
- ), m = n.visible ?? (() => typeof document > "u" || document.visibilityState !== "hidden"), v = /* @__PURE__ */ new Map();
8
- let i = null, s = null, f = 0, d = !1, r = !1;
9
- const c = (e) => {
10
- r || (i && clearTimeout(i), i = setTimeout(() => {
11
- E();
12
- }, e));
13
- }, A = (e) => {
14
- c(e), d = !0;
1
+ import { COMMUNITY_HEAD_ARTICLES_MAX as C, COMMUNITY_HEAD_TOPICS_MAX as S } from "./routes.js";
2
+ const E = 2e4, k = 5 * 6e4, w = 6e4, x = (t, o) => t.lastSeq === o.lastSeq && t.count === o.count && t.lastChangeAt === o.lastChangeAt;
3
+ function D(t, o, n) {
4
+ return b(
5
+ {
6
+ ask: (e, a) => t.discussions.head({ articleIds: e }, a),
7
+ keyOf: (e) => typeof e?.articleId == "string" ? e.articleId.toLowerCase() : null,
8
+ max: C,
9
+ stopsOn: ["surfaceOff"]
10
+ },
11
+ o,
12
+ n
13
+ );
14
+ }
15
+ function F(t, o, n) {
16
+ return b(
17
+ {
18
+ ask: (e, a) => t.forum.topics.head({ conversationIds: e }, a),
19
+ keyOf: (e) => typeof e?.conversationId == "string" ? e.conversationId.toLowerCase() : null,
20
+ max: S,
21
+ stopsOn: ["surfaceOff", "membersOnly"]
22
+ },
23
+ o,
24
+ n
25
+ );
26
+ }
27
+ function b(t, o, n) {
28
+ const e = Math.max(1e3, n.intervalMs ?? E), a = [...new Set((Array.isArray(o) ? o : []).filter((i) => typeof i == "string" && i !== ""))].slice(0, t.max), A = n.visible ?? (() => typeof document > "u" || document.visibilityState !== "hidden"), g = /* @__PURE__ */ new Map();
29
+ let s = null, c = null, f = 0, y = !1, l = !1;
30
+ const u = (i) => {
31
+ l || (s && clearTimeout(s), s = setTimeout(() => {
32
+ L();
33
+ }, i));
34
+ }, p = (i) => {
35
+ u(i), y = !0;
15
36
  }, M = () => {
16
- !r && !s && !d && c(0);
37
+ !l && !c && !y && u(0);
17
38
  };
18
- async function E() {
19
- if (i = null, d = !1, r || s) return;
20
- if (!m()) {
21
- c(l);
39
+ async function L() {
40
+ if (s = null, y = !1, l || c) return;
41
+ if (!A()) {
42
+ u(e);
22
43
  return;
23
44
  }
24
- const e = new AbortController();
25
- s = e;
26
- const T = { signal: e.signal }, t = await a.discussions.head({ articleIds: g }, T);
27
- if (s = null, !r) {
28
- if (t.ok) {
45
+ const i = new AbortController();
46
+ c = i;
47
+ const T = { signal: i.signal }, r = await t.ask(a, T);
48
+ if (c = null, !l) {
49
+ if (r.ok) {
29
50
  f = 0;
30
- const y = [];
31
- for (const u of Array.isArray(t.value.heads) ? t.value.heads : []) {
32
- if (typeof u?.articleId != "string") continue;
33
- const _ = u.articleId.toLowerCase(), S = v.get(_);
34
- v.set(_, u), (!S || !O(S, u)) && y.push(u);
51
+ const h = [];
52
+ for (const d of Array.isArray(r.value.heads) ? r.value.heads : []) {
53
+ const v = t.keyOf(d);
54
+ if (v === null) continue;
55
+ const _ = g.get(v);
56
+ g.set(v, d), (!_ || !x(_, d)) && h.push(d);
35
57
  }
36
- if (y.length > 0)
58
+ if (h.length > 0)
37
59
  try {
38
- n.onChange(y);
60
+ n.onChange(h);
39
61
  } catch {
40
62
  }
41
- c(l);
63
+ u(e);
42
64
  return;
43
65
  }
44
66
  try {
45
- n.onFailure?.(t);
67
+ n.onFailure?.(r);
46
68
  } catch {
47
69
  }
48
- if (t.code === "surfaceOff") return h();
49
- if (t.code === "fast" || t.code === "quota") {
50
- A(Math.max(l, t.retryAfterS !== void 0 ? t.retryAfterS * 1e3 : w));
70
+ if (t.stopsOn.includes(r.code)) return m();
71
+ if (r.code === "fast" || r.code === "quota") {
72
+ p(Math.max(e, r.retryAfterS !== void 0 ? r.retryAfterS * 1e3 : w));
51
73
  return;
52
74
  }
53
- f = Math.min(I, (f || l) * 2), A(f);
75
+ f = Math.min(k, (f || e) * 2), p(f);
54
76
  }
55
77
  }
56
- const b = () => {
57
- m() && M();
58
- }, L = () => h();
59
- function h() {
60
- r || (r = !0, i && clearTimeout(i), i = null, s?.abort(), s = null, typeof document < "u" && document.removeEventListener("visibilitychange", b), n.signal?.removeEventListener("abort", L));
78
+ const O = () => {
79
+ A() && M();
80
+ }, I = () => m();
81
+ function m() {
82
+ l || (l = !0, s && clearTimeout(s), s = null, c?.abort(), c = null, typeof document < "u" && document.removeEventListener("visibilitychange", O), n.signal?.removeEventListener("abort", I));
61
83
  }
62
- return g.length === 0 || n.signal?.aborted ? (r = !0, { stop: () => {
84
+ return a.length === 0 || n.signal?.aborted ? (l = !0, { stop: () => {
63
85
  }, now: () => {
64
- } }) : (typeof document < "u" && document.addEventListener("visibilitychange", b), n.signal?.addEventListener("abort", L, { once: !0 }), c(l), { stop: h, now: M });
86
+ } }) : (typeof document < "u" && document.addEventListener("visibilitychange", O), n.signal?.addEventListener("abort", I, { once: !0 }), u(e), { stop: m, now: M });
65
87
  }
66
88
  export {
67
- C as POLL_INTERVAL_MS,
68
- I as POLL_MAX_BACKOFF_MS,
69
- F as pollDiscussionHead
89
+ E as POLL_INTERVAL_MS,
90
+ k as POLL_MAX_BACKOFF_MS,
91
+ D as pollDiscussionHead,
92
+ F as pollTopicHead
70
93
  };
@@ -9,6 +9,11 @@
9
9
  * const session = createRealtimeSession({ client, store }) // after the first discussions/list
10
10
  * // on unmount: session.stop()
11
11
  *
12
+ * M3 (m3-contracts.md §3.5, §5.2): a forum topic's store (createTopicStore) works the same way — the session re-reads
13
+ * the topic (refreshTopic) where it re-reads a discussion, and falls back to pollTopicHead. The channel is the topic's
14
+ * conversation's; kb-api mints its token only for an onboarded member or an interactive team session (a visitor's
15
+ * host session is refused `notFound`, so it polls — and `forum/topics/head` stops that poller with `membersOnly`).
16
+ *
12
17
  * What it does:
13
18
  * - asks `me/realtime/token` for a connection token AND the URL to connect to — the only source of that URL (this
14
19
  * entry builds none from configuration: the endpoints are `${url}/connection/websocket`, `${url}/connection/
@@ -37,6 +42,7 @@
37
42
  */
38
43
  import type { CommunityClient } from '../client.js';
39
44
  import { type DiscussionStore } from '../store.js';
45
+ import { type TopicStore } from '../topic.js';
40
46
  /** A Centrifuge subscription, as far as this entry uses it. */
41
47
  export interface RealtimeSubscriptionLike {
42
48
  on(event: 'publication' | 'subscribed' | 'unsubscribed' | 'error', listener: (ctx: any) => void): unknown;
@@ -72,9 +78,8 @@ export interface CentrifugeModuleLike {
72
78
  UnauthorizedError?: new (message?: string) => Error;
73
79
  }
74
80
  export type RealtimeMode = 'connecting' | 'live' | 'polling' | 'stopped';
75
- export interface RealtimeSessionOptions {
76
- client: Pick<CommunityClient, 'discussions' | 'me' | 'realtime'>;
77
- store: DiscussionStore;
81
+ /** What every session takes besides its client and store. */
82
+ export interface RealtimeSessionCommonOptions {
78
83
  /**
79
84
  * `disc:<conversationId>`; default the store's conversation (a discussion without one polls until it has one). The id
80
85
  * is used in lower case (kb-api publishes to, and mints tokens for, the lower-case channel).
@@ -93,6 +98,17 @@ export interface RealtimeSessionOptions {
93
98
  /** Aborting it stops the session. */
94
99
  signal?: AbortSignal;
95
100
  }
101
+ /** An article's discussion: its store (createDiscussionStore) and a client that reads discussions. */
102
+ export interface DiscussionRealtimeSessionOptions extends RealtimeSessionCommonOptions {
103
+ client: Pick<CommunityClient, 'discussions' | 'me' | 'realtime'>;
104
+ store: DiscussionStore;
105
+ }
106
+ /** M3: a forum topic: its store (createTopicStore) and a client that reads the forum. */
107
+ export interface TopicRealtimeSessionOptions extends RealtimeSessionCommonOptions {
108
+ client: Pick<CommunityClient, 'forum' | 'me' | 'realtime'>;
109
+ store: TopicStore;
110
+ }
111
+ export type RealtimeSessionOptions = DiscussionRealtimeSessionOptions | TopicRealtimeSessionOptions;
96
112
  export interface RealtimeSession {
97
113
  readonly mode: RealtimeMode;
98
114
  /** Re-reads the discussion now (coalesced with one already on its way). */
@@ -1,18 +1,19 @@
1
- import { COMMUNITY_CHANNELS as le } from "../channels.js";
2
- import { pollDiscussionHead as J } from "../poll.js";
1
+ import { COMMUNITY_CHANNELS as ie } from "../channels.js";
2
+ import { pollTopicHead as oe, pollDiscussionHead as le } from "../poll.js";
3
3
  import { refreshDiscussion as ce } from "../store.js";
4
- const se = 15e3, ue = 4501, ae = (e) => typeof e == "number" && (e >= 3500 && e < 4e3 || e >= 4500 && e < 5e3), fe = 3, de = 1e3, A = (e) => typeof e == "object" && e !== null && !Array.isArray(e);
5
- function me(e) {
4
+ import { isTopicStore as se, refreshTopic as ae } from "../topic.js";
5
+ const ue = 15e3, fe = 4501, de = (e) => typeof e == "number" && (e >= 3500 && e < 4e3 || e >= 4500 && e < 5e3), me = 3, ve = 1e3, L = (e) => typeof e == "object" && e !== null && !Array.isArray(e);
6
+ function pe(e) {
6
7
  if (typeof e != "string" || e.length > 2048) return null;
7
- let t;
8
+ let i;
8
9
  try {
9
- t = new URL(e);
10
+ i = new URL(e);
10
11
  } catch {
11
12
  return null;
12
13
  }
13
- return t.protocol !== "https:" && t.protocol !== "http:" || t.username || t.password || t.search || t.hash ? null : t.href.replace(/\/+$/, "");
14
+ return i.protocol !== "https:" && i.protocol !== "http:" || i.username || i.password || i.search || i.hash ? null : i.href.replace(/\/+$/, "");
14
15
  }
15
- function pe(e) {
16
+ function be(e) {
16
17
  return {
17
18
  endpoints: [
18
19
  { transport: "websocket", endpoint: `${e.replace(/^http/, "ws")}/connection/websocket` },
@@ -21,195 +22,224 @@ function pe(e) {
21
22
  emulationEndpoint: `${e}/emulation`
22
23
  };
23
24
  }
24
- async function ve(e) {
25
+ let J = !1;
26
+ async function ge(e) {
27
+ let i = null;
25
28
  try {
26
- if (typeof e == "function") return await e();
27
- if (e) return e;
28
- const t = await import("centrifuge");
29
- return t && typeof t.Centrifuge == "function" ? t : null;
29
+ typeof e == "function" ? i = await e() : e ? i = e : i = await import("centrifuge");
30
30
  } catch {
31
- return null;
31
+ i = null;
32
+ }
33
+ return i && typeof i.Centrifuge == "function" ? i : (J || (J = !0, console.warn('[community] live updates are off on this page: the "centrifuge" module could not be loaded (not installed, or not resolvable from the SDK at build time); the discussion polls instead.')), null);
34
+ }
35
+ function we(e) {
36
+ const i = e.languageCode ?? null;
37
+ if (se(e.store)) {
38
+ const { store: r, client: n } = e;
39
+ return {
40
+ key: () => r.getState().conversationId,
41
+ conversationId: () => r.getState().conversationId,
42
+ viewerMember: () => r.getState().viewer.memberId,
43
+ subscribe: (o) => r.subscribe(() => o()),
44
+ applyEvent: (o) => r.applyEvent(o),
45
+ read: (o, s) => ae(r, n, { conversationId: o, languageCode: i }, s),
46
+ poll(o, s) {
47
+ const c = {
48
+ visible: s.visible,
49
+ signal: s.signal,
50
+ onChange: (m) => {
51
+ m.some((g) => r.applyHead(g)) && s.onNewer();
52
+ }
53
+ };
54
+ return s.intervalMs !== void 0 && (c.intervalMs = s.intervalMs), oe(n, [o], c);
55
+ }
56
+ };
32
57
  }
58
+ const { store: l, client: h } = e;
59
+ return {
60
+ key: () => l.getState().articleId,
61
+ conversationId: () => l.getState().conversationId,
62
+ viewerMember: () => l.getState().viewer.memberId,
63
+ subscribe: (r) => l.subscribe(() => r()),
64
+ applyEvent: (r) => l.applyEvent(r),
65
+ read: (r, n) => ce(l, h, { articleId: r, languageCode: i }, n),
66
+ poll(r, n) {
67
+ const o = {
68
+ visible: n.visible,
69
+ signal: n.signal,
70
+ onChange: (s) => {
71
+ s.some((c) => l.applyHead(c)) && n.onNewer();
72
+ }
73
+ };
74
+ return n.intervalMs !== void 0 && (o.intervalMs = n.intervalMs), le(h, [r], o);
75
+ }
76
+ };
33
77
  }
34
- function he(e) {
35
- const { client: t, store: d } = e, R = e.visible ?? (() => typeof document > "u" || document.visibilityState !== "hidden");
36
- let m = "connecting", r = !1, p = null, b = null, l = null, v = null, y = 0, E = null, k = null, M = !1, z = !1, I = null, _ = !1, O = 0, P = d.getState().viewer.memberId, D = null;
37
- const T = new AbortController(), C = { signal: T.signal }, g = (n) => {
38
- if (m !== n) {
39
- m = n;
78
+ function Me(e) {
79
+ const { client: i } = e, l = we(e), h = e.visible ?? (() => typeof document > "u" || document.visibilityState !== "hidden");
80
+ let r = "connecting", n = !1, o = null, s = null, c = null, m = null, g = 0, k = null, E = null, M = !1, V = !1, I = null, A = !1, T = 0, W = l.viewerMember(), R = null;
81
+ const _ = new AbortController(), S = { signal: _.signal }, b = (t) => {
82
+ if (r !== t) {
83
+ r = t;
40
84
  try {
41
- e.onMode?.(n);
85
+ e.onMode?.(t);
42
86
  } catch {
43
87
  }
44
88
  }
45
- }, H = () => d.getState().articleId;
89
+ };
46
90
  async function Q() {
47
- const n = H();
48
- !n || r || await ce(d, t, { articleId: n, languageCode: e.languageCode ?? null }, C);
91
+ const t = l.key();
92
+ !t || n || await l.read(t, S);
49
93
  }
50
- function w() {
51
- return r ? Promise.resolve() : I ? (_ = !0, I) : (I = (async () => {
94
+ function y() {
95
+ return n ? Promise.resolve() : I ? (A = !0, I) : (I = (async () => {
52
96
  try {
53
97
  do
54
- _ = !1, await Q();
55
- while (_ && !r);
98
+ A = !1, await Q();
99
+ while (A && !n);
56
100
  } finally {
57
101
  I = null;
58
102
  }
59
103
  })(), I);
60
104
  }
61
105
  function N() {
62
- k && clearTimeout(k), k = null;
106
+ E && clearTimeout(E), E = null;
63
107
  }
64
- function S() {
65
- E && clearTimeout(E), E = null, N(), M = !1;
66
- const n = p;
67
- p = null;
68
- const u = b;
69
- b = null;
108
+ function O() {
109
+ k && clearTimeout(k), k = null, N(), M = !1;
110
+ const t = o;
111
+ o = null;
112
+ const w = s;
113
+ s = null;
70
114
  try {
71
- u?.unsubscribe(), n?.removeSubscription?.(u), n?.disconnect();
115
+ w?.unsubscribe(), t?.removeSubscription?.(w), t?.disconnect();
72
116
  } catch {
73
117
  }
74
118
  }
75
- function U() {
76
- const n = H();
77
- if (!n || l || r) return;
78
- const u = {
79
- visible: R,
80
- signal: T.signal,
81
- onChange: (L) => {
82
- L.some((h) => d.applyHead(h)) && w();
83
- }
84
- };
85
- e.pollIntervalMs !== void 0 && (u.intervalMs = e.pollIntervalMs), l = J(t, [n], u);
119
+ const q = (t) => l.poll(t, { visible: h, signal: _.signal, intervalMs: e.pollIntervalMs, onNewer: () => {
120
+ y();
121
+ } });
122
+ function D() {
123
+ const t = l.key();
124
+ !t || c || n || (c = q(t));
86
125
  }
87
- function a() {
88
- r || (S(), g("polling"), !l && (U(), !l && !v && (v = d.subscribe(() => U()))));
126
+ function v() {
127
+ n || (O(), b("polling"), !c && (D(), !c && !m && (m = l.subscribe(() => D()))));
89
128
  }
90
- function $(n) {
91
- r || M || n !== p || b === null || (N(), k = setTimeout(() => {
92
- k = null, !(r || n !== p || b === null || m === "live" || m === "polling") && (M = !0, g("polling"), U());
93
- }, se));
129
+ function H(t) {
130
+ n || M || t !== o || s === null || (N(), E = setTimeout(() => {
131
+ E = null, !(n || t !== o || s === null || r === "live" || r === "polling") && (M = !0, b("polling"), D());
132
+ }, ue));
94
133
  }
95
134
  function Z() {
96
- N(), M && (M = !1, l?.stop(), l = null), g("live");
135
+ N(), M && (M = !1, c?.stop(), c = null), b("live");
97
136
  }
98
137
  async function x() {
99
- S(), l?.stop(), l = null, v?.(), v = null, g("connecting");
100
- const n = O, u = await t.me.realtime.token(C);
101
- if (!(r || n !== O)) {
102
- if (!u.ok) return a();
103
- await V(u.value);
138
+ O(), c?.stop(), c = null, m?.(), m = null, b("connecting");
139
+ const t = T, w = await i.me.realtime.token(S);
140
+ if (!(n || t !== T)) {
141
+ if (!w.ok) return v();
142
+ await U(w.value);
104
143
  }
105
144
  }
106
- const j = () => {
145
+ const z = () => {
107
146
  if (typeof e.channel == "string" && e.channel !== "") return e.channel.toLowerCase();
108
- const n = d.getState().conversationId;
109
- return n ? le.discussion(n) : null;
147
+ const t = l.conversationId();
148
+ return t ? ie.discussion(t) : null;
110
149
  };
111
- async function V(n) {
112
- const u = ++O, L = () => u === O && !r, h = await ve(e.centrifuge);
113
- if (!L()) return;
114
- if (!h) return a();
115
- const q = n ? { ok: !0, value: n } : await t.me.realtime.token(C);
116
- if (!L()) return;
117
- if (!q.ok) return a();
118
- const G = me(q.value.url);
119
- if (!G) return a();
120
- const K = (i) => h.UnauthorizedError ? new h.UnauthorizedError(i) : new Error(i), ne = async () => {
121
- const i = await t.me.realtime.token(C);
122
- if (i.ok) return i.value.token;
123
- throw i.code === "realtimeOff" || i.code === "signIn" ? (queueMicrotask(a), K(i.code)) : new Error(i.code);
124
- }, { endpoints: te, emulationEndpoint: re } = pe(G), c = new h.Centrifuge(te, { token: q.value.token, getToken: ne, emulationEndpoint: re, name: "blogmcp-community" });
125
- p = c, c.on("connected", () => {
126
- c === p && (y = 0);
127
- }), c.on("connecting", () => {
128
- c !== p || r || (m === "live" && g("connecting"), $(c));
129
- }), c.on("disconnected", (i) => {
130
- if (c !== p || r) return;
131
- const s = A(i) ? i.code : void 0;
132
- if (s === 1 || s === 3500) {
133
- if (y >= fe) return a();
134
- y += 1, m === "live" && g("connecting"), $(c);
135
- const f = de * 2 ** (y - 1) * (0.5 + Math.random());
136
- E = setTimeout(() => {
137
- E = null, c === p && !r && c.connect();
138
- }, f);
150
+ async function U(t) {
151
+ const w = ++T, B = () => w === T && !n, C = await ge(e.centrifuge);
152
+ if (!B()) return;
153
+ if (!C) return v();
154
+ const P = t ? { ok: !0, value: t } : await i.me.realtime.token(S);
155
+ if (!B()) return;
156
+ if (!P.ok) return v();
157
+ const F = pe(P.value.url);
158
+ if (!F) return v();
159
+ const G = (a) => C.UnauthorizedError ? new C.UnauthorizedError(a) : new Error(a), ne = async () => {
160
+ const a = await i.me.realtime.token(S);
161
+ if (a.ok) return a.value.token;
162
+ throw a.code === "realtimeOff" || a.code === "signIn" ? (queueMicrotask(v), G(a.code)) : new Error(a.code);
163
+ }, { endpoints: te, emulationEndpoint: re } = be(F), u = new C.Centrifuge(te, { token: P.value.token, getToken: ne, emulationEndpoint: re, name: "blogmcp-community" });
164
+ o = u, u.on("connected", () => {
165
+ u === o && (g = 0);
166
+ }), u.on("connecting", () => {
167
+ u !== o || n || (r === "live" && b("connecting"), H(u));
168
+ }), u.on("disconnected", (a) => {
169
+ if (u !== o || n) return;
170
+ const f = L(a) ? a.code : void 0;
171
+ if (f === 1 || f === 3500) {
172
+ if (g >= me) return v();
173
+ g += 1, r === "live" && b("connecting"), H(u);
174
+ const p = ve * 2 ** (g - 1) * (0.5 + Math.random());
175
+ k = setTimeout(() => {
176
+ k = null, u === o && !n && u.connect();
177
+ }, p);
139
178
  return;
140
179
  }
141
- if (s === ue && !z) {
142
- z = !0, x().catch(() => a());
180
+ if (f === fe && !V) {
181
+ V = !0, x().catch(() => v());
143
182
  return;
144
183
  }
145
- ae(s) && a();
184
+ de(f) && v();
146
185
  });
147
- const X = (i) => {
148
- const s = c.newSubscription(i, {
149
- getToken: async (f) => {
150
- const o = await t.realtime.subscribeToken({ channel: f?.channel ?? i }, C);
151
- if (o.ok) return o.value.token;
152
- throw o.code === "notFound" || o.code === "realtimeOff" || o.code === "signIn" || o.code === "surfaceOff" ? (queueMicrotask(a), K(o.code)) : new Error(o.code);
186
+ const X = (a) => {
187
+ const f = u.newSubscription(a, {
188
+ getToken: async (p) => {
189
+ const d = await i.realtime.subscribeToken({ channel: p?.channel ?? a }, S);
190
+ if (d.ok) return d.value.token;
191
+ throw d.code === "notFound" || d.code === "realtimeOff" || d.code === "signIn" || d.code === "surfaceOff" ? (queueMicrotask(v), G(d.code)) : new Error(d.code);
153
192
  }
154
193
  });
155
- b = s, $(c), s.on("publication", (f) => {
156
- if (s !== b || r) return;
157
- const o = A(f) ? f.data : void 0;
158
- A(o) && d.applyEvent(o) === "resync" && w();
159
- }), s.on("subscribed", () => {
160
- s !== b || r || (Z(), w());
161
- }), s.on("unsubscribed", (f) => {
162
- s !== b || r || A(f) && f.code !== 0 && a();
163
- }), s.subscribe();
164
- }, Y = j();
194
+ s = f, H(u), f.on("publication", (p) => {
195
+ if (f !== s || n) return;
196
+ const d = L(p) ? p.data : void 0;
197
+ L(d) && l.applyEvent(d) === "resync" && y();
198
+ }), f.on("subscribed", () => {
199
+ f !== s || n || (Z(), y());
200
+ }), f.on("unsubscribed", (p) => {
201
+ f !== s || n || L(p) && p.code !== 0 && v();
202
+ }), f.subscribe();
203
+ }, Y = z();
165
204
  if (Y) X(Y);
166
205
  else {
167
- const i = d.subscribe(() => {
168
- const o = j();
169
- !o || c !== p || r || (i(), l?.stop(), l = null, X(o));
170
- }), s = v;
171
- v = () => {
172
- i(), s?.();
206
+ const a = l.subscribe(() => {
207
+ const d = z();
208
+ !d || u !== o || n || (a(), c?.stop(), c = null, X(d));
209
+ }), f = m;
210
+ m = () => {
211
+ a(), f?.();
173
212
  };
174
- const f = H();
175
- if (f) {
176
- const o = {
177
- visible: R,
178
- signal: T.signal,
179
- onChange: (ie) => {
180
- ie.some((oe) => d.applyHead(oe)) && w();
181
- }
182
- };
183
- e.pollIntervalMs !== void 0 && (o.intervalMs = e.pollIntervalMs), l = J(t, [f], o);
184
- }
213
+ const p = l.key();
214
+ p && (c = q(p));
185
215
  }
186
- c.connect();
216
+ u.connect();
187
217
  }
188
218
  function ee() {
189
- r || (S(), l?.stop(), l = null, v?.(), v = null, y = 0, g("connecting"), V().catch(() => a()));
219
+ n || (O(), c?.stop(), c = null, m?.(), m = null, g = 0, b("connecting"), U().catch(() => v()));
190
220
  }
191
- const B = () => {
192
- R() && m === "live" && w();
193
- }, F = () => {
194
- m === "live" && w();
221
+ const K = () => {
222
+ h() && r === "live" && y();
223
+ }, j = () => {
224
+ r === "live" && y();
195
225
  };
196
- function W() {
197
- r || (r = !0, S(), l?.stop(), l = null, v?.(), v = null, D?.(), D = null, T.abort(), typeof document < "u" && document.removeEventListener("visibilitychange", B), typeof window < "u" && window.removeEventListener("online", F), e.signal?.removeEventListener("abort", W), g("stopped"));
226
+ function $() {
227
+ n || (n = !0, O(), c?.stop(), c = null, m?.(), m = null, R?.(), R = null, _.abort(), typeof document < "u" && document.removeEventListener("visibilitychange", K), typeof window < "u" && window.removeEventListener("online", j), e.signal?.removeEventListener("abort", $), b("stopped"));
198
228
  }
199
- return e.signal?.aborted ? (r = !0, m = "stopped") : (typeof document < "u" && document.addEventListener("visibilitychange", B), typeof window < "u" && window.addEventListener("online", F), e.signal?.addEventListener("abort", W, { once: !0 }), D = d.subscribe((n) => {
200
- const u = n.viewer.memberId;
201
- u !== P && (P = u, ee());
202
- }), V().catch(() => a())), {
229
+ return e.signal?.aborted ? (n = !0, r = "stopped") : (typeof document < "u" && document.addEventListener("visibilitychange", K), typeof window < "u" && window.addEventListener("online", j), e.signal?.addEventListener("abort", $, { once: !0 }), R = l.subscribe(() => {
230
+ const t = l.viewerMember();
231
+ t !== W && (W = t, ee());
232
+ }), U().catch(() => v())), {
203
233
  get mode() {
204
- return m;
234
+ return r;
205
235
  },
206
- resync: w,
207
- stop: W
236
+ resync: y,
237
+ stop: $
208
238
  };
209
239
  }
210
240
  export {
211
- se as REALTIME_WATCHDOG_MS,
212
- he as createRealtimeSession,
213
- pe as realtimeEndpointsOf,
214
- me as realtimeUrlOf
241
+ ue as REALTIME_WATCHDOG_MS,
242
+ Me as createRealtimeSession,
243
+ be as realtimeEndpointsOf,
244
+ pe as realtimeUrlOf
215
245
  };