@ably/ai-transport 0.0.1 → 0.1.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.
- package/README.md +54 -47
- package/dist/ably-ai-transport.js +1006 -539
- package/dist/ably-ai-transport.js.map +1 -1
- package/dist/ably-ai-transport.umd.cjs +1 -1
- package/dist/ably-ai-transport.umd.cjs.map +1 -1
- package/dist/constants.d.ts +4 -0
- package/dist/core/codec/types.d.ts +19 -2
- package/dist/core/transport/decode-history.d.ts +8 -6
- package/dist/core/transport/headers.d.ts +4 -2
- package/dist/core/transport/index.d.ts +4 -1
- package/dist/core/transport/pipe-stream.d.ts +3 -2
- package/dist/core/transport/stream-router.d.ts +11 -1
- package/dist/core/transport/tree.d.ts +171 -0
- package/dist/core/transport/turn-manager.d.ts +4 -1
- package/dist/core/transport/types.d.ts +270 -119
- package/dist/core/transport/view.d.ts +166 -0
- package/dist/errors.d.ts +19 -2
- package/dist/index.d.ts +3 -1
- package/dist/react/ably-ai-transport-react.js +1019 -486
- package/dist/react/ably-ai-transport-react.js.map +1 -1
- package/dist/react/ably-ai-transport-react.umd.cjs +1 -1
- package/dist/react/ably-ai-transport-react.umd.cjs.map +1 -1
- package/dist/react/contexts/transport-context.d.ts +31 -0
- package/dist/react/contexts/transport-provider.d.ts +49 -0
- package/dist/react/create-transport-hooks.d.ts +124 -0
- package/dist/react/index.d.ts +14 -8
- package/dist/react/use-ably-messages.d.ts +14 -8
- package/dist/react/use-active-turns.d.ts +7 -3
- package/dist/react/use-client-transport.d.ts +78 -5
- package/dist/react/use-create-view.d.ts +22 -0
- package/dist/react/use-tree.d.ts +20 -0
- package/dist/react/use-view.d.ts +79 -0
- package/dist/vercel/ably-ai-transport-vercel.js +1478 -842
- package/dist/vercel/ably-ai-transport-vercel.js.map +1 -1
- package/dist/vercel/ably-ai-transport-vercel.umd.cjs +1 -1
- package/dist/vercel/ably-ai-transport-vercel.umd.cjs.map +1 -1
- package/dist/vercel/codec/tool-transitions.d.ts +50 -0
- package/dist/vercel/index.d.ts +3 -0
- package/dist/vercel/react/ably-ai-transport-vercel-react.js +9099 -852
- package/dist/vercel/react/ably-ai-transport-vercel-react.js.map +1 -1
- package/dist/vercel/react/ably-ai-transport-vercel-react.umd.cjs +45 -1
- package/dist/vercel/react/ably-ai-transport-vercel-react.umd.cjs.map +1 -1
- package/dist/vercel/react/contexts/chat-transport-context.d.ts +32 -0
- package/dist/vercel/react/contexts/chat-transport-provider.d.ts +84 -0
- package/dist/vercel/react/index.d.ts +5 -0
- package/dist/vercel/react/use-chat-transport.d.ts +61 -20
- package/dist/vercel/react/use-message-sync.d.ts +41 -9
- package/dist/vercel/react/use-staged-add-tool-approval-response.d.ts +30 -0
- package/dist/vercel/tool-approvals.d.ts +124 -0
- package/dist/vercel/tool-events.d.ts +26 -0
- package/dist/vercel/transport/chat-transport.d.ts +33 -11
- package/dist/vercel/transport/index.d.ts +5 -2
- package/package.json +23 -17
- package/src/constants.ts +6 -0
- package/src/core/codec/encoder.ts +10 -1
- package/src/core/codec/types.ts +19 -3
- package/src/core/transport/client-transport.ts +382 -364
- package/src/core/transport/decode-history.ts +229 -81
- package/src/core/transport/headers.ts +6 -2
- package/src/core/transport/index.ts +13 -5
- package/src/core/transport/pipe-stream.ts +8 -5
- package/src/core/transport/server-transport.ts +212 -58
- package/src/core/transport/stream-router.ts +21 -3
- package/src/core/transport/{conversation-tree.ts → tree.ts} +192 -77
- package/src/core/transport/turn-manager.ts +28 -10
- package/src/core/transport/types.ts +318 -139
- package/src/core/transport/view.ts +840 -0
- package/src/errors.ts +21 -1
- package/src/index.ts +10 -5
- package/src/react/contexts/transport-context.ts +37 -0
- package/src/react/contexts/transport-provider.tsx +164 -0
- package/src/react/create-transport-hooks.ts +144 -0
- package/src/react/index.ts +15 -8
- package/src/react/use-ably-messages.ts +34 -16
- package/src/react/use-active-turns.ts +28 -17
- package/src/react/use-client-transport.ts +184 -24
- package/src/react/use-create-view.ts +68 -0
- package/src/react/use-tree.ts +53 -0
- package/src/react/use-view.ts +233 -0
- package/src/react/vite.config.ts +4 -1
- package/src/vercel/codec/accumulator.ts +64 -79
- package/src/vercel/codec/decoder.ts +11 -8
- package/src/vercel/codec/encoder.ts +68 -54
- package/src/vercel/codec/index.ts +0 -2
- package/src/vercel/codec/tool-transitions.ts +122 -0
- package/src/vercel/index.ts +17 -0
- package/src/vercel/react/contexts/chat-transport-context.ts +40 -0
- package/src/vercel/react/contexts/chat-transport-provider.tsx +122 -0
- package/src/vercel/react/index.ts +14 -0
- package/src/vercel/react/use-chat-transport.ts +164 -42
- package/src/vercel/react/use-message-sync.ts +77 -19
- package/src/vercel/react/use-staged-add-tool-approval-response.ts +87 -0
- package/src/vercel/react/vite.config.ts +4 -2
- package/src/vercel/tool-approvals.ts +380 -0
- package/src/vercel/tool-events.ts +53 -0
- package/src/vercel/transport/chat-transport.ts +225 -79
- package/src/vercel/transport/index.ts +14 -3
- package/dist/core/transport/conversation-tree.d.ts +0 -9
- package/dist/react/use-conversation-tree.d.ts +0 -20
- package/dist/react/use-edit.d.ts +0 -7
- package/dist/react/use-history.d.ts +0 -19
- package/dist/react/use-messages.d.ts +0 -7
- package/dist/react/use-regenerate.d.ts +0 -7
- package/dist/react/use-send.d.ts +0 -7
- package/src/react/use-conversation-tree.ts +0 -71
- package/src/react/use-edit.ts +0 -24
- package/src/react/use-history.ts +0 -111
- package/src/react/use-messages.ts +0 -32
- package/src/react/use-regenerate.ts +0 -24
- package/src/react/use-send.ts +0 -25
|
@@ -1,92 +1,72 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import * as
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}, o = "x-ably-turn-id", s = "x-ably-msg-id", c = "x-ably-turn-client-id", l = "x-ably-role", u = "x-ably-cancel-turn-id", d = "x-ably-cancel-own", f = "x-ably-cancel-all", p = "x-ably-cancel-client-id", m = "x-ably-parent", h = "x-ably-fork-of", g = "x-ably-cancel", _ = "x-ably-turn-start", v = "x-ably-turn-end", y = (e) => {
|
|
10
|
-
let [n, i] = r(() => /* @__PURE__ */ new Map());
|
|
11
|
-
return t(() => {
|
|
12
|
-
if (e) return i(e.getActiveTurnIds()), e.on("turn", (e) => {
|
|
13
|
-
i((t) => {
|
|
14
|
-
let n = new Map(t);
|
|
15
|
-
if (e.type === "x-ably-turn-start") {
|
|
16
|
-
let t = new Set(n.get(e.clientId) ?? []);
|
|
17
|
-
t.add(e.turnId), n.set(e.clientId, t);
|
|
18
|
-
} else {
|
|
19
|
-
let t = n.get(e.clientId);
|
|
20
|
-
t && (t.delete(e.turnId), t.size === 0 ? n.delete(e.clientId) : n.set(e.clientId, new Set(t)));
|
|
21
|
-
}
|
|
22
|
-
return n;
|
|
23
|
-
});
|
|
24
|
-
});
|
|
25
|
-
}, [e]), n;
|
|
26
|
-
}, b = /* @__PURE__ */ function(e) {
|
|
27
|
-
return e[e.BadRequest = 4e4] = "BadRequest", e[e.InvalidArgument = 40003] = "InvalidArgument", e[e.EncoderRecoveryFailed = 104e3] = "EncoderRecoveryFailed", e[e.TransportSubscriptionError = 104001] = "TransportSubscriptionError", e[e.CancelListenerError = 104002] = "CancelListenerError", e[e.TurnLifecycleError = 104003] = "TurnLifecycleError", e[e.TransportClosed = 104004] = "TransportClosed", e[e.TransportSendFailed = 104005] = "TransportSendFailed", e;
|
|
28
|
-
}({}), x = (e) => ({
|
|
1
|
+
import { createContext as e, useCallback as t, useContext as n, useEffect as r, useMemo as i, useRef as a, useState as o } from "react";
|
|
2
|
+
import * as s from "ably";
|
|
3
|
+
import { ChannelProvider as c, useChannel as l } from "ably/react";
|
|
4
|
+
import { jsx as u } from "react/jsx-runtime";
|
|
5
|
+
//#region src/react/contexts/transport-context.ts
|
|
6
|
+
var d = e({}), f = e(void 0), p = "x-ably-status", m = "x-ably-turn-id", h = "x-ably-msg-id", g = "x-ably-turn-client-id", _ = "x-ably-role", v = "x-ably-amend", y = "x-ably-cancel-turn-id", b = "x-ably-cancel-own", x = "x-ably-cancel-all", S = "x-ably-cancel-client-id", C = "x-ably-parent", w = "x-ably-fork-of", T = "x-ably-cancel", E = "x-ably-turn-start", D = "x-ably-turn-end", O = /* @__PURE__ */ function(e) {
|
|
7
|
+
return e[e.BadRequest = 4e4] = "BadRequest", e[e.InvalidArgument = 40003] = "InvalidArgument", e[e.EncoderRecoveryFailed = 104e3] = "EncoderRecoveryFailed", e[e.TransportSubscriptionError = 104001] = "TransportSubscriptionError", e[e.CancelListenerError = 104002] = "CancelListenerError", e[e.TurnLifecycleError = 104003] = "TurnLifecycleError", e[e.TransportClosed = 104004] = "TransportClosed", e[e.TransportSendFailed = 104005] = "TransportSendFailed", e[e.ChannelContinuityLost = 104006] = "ChannelContinuityLost", e[e.ChannelNotReady = 104007] = "ChannelNotReady", e[e.StreamError = 104008] = "StreamError", e;
|
|
8
|
+
}({}), ee = (e) => ({
|
|
29
9
|
logAction: (t, n, r) => {
|
|
30
10
|
e.error(n, { detail: r });
|
|
31
11
|
},
|
|
32
12
|
shouldLog: () => !0
|
|
33
|
-
}),
|
|
13
|
+
}), te = s.Realtime.EventEmitter, k = class extends te {
|
|
34
14
|
constructor(e) {
|
|
35
|
-
super(
|
|
15
|
+
super(ee(e));
|
|
36
16
|
}
|
|
37
|
-
},
|
|
17
|
+
}, A = /* @__PURE__ */ function(e) {
|
|
38
18
|
return e.Trace = "trace", e.Debug = "debug", e.Info = "info", e.Warn = "warn", e.Error = "error", e.Silent = "silent", e;
|
|
39
|
-
}({}),
|
|
19
|
+
}({}), ne = (e, t, n) => {
|
|
40
20
|
let r = n ? `, context: ${JSON.stringify(n)}` : "", i = `[${(/* @__PURE__ */ new Date()).toISOString()}] ${t.valueOf().toUpperCase()} ably-ai-transport: ${e}${r}`;
|
|
41
21
|
switch (t) {
|
|
42
|
-
case
|
|
43
|
-
case
|
|
22
|
+
case A.Trace:
|
|
23
|
+
case A.Debug:
|
|
44
24
|
console.log(i);
|
|
45
25
|
break;
|
|
46
|
-
case
|
|
26
|
+
case A.Info:
|
|
47
27
|
console.info(i);
|
|
48
28
|
break;
|
|
49
|
-
case
|
|
29
|
+
case A.Warn:
|
|
50
30
|
console.warn(i);
|
|
51
31
|
break;
|
|
52
|
-
case
|
|
32
|
+
case A.Error:
|
|
53
33
|
console.error(i);
|
|
54
34
|
break;
|
|
55
|
-
case
|
|
35
|
+
case A.Silent: break;
|
|
56
36
|
}
|
|
57
|
-
},
|
|
37
|
+
}, re = (e) => new N(e.logHandler ?? ne, e.logLevel), j = /* @__PURE__ */ function(e) {
|
|
58
38
|
return e[e.Trace = 0] = "Trace", e[e.Debug = 1] = "Debug", e[e.Info = 2] = "Info", e[e.Warn = 3] = "Warn", e[e.Error = 4] = "Error", e[e.Silent = 5] = "Silent", e;
|
|
59
|
-
}(
|
|
60
|
-
[
|
|
61
|
-
[
|
|
62
|
-
[
|
|
63
|
-
[
|
|
64
|
-
[
|
|
65
|
-
[
|
|
66
|
-
]),
|
|
39
|
+
}(j || {}), M = new Map([
|
|
40
|
+
[A.Trace, j.Trace],
|
|
41
|
+
[A.Debug, j.Debug],
|
|
42
|
+
[A.Info, j.Info],
|
|
43
|
+
[A.Warn, j.Warn],
|
|
44
|
+
[A.Error, j.Error],
|
|
45
|
+
[A.Silent, j.Silent]
|
|
46
|
+
]), N = class e {
|
|
67
47
|
constructor(e, t, n) {
|
|
68
48
|
this._handler = e, this._context = n;
|
|
69
|
-
let r =
|
|
70
|
-
if (r === void 0) throw new
|
|
49
|
+
let r = M.get(t);
|
|
50
|
+
if (r === void 0) throw new s.ErrorInfo(`unable to create logger; invalid log level: ${t}`, O.InvalidArgument, 400);
|
|
71
51
|
this._levelNumber = r;
|
|
72
52
|
}
|
|
73
53
|
trace(e, t) {
|
|
74
|
-
this._write(e,
|
|
54
|
+
this._write(e, A.Trace, j.Trace, t);
|
|
75
55
|
}
|
|
76
56
|
debug(e, t) {
|
|
77
|
-
this._write(e,
|
|
57
|
+
this._write(e, A.Debug, j.Debug, t);
|
|
78
58
|
}
|
|
79
59
|
info(e, t) {
|
|
80
|
-
this._write(e,
|
|
60
|
+
this._write(e, A.Info, j.Info, t);
|
|
81
61
|
}
|
|
82
62
|
warn(e, t) {
|
|
83
|
-
this._write(e,
|
|
63
|
+
this._write(e, A.Warn, j.Warn, t);
|
|
84
64
|
}
|
|
85
65
|
error(e, t) {
|
|
86
|
-
this._write(e,
|
|
66
|
+
this._write(e, A.Error, j.Error, t);
|
|
87
67
|
}
|
|
88
68
|
withContext(t) {
|
|
89
|
-
let n = [...
|
|
69
|
+
let n = [...M.entries()].find(([, e]) => e === this._levelNumber)?.[0] ?? A.Error;
|
|
90
70
|
return new e(this._handler, n, this._mergeContext(t));
|
|
91
71
|
}
|
|
92
72
|
_write(e, t, n, r) {
|
|
@@ -98,14 +78,70 @@ var a = (e) => {
|
|
|
98
78
|
...e
|
|
99
79
|
} : this._context : e ?? void 0;
|
|
100
80
|
}
|
|
101
|
-
},
|
|
81
|
+
}, P = (e) => {
|
|
102
82
|
let t = e.extras;
|
|
103
83
|
if (!t || typeof t != "object") return {};
|
|
104
84
|
let n = t.headers;
|
|
105
85
|
return !n || typeof n != "object" ? {} : n;
|
|
106
|
-
},
|
|
86
|
+
}, F = (e) => {
|
|
87
|
+
let t = {
|
|
88
|
+
[_]: e.role,
|
|
89
|
+
[m]: e.turnId,
|
|
90
|
+
[h]: e.msgId
|
|
91
|
+
};
|
|
92
|
+
return e.turnClientId !== void 0 && (t[g] = e.turnClientId), e.parent && (t[C] = e.parent), e.forkOf && (t[w] = e.forkOf), e.amend && (t[v] = e.amend), t;
|
|
93
|
+
}, I = class {
|
|
107
94
|
constructor(e, t) {
|
|
108
|
-
this.
|
|
95
|
+
this._turns = /* @__PURE__ */ new Map(), this._isTerminal = e, this._logger = t;
|
|
96
|
+
}
|
|
97
|
+
createStream(e) {
|
|
98
|
+
this._logger.trace("StreamRouter.createStream();", { turnId: e });
|
|
99
|
+
let t = {}, n = new ReadableStream({ start(e) {
|
|
100
|
+
t.controller = e;
|
|
101
|
+
} });
|
|
102
|
+
if (!t.controller) throw new s.ErrorInfo("unable to create stream; ReadableStream start() was not called synchronously", O.TransportSubscriptionError, 500);
|
|
103
|
+
return this._turns.set(e, {
|
|
104
|
+
controller: t.controller,
|
|
105
|
+
turnId: e
|
|
106
|
+
}), n;
|
|
107
|
+
}
|
|
108
|
+
closeStream(e) {
|
|
109
|
+
let t = this._turns.get(e);
|
|
110
|
+
if (!t) return !1;
|
|
111
|
+
this._logger.debug("StreamRouter.closeStream(); closing stream", { turnId: e });
|
|
112
|
+
try {
|
|
113
|
+
t.controller.close();
|
|
114
|
+
} catch {}
|
|
115
|
+
return this._turns.delete(e), !0;
|
|
116
|
+
}
|
|
117
|
+
errorStream(e, t) {
|
|
118
|
+
let n = this._turns.get(e);
|
|
119
|
+
if (!n) return !1;
|
|
120
|
+
this._logger.debug("StreamRouter.errorStream(); erroring stream", { turnId: e });
|
|
121
|
+
try {
|
|
122
|
+
n.controller.error(t);
|
|
123
|
+
} catch {}
|
|
124
|
+
return this._turns.delete(e), !0;
|
|
125
|
+
}
|
|
126
|
+
route(e, t) {
|
|
127
|
+
let n = this._turns.get(e);
|
|
128
|
+
if (!n) return !1;
|
|
129
|
+
try {
|
|
130
|
+
n.controller.enqueue(t);
|
|
131
|
+
} catch {
|
|
132
|
+
return this._turns.delete(e), !1;
|
|
133
|
+
}
|
|
134
|
+
return this._isTerminal(t) && this.closeStream(e), !0;
|
|
135
|
+
}
|
|
136
|
+
has(e) {
|
|
137
|
+
return this._turns.has(e);
|
|
138
|
+
}
|
|
139
|
+
}, L = (e, t) => new I(e, t), R = class {
|
|
140
|
+
get structuralVersion() {
|
|
141
|
+
return this._structuralVersion;
|
|
142
|
+
}
|
|
143
|
+
constructor(e) {
|
|
144
|
+
this._nodeIndex = /* @__PURE__ */ new Map(), this._sortedList = [], this._parentIndex = /* @__PURE__ */ new Map(), this._turnClientIds = /* @__PURE__ */ new Map(), this._seqCounter = 0, this._structuralVersion = 0, this._logger = e, this._emitter = new k(e);
|
|
109
145
|
}
|
|
110
146
|
_compareNodes(e, t) {
|
|
111
147
|
let n = e.node.serial, r = t.node.serial;
|
|
@@ -164,7 +200,7 @@ var a = (e) => {
|
|
|
164
200
|
}
|
|
165
201
|
return !1;
|
|
166
202
|
}
|
|
167
|
-
|
|
203
|
+
getGroupRoot(e) {
|
|
168
204
|
let t = this._nodeIndex.get(e);
|
|
169
205
|
if (!t) return e;
|
|
170
206
|
let n = t.node, r = new Set([n.msgId]);
|
|
@@ -175,75 +211,63 @@ var a = (e) => {
|
|
|
175
211
|
}
|
|
176
212
|
return n.msgId;
|
|
177
213
|
}
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
214
|
+
flattenNodes(e) {
|
|
215
|
+
this._logger.trace("DefaultTree.flattenNodes();");
|
|
216
|
+
let t = [], n = /* @__PURE__ */ new Set(), r = /* @__PURE__ */ new Map();
|
|
217
|
+
for (let i of this._sortedList) {
|
|
218
|
+
let a = i.node, { msgId: o, parentId: s } = a;
|
|
219
|
+
if (s !== void 0 && !n.has(s)) continue;
|
|
220
|
+
let c = this._getSiblingGroup(o);
|
|
221
|
+
if (c.length > 1) {
|
|
222
|
+
let t = this.getGroupRoot(o), n = r.get(t);
|
|
223
|
+
if (n === void 0) {
|
|
224
|
+
let i = e.get(t);
|
|
225
|
+
if (i && c.some((e) => e.msgId === i)) n = i;
|
|
226
|
+
else {
|
|
227
|
+
let e = c.at(-1);
|
|
228
|
+
if (!e) break;
|
|
229
|
+
n = e.msgId;
|
|
230
|
+
}
|
|
231
|
+
r.set(t, n);
|
|
190
232
|
}
|
|
191
|
-
if (
|
|
233
|
+
if (o !== n) continue;
|
|
192
234
|
}
|
|
193
|
-
|
|
235
|
+
n.add(o), t.push(a);
|
|
194
236
|
}
|
|
195
|
-
return
|
|
237
|
+
return t;
|
|
196
238
|
}
|
|
197
239
|
getSiblings(e) {
|
|
198
|
-
return this._getSiblingGroup(e).map((e) => e.message);
|
|
240
|
+
return this._logger.trace("DefaultTree.getSiblings();", { msgId: e }), this._getSiblingGroup(e).map((e) => e.message);
|
|
241
|
+
}
|
|
242
|
+
getSiblingNodes(e) {
|
|
243
|
+
return this._getSiblingGroup(e);
|
|
199
244
|
}
|
|
200
245
|
hasSiblings(e) {
|
|
201
246
|
return this._getSiblingGroup(e).length > 1;
|
|
202
247
|
}
|
|
203
|
-
getSelectedIndex(e) {
|
|
204
|
-
let t = this._getSiblingGroup(e);
|
|
205
|
-
if (t.length <= 1) return 0;
|
|
206
|
-
let n = this._getGroupRoot(e), r = this._selections.get(n);
|
|
207
|
-
return r === void 0 ? t.length - 1 : Math.max(0, Math.min(r, t.length - 1));
|
|
208
|
-
}
|
|
209
|
-
select(e, t) {
|
|
210
|
-
this._logger.debug("ConversationTree.select();", {
|
|
211
|
-
msgId: e,
|
|
212
|
-
index: t
|
|
213
|
-
});
|
|
214
|
-
let n = this._getSiblingGroup(e);
|
|
215
|
-
if (n.length <= 1) return;
|
|
216
|
-
let r = this._getGroupRoot(e);
|
|
217
|
-
this._selections.set(r, Math.max(0, Math.min(t, n.length - 1)));
|
|
218
|
-
}
|
|
219
248
|
getNode(e) {
|
|
220
|
-
return this._nodeIndex.get(e)?.node;
|
|
221
|
-
}
|
|
222
|
-
getNodeByKey(e) {
|
|
223
|
-
let t = this._codecKeyIndex.get(e);
|
|
224
|
-
if (t) return this._nodeIndex.get(t)?.node;
|
|
249
|
+
return this._logger.trace("DefaultTree.getNode();", { msgId: e }), this._nodeIndex.get(e)?.node;
|
|
225
250
|
}
|
|
226
251
|
getHeaders(e) {
|
|
227
|
-
return this._nodeIndex.get(e)?.node.headers;
|
|
252
|
+
return this._logger.trace("DefaultTree.getHeaders();", { msgId: e }), this._nodeIndex.get(e)?.node.headers;
|
|
228
253
|
}
|
|
229
254
|
upsert(e, t, n, r) {
|
|
230
|
-
let i = n["x-ably-parent"] ?? void 0, a = n["x-ably-fork-of"] ?? void 0;
|
|
231
|
-
this._codecKeyIndex.set(this._getKey(t), e);
|
|
232
|
-
let o = this._nodeIndex.get(e);
|
|
255
|
+
let i = n["x-ably-parent"] ?? void 0, a = n["x-ably-fork-of"] ?? void 0, o = this._nodeIndex.get(e);
|
|
233
256
|
if (o) {
|
|
234
|
-
o.node.message = t, Object.keys(n).length > 0 && (o.node.headers = { ...n }), r && !o.node.serial && (this._logger.debug("
|
|
257
|
+
o.node.message = t, Object.keys(n).length > 0 && (o.node.headers = { ...n }), r && !o.node.serial && (this._logger.debug("Tree.upsert(); promoting serial", {
|
|
235
258
|
msgId: e,
|
|
236
259
|
serial: r
|
|
237
|
-
}), o.node.serial = r, this._removeSorted(o), this._insertSorted(o));
|
|
260
|
+
}), o.node.serial = r, this._removeSorted(o), this._insertSorted(o), this._structuralVersion++), this._emitter.emit("update");
|
|
238
261
|
return;
|
|
239
262
|
}
|
|
240
|
-
this._logger.trace("
|
|
263
|
+
this._logger.trace("Tree.upsert(); inserting new node", {
|
|
241
264
|
msgId: e,
|
|
242
265
|
parentId: i,
|
|
243
266
|
forkOf: a
|
|
244
267
|
});
|
|
245
268
|
let s = {
|
|
246
269
|
node: {
|
|
270
|
+
kind: "message",
|
|
247
271
|
message: t,
|
|
248
272
|
msgId: e,
|
|
249
273
|
parentId: i,
|
|
@@ -253,19 +277,60 @@ var a = (e) => {
|
|
|
253
277
|
},
|
|
254
278
|
insertSeq: this._seqCounter++
|
|
255
279
|
};
|
|
256
|
-
this._nodeIndex.set(e, s), this._addToParentIndex(i, e), this._insertSorted(s);
|
|
280
|
+
this._nodeIndex.set(e, s), this._addToParentIndex(i, e), this._insertSorted(s), this._structuralVersion++, this._emitter.emit("update");
|
|
257
281
|
}
|
|
258
282
|
delete(e) {
|
|
259
283
|
let t = this._nodeIndex.get(e);
|
|
260
284
|
if (!t) return;
|
|
261
|
-
this._logger.debug("
|
|
262
|
-
let { node: n } = t
|
|
263
|
-
this.
|
|
285
|
+
this._logger.debug("Tree.delete();", { msgId: e });
|
|
286
|
+
let { node: n } = t;
|
|
287
|
+
this._removeFromParentIndex(n.parentId, e), this._removeSorted(t), this._nodeIndex.delete(e), this._structuralVersion++, this._emitter.emit("update");
|
|
288
|
+
}
|
|
289
|
+
getActiveTurnIds() {
|
|
290
|
+
this._logger.trace("DefaultTree.getActiveTurnIds();");
|
|
291
|
+
let e = /* @__PURE__ */ new Map();
|
|
292
|
+
for (let [t, n] of this._turnClientIds) {
|
|
293
|
+
let r = e.get(n);
|
|
294
|
+
r || (r = /* @__PURE__ */ new Set(), e.set(n, r)), r.add(t);
|
|
295
|
+
}
|
|
296
|
+
return e;
|
|
297
|
+
}
|
|
298
|
+
on(e, t) {
|
|
299
|
+
let n = t;
|
|
300
|
+
return this._emitter.on(e, n), () => {
|
|
301
|
+
this._emitter.off(e, n);
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
emitAblyMessage(e) {
|
|
305
|
+
this._logger.trace("DefaultTree.emitAblyMessage();"), this._emitter.emit("ably-message", e);
|
|
306
|
+
}
|
|
307
|
+
emitTurn(e) {
|
|
308
|
+
this._logger.trace("DefaultTree.emitTurn();", { turnId: e.turnId }), this._emitter.emit("turn", e);
|
|
309
|
+
}
|
|
310
|
+
trackTurn(e, t) {
|
|
311
|
+
this._logger.trace("DefaultTree.trackTurn();", {
|
|
312
|
+
turnId: e,
|
|
313
|
+
clientId: t
|
|
314
|
+
}), this._turnClientIds.set(e, t);
|
|
315
|
+
}
|
|
316
|
+
untrackTurn(e) {
|
|
317
|
+
this._logger.trace("DefaultTree.untrackTurn();", { turnId: e }), this._turnClientIds.delete(e);
|
|
264
318
|
}
|
|
265
|
-
},
|
|
266
|
-
let t = [...e.rawMessages].toReversed(), n = e.codec.createDecoder(), r = /* @__PURE__ */ new Map(), i = e.codec.createAccumulator(), a = 0,
|
|
319
|
+
}, z = (e) => new R(e), B = (e) => {
|
|
320
|
+
let t = [...e.rawMessages].toReversed(), n = e.codec.createDecoder(), r = /* @__PURE__ */ new Map(), i = e.codec.createAccumulator(), a = 0, o = /* @__PURE__ */ new Map(), s = /* @__PURE__ */ new Map(), c = [], l = [];
|
|
267
321
|
for (let u of t) {
|
|
268
|
-
let t = n.decode(u), d =
|
|
322
|
+
let t = n.decode(u), d = P(u), f = d[m], p = d[h], g = u.serial, _ = d[v];
|
|
323
|
+
if (_) {
|
|
324
|
+
for (let e of r.values()) if (e.msgHeaders.has(_)) {
|
|
325
|
+
let n = [...e.msgHeaders.keys()].indexOf(_), r = n === -1 ? void 0 : e.accumulator.messages[n];
|
|
326
|
+
r && e.accumulator.initMessage(_, r), e.accumulator.processOutputs(t), l.push({
|
|
327
|
+
accumulator: e.accumulator,
|
|
328
|
+
messageId: _
|
|
329
|
+
});
|
|
330
|
+
break;
|
|
331
|
+
}
|
|
332
|
+
continue;
|
|
333
|
+
}
|
|
269
334
|
if (f) {
|
|
270
335
|
let n = r.get(f);
|
|
271
336
|
if (n || (n = {
|
|
@@ -275,202 +340,517 @@ var a = (e) => {
|
|
|
275
340
|
msgSerials: /* @__PURE__ */ new Map()
|
|
276
341
|
}, r.set(f, n)), p) {
|
|
277
342
|
let e = n.msgHeaders.get(p);
|
|
278
|
-
e ? Object.keys(d).length > 0 && Object.assign(e, d) : (n.msgHeaders.set(p, { ...d }),
|
|
343
|
+
e ? Object.keys(d).length > 0 && Object.assign(e, d) : (n.msgHeaders.set(p, { ...d }), g && n.msgSerials.set(p, g));
|
|
279
344
|
}
|
|
280
345
|
n.accumulator.processOutputs(t);
|
|
281
|
-
} else
|
|
282
|
-
|
|
283
|
-
let t
|
|
284
|
-
|
|
346
|
+
} else {
|
|
347
|
+
i.processOutputs(t);
|
|
348
|
+
for (let e of t) if (e.kind === "message" && p) {
|
|
349
|
+
c.push(p);
|
|
350
|
+
let e = o.get(p);
|
|
351
|
+
e ? Object.keys(d).length > 0 && Object.assign(e, d) : (o.set(p, { ...d }), g && s.set(p, g));
|
|
352
|
+
}
|
|
285
353
|
}
|
|
286
354
|
}
|
|
355
|
+
for (let { accumulator: e, messageId: t } of l) e.completeMessage(t);
|
|
287
356
|
let u = [];
|
|
288
|
-
for (let t of i.completedMessages) {
|
|
289
|
-
let n = e
|
|
357
|
+
for (let [e, t] of i.completedMessages.entries()) {
|
|
358
|
+
let n = c[e];
|
|
290
359
|
u.push({
|
|
291
360
|
message: t,
|
|
292
|
-
headers:
|
|
293
|
-
serial:
|
|
361
|
+
headers: n ? o.get(n) ?? {} : {},
|
|
362
|
+
serial: n ? s.get(n) ?? "" : ""
|
|
294
363
|
});
|
|
295
364
|
}
|
|
296
365
|
let d = [...r.values()].toSorted((e, t) => e.firstSeen - t.firstSeen);
|
|
297
|
-
for (let
|
|
298
|
-
let
|
|
299
|
-
for (let
|
|
300
|
-
let
|
|
301
|
-
if (
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
if (!r.has(s) && c) {
|
|
313
|
-
let [e, n] = c;
|
|
314
|
-
r.set(s, n);
|
|
315
|
-
let a = t.msgSerials.get(e);
|
|
316
|
-
a && i.set(s, a), o++;
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
for (let n of t.accumulator.completedMessages) {
|
|
320
|
-
let t = e.getMessageKey(n);
|
|
321
|
-
u.push({
|
|
322
|
-
message: n,
|
|
323
|
-
headers: r.get(t) ?? {},
|
|
324
|
-
serial: i.get(t) ?? ""
|
|
366
|
+
for (let e of d) {
|
|
367
|
+
let t = [...e.msgHeaders.entries()], n = 0;
|
|
368
|
+
for (let r of e.accumulator.completedMessages) {
|
|
369
|
+
let i = t[n];
|
|
370
|
+
if (i) {
|
|
371
|
+
let [t, a] = i;
|
|
372
|
+
u.push({
|
|
373
|
+
message: r,
|
|
374
|
+
headers: a,
|
|
375
|
+
serial: e.msgSerials.get(t) ?? ""
|
|
376
|
+
}), n++;
|
|
377
|
+
} else u.push({
|
|
378
|
+
message: r,
|
|
379
|
+
headers: {},
|
|
380
|
+
serial: ""
|
|
325
381
|
});
|
|
326
382
|
}
|
|
327
383
|
}
|
|
328
384
|
return u.toReversed();
|
|
329
|
-
},
|
|
330
|
-
e.rawMessages.
|
|
331
|
-
let
|
|
332
|
-
|
|
385
|
+
}, ie = (e) => {
|
|
386
|
+
if (e.cachedDecode && e.cachedAtRawLength === e.rawMessages.length) return e.cachedDecode;
|
|
387
|
+
let t = B(e);
|
|
388
|
+
return e.cachedDecode = t, e.cachedAtRawLength = e.rawMessages.length, t;
|
|
389
|
+
}, V = (e, t) => {
|
|
390
|
+
for (let n of t) {
|
|
391
|
+
let t = P(n), r = t[h];
|
|
392
|
+
if (!r || t["x-ably-amend"]) continue;
|
|
393
|
+
let i = n.action, a = i === "message.create" && "x-ably-discrete" in t, o = t["x-ably-stream"] === "true" && (i === "message.create" || i === "message.update" || i === "message.append"), s = t[p], c = s === "finished" || s === "aborted";
|
|
394
|
+
(a || o) && e.startedMsgIds.add(r), (a || c) && e.terminatedMsgIds.add(r), e.startedMsgIds.has(r) && e.terminatedMsgIds.has(r) && e.completedMsgIds.add(r);
|
|
395
|
+
}
|
|
396
|
+
}, H = async (e, t, n) => {
|
|
397
|
+
e.rawMessages.push(...t.items), e.lastAblyPage = t, V(e, t.items);
|
|
398
|
+
let r = e.returnedCount + n;
|
|
399
|
+
for (; e.completedMsgIds.size < r && t.hasNext();) {
|
|
333
400
|
e.logger.debug("decodeHistory.fetchUntilLimit(); fetching next page", {
|
|
334
401
|
collected: e.rawMessages.length,
|
|
335
|
-
|
|
402
|
+
completed: e.completedMsgIds.size
|
|
336
403
|
});
|
|
337
404
|
let n = await t.next();
|
|
338
405
|
if (!n) break;
|
|
339
|
-
t = n, e.rawMessages.push(...n.items), e.lastAblyPage = n,
|
|
406
|
+
t = n, e.rawMessages.push(...n.items), e.lastAblyPage = n, V(e, n.items);
|
|
340
407
|
}
|
|
341
|
-
},
|
|
342
|
-
let n =
|
|
408
|
+
}, U = (e, t) => {
|
|
409
|
+
let n = ie(e), r = n.slice(e.returnedCount, e.returnedCount + t), i = [...r].toReversed();
|
|
343
410
|
e.returnedCount += r.length;
|
|
344
411
|
let a = n.length > e.returnedCount, o = e.lastAblyPage?.hasNext() ?? !1, s = e.rawMessages.length - e.returnedRawCount > 0 ? e.rawMessages.slice(e.returnedRawCount).toReversed() : [];
|
|
345
412
|
return e.returnedRawCount = e.rawMessages.length, {
|
|
346
|
-
items: i.map((e) =>
|
|
347
|
-
|
|
348
|
-
|
|
413
|
+
items: i.map((e) => ({
|
|
414
|
+
message: e.message,
|
|
415
|
+
headers: e.headers,
|
|
416
|
+
serial: e.serial
|
|
417
|
+
})),
|
|
349
418
|
rawMessages: s,
|
|
350
419
|
hasNext: () => a || o,
|
|
351
420
|
next: async () => {
|
|
352
|
-
if (a) return
|
|
421
|
+
if (a) return U(e, t);
|
|
353
422
|
if (!o || !e.lastAblyPage) return;
|
|
354
423
|
let n = await e.lastAblyPage.next();
|
|
355
|
-
if (n) return await
|
|
424
|
+
if (n) return await H(e, n, t), U(e, t);
|
|
356
425
|
}
|
|
357
426
|
};
|
|
358
|
-
},
|
|
427
|
+
}, ae = async (e, t, n, r) => {
|
|
359
428
|
let i = n?.limit ?? 100, a = {
|
|
360
429
|
codec: t,
|
|
361
430
|
rawMessages: [],
|
|
362
431
|
returnedCount: 0,
|
|
363
432
|
returnedRawCount: 0,
|
|
364
433
|
lastAblyPage: void 0,
|
|
365
|
-
|
|
434
|
+
cachedDecode: void 0,
|
|
435
|
+
cachedAtRawLength: 0,
|
|
436
|
+
startedMsgIds: /* @__PURE__ */ new Set(),
|
|
437
|
+
terminatedMsgIds: /* @__PURE__ */ new Set(),
|
|
438
|
+
completedMsgIds: /* @__PURE__ */ new Set(),
|
|
366
439
|
logger: r
|
|
367
440
|
};
|
|
368
441
|
r.trace("decodeHistory();", { limit: i });
|
|
369
442
|
let o = i * 10;
|
|
370
|
-
return await e.attach(), await
|
|
443
|
+
return await e.attach(), await H(a, await e.history({
|
|
371
444
|
untilAttach: !0,
|
|
372
445
|
limit: o
|
|
373
|
-
}), i),
|
|
374
|
-
},
|
|
375
|
-
|
|
376
|
-
[
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
this._turns = /* @__PURE__ */ new Map(), this._isTerminal = e, this._logger = t;
|
|
446
|
+
}), i), U(a, i);
|
|
447
|
+
}, oe = class {
|
|
448
|
+
constructor(e) {
|
|
449
|
+
this._branchSelections = /* @__PURE__ */ new Map(), this._withheldMsgIds = /* @__PURE__ */ new Set(), this._lastVisibleIds = [], this._lastVisibleMessages = [], this._lastVisibleTurnIds = /* @__PURE__ */ new Set(), this._hasMoreHistory = !1, this._withheldBuffer = [], this._unsubs = [], this._cachedNodes = [], this._lastStructuralVersion = -1, this._loadingOlder = !1, this._processingHistory = !1, this._closed = !1, this._tree = e.tree, this._channel = e.channel, this._codec = e.codec, this._sendDelegate = e.sendDelegate, this._onClose = e.onClose, this._logger = e.logger.withContext({ component: "View" }), this._logger.trace("DefaultView();"), this._emitter = new k(this._logger), this._cachedNodes = this._computeFlatNodes(), this._lastStructuralVersion = this._tree.structuralVersion, this._updateVisibleSnapshot(this._cachedNodes), this._unsubs.push(this._tree.on("update", () => {
|
|
450
|
+
this._onTreeUpdate();
|
|
451
|
+
}), this._tree.on("ably-message", (e) => {
|
|
452
|
+
this._onTreeAblyMessage(e);
|
|
453
|
+
}), this._tree.on("turn", (e) => {
|
|
454
|
+
this._onTreeTurn(e);
|
|
455
|
+
}));
|
|
384
456
|
}
|
|
385
|
-
|
|
386
|
-
this.
|
|
387
|
-
let t = {}, n = new ReadableStream({ start(e) {
|
|
388
|
-
t.controller = e;
|
|
389
|
-
} });
|
|
390
|
-
if (!t.controller) throw new i.ErrorInfo("unable to create stream; ReadableStream start() was not called synchronously", b.TransportSubscriptionError, 500);
|
|
391
|
-
return this._turns.set(e, {
|
|
392
|
-
controller: t.controller,
|
|
393
|
-
turnId: e
|
|
394
|
-
}), n;
|
|
457
|
+
getMessages() {
|
|
458
|
+
return this.flattenNodes().map((e) => e.message);
|
|
395
459
|
}
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
if (!t) return !1;
|
|
399
|
-
this._logger.debug("StreamRouter.closeStream(); closing stream", { turnId: e });
|
|
400
|
-
try {
|
|
401
|
-
t.controller.close();
|
|
402
|
-
} catch {}
|
|
403
|
-
return this._turns.delete(e), !0;
|
|
460
|
+
flattenNodes() {
|
|
461
|
+
return this._cachedNodes;
|
|
404
462
|
}
|
|
405
|
-
|
|
406
|
-
let
|
|
407
|
-
|
|
463
|
+
_computeFlatNodes() {
|
|
464
|
+
let e = this._tree.flattenNodes(this._resolveSelections());
|
|
465
|
+
return this._withheldMsgIds.size === 0 ? e : e.filter((e) => !this._withheldMsgIds.has(e.msgId));
|
|
466
|
+
}
|
|
467
|
+
hasOlder() {
|
|
468
|
+
return this._withheldBuffer.length > 0 || this._hasMoreHistory;
|
|
469
|
+
}
|
|
470
|
+
async loadOlder(e = 100) {
|
|
471
|
+
if (!(this._closed || this._loadingOlder)) {
|
|
472
|
+
this._loadingOlder = !0, this._logger.trace("DefaultView.loadOlder();", { limit: e });
|
|
473
|
+
try {
|
|
474
|
+
if (this._withheldBuffer.length > 0) {
|
|
475
|
+
let t = this._withheldBuffer.splice(-e, e);
|
|
476
|
+
this._releaseWithheld(t);
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
if (!this._hasMoreHistory && !this._lastHistoryPage) {
|
|
480
|
+
await this._loadFirstPage(e);
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
if (!this._hasMoreHistory) return;
|
|
484
|
+
if (!this._lastHistoryPage?.hasNext()) {
|
|
485
|
+
this._hasMoreHistory = !1;
|
|
486
|
+
return;
|
|
487
|
+
}
|
|
488
|
+
let t = await this._lastHistoryPage.next();
|
|
489
|
+
if (this._closed || !t) {
|
|
490
|
+
t || (this._hasMoreHistory = !1);
|
|
491
|
+
return;
|
|
492
|
+
}
|
|
493
|
+
await this._loadAndReveal(t, e);
|
|
494
|
+
} catch (e) {
|
|
495
|
+
throw this._logger.error("DefaultView.loadOlder(); failed", { error: e }), e;
|
|
496
|
+
} finally {
|
|
497
|
+
this._loadingOlder = !1;
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
select(e, t) {
|
|
502
|
+
this._logger.trace("DefaultView.select();", {
|
|
503
|
+
msgId: e,
|
|
504
|
+
index: t
|
|
505
|
+
});
|
|
506
|
+
let n = this._tree.getSiblingNodes(e);
|
|
507
|
+
if (n.length <= 1) return;
|
|
508
|
+
let r = this._tree.getGroupRoot(e), i = Math.max(0, Math.min(t, n.length - 1)), a = n[i];
|
|
509
|
+
a && (this._branchSelections.set(r, {
|
|
510
|
+
kind: "user",
|
|
511
|
+
selectedId: a.msgId
|
|
512
|
+
}), this._logger.debug("DefaultView.select();", {
|
|
513
|
+
msgId: e,
|
|
514
|
+
index: i,
|
|
515
|
+
selectedId: a.msgId
|
|
516
|
+
}), this._cachedNodes = this._computeFlatNodes(), this._updateVisibleSnapshot(this._cachedNodes), this._emitter.emit("update"));
|
|
517
|
+
}
|
|
518
|
+
getSelectedIndex(e) {
|
|
519
|
+
this._logger.trace("DefaultView.getSelectedIndex();", { msgId: e });
|
|
520
|
+
let t = this._tree.getSiblingNodes(e);
|
|
521
|
+
if (t.length <= 1) return 0;
|
|
522
|
+
let n = this._tree.getGroupRoot(e), r = this._branchSelections.get(n);
|
|
523
|
+
if (!r || r.kind === "pending") return t.length - 1;
|
|
524
|
+
let i = t.findIndex((e) => e.msgId === r.selectedId);
|
|
525
|
+
return i === -1 ? t.length - 1 : i;
|
|
526
|
+
}
|
|
527
|
+
getSiblings(e) {
|
|
528
|
+
return this._tree.getSiblings(e);
|
|
529
|
+
}
|
|
530
|
+
hasSiblings(e) {
|
|
531
|
+
return this._tree.hasSiblings(e);
|
|
532
|
+
}
|
|
533
|
+
getNode(e) {
|
|
534
|
+
return this._tree.getNode(e);
|
|
535
|
+
}
|
|
536
|
+
async send(e, t) {
|
|
537
|
+
if (this._logger.trace("DefaultView.send();"), this._closed) throw new s.ErrorInfo("unable to send; view is closed", O.InvalidArgument, 400);
|
|
538
|
+
let n = this.flattenNodes(), r = await this._sendDelegate(e, t, n);
|
|
539
|
+
if (t?.forkOf) {
|
|
540
|
+
let e = this._tree.getGroupRoot(t.forkOf);
|
|
541
|
+
if (r.optimisticMsgIds.length > 0) {
|
|
542
|
+
let t = r.optimisticMsgIds.at(-1);
|
|
543
|
+
t && (this._branchSelections.set(e, {
|
|
544
|
+
kind: "auto",
|
|
545
|
+
selectedId: t
|
|
546
|
+
}), this._cachedNodes = this._computeFlatNodes(), this._updateVisibleSnapshot(this._cachedNodes), this._emitter.emit("update"));
|
|
547
|
+
} else {
|
|
548
|
+
this._branchSelections.set(e, {
|
|
549
|
+
kind: "pending",
|
|
550
|
+
turnId: r.turnId
|
|
551
|
+
}), this._logger.debug("DefaultView.send(); deferring fork auto-selection", {
|
|
552
|
+
forkOf: t.forkOf,
|
|
553
|
+
groupRoot: e,
|
|
554
|
+
turnId: r.turnId
|
|
555
|
+
});
|
|
556
|
+
let n = this._tree.on("turn", (t) => {
|
|
557
|
+
if (t.type !== "x-ably-turn-end" || t.turnId !== r.turnId) return;
|
|
558
|
+
let i = this._branchSelections.get(e);
|
|
559
|
+
i?.kind === "pending" && i.turnId === r.turnId && this._branchSelections.delete(e), n();
|
|
560
|
+
let a = this._unsubs.indexOf(n);
|
|
561
|
+
a !== -1 && this._unsubs.splice(a, 1);
|
|
562
|
+
});
|
|
563
|
+
this._unsubs.push(n);
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
return r;
|
|
567
|
+
}
|
|
568
|
+
async regenerate(e, t) {
|
|
569
|
+
this._logger.trace("DefaultView.regenerate();", { messageId: e });
|
|
570
|
+
let n = this._tree.getNode(e);
|
|
571
|
+
if (!n) throw new s.ErrorInfo(`unable to regenerate; message not found in tree: ${e}`, O.InvalidArgument, 400);
|
|
572
|
+
let r = n.parentId;
|
|
573
|
+
return this.send([], {
|
|
574
|
+
...t,
|
|
575
|
+
body: {
|
|
576
|
+
history: this._getHistoryBefore(e),
|
|
577
|
+
...t?.body
|
|
578
|
+
},
|
|
579
|
+
forkOf: e,
|
|
580
|
+
parent: r
|
|
581
|
+
});
|
|
582
|
+
}
|
|
583
|
+
async edit(e, t, n) {
|
|
584
|
+
this._logger.trace("DefaultView.edit();", { messageId: e });
|
|
585
|
+
let r = this._tree.getNode(e);
|
|
586
|
+
if (!r) throw new s.ErrorInfo(`unable to edit; message not found in tree: ${e}`, O.InvalidArgument, 400);
|
|
587
|
+
let i = r.parentId;
|
|
588
|
+
return this.send(t, {
|
|
589
|
+
...n,
|
|
590
|
+
body: {
|
|
591
|
+
history: this._getHistoryBefore(e),
|
|
592
|
+
...n?.body
|
|
593
|
+
},
|
|
594
|
+
forkOf: e,
|
|
595
|
+
parent: i
|
|
596
|
+
});
|
|
597
|
+
}
|
|
598
|
+
async update(e, t, n) {
|
|
599
|
+
if (this._closed) throw new s.ErrorInfo("unable to update; view is closed", O.InvalidArgument, 400);
|
|
600
|
+
this._logger.trace("DefaultView.update();", {
|
|
601
|
+
msgId: e,
|
|
602
|
+
eventCount: t.length
|
|
603
|
+
});
|
|
604
|
+
let r = [{
|
|
605
|
+
kind: "event",
|
|
606
|
+
msgId: e,
|
|
607
|
+
events: t
|
|
608
|
+
}];
|
|
609
|
+
return this._sendDelegate([], n, this.flattenNodes(), r);
|
|
610
|
+
}
|
|
611
|
+
_getHistoryBefore(e) {
|
|
612
|
+
this._logger.trace("DefaultView._getHistoryBefore();", { messageId: e });
|
|
613
|
+
let t = this.flattenNodes(), n = t.findIndex((t) => t.msgId === e);
|
|
614
|
+
return n === -1 ? (this._logger.warn("DefaultView._getHistoryBefore(); target not in visible nodes, returning full list", { messageId: e }), t) : t.slice(0, n);
|
|
615
|
+
}
|
|
616
|
+
getActiveTurnIds() {
|
|
617
|
+
this._logger.trace("DefaultView.getActiveTurnIds();");
|
|
618
|
+
let e = this._tree.getActiveTurnIds();
|
|
619
|
+
if (this._withheldMsgIds.size === 0) return e;
|
|
620
|
+
let t = /* @__PURE__ */ new Map();
|
|
621
|
+
for (let [n, r] of e) {
|
|
622
|
+
let e = /* @__PURE__ */ new Set();
|
|
623
|
+
for (let t of r) this._lastVisibleTurnIds.has(t) && e.add(t);
|
|
624
|
+
e.size > 0 && t.set(n, e);
|
|
625
|
+
}
|
|
626
|
+
return t;
|
|
627
|
+
}
|
|
628
|
+
on(e, t) {
|
|
629
|
+
let n = t;
|
|
630
|
+
return this._emitter.on(e, n), () => {
|
|
631
|
+
this._emitter.off(e, n);
|
|
632
|
+
};
|
|
633
|
+
}
|
|
634
|
+
close() {
|
|
635
|
+
this._logger.info("DefaultView.close();"), this._closed = !0, this._loadingOlder = !1;
|
|
636
|
+
for (let e of this._unsubs) e();
|
|
637
|
+
this._unsubs.length = 0, this._emitter.off(), this._branchSelections.clear(), this._withheldMsgIds.clear(), this._withheldBuffer.length = 0, this._onClose?.();
|
|
638
|
+
}
|
|
639
|
+
async _loadFirstPage(e) {
|
|
640
|
+
let t = new Set(this._tree.flattenNodes(this._resolveSelections()).map((e) => e.msgId)), n = await ae(this._channel, this._codec, { limit: e }, this._logger);
|
|
641
|
+
if (this._closed) return;
|
|
642
|
+
let { newVisible: r, lastPage: i } = await this._loadUntilVisible(n, e, t);
|
|
643
|
+
if (this._closed) return;
|
|
644
|
+
this._lastHistoryPage = i, this._hasMoreHistory = i.hasNext();
|
|
645
|
+
let a = r.slice(-e), o = r.slice(0, -e);
|
|
646
|
+
for (let e of o) this._withheldMsgIds.add(e.msgId);
|
|
647
|
+
this._withheldBuffer.push(...o), this._releaseWithheld(a);
|
|
648
|
+
}
|
|
649
|
+
async _loadAndReveal(e, t) {
|
|
650
|
+
let n = new Set(this._tree.flattenNodes(this._resolveSelections()).map((e) => e.msgId)), { newVisible: r, lastPage: i } = await this._loadUntilVisible(e, t, n);
|
|
651
|
+
if (this._closed) return;
|
|
652
|
+
this._lastHistoryPage = i, this._hasMoreHistory = i.hasNext();
|
|
653
|
+
let a = r.slice(-t), o = r.slice(0, -t);
|
|
654
|
+
for (let e of o) this._withheldMsgIds.add(e.msgId);
|
|
655
|
+
this._withheldBuffer.push(...o), this._releaseWithheld(a);
|
|
656
|
+
}
|
|
657
|
+
_processHistoryPage(e) {
|
|
658
|
+
this._processingHistory = !0;
|
|
408
659
|
try {
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
660
|
+
for (let t of e.items) {
|
|
661
|
+
let e = t.headers[h];
|
|
662
|
+
e && this._tree.upsert(e, t.message, t.headers, t.serial);
|
|
663
|
+
}
|
|
664
|
+
for (let t of e.rawMessages) this._tree.emitAblyMessage(t);
|
|
665
|
+
} finally {
|
|
666
|
+
this._processingHistory = !1;
|
|
412
667
|
}
|
|
413
|
-
return this._isTerminal(t) && this.closeStream(e), !0;
|
|
414
668
|
}
|
|
415
|
-
|
|
416
|
-
|
|
669
|
+
async _loadUntilVisible(e, t, n) {
|
|
670
|
+
this._processHistoryPage(e);
|
|
671
|
+
let r = e, i = () => {
|
|
672
|
+
let e = 0;
|
|
673
|
+
for (let t of this._tree.flattenNodes(this._resolveSelections())) n.has(t.msgId) || e++;
|
|
674
|
+
return e;
|
|
675
|
+
};
|
|
676
|
+
for (; i() < t && r.hasNext();) {
|
|
677
|
+
let e = await r.next();
|
|
678
|
+
if (!e || this._closed) break;
|
|
679
|
+
this._processHistoryPage(e), r = e;
|
|
680
|
+
}
|
|
681
|
+
return {
|
|
682
|
+
newVisible: this._tree.flattenNodes(this._resolveSelections()).filter((e) => !n.has(e.msgId)),
|
|
683
|
+
lastPage: r
|
|
684
|
+
};
|
|
685
|
+
}
|
|
686
|
+
_releaseWithheld(e) {
|
|
687
|
+
for (let t of e) this._withheldMsgIds.delete(t.msgId);
|
|
688
|
+
e.length > 0 && (this._cachedNodes = this._computeFlatNodes(), this._updateVisibleSnapshot(this._cachedNodes), this._emitter.emit("update"));
|
|
689
|
+
}
|
|
690
|
+
_updateVisibleSnapshot(e) {
|
|
691
|
+
let t = e ?? this.flattenNodes();
|
|
692
|
+
this._lastVisibleIds = t.map((e) => e.msgId), this._lastVisibleMessages = t.map((e) => e.message), this._lastVisibleTurnIds = /* @__PURE__ */ new Set();
|
|
693
|
+
for (let e of t) {
|
|
694
|
+
let t = e.headers[m];
|
|
695
|
+
t && this._lastVisibleTurnIds.add(t);
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
_onTreeUpdate() {
|
|
699
|
+
if (this._processingHistory) return;
|
|
700
|
+
let e = this._tree.structuralVersion;
|
|
701
|
+
if (e === this._lastStructuralVersion) {
|
|
702
|
+
this._cachedNodes.some((e, t) => e.message !== this._lastVisibleMessages[t]) && (this._lastVisibleMessages = this._cachedNodes.map((e) => e.message), this._cachedNodes = [...this._cachedNodes], this._emitter.emit("update"));
|
|
703
|
+
return;
|
|
704
|
+
}
|
|
705
|
+
this._lastStructuralVersion = e, this._pinBranchSelections(), this._resolvePendingSelections();
|
|
706
|
+
let t = this._computeFlatNodes(), n = t.map((e) => e.msgId), r = t.map((e) => e.message);
|
|
707
|
+
this._visibleChanged(n, r) && (this._cachedNodes = t, this._updateVisibleSnapshot(t), this._emitter.emit("update"));
|
|
708
|
+
}
|
|
709
|
+
_resolveSelections() {
|
|
710
|
+
let e = /* @__PURE__ */ new Map();
|
|
711
|
+
for (let [t, n] of this._branchSelections) n.kind !== "pending" && e.set(t, n.selectedId);
|
|
712
|
+
return e;
|
|
713
|
+
}
|
|
714
|
+
_pinBranchSelections() {
|
|
715
|
+
for (let e of this._lastVisibleIds) {
|
|
716
|
+
if (!this._tree.hasSiblings(e)) continue;
|
|
717
|
+
let t = this._tree.getGroupRoot(e), n = this._branchSelections.get(t);
|
|
718
|
+
if (n?.kind === "pending") {
|
|
719
|
+
let r = this._tree.getSiblingNodes(e).at(-1);
|
|
720
|
+
r && r.msgId !== e && r.headers["x-ably-turn-id"] === n.turnId && (this._logger.debug("DefaultView._pinBranchSelections(); auto-selecting pending fork", {
|
|
721
|
+
msgId: e,
|
|
722
|
+
newestId: r.msgId,
|
|
723
|
+
turnId: n.turnId
|
|
724
|
+
}), this._branchSelections.set(t, {
|
|
725
|
+
kind: "auto",
|
|
726
|
+
selectedId: r.msgId
|
|
727
|
+
}));
|
|
728
|
+
continue;
|
|
729
|
+
}
|
|
730
|
+
n || this._branchSelections.set(t, {
|
|
731
|
+
kind: "pinned",
|
|
732
|
+
selectedId: e
|
|
733
|
+
});
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
_resolvePendingSelections() {
|
|
737
|
+
for (let [e, t] of this._branchSelections) {
|
|
738
|
+
if (t.kind !== "pending") continue;
|
|
739
|
+
let n = this._tree.getSiblingNodes(e);
|
|
740
|
+
if (n.length <= 1) continue;
|
|
741
|
+
let r = n.at(-1);
|
|
742
|
+
!r || r.msgId === e || r.headers["x-ably-turn-id"] === t.turnId && (this._logger.debug("DefaultView._resolvePendingSelections(); resolving off-branch pending", {
|
|
743
|
+
groupRoot: e,
|
|
744
|
+
newestId: r.msgId,
|
|
745
|
+
turnId: t.turnId
|
|
746
|
+
}), this._branchSelections.set(e, {
|
|
747
|
+
kind: "auto",
|
|
748
|
+
selectedId: r.msgId
|
|
749
|
+
}));
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
_onTreeAblyMessage(e) {
|
|
753
|
+
let t = P(e)[h];
|
|
754
|
+
if (!t) {
|
|
755
|
+
this._emitter.emit("ably-message", e);
|
|
756
|
+
return;
|
|
757
|
+
}
|
|
758
|
+
this._lastVisibleIds.includes(t) && this._emitter.emit("ably-message", e);
|
|
759
|
+
}
|
|
760
|
+
_onTreeTurn(e) {
|
|
761
|
+
if (this._lastVisibleTurnIds.has(e.turnId)) {
|
|
762
|
+
this._emitter.emit("turn", e);
|
|
763
|
+
return;
|
|
764
|
+
}
|
|
765
|
+
e.type === "x-ably-turn-start" && this._isTurnStartVisible(e) && (this._lastVisibleTurnIds.add(e.turnId), this._emitter.emit("turn", e));
|
|
766
|
+
}
|
|
767
|
+
_isTurnStartVisible(e) {
|
|
768
|
+
let { parent: t } = e;
|
|
769
|
+
return t === void 0 ? !0 : this._lastVisibleIds.includes(t);
|
|
417
770
|
}
|
|
418
|
-
|
|
771
|
+
_visibleChanged(e, t) {
|
|
772
|
+
if (e.length !== this._lastVisibleIds.length) return !0;
|
|
773
|
+
for (let [t, n] of e.entries()) if (n !== this._lastVisibleIds[t]) return !0;
|
|
774
|
+
for (let [e, n] of t.entries()) if (n !== this._lastVisibleMessages[e]) return !0;
|
|
775
|
+
return !1;
|
|
776
|
+
}
|
|
777
|
+
}, W = (e) => new oe(e), se = () => {}, G = /* @__PURE__ */ function(e) {
|
|
778
|
+
return e.READY = "ready", e.CLOSED = "closed", e;
|
|
779
|
+
}(G || {}), ce = class {
|
|
419
780
|
constructor(e) {
|
|
420
|
-
if (this._ownMsgIds = /* @__PURE__ */ new Set(), this._ownTurnIds = /* @__PURE__ */ new Set(), this.
|
|
781
|
+
if (this._ownMsgIds = /* @__PURE__ */ new Set(), this._ownTurnIds = /* @__PURE__ */ new Set(), this._turnMsgIds = /* @__PURE__ */ new Map(), this._turnObservers = /* @__PURE__ */ new Map(), this._closeResolvers = [], this._views = /* @__PURE__ */ new Set(), this._state = G.READY, this._pendingLocalEvents = [], this._channel = e.channel, this._codec = e.codec, this._clientId = e.clientId, this._api = e.api, this._credentials = e.credentials, this._headersFn = typeof e.headers == "function" ? e.headers : e.headers ? () => e.headers : void 0, this._bodyFn = typeof e.body == "function" ? e.body : e.body ? () => e.body : void 0, this._fetchFn = e.fetch ?? globalThis.fetch.bind(globalThis), this._logger = (e.logger ?? re({ logLevel: A.Silent })).withContext({ component: "ClientTransport" }), this._emitter = new k(this._logger), this._hasAttachedOnce = this._channel.state === "attached", this._tree = z(this._logger), this._view = W({
|
|
782
|
+
tree: this._tree,
|
|
783
|
+
channel: this._channel,
|
|
784
|
+
codec: this._codec,
|
|
785
|
+
sendDelegate: this._internalSend.bind(this),
|
|
786
|
+
logger: this._logger,
|
|
787
|
+
onClose: () => this._views.delete(this._view)
|
|
788
|
+
}), this._router = L(this._codec.isTerminal.bind(this._codec), this._logger), this._decoder = this._codec.createDecoder(), this._views.add(this._view), this.tree = this._tree, this.view = this._view, e.messages) {
|
|
421
789
|
let t;
|
|
422
790
|
for (let n of e.messages) {
|
|
423
|
-
let e =
|
|
424
|
-
t && (r[
|
|
791
|
+
let e = crypto.randomUUID(), r = { [h]: e };
|
|
792
|
+
t && (r[C] = t), this._tree.upsert(e, n, r), t = e;
|
|
425
793
|
}
|
|
426
|
-
this._emitter.emit("message");
|
|
427
794
|
}
|
|
428
795
|
this._onMessage = (e) => {
|
|
429
796
|
this._handleMessage(e);
|
|
430
|
-
}, this._attachPromise = this._channel.subscribe(this._onMessage)
|
|
797
|
+
}, this._attachPromise = this._channel.subscribe(this._onMessage), this._onChannelStateChange = (e) => {
|
|
798
|
+
this._handleChannelStateChange(e);
|
|
799
|
+
}, this._channel.on(this._onChannelStateChange);
|
|
431
800
|
}
|
|
432
801
|
_handleMessage(e) {
|
|
433
|
-
if (
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
if (
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
802
|
+
if (this._state !== G.CLOSED) try {
|
|
803
|
+
if (e.name === "x-ably-turn-start") {
|
|
804
|
+
let t = P(e), n = t[m], r = t["x-ably-turn-client-id"] ?? "";
|
|
805
|
+
if (n) {
|
|
806
|
+
this._tree.trackTurn(n, r);
|
|
807
|
+
let e = t[C], i = t[w];
|
|
808
|
+
this._tree.emitTurn({
|
|
809
|
+
type: E,
|
|
440
810
|
turnId: n,
|
|
441
|
-
clientId: r
|
|
442
|
-
|
|
443
|
-
|
|
811
|
+
clientId: r,
|
|
812
|
+
...e !== void 0 && { parent: e },
|
|
813
|
+
...i !== void 0 && { forkOf: i }
|
|
814
|
+
});
|
|
444
815
|
}
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
turnId: n,
|
|
457
|
-
clientId: r,
|
|
458
|
-
reason: i
|
|
459
|
-
});
|
|
816
|
+
this._tree.emitAblyMessage(e);
|
|
817
|
+
return;
|
|
818
|
+
}
|
|
819
|
+
if (e.name === "x-ably-turn-end") {
|
|
820
|
+
let t = P(e), n = t[m], r = t["x-ably-turn-client-id"] ?? "", i = t["x-ably-turn-reason"] ?? "complete";
|
|
821
|
+
if (n) {
|
|
822
|
+
this._router.closeStream(n), this._turnObservers.delete(n), this._tree.untrackTurn(n);
|
|
823
|
+
let e = this._turnMsgIds.get(n);
|
|
824
|
+
if (e) {
|
|
825
|
+
for (let t of e) this._ownMsgIds.delete(t);
|
|
826
|
+
this._turnMsgIds.delete(n);
|
|
460
827
|
}
|
|
461
|
-
|
|
828
|
+
this._ownTurnIds.delete(n), this._tree.emitTurn({
|
|
829
|
+
type: D,
|
|
830
|
+
turnId: n,
|
|
831
|
+
clientId: r,
|
|
832
|
+
reason: i
|
|
833
|
+
});
|
|
462
834
|
}
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
835
|
+
this._tree.emitAblyMessage(e);
|
|
836
|
+
return;
|
|
837
|
+
}
|
|
838
|
+
let t = this._decoder.decode(e), n = P(e), r = e.serial, i = n[v];
|
|
839
|
+
if (i) {
|
|
840
|
+
for (let e of t) e.kind === "event" && this._handleAmendmentEvent(i, e);
|
|
841
|
+
return;
|
|
469
842
|
}
|
|
843
|
+
let a = n[m];
|
|
844
|
+
a && this._updateTurnObserverHeaders(a, n, r);
|
|
845
|
+
for (let i of t) i.kind === "message" ? this._handleMessageOutput(i.message, n, r, e.action) : this._handleEventOutput(i, n);
|
|
846
|
+
this._tree.emitAblyMessage(e);
|
|
847
|
+
} catch (e) {
|
|
848
|
+
let t = e instanceof s.ErrorInfo ? e : void 0;
|
|
849
|
+
this._emitter.emit("error", new s.ErrorInfo(`unable to process channel message; ${e instanceof Error ? e.message : String(e)}`, O.TransportSubscriptionError, 500, t));
|
|
470
850
|
}
|
|
471
851
|
}
|
|
472
852
|
_handleMessageOutput(e, t, n, r) {
|
|
473
|
-
let i = t[
|
|
853
|
+
let i = t[h];
|
|
474
854
|
if (i && this._ownMsgIds.has(i)) {
|
|
475
855
|
this._upsertAndNotify(e, t, n);
|
|
476
856
|
return;
|
|
@@ -479,7 +859,7 @@ var a = (e) => {
|
|
|
479
859
|
}
|
|
480
860
|
_handleEventOutput(e, t) {
|
|
481
861
|
if (e.kind !== "event") return;
|
|
482
|
-
let n = e.event, r = t[
|
|
862
|
+
let n = e.event, r = t[m];
|
|
483
863
|
if (r) {
|
|
484
864
|
if (this._router.route(r, n)) {
|
|
485
865
|
this._accumulateAndEmit(r, e), this._codec.isTerminal(n) && this._turnObservers.delete(r);
|
|
@@ -488,9 +868,38 @@ var a = (e) => {
|
|
|
488
868
|
this._ownTurnIds.has(r) && !this._turnObservers.has(r) || (this._accumulateAndEmit(r, e), this._codec.isTerminal(n) && this._turnObservers.delete(r));
|
|
489
869
|
}
|
|
490
870
|
}
|
|
871
|
+
_handleAmendmentEvent(e, t) {
|
|
872
|
+
this._logger.trace("ClientTransport._handleAmendmentEvent();", { targetMsgId: e });
|
|
873
|
+
let n = this._tree.getNode(e);
|
|
874
|
+
if (!n) {
|
|
875
|
+
this._logger.debug("ClientTransport._handleAmendmentEvent(); target not found, dropping", { targetMsgId: e });
|
|
876
|
+
return;
|
|
877
|
+
}
|
|
878
|
+
let r = this._codec.createAccumulator();
|
|
879
|
+
r.initMessage(e, n.message), r.processOutputs([t]);
|
|
880
|
+
let i = r.messages.at(-1);
|
|
881
|
+
i && this._tree.upsert(e, i, n.headers, n.serial);
|
|
882
|
+
}
|
|
883
|
+
_handleChannelStateChange(e) {
|
|
884
|
+
if (this._state === G.CLOSED) return;
|
|
885
|
+
let { current: t, resumed: n } = e;
|
|
886
|
+
if (t === "attached" && !this._hasAttachedOnce) {
|
|
887
|
+
this._hasAttachedOnce = !0;
|
|
888
|
+
return;
|
|
889
|
+
}
|
|
890
|
+
if (!(t === "failed" || t === "suspended" || t === "detached" || t === "attached" && !n)) return;
|
|
891
|
+
this._logger.error("ClientTransport._handleChannelStateChange(); channel continuity lost", {
|
|
892
|
+
current: t,
|
|
893
|
+
resumed: n,
|
|
894
|
+
previous: e.previous
|
|
895
|
+
});
|
|
896
|
+
let r = new s.ErrorInfo(`unable to deliver events; channel continuity lost (${t}${t === "attached" ? ", resumed: false" : ""})`, O.ChannelContinuityLost, 500, e.reason);
|
|
897
|
+
for (let e of this._ownTurnIds) this._router.errorStream(e, r);
|
|
898
|
+
this._emitter.emit("error", r);
|
|
899
|
+
}
|
|
491
900
|
_upsertAndNotify(e, t, n) {
|
|
492
|
-
let r =
|
|
493
|
-
this._tree.upsert(
|
|
901
|
+
let r = t[h];
|
|
902
|
+
r && this._tree.upsert(r, e, t, n);
|
|
494
903
|
}
|
|
495
904
|
_updateTurnObserverHeaders(e, t, n) {
|
|
496
905
|
let r = this._turnObservers.get(e);
|
|
@@ -503,321 +912,445 @@ var a = (e) => {
|
|
|
503
912
|
_accumulateAndEmit(e, t) {
|
|
504
913
|
let n = this._turnObservers.get(e);
|
|
505
914
|
if (!n) return;
|
|
915
|
+
let r = n.headers[h];
|
|
916
|
+
if (r) {
|
|
917
|
+
let e = this._tree.getNode(r);
|
|
918
|
+
e && n.accumulator.initMessage(r, e.message);
|
|
919
|
+
}
|
|
506
920
|
n.accumulator.processOutputs([t]);
|
|
507
|
-
let
|
|
508
|
-
if (
|
|
509
|
-
let
|
|
921
|
+
let i = n.accumulator.messages;
|
|
922
|
+
if (i.length === 0) return;
|
|
923
|
+
let a;
|
|
510
924
|
try {
|
|
511
|
-
|
|
925
|
+
a = structuredClone(i.at(-1));
|
|
512
926
|
} catch {
|
|
513
|
-
|
|
927
|
+
a = i.at(-1);
|
|
928
|
+
}
|
|
929
|
+
if (a) {
|
|
930
|
+
let e = n.headers[h];
|
|
931
|
+
e && this._tree.upsert(e, a, { ...n.headers }, n.serial);
|
|
514
932
|
}
|
|
515
|
-
i && (this._tree.upsert(n.headers["x-ably-msg-id"] ?? this._codec.getMessageKey(i), i, { ...n.headers }, n.serial), this._emitter.emit("message"));
|
|
516
933
|
}
|
|
517
934
|
async _publishCancel(e) {
|
|
518
935
|
this._logger.trace("ClientTransport._publishCancel();", { filter: e });
|
|
519
936
|
let t = {};
|
|
520
|
-
e.turnId ? t[
|
|
521
|
-
name:
|
|
937
|
+
e.turnId ? t[y] = e.turnId : e.own ? t[b] = "true" : e.clientId ? t[S] = e.clientId : e.all && (t[x] = "true"), await this._channel.publish({
|
|
938
|
+
name: T,
|
|
522
939
|
extras: { headers: t }
|
|
523
940
|
});
|
|
524
941
|
}
|
|
525
942
|
_closeMatchingTurnStreams(e) {
|
|
526
|
-
|
|
527
|
-
else if (e.own) for (let e of this._ownTurnIds) this._router.closeStream(e);
|
|
528
|
-
else if (e.clientId) for (let [t, n] of this._turnClientIds) n === e.clientId && this._router.closeStream(t);
|
|
529
|
-
else e.turnId && this._router.closeStream(e.turnId);
|
|
943
|
+
for (let t of this._getMatchingTurnIds(e)) this._router.closeStream(t);
|
|
530
944
|
}
|
|
531
945
|
_getMatchingTurnIds(e) {
|
|
532
|
-
let t = /* @__PURE__ */ new Set();
|
|
533
|
-
if (e.all) for (let e of
|
|
534
|
-
else if (e.own)
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
_getHistoryBefore(e) {
|
|
546
|
-
let t = this._getMessagesWithHeaders(), n = t.findIndex((t) => t.headers?.[s] === e);
|
|
547
|
-
return n === -1 ? t : t.slice(0, n);
|
|
548
|
-
}
|
|
549
|
-
_processHistoryPage(e) {
|
|
550
|
-
for (let [t, n] of e.items.entries()) {
|
|
551
|
-
let r = e.itemHeaders?.[t] ?? {}, i = e.itemSerials?.[t], a = this._codec.getMessageKey(n), o = r["x-ably-msg-id"] ?? a;
|
|
552
|
-
this._tree.upsert(o, n, r, i);
|
|
553
|
-
}
|
|
554
|
-
this._emitter.emit("message"), e.rawMessages && e.rawMessages.length > 0 && (this._ablyMessages.unshift(...e.rawMessages), this._emitter.emit("ably-message"));
|
|
555
|
-
}
|
|
556
|
-
async _loadUntilVisible(e, t, n) {
|
|
557
|
-
this._processHistoryPage(e);
|
|
558
|
-
let r = e, i = () => {
|
|
559
|
-
let e = 0;
|
|
560
|
-
for (let t of this._tree.flatten()) n.has(this._codec.getMessageKey(t)) || e++;
|
|
561
|
-
return e;
|
|
562
|
-
};
|
|
563
|
-
for (; i() < t && r.hasNext();) {
|
|
564
|
-
let e = await r.next();
|
|
565
|
-
if (!e) break;
|
|
566
|
-
this._processHistoryPage(e), r = e;
|
|
946
|
+
let t = /* @__PURE__ */ new Set(), n = this._tree.getActiveTurnIds();
|
|
947
|
+
if (e.all) for (let e of n.values()) for (let n of e) t.add(n);
|
|
948
|
+
else if (e.own) {
|
|
949
|
+
let e = n.get(this._clientId ?? "");
|
|
950
|
+
if (e) for (let n of e) t.add(n);
|
|
951
|
+
} else if (e.clientId) {
|
|
952
|
+
let r = n.get(e.clientId);
|
|
953
|
+
if (r) for (let e of r) t.add(e);
|
|
954
|
+
} else if (e.turnId) {
|
|
955
|
+
for (let r of n.values()) if (r.has(e.turnId)) {
|
|
956
|
+
t.add(e.turnId);
|
|
957
|
+
break;
|
|
958
|
+
}
|
|
567
959
|
}
|
|
568
|
-
return
|
|
569
|
-
newVisible: this._tree.flatten().filter((e) => !n.has(this._codec.getMessageKey(e))),
|
|
570
|
-
lastPage: r
|
|
571
|
-
};
|
|
572
|
-
}
|
|
573
|
-
_releaseWithheld(e) {
|
|
574
|
-
for (let t of e) this._withheldKeys.delete(this._codec.getMessageKey(t));
|
|
575
|
-
e.length > 0 && this._emitter.emit("message");
|
|
960
|
+
return t;
|
|
576
961
|
}
|
|
577
|
-
|
|
578
|
-
if (this.
|
|
579
|
-
this._logger.trace("
|
|
580
|
-
let
|
|
581
|
-
|
|
582
|
-
|
|
962
|
+
createView() {
|
|
963
|
+
if (this._state === G.CLOSED) throw new s.ErrorInfo("unable to create view; transport is closed", O.TransportClosed, 400);
|
|
964
|
+
this._logger.trace("DefaultClientTransport.createView();");
|
|
965
|
+
let e = W({
|
|
966
|
+
tree: this._tree,
|
|
967
|
+
channel: this._channel,
|
|
968
|
+
codec: this._codec,
|
|
969
|
+
sendDelegate: this._internalSend.bind(this),
|
|
970
|
+
logger: this._logger,
|
|
971
|
+
onClose: () => this._views.delete(e)
|
|
972
|
+
});
|
|
973
|
+
return this._views.add(e), e;
|
|
974
|
+
}
|
|
975
|
+
async _internalSend(e, t, n, r) {
|
|
976
|
+
if (this._state === G.CLOSED || (await this._attachPromise, this._state === G.CLOSED)) throw new s.ErrorInfo("unable to send; transport is closed", O.TransportClosed, 400);
|
|
977
|
+
let i = this._channel.state;
|
|
978
|
+
if (i !== "attached" && i !== "attaching") throw new s.ErrorInfo(`unable to send; channel is ${i}`, O.ChannelNotReady, 400);
|
|
979
|
+
this._logger.trace("ClientTransport._internalSend();");
|
|
980
|
+
let a = Array.isArray(e) ? e : [e], o = crypto.randomUUID();
|
|
981
|
+
this._ownTurnIds.add(o), this._tree.trackTurn(o, this._clientId ?? "");
|
|
982
|
+
let c = this._pendingLocalEvents;
|
|
983
|
+
this._pendingLocalEvents = [], r && r.length > 0 && this._applyEventsToTree(r);
|
|
984
|
+
let l = [...c, ...r ?? []], u = /* @__PURE__ */ new Set(), d = [], f = n, p;
|
|
583
985
|
if (t?.parent === void 0 && !t?.forkOf) {
|
|
584
|
-
let e =
|
|
585
|
-
|
|
586
|
-
let t = e.at(-1);
|
|
587
|
-
if (t) {
|
|
588
|
-
let e = this._codec.getMessageKey(t);
|
|
589
|
-
u = this._tree.getNodeByKey(e)?.msgId ?? e;
|
|
590
|
-
}
|
|
591
|
-
}
|
|
986
|
+
let e = f.at(-1);
|
|
987
|
+
e && (p = e.msgId);
|
|
592
988
|
}
|
|
593
|
-
let
|
|
594
|
-
for (let e of
|
|
989
|
+
let m = t?.parent === void 0 ? p : t.parent;
|
|
990
|
+
for (let e of a) {
|
|
595
991
|
let n = crypto.randomUUID();
|
|
596
|
-
this._ownMsgIds.add(n),
|
|
597
|
-
let
|
|
992
|
+
this._ownMsgIds.add(n), u.add(n);
|
|
993
|
+
let r = t?.parent === void 0 ? p : t.parent, i = F({
|
|
598
994
|
role: "user",
|
|
599
|
-
turnId:
|
|
995
|
+
turnId: o,
|
|
600
996
|
msgId: n,
|
|
601
997
|
turnClientId: this._clientId,
|
|
602
|
-
parent:
|
|
998
|
+
parent: r,
|
|
603
999
|
forkOf: t?.forkOf
|
|
604
1000
|
});
|
|
605
|
-
this._upsertAndNotify(e,
|
|
606
|
-
|
|
607
|
-
[s]: n,
|
|
608
|
-
[l]: "user"
|
|
609
|
-
};
|
|
610
|
-
i && (d[m] = i), o.push({
|
|
1001
|
+
this._upsertAndNotify(e, i), d.push({
|
|
1002
|
+
kind: "message",
|
|
611
1003
|
message: e,
|
|
612
|
-
|
|
613
|
-
|
|
1004
|
+
msgId: n,
|
|
1005
|
+
parentId: r,
|
|
1006
|
+
forkOf: t?.forkOf,
|
|
1007
|
+
headers: i,
|
|
1008
|
+
serial: void 0
|
|
1009
|
+
}), t?.parent === void 0 && !t?.forkOf && (p = n);
|
|
614
1010
|
}
|
|
615
|
-
this._turnMsgIds.set(
|
|
616
|
-
let
|
|
1011
|
+
this._turnMsgIds.set(o, u);
|
|
1012
|
+
let h = this._router.createStream(o), g = this._headersFn?.() ?? {}, _ = {
|
|
617
1013
|
...this._bodyFn?.() ?? {},
|
|
618
|
-
history:
|
|
1014
|
+
history: f,
|
|
619
1015
|
...t?.body,
|
|
620
|
-
turnId:
|
|
1016
|
+
turnId: o,
|
|
621
1017
|
clientId: this._clientId,
|
|
622
|
-
messages:
|
|
1018
|
+
messages: d,
|
|
623
1019
|
...t?.forkOf !== void 0 && { forkOf: t.forkOf },
|
|
624
|
-
...
|
|
625
|
-
|
|
626
|
-
|
|
1020
|
+
...m !== void 0 && { parent: m },
|
|
1021
|
+
...l.length > 0 && { events: l }
|
|
1022
|
+
}, v = {
|
|
1023
|
+
...g,
|
|
627
1024
|
...t?.headers
|
|
628
1025
|
};
|
|
629
1026
|
return this._fetchFn(this._api, {
|
|
630
1027
|
method: "POST",
|
|
631
1028
|
headers: {
|
|
632
1029
|
"Content-Type": "application/json",
|
|
633
|
-
...
|
|
1030
|
+
...v
|
|
634
1031
|
},
|
|
635
|
-
body: JSON.stringify(
|
|
1032
|
+
body: JSON.stringify(_),
|
|
636
1033
|
...this._credentials ? { credentials: this._credentials } : {}
|
|
637
1034
|
}).then((e) => {
|
|
638
|
-
|
|
1035
|
+
if (!e.ok) {
|
|
1036
|
+
let t = new s.ErrorInfo(`unable to send; HTTP POST to ${this._api} returned ${String(e.status)} ${e.statusText}`, O.TransportSendFailed, e.status);
|
|
1037
|
+
this._emitter.emit("error", t), this._router.errorStream(o, t);
|
|
1038
|
+
}
|
|
639
1039
|
}).catch((e) => {
|
|
640
|
-
let t = e instanceof
|
|
641
|
-
this._emitter.emit("error",
|
|
1040
|
+
let t = e instanceof s.ErrorInfo ? e : void 0, n = new s.ErrorInfo(`unable to send; HTTP POST to ${this._api} failed: ${e instanceof Error ? e.message : String(e)}`, O.TransportSendFailed, 500, t);
|
|
1041
|
+
this._emitter.emit("error", n), this._router.errorStream(o, n);
|
|
642
1042
|
}), {
|
|
643
|
-
stream:
|
|
644
|
-
turnId:
|
|
645
|
-
cancel: async () => this.cancel({ turnId:
|
|
1043
|
+
stream: h,
|
|
1044
|
+
turnId: o,
|
|
1045
|
+
cancel: async () => this.cancel({ turnId: o }),
|
|
1046
|
+
optimisticMsgIds: [...u]
|
|
646
1047
|
};
|
|
647
1048
|
}
|
|
648
|
-
async regenerate(e, t) {
|
|
649
|
-
this._logger.trace("ClientTransport.regenerate();", { messageId: e });
|
|
650
|
-
let n = this._tree.getNode(e)?.parentId;
|
|
651
|
-
return this.send([], {
|
|
652
|
-
...t,
|
|
653
|
-
body: {
|
|
654
|
-
history: this._getHistoryBefore(e),
|
|
655
|
-
...t?.body
|
|
656
|
-
},
|
|
657
|
-
forkOf: e,
|
|
658
|
-
parent: n
|
|
659
|
-
});
|
|
660
|
-
}
|
|
661
|
-
async edit(e, t, n) {
|
|
662
|
-
this._logger.trace("ClientTransport.edit();", { messageId: e });
|
|
663
|
-
let r = this._tree.getNode(e)?.parentId;
|
|
664
|
-
return this.send(t, {
|
|
665
|
-
...n,
|
|
666
|
-
body: {
|
|
667
|
-
history: this._getHistoryBefore(e),
|
|
668
|
-
...n?.body
|
|
669
|
-
},
|
|
670
|
-
forkOf: e,
|
|
671
|
-
parent: r
|
|
672
|
-
});
|
|
673
|
-
}
|
|
674
1049
|
async cancel(e) {
|
|
675
|
-
if (this.
|
|
1050
|
+
if (this._state === G.CLOSED) return;
|
|
676
1051
|
let t = e ?? { own: !0 };
|
|
677
1052
|
this._logger.debug("ClientTransport.cancel();", { filter: t }), await this._publishCancel(t), this._closeMatchingTurnStreams(t);
|
|
678
1053
|
}
|
|
1054
|
+
stageEvents(e, t) {
|
|
1055
|
+
if (this._logger.trace("ClientTransport.stageEvents();", {
|
|
1056
|
+
msgId: e,
|
|
1057
|
+
eventCount: t.length
|
|
1058
|
+
}), this._state === G.CLOSED) {
|
|
1059
|
+
this._logger.warn("ClientTransport.stageEvents(); transport is closed", { msgId: e });
|
|
1060
|
+
return;
|
|
1061
|
+
}
|
|
1062
|
+
if (!this._tree.getNode(e)) {
|
|
1063
|
+
this._logger.warn("ClientTransport.stageEvents(); msgId not found in tree", { msgId: e });
|
|
1064
|
+
return;
|
|
1065
|
+
}
|
|
1066
|
+
if (t.length === 0) return;
|
|
1067
|
+
let n = {
|
|
1068
|
+
kind: "event",
|
|
1069
|
+
msgId: e,
|
|
1070
|
+
events: t
|
|
1071
|
+
};
|
|
1072
|
+
this._applyEventsToTree([n]), this._pendingLocalEvents.push(n);
|
|
1073
|
+
}
|
|
1074
|
+
stageMessage(e, t) {
|
|
1075
|
+
if (this._logger.trace("ClientTransport.stageMessage();", { msgId: e }), this._state === G.CLOSED) {
|
|
1076
|
+
this._logger.warn("ClientTransport.stageMessage(); transport is closed", { msgId: e });
|
|
1077
|
+
return;
|
|
1078
|
+
}
|
|
1079
|
+
let n = this._tree.getNode(e);
|
|
1080
|
+
if (!n) {
|
|
1081
|
+
this._logger.warn("ClientTransport.stageMessage(); msgId not found in tree", { msgId: e });
|
|
1082
|
+
return;
|
|
1083
|
+
}
|
|
1084
|
+
this._tree.upsert(e, t, n.headers, n.serial);
|
|
1085
|
+
}
|
|
1086
|
+
_applyEventsToTree(e) {
|
|
1087
|
+
for (let t of e) {
|
|
1088
|
+
let e = this._tree.getNode(t.msgId);
|
|
1089
|
+
if (!e) continue;
|
|
1090
|
+
let n = t.events.map((e) => ({
|
|
1091
|
+
kind: "event",
|
|
1092
|
+
event: e,
|
|
1093
|
+
messageId: t.msgId
|
|
1094
|
+
})), r = this._codec.createAccumulator();
|
|
1095
|
+
r.initMessage(t.msgId, e.message), r.processOutputs(n);
|
|
1096
|
+
let i = r.messages.at(-1);
|
|
1097
|
+
i && this._tree.upsert(t.msgId, i, e.headers, e.serial);
|
|
1098
|
+
}
|
|
1099
|
+
}
|
|
679
1100
|
async waitForTurn(e) {
|
|
680
|
-
if (this.
|
|
1101
|
+
if (this._state === G.CLOSED) return;
|
|
681
1102
|
let t = e ?? { own: !0 }, n = this._getMatchingTurnIds(t);
|
|
682
1103
|
if (n.size !== 0) return this._logger.debug("ClientTransport.waitForTurn();", { turnIds: [...n] }), new Promise((e) => {
|
|
683
|
-
let t = (
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
1104
|
+
let t = !1, r = () => {
|
|
1105
|
+
if (t) return;
|
|
1106
|
+
t = !0, i();
|
|
1107
|
+
let n = this._closeResolvers.indexOf(r);
|
|
1108
|
+
n !== -1 && this._closeResolvers.splice(n, 1), e();
|
|
1109
|
+
}, i = this._tree.on("turn", (e) => {
|
|
1110
|
+
e.type === "x-ably-turn-end" && (n.delete(e.turnId), n.size === 0 && r());
|
|
1111
|
+
});
|
|
1112
|
+
this._closeResolvers.push(r);
|
|
687
1113
|
});
|
|
688
1114
|
}
|
|
689
1115
|
on(e, t) {
|
|
690
|
-
if (this.
|
|
1116
|
+
if (this._state === G.CLOSED) return se;
|
|
691
1117
|
let n = t;
|
|
692
1118
|
return this._emitter.on(e, n), () => {
|
|
693
1119
|
this._emitter.off(e, n);
|
|
694
1120
|
};
|
|
695
1121
|
}
|
|
696
|
-
getTree() {
|
|
697
|
-
return this._tree;
|
|
698
|
-
}
|
|
699
|
-
getActiveTurnIds() {
|
|
700
|
-
let e = /* @__PURE__ */ new Map();
|
|
701
|
-
for (let [t, n] of this._turnClientIds) {
|
|
702
|
-
let r = e.get(n);
|
|
703
|
-
r || (r = /* @__PURE__ */ new Set(), e.set(n, r)), r.add(t);
|
|
704
|
-
}
|
|
705
|
-
return e;
|
|
706
|
-
}
|
|
707
|
-
getMessageHeaders(e) {
|
|
708
|
-
let t = this._codec.getMessageKey(e);
|
|
709
|
-
return this._tree.getNodeByKey(t)?.headers;
|
|
710
|
-
}
|
|
711
|
-
getMessages() {
|
|
712
|
-
return this._withheldKeys.size === 0 ? this._tree.flatten() : this._tree.flatten().filter((e) => !this._withheldKeys.has(this._codec.getMessageKey(e)));
|
|
713
|
-
}
|
|
714
|
-
getMessagesWithHeaders() {
|
|
715
|
-
return this._getMessagesWithHeaders();
|
|
716
|
-
}
|
|
717
|
-
getAblyMessages() {
|
|
718
|
-
return [...this._ablyMessages];
|
|
719
|
-
}
|
|
720
|
-
async history(e) {
|
|
721
|
-
if (this._closed) throw new i.ErrorInfo("unable to load history; transport is closed", b.TransportClosed, 400);
|
|
722
|
-
this._logger.trace("ClientTransport.history();", { limit: e?.limit });
|
|
723
|
-
let t = e?.limit ?? 100, n = new Set(this._tree.flatten().map((e) => this._codec.getMessageKey(e))), r = await I(this._channel, this._codec, e, this._logger), a = await this._loadUntilVisible(r, t, n);
|
|
724
|
-
r = a.lastPage;
|
|
725
|
-
let o = a.newVisible;
|
|
726
|
-
for (let e of o) this._withheldKeys.add(this._codec.getMessageKey(e));
|
|
727
|
-
let s = o.slice(-t), c = o.slice(0, -t);
|
|
728
|
-
this._releaseWithheld(s);
|
|
729
|
-
let l = (e) => ({
|
|
730
|
-
items: e,
|
|
731
|
-
hasNext: () => c.length > 0 || r.hasNext(),
|
|
732
|
-
next: async () => {
|
|
733
|
-
if (c.length > 0) {
|
|
734
|
-
let e = c.splice(-t, t);
|
|
735
|
-
return this._releaseWithheld(e), l(e);
|
|
736
|
-
}
|
|
737
|
-
if (!r.hasNext()) return;
|
|
738
|
-
let e = await r.next();
|
|
739
|
-
if (!e) return;
|
|
740
|
-
let i = new Set(n);
|
|
741
|
-
for (let e of this._tree.flatten()) i.add(this._codec.getMessageKey(e));
|
|
742
|
-
let a = await this._loadUntilVisible(e, t, i);
|
|
743
|
-
r = a.lastPage;
|
|
744
|
-
let o = a.newVisible;
|
|
745
|
-
for (let e of o) this._withheldKeys.add(this._codec.getMessageKey(e));
|
|
746
|
-
let s = o.splice(-t, t);
|
|
747
|
-
if (c.push(...o), this._releaseWithheld(s), s.length !== 0) return l(s);
|
|
748
|
-
}
|
|
749
|
-
});
|
|
750
|
-
return l(s);
|
|
751
|
-
}
|
|
752
1122
|
async close(e) {
|
|
753
|
-
if (
|
|
754
|
-
if (this.
|
|
1123
|
+
if (this._state !== G.CLOSED) {
|
|
1124
|
+
if (this._state = G.CLOSED, this._logger.info("ClientTransport.close();"), e?.cancel) {
|
|
755
1125
|
try {
|
|
756
1126
|
await this._publishCancel(e.cancel);
|
|
757
1127
|
} catch {}
|
|
758
1128
|
this._closeMatchingTurnStreams(e.cancel);
|
|
759
1129
|
}
|
|
760
|
-
this._channel.unsubscribe(this._onMessage);
|
|
1130
|
+
this._channel.unsubscribe(this._onMessage), this._channel.off(this._onChannelStateChange);
|
|
761
1131
|
for (let e of this._ownTurnIds) this._router.closeStream(e);
|
|
762
|
-
this._turnObservers.clear(), this._emitter.off()
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
}
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
1132
|
+
this._turnObservers.clear(), this._emitter.off();
|
|
1133
|
+
for (let e of this._views) e.close();
|
|
1134
|
+
this._views.clear();
|
|
1135
|
+
for (let e of this._closeResolvers) e();
|
|
1136
|
+
this._closeResolvers.length = 0, this._ownTurnIds.clear(), this._ownMsgIds.clear(), this._turnMsgIds.clear();
|
|
1137
|
+
}
|
|
1138
|
+
}
|
|
1139
|
+
}, le = (e) => new ce(e), ue = ({ channelName: e, children: t, ...o }) => {
|
|
1140
|
+
let { channel: c } = l({ channelName: e }), p = a(void 0), m = a(e), h = a([]), g = a(!1), _ = a(void 0);
|
|
1141
|
+
if (!(p.current || _.current) || m.current !== e) {
|
|
1142
|
+
m.current = e, p.current && h.current.push(p.current);
|
|
1143
|
+
try {
|
|
1144
|
+
p.current = le({
|
|
1145
|
+
...o,
|
|
1146
|
+
channel: c
|
|
1147
|
+
}), _.current = void 0;
|
|
1148
|
+
} catch (e) {
|
|
1149
|
+
p.current = void 0, _.current = e instanceof s.ErrorInfo ? e : new s.ErrorInfo("Unknown error while creating transport", O.BadRequest, 400);
|
|
1150
|
+
}
|
|
1151
|
+
}
|
|
1152
|
+
let v = n(d), y = p.current, b = _.current, x = i(() => ({
|
|
1153
|
+
transport: y,
|
|
1154
|
+
error: b
|
|
1155
|
+
}), [y, b]), S = i(() => ({
|
|
1156
|
+
...v,
|
|
1157
|
+
[e]: x
|
|
1158
|
+
}), [
|
|
1159
|
+
e,
|
|
1160
|
+
v,
|
|
1161
|
+
x
|
|
1162
|
+
]);
|
|
1163
|
+
return r(() => () => {
|
|
1164
|
+
for (let e of h.current) e.close();
|
|
1165
|
+
}, [e]), r(() => (g.current = !1, () => {
|
|
1166
|
+
g.current = !0, Promise.resolve().then(() => {
|
|
1167
|
+
g.current && p.current?.close();
|
|
1168
|
+
});
|
|
1169
|
+
}), []), /* @__PURE__ */ u(d.Provider, {
|
|
1170
|
+
value: S,
|
|
1171
|
+
children: /* @__PURE__ */ u(f.Provider, {
|
|
1172
|
+
value: x,
|
|
1173
|
+
children: t
|
|
1174
|
+
})
|
|
1175
|
+
});
|
|
1176
|
+
}, K = (e) => /* @__PURE__ */ u(c, {
|
|
1177
|
+
channelName: e.channelName,
|
|
1178
|
+
children: /* @__PURE__ */ u(ue, { ...e })
|
|
1179
|
+
}), q = ({ transport: e, skip: t } = {}) => {
|
|
1180
|
+
let i = n(f), s = t ? void 0 : e ?? i?.transport, [c, l] = o([]), u = a([]);
|
|
1181
|
+
return r(() => {
|
|
1182
|
+
if (u.current = [], l([]), s) return s.tree.on("ably-message", (e) => {
|
|
1183
|
+
let t = [...u.current, e];
|
|
1184
|
+
u.current = t, l(t);
|
|
1185
|
+
});
|
|
1186
|
+
}, [s]), c;
|
|
1187
|
+
}, J = ({ transport: e } = {}) => {
|
|
1188
|
+
let t = n(f), i = e ?? t?.transport, [a, s] = o(() => /* @__PURE__ */ new Map());
|
|
1189
|
+
return r(() => {
|
|
1190
|
+
if (i) return s(i.tree.getActiveTurnIds()), i.tree.on("turn", (e) => {
|
|
1191
|
+
s((t) => {
|
|
1192
|
+
let n = new Map(t);
|
|
1193
|
+
if (e.type === "x-ably-turn-start") {
|
|
1194
|
+
let t = new Set(n.get(e.clientId));
|
|
1195
|
+
t.add(e.turnId), n.set(e.clientId, t);
|
|
1196
|
+
} else {
|
|
1197
|
+
let t = n.get(e.clientId);
|
|
1198
|
+
if (t) {
|
|
1199
|
+
let r = new Set(t);
|
|
1200
|
+
r.delete(e.turnId), r.size === 0 ? n.delete(e.clientId) : n.set(e.clientId, r);
|
|
1201
|
+
}
|
|
1202
|
+
}
|
|
1203
|
+
return n;
|
|
1204
|
+
});
|
|
1205
|
+
});
|
|
1206
|
+
}, [i]), a;
|
|
1207
|
+
}, Y = {
|
|
1208
|
+
get tree() {
|
|
1209
|
+
throw new s.ErrorInfo("unable to access tree; hook is skipped", O.InvalidArgument, 400);
|
|
1210
|
+
},
|
|
1211
|
+
get view() {
|
|
1212
|
+
throw new s.ErrorInfo("unable to access view; hook is skipped", O.InvalidArgument, 400);
|
|
1213
|
+
},
|
|
1214
|
+
createView: () => {
|
|
1215
|
+
throw new s.ErrorInfo("unable to create view; hook is skipped", O.InvalidArgument, 400);
|
|
1216
|
+
},
|
|
1217
|
+
cancel: () => {
|
|
1218
|
+
throw new s.ErrorInfo("unable to cancel; hook is skipped", O.InvalidArgument, 400);
|
|
1219
|
+
},
|
|
1220
|
+
stageEvents: () => {
|
|
1221
|
+
throw new s.ErrorInfo("unable to stage events; hook is skipped", O.InvalidArgument, 400);
|
|
1222
|
+
},
|
|
1223
|
+
stageMessage: () => {
|
|
1224
|
+
throw new s.ErrorInfo("unable to stage message; hook is skipped", O.InvalidArgument, 400);
|
|
1225
|
+
},
|
|
1226
|
+
waitForTurn: () => {
|
|
1227
|
+
throw new s.ErrorInfo("unable to wait for turn; hook is skipped", O.InvalidArgument, 400);
|
|
1228
|
+
},
|
|
1229
|
+
on: () => {
|
|
1230
|
+
throw new s.ErrorInfo("unable to subscribe; hook is skipped", O.InvalidArgument, 400);
|
|
1231
|
+
},
|
|
1232
|
+
close: () => {
|
|
1233
|
+
throw new s.ErrorInfo("unable to close; hook is skipped", O.InvalidArgument, 400);
|
|
1234
|
+
}
|
|
1235
|
+
}, X = ({ channelName: e, skip: t, onError: i } = {}) => {
|
|
1236
|
+
let o = n(d), c = n(f), l = a(i);
|
|
1237
|
+
l.current = i;
|
|
1238
|
+
let u = t ? void 0 : e === void 0 ? c?.transport : o[e]?.transport;
|
|
1239
|
+
if (r(() => {
|
|
1240
|
+
if (u) return u.on("error", (e) => {
|
|
1241
|
+
l.current?.(e);
|
|
1242
|
+
});
|
|
1243
|
+
}, [u]), t) return { transport: Y };
|
|
1244
|
+
if (e !== void 0) {
|
|
1245
|
+
let t = o[e];
|
|
1246
|
+
return t ? t.transport ? { transport: t.transport } : {
|
|
1247
|
+
transport: Y,
|
|
1248
|
+
transportError: t.error
|
|
1249
|
+
} : {
|
|
1250
|
+
transport: Y,
|
|
1251
|
+
transportError: new s.ErrorInfo(`unable to use transport; no TransportProvider found for channelName "${e}"`, O.BadRequest, 400)
|
|
1252
|
+
};
|
|
1253
|
+
}
|
|
1254
|
+
return c ? c.transport ? { transport: c.transport } : {
|
|
1255
|
+
transport: Y,
|
|
1256
|
+
transportError: c.error
|
|
1257
|
+
} : {
|
|
1258
|
+
transport: Y,
|
|
1259
|
+
transportError: new s.ErrorInfo("unable to use transport; no TransportProvider found in the tree", O.BadRequest, 400)
|
|
780
1260
|
};
|
|
781
|
-
},
|
|
782
|
-
let [o,
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
try {
|
|
788
|
-
let t = await f.current.history(e);
|
|
789
|
-
d.current = t, s(t.hasNext());
|
|
790
|
-
} finally {
|
|
791
|
-
u.current = !1, l(!1);
|
|
792
|
-
}
|
|
1261
|
+
}, Z = ({ transport: e, view: c, limit: l, skip: u } = {}) => {
|
|
1262
|
+
let d = n(f), p = u ? void 0 : e ?? d?.transport, m = u ? void 0 : c ?? p?.view, [h, g] = o(() => m?.flattenNodes() ?? []), [_, v] = o(() => m?.hasOlder() ?? !1), [y, b] = o(!1), [x, S] = o(), C = a(!1), w = l !== void 0, T = a(!1);
|
|
1263
|
+
r(() => {
|
|
1264
|
+
if (!m) {
|
|
1265
|
+
g([]), v(!1), S(void 0);
|
|
1266
|
+
return;
|
|
793
1267
|
}
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
1268
|
+
return T.current = !1, g(m.flattenNodes()), v(m.hasOlder()), S(void 0), m.on("update", () => {
|
|
1269
|
+
g(m.flattenNodes()), v(m.hasOlder());
|
|
1270
|
+
});
|
|
1271
|
+
}, [m]);
|
|
1272
|
+
let E = t(async () => {
|
|
1273
|
+
if (!(!m || C.current)) {
|
|
1274
|
+
C.current = !0, b(!0);
|
|
798
1275
|
try {
|
|
799
|
-
|
|
800
|
-
|
|
1276
|
+
await m.loadOlder(l), S(void 0);
|
|
1277
|
+
} catch (e) {
|
|
1278
|
+
e instanceof s.ErrorInfo ? S(e) : S(new s.ErrorInfo("Unknown error loading older messages", O.BadRequest, 400));
|
|
801
1279
|
} finally {
|
|
802
|
-
|
|
1280
|
+
C.current = !1, b(!1);
|
|
803
1281
|
}
|
|
804
1282
|
}
|
|
805
|
-
}, [
|
|
806
|
-
return
|
|
807
|
-
!
|
|
808
|
-
}, [
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
1283
|
+
}, [m, l]);
|
|
1284
|
+
return r(() => {
|
|
1285
|
+
!w || T.current || !m || (T.current = !0, E());
|
|
1286
|
+
}, [
|
|
1287
|
+
w,
|
|
1288
|
+
m,
|
|
1289
|
+
E
|
|
1290
|
+
]), {
|
|
1291
|
+
messages: i(() => h.map((e) => e.message), [h]),
|
|
1292
|
+
nodes: h,
|
|
1293
|
+
hasOlder: _,
|
|
1294
|
+
loading: y,
|
|
1295
|
+
loadError: x,
|
|
1296
|
+
loadOlder: E,
|
|
1297
|
+
select: t((e, t) => {
|
|
1298
|
+
m?.select(e, t);
|
|
1299
|
+
}, [m]),
|
|
1300
|
+
getSelectedIndex: t((e) => m?.getSelectedIndex(e) ?? 0, [m]),
|
|
1301
|
+
getSiblings: t((e) => m?.getSiblings(e) ?? [], [m]),
|
|
1302
|
+
hasSiblings: t((e) => m?.hasSiblings(e) ?? !1, [m]),
|
|
1303
|
+
getNode: t((e) => m?.getNode(e), [m]),
|
|
1304
|
+
send: t(async (e, t) => {
|
|
1305
|
+
if (!m) throw new s.ErrorInfo("unable to send; view is not available", O.InvalidArgument, 400);
|
|
1306
|
+
return m.send(e, t);
|
|
1307
|
+
}, [m]),
|
|
1308
|
+
regenerate: t(async (e, t) => {
|
|
1309
|
+
if (!m) throw new s.ErrorInfo("unable to regenerate; view is not available", O.InvalidArgument, 400);
|
|
1310
|
+
return m.regenerate(e, t);
|
|
1311
|
+
}, [m]),
|
|
1312
|
+
edit: t(async (e, t, n) => {
|
|
1313
|
+
if (!m) throw new s.ErrorInfo("unable to edit; view is not available", O.InvalidArgument, 400);
|
|
1314
|
+
return m.edit(e, t, n);
|
|
1315
|
+
}, [m]),
|
|
1316
|
+
update: t(async (e, t, n) => {
|
|
1317
|
+
if (!m) throw new s.ErrorInfo("unable to update; view is not available", O.InvalidArgument, 400);
|
|
1318
|
+
return m.update(e, t, n);
|
|
1319
|
+
}, [m])
|
|
1320
|
+
};
|
|
1321
|
+
}, Q = ({ transport: e, limit: t, skip: i } = {}) => {
|
|
1322
|
+
let a = n(f), s = i ? void 0 : e ?? a?.transport, [c, l] = o();
|
|
1323
|
+
return r(() => {
|
|
1324
|
+
if (!s) {
|
|
1325
|
+
l(void 0);
|
|
1326
|
+
return;
|
|
1327
|
+
}
|
|
1328
|
+
let e = s.createView();
|
|
1329
|
+
return l(e), () => {
|
|
1330
|
+
e.close();
|
|
1331
|
+
};
|
|
1332
|
+
}, [s]), Z({
|
|
1333
|
+
view: c,
|
|
1334
|
+
limit: t,
|
|
1335
|
+
skip: i
|
|
1336
|
+
});
|
|
1337
|
+
}, $ = ({ transport: e } = {}) => {
|
|
1338
|
+
let r = n(f), i = e ?? r?.transport;
|
|
1339
|
+
return {
|
|
1340
|
+
getSiblings: t((e) => i?.tree.getSiblings(e) ?? [], [i]),
|
|
1341
|
+
hasSiblings: t((e) => i?.tree.hasSiblings(e) ?? !1, [i]),
|
|
1342
|
+
getNode: t((e) => i?.tree.getNode(e), [i])
|
|
813
1343
|
};
|
|
814
|
-
},
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
1344
|
+
}, de = () => ({
|
|
1345
|
+
TransportProvider: K,
|
|
1346
|
+
useClientTransport: (e) => X(e ?? {}),
|
|
1347
|
+
useView: (e) => Z(e ?? {}),
|
|
1348
|
+
useActiveTurns: (e) => J(e ?? {}),
|
|
1349
|
+
useTree: (e) => $(e ?? {}),
|
|
1350
|
+
useAblyMessages: (e) => q(e ?? {}),
|
|
1351
|
+
useCreateView: (e) => Q(e ?? {})
|
|
1352
|
+
});
|
|
820
1353
|
//#endregion
|
|
821
|
-
export {
|
|
1354
|
+
export { f as NearestTransportContext, K as TransportProvider, de as createTransportHooks, q as useAblyMessages, J as useActiveTurns, X as useClientTransport, Q as useCreateView, $ as useTree, Z as useView };
|
|
822
1355
|
|
|
823
1356
|
//# sourceMappingURL=ably-ai-transport-react.js.map
|