@vite-hub/comments 0.3.1 → 0.5.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 +37 -15
- package/dist/anchor.d.ts +2 -2
- package/dist/{client-diagnostics-CcFpkbAk.js → client-diagnostics-Bubm7Shd.js} +20 -5
- package/dist/client-files-CJZl5Kc4.js +53 -0
- package/dist/client.d.ts +567 -13
- package/dist/client.js +1209 -34
- package/dist/{console-h4KlhIik.d.ts → console-D1lwNMZn.d.ts} +2 -0
- package/dist/{migrate-BDQ8xqUs.js → database-6hXRnqDE.js} +47 -2
- package/dist/database-CTo7mx6t.d.ts +575 -0
- package/dist/database.d.ts +2 -1
- package/dist/database.js +2 -2
- package/dist/{define-43mMR39K.js → define-BMBSVbD9.js} +1 -1
- package/dist/{define-B3TF9zzn.d.ts → define-CmIFU96x.d.ts} +8 -4
- package/dist/{errors-4gelGVmK.js → errors-BJTZ9iyv.js} +24 -0
- package/dist/{errors-C6qptPoG.js → errors-Dq4AHjza.js} +23 -3
- package/dist/index-RiHzmV16.d.ts +194 -0
- package/dist/index.d.ts +6 -7
- package/dist/index.js +57 -9
- package/dist/{install-W9ghhGoh.d.ts → install-BGGQKbQ4.d.ts} +9 -3
- package/dist/{install-BcMPgPk_.js → install-DrAse1Fa.js} +912 -60
- package/dist/{installed-B-PfERBU.js → installed-C3EoqJEY.js} +7 -6
- package/dist/{operations-GLeOqgMt.d.ts → operations-BKhUWYkb.d.ts} +10 -5
- package/dist/{operations-D4A1Fcgf.d.ts → operations-VyyNZusz.d.ts} +13 -2
- package/dist/{operations-DMMIfYPW.js → operations-lsVKE0GH.js} +312 -219
- package/dist/operations.d.ts +2 -2
- package/dist/operations.js +2 -2
- package/dist/overlay/client-DBmJ4C1M.js +4 -0
- package/dist/overlay/overlay.js +41 -65
- package/dist/page/loader.js +39 -47
- package/dist/{page-DtbMOEMd.d.ts → page-Bzj0CszG.d.ts} +1 -10
- package/dist/{page-B41ozN63.js → page-DRz_sDY4.js} +1 -1
- package/dist/{page-tools-shared-NHUjKI8E.js → page-tools-shared-BNnuPK93.js} +6 -3
- package/dist/{plugin-PS8PMYus.js → plugin-DRsNaJaK.js} +34 -8
- package/dist/realtime.d.ts +2 -2
- package/dist/realtime.js +2 -2
- package/dist/reply-id-P_bSuEng.js +12 -0
- package/dist/request-log-Dj9Vw7qG.js +105 -0
- package/dist/request-origin-CFU0TQ7E.js +49 -0
- package/dist/{rpc-RKPAusHp.js → rpc-BQqYx8kb.js} +23 -7
- package/dist/{rpc-HBcWtg0T.d.ts → rpc-CuJ5GLIz.d.ts} +1 -1
- package/dist/rpc.d.ts +1 -1
- package/dist/rpc.js +1 -1
- package/dist/runtime/auth.d.ts +2 -2
- package/dist/runtime/auth.js +2 -2
- package/dist/runtime/console.d.ts +1 -1
- package/dist/runtime/console.js +3 -3
- package/dist/runtime/index.d.ts +42 -13
- package/dist/runtime/index.js +224 -22
- package/dist/{server-D8k-0GXe.js → server-Yk610niJ.js} +1 -1
- package/dist/server.d.ts +6 -5
- package/dist/server.js +6 -6
- package/dist/service-Bj-Mg63c.d.ts +73 -0
- package/dist/{service-GHhhyQj4.js → service-DA5BwfPQ.js} +71 -14
- package/dist/{session-DVx-1Tym.d.ts → session-DUcEor2W.d.ts} +3 -3
- package/dist/{session-EkudzUmm.js → session-X_g9dnU5.js} +27 -30
- package/dist/{service-CFVm23k7.d.ts → store-CsUNfC1w.d.ts} +11 -72
- package/dist/table-names-B9LlRC0B.js +16 -0
- package/dist/testing.d.ts +1 -1
- package/dist/testing.js +38 -5
- package/dist/{types-CZujrMFd.d.ts → types-BLaYh0_f.d.ts} +48 -4
- package/dist/{types-BIS0hCEl.d.ts → types-C35INXIs.d.ts} +88 -4
- package/dist/vite.d.ts +24 -7
- package/dist/vite.js +322 -173
- package/package.json +9 -9
- package/dist/errors-CyQyKAvy.d.ts +0 -185
- package/dist/index-DNhy69Wt.d.ts +0 -83
- package/dist/overlay/client-DmsSMOzi.js +0 -4
package/dist/client.js
CHANGED
|
@@ -1,8 +1,733 @@
|
|
|
1
|
-
import { i as commentsUnansweredError, r as commentsClientDiagnostics, t as commentsAnswerError } from "./client-diagnostics-
|
|
2
|
-
import { t as
|
|
3
|
-
import { t as
|
|
4
|
-
import {
|
|
1
|
+
import { i as commentsUnansweredError, r as commentsClientDiagnostics, t as commentsAnswerError } from "./client-diagnostics-Bubm7Shd.js";
|
|
2
|
+
import { i as withCommentFiles, n as outboxAccountHeader, r as retryAfterOf, t as failedCommentPreparation } from "./client-files-CJZl5Kc4.js";
|
|
3
|
+
import { t as replyMessageId } from "./reply-id-P_bSuEng.js";
|
|
4
|
+
import { t as commentsErrorDiagnostics } from "./errors-Dq4AHjza.js";
|
|
5
|
+
import { i as commentsRoute } from "./page-DRz_sDY4.js";
|
|
5
6
|
import { n as isRecordOrFunction } from "./object-Da1zyPCR.js";
|
|
7
|
+
import { t as foldName } from "./fold-name-B7Zb3-MF.js";
|
|
8
|
+
//#region ../internal/src/browser-environment.ts
|
|
9
|
+
/** The real browser: window events, Web Locks, and a BroadcastChannel, each only when the browser has it. */
|
|
10
|
+
function createBrowserEnvironment(options) {
|
|
11
|
+
let channel;
|
|
12
|
+
return {
|
|
13
|
+
listen(listener) {
|
|
14
|
+
const online = () => listener("online");
|
|
15
|
+
const offline = () => listener("offline");
|
|
16
|
+
const visibility = () => listener(document.visibilityState === "hidden" ? "hidden" : "visible");
|
|
17
|
+
window.addEventListener("online", online);
|
|
18
|
+
window.addEventListener("offline", offline);
|
|
19
|
+
document.addEventListener("visibilitychange", visibility);
|
|
20
|
+
return () => {
|
|
21
|
+
window.removeEventListener("online", online);
|
|
22
|
+
window.removeEventListener("offline", offline);
|
|
23
|
+
document.removeEventListener("visibilitychange", visibility);
|
|
24
|
+
};
|
|
25
|
+
},
|
|
26
|
+
isVisible: () => document.visibilityState !== "hidden",
|
|
27
|
+
isOnline: () => navigator.onLine !== false,
|
|
28
|
+
lock(name, task) {
|
|
29
|
+
const locks = navigator.locks;
|
|
30
|
+
return locks ? locks.request(name, task) : task();
|
|
31
|
+
},
|
|
32
|
+
lead(name, task) {
|
|
33
|
+
const controller = new AbortController();
|
|
34
|
+
const run = () => controller.signal.aborted ? Promise.resolve() : task(controller.signal);
|
|
35
|
+
const locks = navigator.locks;
|
|
36
|
+
if (locks) locks.request(name, { signal: controller.signal }, run).catch(() => void 0);
|
|
37
|
+
else run();
|
|
38
|
+
return () => controller.abort();
|
|
39
|
+
},
|
|
40
|
+
post(message) {
|
|
41
|
+
if (channel) channel.postMessage(message);
|
|
42
|
+
else postOnce(options.channel, message);
|
|
43
|
+
},
|
|
44
|
+
receive(listener) {
|
|
45
|
+
if (typeof BroadcastChannel !== "function") return () => void 0;
|
|
46
|
+
const own = new BroadcastChannel(options.channel);
|
|
47
|
+
channel = own;
|
|
48
|
+
own.onmessage = (event) => {
|
|
49
|
+
if (options.isMessage(event.data)) listener(event.data);
|
|
50
|
+
};
|
|
51
|
+
return () => {
|
|
52
|
+
own.close();
|
|
53
|
+
if (channel === own) channel = void 0;
|
|
54
|
+
};
|
|
55
|
+
},
|
|
56
|
+
schedule(task, ms) {
|
|
57
|
+
const timer = setTimeout(task, ms);
|
|
58
|
+
return () => clearTimeout(timer);
|
|
59
|
+
},
|
|
60
|
+
random: Math.random
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/** Posts one message on a channel of its own, so every open channel of that name gets it. */
|
|
64
|
+
function postOnce(channel, message) {
|
|
65
|
+
if (typeof BroadcastChannel !== "function") return;
|
|
66
|
+
const own = new BroadcastChannel(channel);
|
|
67
|
+
own.postMessage(message);
|
|
68
|
+
own.close();
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* A version 4 UUID. `crypto.randomUUID()` exists only on HTTPS and localhost, and an app on a LAN address in development
|
|
72
|
+
* has neither, so this falls back to `crypto.getRandomValues()`.
|
|
73
|
+
*/
|
|
74
|
+
function randomUUID() {
|
|
75
|
+
if (typeof crypto.randomUUID === "function") return crypto.randomUUID();
|
|
76
|
+
const bytes = crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(16));
|
|
77
|
+
bytes[6] = bytes[6] & 15 | 64;
|
|
78
|
+
bytes[8] = bytes[8] & 63 | 128;
|
|
79
|
+
const hex = Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
80
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
81
|
+
}
|
|
82
|
+
/** The wait before try `attempt + 1`: 1 s doubling to 30 s, with jitter over its upper half. `random` is in [0, 1). */
|
|
83
|
+
function backoffDelay(attempt, random) {
|
|
84
|
+
const ceiling = Math.min(3e4, 1e3 * 2 ** attempt);
|
|
85
|
+
return ceiling / 2 + random * ceiling / 2;
|
|
86
|
+
}
|
|
87
|
+
//#endregion
|
|
88
|
+
//#region src/client-outbox.ts
|
|
89
|
+
function changeable(entry) {
|
|
90
|
+
return entry.status !== "sending" && !entry.uncertain;
|
|
91
|
+
}
|
|
92
|
+
const channel = "vitehub-comments";
|
|
93
|
+
function isOutboxMessage(value) {
|
|
94
|
+
if (!value || typeof value !== "object") return false;
|
|
95
|
+
const type = Reflect.get(value, "type");
|
|
96
|
+
if (typeof Reflect.get(value, "account") !== "string") return false;
|
|
97
|
+
if (type === "change") return true;
|
|
98
|
+
const kind = Reflect.get(value, "kind");
|
|
99
|
+
return type === "sent" && (kind === "create" || kind === "reply") && typeof Reflect.get(value, "id") === "string" && typeof Reflect.get(value, "key") === "string" && Boolean(Reflect.get(value, "thread"));
|
|
100
|
+
}
|
|
101
|
+
function copyEntry(entry) {
|
|
102
|
+
const input = JSON.parse(JSON.stringify(entry.input));
|
|
103
|
+
const subject = entry.kind === "reply" && entry.subject ? {
|
|
104
|
+
kind: entry.subject.kind,
|
|
105
|
+
id: entry.subject.id
|
|
106
|
+
} : void 0;
|
|
107
|
+
return {
|
|
108
|
+
...entry,
|
|
109
|
+
input,
|
|
110
|
+
files: [...entry.files],
|
|
111
|
+
...subject ? { subject } : {},
|
|
112
|
+
...entry.uploaded ? { uploaded: [...entry.uploaded] } : {},
|
|
113
|
+
...entry.error ? { error: { ...entry.error } } : {}
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
function sortEntries(entries) {
|
|
117
|
+
return entries.sort((a, b) => a.order - b.order);
|
|
118
|
+
}
|
|
119
|
+
/** An outbox store in memory. Entries live as long as the store object. Pass one store to two clients to test a reload. */
|
|
120
|
+
function createMemoryOutboxStore() {
|
|
121
|
+
const accounts = /* @__PURE__ */ new Map();
|
|
122
|
+
return {
|
|
123
|
+
persisted: false,
|
|
124
|
+
async list(account) {
|
|
125
|
+
return sortEntries([...accounts.get(account)?.values() ?? []].map(copyEntry));
|
|
126
|
+
},
|
|
127
|
+
async update(account, change) {
|
|
128
|
+
const next = new Map([...accounts.get(account) ?? []].map(([id, entry]) => [id, copyEntry(entry)]));
|
|
129
|
+
const result = change(next);
|
|
130
|
+
accounts.set(account, new Map([...next].map(([id, entry]) => [id, copyEntry(entry)])));
|
|
131
|
+
return result;
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
const databaseName = "vitehub-comments";
|
|
136
|
+
const format = 2;
|
|
137
|
+
function done(transaction) {
|
|
138
|
+
return new Promise((resolve, reject) => {
|
|
139
|
+
transaction.oncomplete = () => resolve();
|
|
140
|
+
transaction.onabort = () => reject(transaction.error ?? /* @__PURE__ */ new Error("The IndexedDB transaction was aborted."));
|
|
141
|
+
transaction.onerror = () => reject(transaction.error ?? /* @__PURE__ */ new Error("The IndexedDB transaction failed."));
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
function requested(request) {
|
|
145
|
+
return new Promise((resolve, reject) => {
|
|
146
|
+
request.onsuccess = () => resolve(request.result);
|
|
147
|
+
request.onerror = () => reject(request.error ?? /* @__PURE__ */ new Error("The IndexedDB request failed."));
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
function openDatabase() {
|
|
151
|
+
return new Promise((resolve, reject) => {
|
|
152
|
+
const request = indexedDB.open(databaseName, format);
|
|
153
|
+
request.onupgradeneeded = (event) => {
|
|
154
|
+
const drafts = request.result.createObjectStore("drafts", { keyPath: ["account", "id"] });
|
|
155
|
+
drafts.createIndex("account", "account");
|
|
156
|
+
if (event.oldVersion === 1 && request.transaction) {
|
|
157
|
+
const previous = request.transaction.objectStore("outbox").openCursor();
|
|
158
|
+
previous.onsuccess = () => {
|
|
159
|
+
const cursor = previous.result;
|
|
160
|
+
if (!cursor) {
|
|
161
|
+
request.result.deleteObjectStore("outbox");
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
const entry = cursor.value;
|
|
165
|
+
drafts.put({
|
|
166
|
+
...entry,
|
|
167
|
+
id: entry.requestId,
|
|
168
|
+
uncertain: true
|
|
169
|
+
});
|
|
170
|
+
cursor.continue();
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
request.onsuccess = () => {
|
|
175
|
+
const database = request.result;
|
|
176
|
+
database.onversionchange = () => database.close();
|
|
177
|
+
resolve(database);
|
|
178
|
+
};
|
|
179
|
+
request.onerror = () => reject(request.error ?? /* @__PURE__ */ new Error("IndexedDB did not open."));
|
|
180
|
+
request.onblocked = () => reject(/* @__PURE__ */ new Error("IndexedDB is blocked by another tab."));
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* The default store: the `drafts` object store of the `vitehub-comments` IndexedDB database. When IndexedDB is missing or
|
|
185
|
+
* does not open, as in some private windows, it keeps the entries in memory and `persisted` is `false`.
|
|
186
|
+
*/
|
|
187
|
+
function createIndexedDBOutboxStore() {
|
|
188
|
+
let backend;
|
|
189
|
+
let persisted = typeof indexedDB !== "undefined";
|
|
190
|
+
const open = () => backend ??= (persisted ? openDatabase() : Promise.reject(/* @__PURE__ */ new Error("No IndexedDB."))).then((database) => ({ database }), () => {
|
|
191
|
+
persisted = false;
|
|
192
|
+
return { memory: createMemoryOutboxStore() };
|
|
193
|
+
});
|
|
194
|
+
const run = async (memory, database) => {
|
|
195
|
+
const opened = await open();
|
|
196
|
+
return "database" in opened ? database(opened.database) : memory(opened.memory);
|
|
197
|
+
};
|
|
198
|
+
return {
|
|
199
|
+
get persisted() {
|
|
200
|
+
return persisted;
|
|
201
|
+
},
|
|
202
|
+
list: (account) => run((store) => store.list(account), async (database) => {
|
|
203
|
+
return sortEntries(await requested(database.transaction("drafts", "readonly").objectStore("drafts").index("account").getAll(account)));
|
|
204
|
+
}),
|
|
205
|
+
update: (account, change) => run((store) => store.update(account, change), async (database) => {
|
|
206
|
+
const transaction = database.transaction("drafts", "readwrite");
|
|
207
|
+
const drafts = transaction.objectStore("drafts");
|
|
208
|
+
const read = drafts.index("account").getAll(account);
|
|
209
|
+
const complete = done(transaction);
|
|
210
|
+
const result = new Promise((resolve, reject) => {
|
|
211
|
+
read.onsuccess = () => {
|
|
212
|
+
const previous = new Map(read.result.map((entry) => [entry.id, entry]));
|
|
213
|
+
const next = new Map(previous);
|
|
214
|
+
try {
|
|
215
|
+
const value = change(next);
|
|
216
|
+
for (const id of previous.keys()) if (!next.has(id)) drafts.delete([account, id]);
|
|
217
|
+
for (const [id, entry] of next) if (previous.get(id) !== entry) drafts.put(copyEntry(entry));
|
|
218
|
+
resolve(value);
|
|
219
|
+
} catch (error) {
|
|
220
|
+
reject(error);
|
|
221
|
+
transaction.abort();
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
read.onerror = () => reject(read.error);
|
|
225
|
+
});
|
|
226
|
+
return (await Promise.all([result, complete]))[0];
|
|
227
|
+
})
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
/** The error of a try, as plain data that the store keeps. */
|
|
231
|
+
function deliveryError(error) {
|
|
232
|
+
const field = (name) => error && typeof error === "object" ? Reflect.get(error, name) : void 0;
|
|
233
|
+
const status = field("status");
|
|
234
|
+
const code = field("code");
|
|
235
|
+
const why = field("why") ?? field("message");
|
|
236
|
+
const fix = field("fix");
|
|
237
|
+
const retryAfter = field("retryAfter");
|
|
238
|
+
return {
|
|
239
|
+
status: typeof status === "number" ? status : 0,
|
|
240
|
+
why: typeof why === "string" && why ? why : String(error),
|
|
241
|
+
...typeof code === "string" ? { code } : {},
|
|
242
|
+
...typeof fix === "string" ? { fix } : {},
|
|
243
|
+
...typeof retryAfter === "number" && retryAfter >= 0 ? { retryAfter } : {}
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
/** `wait`: try again later. `hold`: wait for `retry()`, such as after a sign-in or a plan upgrade. `fail`: the server refused this entry. */
|
|
247
|
+
function outcomeOf(status) {
|
|
248
|
+
if (status === 401 || status === 402) return "hold";
|
|
249
|
+
if (status === 408 || status === 429) return "wait";
|
|
250
|
+
return status >= 400 && status < 500 ? "fail" : "wait";
|
|
251
|
+
}
|
|
252
|
+
/** The outbox of one client. */
|
|
253
|
+
function createOutbox(options, host) {
|
|
254
|
+
const { account } = options;
|
|
255
|
+
const store = options.store ?? createIndexedDBOutboxStore();
|
|
256
|
+
const environment = options.environment ?? createBrowserEnvironment({
|
|
257
|
+
channel,
|
|
258
|
+
isMessage: isOutboxMessage
|
|
259
|
+
});
|
|
260
|
+
const lockName = `vitehub-comments-outbox:${account}`;
|
|
261
|
+
let entries = [];
|
|
262
|
+
const sent = /* @__PURE__ */ new Map();
|
|
263
|
+
const receipts = /* @__PURE__ */ new Map();
|
|
264
|
+
const keyOf = (entry) => entry.kind === "create" ? JSON.stringify([
|
|
265
|
+
entry.kind,
|
|
266
|
+
entry.input.subject.kind,
|
|
267
|
+
entry.input.subject.id,
|
|
268
|
+
entry.requestId
|
|
269
|
+
]) : JSON.stringify([
|
|
270
|
+
entry.kind,
|
|
271
|
+
entry.input.threadId,
|
|
272
|
+
entry.requestId
|
|
273
|
+
]);
|
|
274
|
+
const replyKey = (threadId, requestId) => JSON.stringify([threadId, requestId]);
|
|
275
|
+
let replyIds = /* @__PURE__ */ new Map();
|
|
276
|
+
let loading = Promise.resolve();
|
|
277
|
+
let leading;
|
|
278
|
+
let again = false;
|
|
279
|
+
let cancelWait;
|
|
280
|
+
const post = (message) => environment.post(message);
|
|
281
|
+
const reload = () => loading = loading.catch(() => void 0).then(async () => {
|
|
282
|
+
const pending = await store.list(account);
|
|
283
|
+
const ids = /* @__PURE__ */ new Map();
|
|
284
|
+
if (globalThis.crypto?.subtle) await Promise.all(pending.map(async (entry) => {
|
|
285
|
+
if (entry.kind !== "reply") return;
|
|
286
|
+
const threadId = realId(entry.input.threadId);
|
|
287
|
+
const key = replyKey(threadId, entry.requestId);
|
|
288
|
+
ids.set(key, replyIds.get(key) ?? await replyMessageId(threadId, entry.requestId));
|
|
289
|
+
}));
|
|
290
|
+
replyIds = ids;
|
|
291
|
+
entries = pending;
|
|
292
|
+
host.changed();
|
|
293
|
+
});
|
|
294
|
+
const changed = async () => {
|
|
295
|
+
await reload();
|
|
296
|
+
post({
|
|
297
|
+
type: "change",
|
|
298
|
+
account
|
|
299
|
+
});
|
|
300
|
+
};
|
|
301
|
+
const serverThread = (id) => {
|
|
302
|
+
for (const thread of host.threads()) if (thread.id === id) return thread;
|
|
303
|
+
};
|
|
304
|
+
/** The server id of a thread that the client may still name by its local id. */
|
|
305
|
+
const realId = (threadId) => {
|
|
306
|
+
return sent.get(threadId)?.id ?? threadId;
|
|
307
|
+
};
|
|
308
|
+
const delivery = (entry) => ({
|
|
309
|
+
status: entry.status === "failed" ? "failed" : "pending",
|
|
310
|
+
requestId: entry.requestId,
|
|
311
|
+
files: entry.files,
|
|
312
|
+
...entry.error ? { error: entry.error } : {}
|
|
313
|
+
});
|
|
314
|
+
const author = () => host.user?.id === account ? host.user : { id: account };
|
|
315
|
+
const localMessage = (entry, threadId) => ({
|
|
316
|
+
id: entry.id,
|
|
317
|
+
threadId,
|
|
318
|
+
body: entry.input.body,
|
|
319
|
+
author: author(),
|
|
320
|
+
mentions: entry.input.mentions ?? [],
|
|
321
|
+
attachments: entry.input.attachments ?? [],
|
|
322
|
+
createdAt: entry.createdAt,
|
|
323
|
+
delivery: delivery(entry)
|
|
324
|
+
});
|
|
325
|
+
const overlay = (threads) => {
|
|
326
|
+
const replies = /* @__PURE__ */ new Map();
|
|
327
|
+
for (const entry of entries) {
|
|
328
|
+
if (entry.kind !== "reply") continue;
|
|
329
|
+
const id = realId(entry.input.threadId);
|
|
330
|
+
replies.set(id, [...replies.get(id) ?? [], entry]);
|
|
331
|
+
}
|
|
332
|
+
const withReplies = (thread) => {
|
|
333
|
+
const waiting = replies.get(thread.id);
|
|
334
|
+
if (!waiting) return thread;
|
|
335
|
+
const have = new Set(thread.messages.filter((message) => message.author.id === account).map((message) => message.id));
|
|
336
|
+
const added = waiting.filter((entry) => {
|
|
337
|
+
const id = thread.delivery ? void 0 : replyIds.get(replyKey(thread.id, entry.requestId));
|
|
338
|
+
return !(id && have.has(id));
|
|
339
|
+
}).map((entry) => localMessage(entry, thread.id));
|
|
340
|
+
return added.length ? {
|
|
341
|
+
...thread,
|
|
342
|
+
messages: [...thread.messages, ...added]
|
|
343
|
+
} : thread;
|
|
344
|
+
};
|
|
345
|
+
const server = threads.map(withReplies);
|
|
346
|
+
const local = [];
|
|
347
|
+
for (const entry of entries) {
|
|
348
|
+
if (entry.kind !== "create" || sent.has(entry.id) || threads.some((thread) => matchesCreate(entry, thread))) continue;
|
|
349
|
+
local.push(withReplies(localThread(entry)));
|
|
350
|
+
}
|
|
351
|
+
return {
|
|
352
|
+
server,
|
|
353
|
+
local
|
|
354
|
+
};
|
|
355
|
+
};
|
|
356
|
+
const matchesCreate = (entry, thread) => thread.requestId === entry.requestId && thread.author.id === account && thread.subject.kind === entry.input.subject.kind && thread.subject.id === entry.input.subject.id;
|
|
357
|
+
const localThread = (entry) => {
|
|
358
|
+
const { input } = entry;
|
|
359
|
+
return {
|
|
360
|
+
id: entry.id,
|
|
361
|
+
scope: "",
|
|
362
|
+
n: 0,
|
|
363
|
+
subject: input.subject,
|
|
364
|
+
anchor: input.anchor,
|
|
365
|
+
status: "open",
|
|
366
|
+
author: author(),
|
|
367
|
+
messages: [localMessage(entry, entry.id)],
|
|
368
|
+
...input.metadata === void 0 ? {} : { metadata: input.metadata },
|
|
369
|
+
...input.revision === void 0 ? {} : { revision: input.revision },
|
|
370
|
+
createdAt: entry.createdAt,
|
|
371
|
+
updatedAt: entry.createdAt,
|
|
372
|
+
requestId: entry.requestId,
|
|
373
|
+
delivery: delivery(entry)
|
|
374
|
+
};
|
|
375
|
+
};
|
|
376
|
+
/** The first entry that may go out now. A reply waits while its comment has not gone out. */
|
|
377
|
+
const next = (list) => {
|
|
378
|
+
const waiting = new Set(list.flatMap((entry) => entry.kind === "create" ? [entry.id] : []));
|
|
379
|
+
return list.find((entry) => entry.status !== "failed" && !(entry.kind === "reply" && waiting.has(entry.input.threadId)));
|
|
380
|
+
};
|
|
381
|
+
const wait = (ms) => {
|
|
382
|
+
cancelWait?.();
|
|
383
|
+
cancelWait = environment.schedule(() => {
|
|
384
|
+
cancelWait = void 0;
|
|
385
|
+
kick();
|
|
386
|
+
}, ms);
|
|
387
|
+
};
|
|
388
|
+
const update = (change) => store.update(account, change);
|
|
389
|
+
const send = async (entry) => {
|
|
390
|
+
const withClaim = (change) => update((drafts) => {
|
|
391
|
+
const current = drafts.get(entry.id);
|
|
392
|
+
if (current && current.claim === entry.claim) return change(current, drafts);
|
|
393
|
+
});
|
|
394
|
+
let thread;
|
|
395
|
+
let writing = false;
|
|
396
|
+
const outbox = {
|
|
397
|
+
account,
|
|
398
|
+
async uploaded(key) {
|
|
399
|
+
if (!await withClaim((current, drafts) => drafts.set(entry.id, {
|
|
400
|
+
...current,
|
|
401
|
+
uploaded: [...current.uploaded ?? [], key]
|
|
402
|
+
}))) throw new DOMException("This draft is no longer in the outbox.", "AbortError");
|
|
403
|
+
}
|
|
404
|
+
};
|
|
405
|
+
try {
|
|
406
|
+
const files = {
|
|
407
|
+
files: entry.files.slice(entry.uploaded?.length ?? 0),
|
|
408
|
+
outbox
|
|
409
|
+
};
|
|
410
|
+
const attachments = [...entry.input.attachments ?? [], ...entry.uploaded ?? []];
|
|
411
|
+
const extra = {
|
|
412
|
+
...files,
|
|
413
|
+
requestId: entry.requestId,
|
|
414
|
+
...attachments.length ? { attachments } : {}
|
|
415
|
+
};
|
|
416
|
+
if (entry.kind === "create") {
|
|
417
|
+
if (!await withClaim(() => true)) return;
|
|
418
|
+
writing = true;
|
|
419
|
+
thread = await host.transport.create({
|
|
420
|
+
...entry.input,
|
|
421
|
+
...extra
|
|
422
|
+
});
|
|
423
|
+
} else {
|
|
424
|
+
const threadId = realId(entry.input.threadId);
|
|
425
|
+
const subject = entry.subject ?? serverThread(threadId)?.subject ?? (files.files?.length ? (await host.transport.get({ threadId }))?.subject : void 0);
|
|
426
|
+
if (files.files?.length && !subject) throw Object.assign(/* @__PURE__ */ new Error("The thread for this reply does not exist."), { status: 404 });
|
|
427
|
+
if (!await withClaim(() => true)) return;
|
|
428
|
+
writing = true;
|
|
429
|
+
thread = await host.transport.reply({
|
|
430
|
+
...entry.input,
|
|
431
|
+
...extra,
|
|
432
|
+
threadId,
|
|
433
|
+
...subject ? { subject } : {}
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
} catch (cause) {
|
|
437
|
+
const preparation = failedCommentPreparation.get(outbox);
|
|
438
|
+
const beforeWrite = preparation !== void 0 && Object.is(preparation.cause, cause);
|
|
439
|
+
const { retryAfter, ...error } = deliveryError(cause);
|
|
440
|
+
const outcome = outcomeOf(error.status);
|
|
441
|
+
const attempts = entry.attempts ?? 0;
|
|
442
|
+
if (await withClaim((current, drafts) => drafts.set(entry.id, {
|
|
443
|
+
...current,
|
|
444
|
+
status: outcome === "fail" ? "failed" : "pending",
|
|
445
|
+
uncertain: entry.uncertain || writing && !beforeWrite && outcome === "wait" && error.status !== 429,
|
|
446
|
+
held: outcome === "hold",
|
|
447
|
+
retryAt: outcome === "wait" ? Date.now() + Math.max(1, retryAfter ?? backoffDelay(attempts, environment.random())) : void 0,
|
|
448
|
+
attempts: attempts + 1,
|
|
449
|
+
error
|
|
450
|
+
}))) await changed();
|
|
451
|
+
return;
|
|
452
|
+
}
|
|
453
|
+
if (!await withClaim((current, drafts) => {
|
|
454
|
+
if (current.kind === "create") {
|
|
455
|
+
for (const reply of drafts.values()) if (reply.kind === "reply" && reply.input.threadId === entry.id) drafts.set(reply.id, {
|
|
456
|
+
...reply,
|
|
457
|
+
subject: thread.subject,
|
|
458
|
+
input: {
|
|
459
|
+
...reply.input,
|
|
460
|
+
threadId: thread.id
|
|
461
|
+
}
|
|
462
|
+
});
|
|
463
|
+
}
|
|
464
|
+
return drafts.delete(entry.id);
|
|
465
|
+
})) return;
|
|
466
|
+
const key = keyOf(entry);
|
|
467
|
+
if (entry.kind === "create") sent.set(entry.id, thread);
|
|
468
|
+
receipts.set(key, thread);
|
|
469
|
+
host.upsert(thread);
|
|
470
|
+
await reload();
|
|
471
|
+
post({
|
|
472
|
+
type: "sent",
|
|
473
|
+
account,
|
|
474
|
+
id: entry.id,
|
|
475
|
+
kind: entry.kind,
|
|
476
|
+
key,
|
|
477
|
+
thread
|
|
478
|
+
});
|
|
479
|
+
};
|
|
480
|
+
const drain = async (signal) => {
|
|
481
|
+
cancelWait?.();
|
|
482
|
+
cancelWait = void 0;
|
|
483
|
+
while (!signal.aborted && environment.isOnline()) {
|
|
484
|
+
const claim = randomUUID();
|
|
485
|
+
const selected = await update((drafts) => {
|
|
486
|
+
if (signal.aborted) return;
|
|
487
|
+
const entry = next(sortEntries([...drafts.values()]));
|
|
488
|
+
if (!entry || entry.held) return;
|
|
489
|
+
const delay = (entry.retryAt ?? 0) - Date.now();
|
|
490
|
+
if (delay > 0) return { delay };
|
|
491
|
+
const claimed = {
|
|
492
|
+
...entry,
|
|
493
|
+
claim,
|
|
494
|
+
status: "sending",
|
|
495
|
+
uncertain: entry.uncertain || entry.status === "sending"
|
|
496
|
+
};
|
|
497
|
+
drafts.set(entry.id, claimed);
|
|
498
|
+
return { claimed };
|
|
499
|
+
});
|
|
500
|
+
if (!selected) return;
|
|
501
|
+
if (selected.delay !== void 0) {
|
|
502
|
+
wait(selected.delay);
|
|
503
|
+
return;
|
|
504
|
+
}
|
|
505
|
+
await reload();
|
|
506
|
+
await send(selected.claimed);
|
|
507
|
+
}
|
|
508
|
+
};
|
|
509
|
+
/** Sends what waits, in the tab that holds the lock. */
|
|
510
|
+
function kick() {
|
|
511
|
+
if (leading) {
|
|
512
|
+
again = true;
|
|
513
|
+
return;
|
|
514
|
+
}
|
|
515
|
+
const run = { giveUp: () => void 0 };
|
|
516
|
+
leading = run;
|
|
517
|
+
run.giveUp = environment.lead(lockName, async (signal) => {
|
|
518
|
+
try {
|
|
519
|
+
do {
|
|
520
|
+
again = false;
|
|
521
|
+
await drain(signal);
|
|
522
|
+
} while (again && !signal.aborted);
|
|
523
|
+
} finally {
|
|
524
|
+
if (leading === run) leading = void 0;
|
|
525
|
+
}
|
|
526
|
+
});
|
|
527
|
+
}
|
|
528
|
+
const enqueue = async (entry) => {
|
|
529
|
+
const saved = await update((drafts) => {
|
|
530
|
+
const key = keyOf(entry);
|
|
531
|
+
const existing = [...drafts.values()].find((draft) => keyOf(draft) === key);
|
|
532
|
+
if (existing) return existing;
|
|
533
|
+
let order = entry.createdAt;
|
|
534
|
+
for (const draft of drafts.values()) order = Math.max(order, draft.order + 1);
|
|
535
|
+
const ordered = {
|
|
536
|
+
...entry,
|
|
537
|
+
order
|
|
538
|
+
};
|
|
539
|
+
drafts.set(entry.id, ordered);
|
|
540
|
+
return ordered;
|
|
541
|
+
});
|
|
542
|
+
await changed();
|
|
543
|
+
return saved;
|
|
544
|
+
};
|
|
545
|
+
const fields = (requestId, files) => {
|
|
546
|
+
const now = Date.now();
|
|
547
|
+
const id = randomUUID();
|
|
548
|
+
return {
|
|
549
|
+
id,
|
|
550
|
+
requestId: requestId ?? id,
|
|
551
|
+
account,
|
|
552
|
+
files: [...files ?? []],
|
|
553
|
+
createdAt: now,
|
|
554
|
+
order: now,
|
|
555
|
+
status: "pending"
|
|
556
|
+
};
|
|
557
|
+
};
|
|
558
|
+
reload();
|
|
559
|
+
return {
|
|
560
|
+
get entries() {
|
|
561
|
+
return entries;
|
|
562
|
+
},
|
|
563
|
+
get queued() {
|
|
564
|
+
return entries.filter((entry) => entry.status !== "failed");
|
|
565
|
+
},
|
|
566
|
+
get failures() {
|
|
567
|
+
return entries.filter((entry) => entry.status === "failed");
|
|
568
|
+
},
|
|
569
|
+
get persisted() {
|
|
570
|
+
return store.persisted;
|
|
571
|
+
},
|
|
572
|
+
overlay,
|
|
573
|
+
owns: (threadId, messageId) => entries.some((entry) => entry.id === (messageId ?? threadId)),
|
|
574
|
+
async create({ files, requestId, ...input }) {
|
|
575
|
+
const entry = {
|
|
576
|
+
...fields(requestId, files),
|
|
577
|
+
kind: "create",
|
|
578
|
+
input
|
|
579
|
+
};
|
|
580
|
+
const receipt = () => receipts.get(keyOf(entry)) ?? [...host.threads()].find((thread) => matchesCreate(entry, thread));
|
|
581
|
+
const earlier = receipt();
|
|
582
|
+
if (earlier) return earlier;
|
|
583
|
+
const saved = await enqueue(entry);
|
|
584
|
+
kick();
|
|
585
|
+
return receipt() ?? localThread(saved.kind === "create" ? saved : entry);
|
|
586
|
+
},
|
|
587
|
+
async reply({ files, requestId, subject, ...input }) {
|
|
588
|
+
const threadId = realId(input.threadId);
|
|
589
|
+
const parent = entries.find((entry) => entry.kind === "create" && entry.id === threadId);
|
|
590
|
+
const cached = serverThread(threadId);
|
|
591
|
+
const target = subject ?? cached?.subject ?? (parent?.kind === "create" ? parent.input.subject : void 0);
|
|
592
|
+
const entry = {
|
|
593
|
+
...fields(requestId, files),
|
|
594
|
+
kind: "reply",
|
|
595
|
+
input: {
|
|
596
|
+
...input,
|
|
597
|
+
threadId
|
|
598
|
+
},
|
|
599
|
+
...target ? { subject: target } : {}
|
|
600
|
+
};
|
|
601
|
+
const earlier = receipts.get(keyOf(entry));
|
|
602
|
+
if (earlier) return serverThread(earlier.id) ?? earlier;
|
|
603
|
+
const saved = await enqueue(entry);
|
|
604
|
+
kick();
|
|
605
|
+
const thread = serverThread(realId(threadId)) ?? cached;
|
|
606
|
+
if (thread) return overlay([thread]).server[0] ?? thread;
|
|
607
|
+
const local = overlay([]).local.find((local) => local.id === threadId);
|
|
608
|
+
if (local) return local;
|
|
609
|
+
return {
|
|
610
|
+
id: threadId,
|
|
611
|
+
scope: "",
|
|
612
|
+
n: 0,
|
|
613
|
+
subject: target ?? {
|
|
614
|
+
kind: "",
|
|
615
|
+
id: ""
|
|
616
|
+
},
|
|
617
|
+
anchor: { kind: "page" },
|
|
618
|
+
status: "open",
|
|
619
|
+
author: author(),
|
|
620
|
+
messages: [localMessage(saved, threadId)],
|
|
621
|
+
createdAt: entry.createdAt,
|
|
622
|
+
updatedAt: entry.createdAt
|
|
623
|
+
};
|
|
624
|
+
},
|
|
625
|
+
async retry(id) {
|
|
626
|
+
await update((drafts) => {
|
|
627
|
+
for (const entry of drafts.values()) {
|
|
628
|
+
if (entry.status === "sending" || id !== void 0 && entry.id !== id) continue;
|
|
629
|
+
if (entry.status === "failed" && id === void 0) continue;
|
|
630
|
+
drafts.set(entry.id, {
|
|
631
|
+
...entry,
|
|
632
|
+
status: "pending",
|
|
633
|
+
held: false,
|
|
634
|
+
attempts: 0,
|
|
635
|
+
retryAt: void 0,
|
|
636
|
+
error: void 0
|
|
637
|
+
});
|
|
638
|
+
}
|
|
639
|
+
});
|
|
640
|
+
await changed();
|
|
641
|
+
kick();
|
|
642
|
+
},
|
|
643
|
+
async dismiss(id) {
|
|
644
|
+
const removed = await update((drafts) => {
|
|
645
|
+
const entry = drafts.get(id);
|
|
646
|
+
if (!entry || !changeable(entry)) return false;
|
|
647
|
+
if (entry.kind === "create") {
|
|
648
|
+
for (const reply of drafts.values()) if (reply.kind === "reply" && reply.input.threadId === id) drafts.delete(reply.id);
|
|
649
|
+
}
|
|
650
|
+
return drafts.delete(id);
|
|
651
|
+
});
|
|
652
|
+
if (removed) {
|
|
653
|
+
await changed();
|
|
654
|
+
kick();
|
|
655
|
+
}
|
|
656
|
+
return removed;
|
|
657
|
+
},
|
|
658
|
+
async edit(id, body, mentions) {
|
|
659
|
+
const edited = await update((drafts) => {
|
|
660
|
+
const entry = drafts.get(id);
|
|
661
|
+
if (!entry || !changeable(entry)) return false;
|
|
662
|
+
const fields = {
|
|
663
|
+
body,
|
|
664
|
+
...mentions ? { mentions } : {}
|
|
665
|
+
};
|
|
666
|
+
if (entry.kind === "create") drafts.set(id, {
|
|
667
|
+
...entry,
|
|
668
|
+
input: {
|
|
669
|
+
...entry.input,
|
|
670
|
+
...fields
|
|
671
|
+
},
|
|
672
|
+
status: "pending",
|
|
673
|
+
error: void 0
|
|
674
|
+
});
|
|
675
|
+
else drafts.set(id, {
|
|
676
|
+
...entry,
|
|
677
|
+
input: {
|
|
678
|
+
...entry.input,
|
|
679
|
+
...fields
|
|
680
|
+
},
|
|
681
|
+
status: "pending",
|
|
682
|
+
error: void 0
|
|
683
|
+
});
|
|
684
|
+
return true;
|
|
685
|
+
});
|
|
686
|
+
if (edited) {
|
|
687
|
+
await changed();
|
|
688
|
+
kick();
|
|
689
|
+
}
|
|
690
|
+
return edited;
|
|
691
|
+
},
|
|
692
|
+
async clear() {
|
|
693
|
+
cancelWait?.();
|
|
694
|
+
cancelWait = void 0;
|
|
695
|
+
await update((drafts) => drafts.clear());
|
|
696
|
+
sent.clear();
|
|
697
|
+
receipts.clear();
|
|
698
|
+
await changed();
|
|
699
|
+
},
|
|
700
|
+
start() {
|
|
701
|
+
const stops = [environment.listen((event) => {
|
|
702
|
+
if (event !== "online" && event !== "visible") return;
|
|
703
|
+
update((drafts) => {
|
|
704
|
+
for (const entry of drafts.values()) if (entry.status === "pending" && entry.retryAt !== void 0) drafts.set(entry.id, {
|
|
705
|
+
...entry,
|
|
706
|
+
retryAt: void 0
|
|
707
|
+
});
|
|
708
|
+
}).then(changed).then(kick);
|
|
709
|
+
}), environment.receive((message) => {
|
|
710
|
+
if (message.account !== account) return;
|
|
711
|
+
if (message.type === "sent") {
|
|
712
|
+
if (message.kind === "create") sent.set(message.id, message.thread);
|
|
713
|
+
receipts.set(message.key, message.thread);
|
|
714
|
+
host.upsert(message.thread);
|
|
715
|
+
}
|
|
716
|
+
reload();
|
|
717
|
+
})];
|
|
718
|
+
reload();
|
|
719
|
+
kick();
|
|
720
|
+
return () => {
|
|
721
|
+
for (const stop of stops.splice(0)) stop();
|
|
722
|
+
cancelWait?.();
|
|
723
|
+
cancelWait = void 0;
|
|
724
|
+
leading?.giveUp();
|
|
725
|
+
leading = void 0;
|
|
726
|
+
};
|
|
727
|
+
}
|
|
728
|
+
};
|
|
729
|
+
}
|
|
730
|
+
//#endregion
|
|
6
731
|
//#region src/client-anchor.ts
|
|
7
732
|
const defaultContext = 32;
|
|
8
733
|
const clamp = (value) => Math.min(1e3, Math.max(0, value));
|
|
@@ -406,17 +1131,371 @@ function resolveAnchor(anchor, root = document) {
|
|
|
406
1131
|
};
|
|
407
1132
|
}
|
|
408
1133
|
//#endregion
|
|
1134
|
+
//#region src/client-send.ts
|
|
1135
|
+
const reasonOf = (error) => error instanceof Error ? error.message : String(error);
|
|
1136
|
+
/**
|
|
1137
|
+
* The Send segment of a comment pill, without a framework: it hands the member's waiting threads to their agent, and
|
|
1138
|
+
* pairs one in place first when there is none. `CommentSend` of `@vite-hub/ui` renders it.
|
|
1139
|
+
*/
|
|
1140
|
+
function createCommentSend(transport, options = {}) {
|
|
1141
|
+
const pairingInterval = options.pairingInterval ?? 1500;
|
|
1142
|
+
const statusInterval = options.statusInterval ?? 5e3;
|
|
1143
|
+
const sentFor = options.sentFor ?? 2e3;
|
|
1144
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
1145
|
+
let state = {
|
|
1146
|
+
status: "idle",
|
|
1147
|
+
count: 0,
|
|
1148
|
+
inbox: false
|
|
1149
|
+
};
|
|
1150
|
+
let agent;
|
|
1151
|
+
let count = 0;
|
|
1152
|
+
let countRevision = 0;
|
|
1153
|
+
let selection = "";
|
|
1154
|
+
let selections = 0;
|
|
1155
|
+
let code;
|
|
1156
|
+
let fetching = false;
|
|
1157
|
+
let sendId;
|
|
1158
|
+
let thread;
|
|
1159
|
+
let pushed;
|
|
1160
|
+
let step = 0;
|
|
1161
|
+
let timer;
|
|
1162
|
+
let run = 0;
|
|
1163
|
+
const notify = () => {
|
|
1164
|
+
for (const listener of listeners) listener();
|
|
1165
|
+
};
|
|
1166
|
+
const set = (next) => {
|
|
1167
|
+
step++;
|
|
1168
|
+
clearTimeout(timer);
|
|
1169
|
+
timer = void 0;
|
|
1170
|
+
state = next;
|
|
1171
|
+
notify();
|
|
1172
|
+
};
|
|
1173
|
+
const later = (ms, then) => {
|
|
1174
|
+
const at = step;
|
|
1175
|
+
clearTimeout(timer);
|
|
1176
|
+
timer = setTimeout(() => at === step && then(), ms);
|
|
1177
|
+
};
|
|
1178
|
+
const inbox = () => Boolean(agent && !agent.host);
|
|
1179
|
+
const idleState = () => ({
|
|
1180
|
+
status: "idle",
|
|
1181
|
+
count,
|
|
1182
|
+
inbox: inbox(),
|
|
1183
|
+
...agent && count > 0 ? { canPairAnother: true } : {}
|
|
1184
|
+
});
|
|
1185
|
+
const idle = () => set(idleState());
|
|
1186
|
+
const finish = () => {
|
|
1187
|
+
sendId = void 0;
|
|
1188
|
+
thread = void 0;
|
|
1189
|
+
idle();
|
|
1190
|
+
};
|
|
1191
|
+
const working = () => {
|
|
1192
|
+
set({
|
|
1193
|
+
status: "working",
|
|
1194
|
+
count,
|
|
1195
|
+
...agent ? { agent } : {},
|
|
1196
|
+
...thread ? { thread } : {}
|
|
1197
|
+
});
|
|
1198
|
+
if (!transport.subscribe) later(statusInterval, () => void refresh(working));
|
|
1199
|
+
};
|
|
1200
|
+
const limited = (retryAt) => {
|
|
1201
|
+
set({
|
|
1202
|
+
status: "limited",
|
|
1203
|
+
count,
|
|
1204
|
+
retryAt
|
|
1205
|
+
});
|
|
1206
|
+
later(Math.max(0, retryAt - Date.now()), () => void refresh(idle));
|
|
1207
|
+
};
|
|
1208
|
+
const pending = () => {
|
|
1209
|
+
set({
|
|
1210
|
+
status: "sending",
|
|
1211
|
+
count
|
|
1212
|
+
});
|
|
1213
|
+
later(statusInterval, () => void refresh(pending, true));
|
|
1214
|
+
};
|
|
1215
|
+
const apply = (status) => {
|
|
1216
|
+
agent = status.agent;
|
|
1217
|
+
if (status.pending) return pending();
|
|
1218
|
+
if (status.working) {
|
|
1219
|
+
sendId = status.working.id;
|
|
1220
|
+
thread = status.working.thread;
|
|
1221
|
+
if (state.status === "sent") {
|
|
1222
|
+
state = {
|
|
1223
|
+
...state,
|
|
1224
|
+
count,
|
|
1225
|
+
inbox: inbox()
|
|
1226
|
+
};
|
|
1227
|
+
return notify();
|
|
1228
|
+
}
|
|
1229
|
+
return working();
|
|
1230
|
+
}
|
|
1231
|
+
sendId = void 0;
|
|
1232
|
+
thread = void 0;
|
|
1233
|
+
if (status.retryAt !== void 0 && status.retryAt > Date.now()) return limited(status.retryAt);
|
|
1234
|
+
idle();
|
|
1235
|
+
};
|
|
1236
|
+
const refresh = async (onError, deliveryPending = false) => {
|
|
1237
|
+
const at = step;
|
|
1238
|
+
const canApply = () => at === step && !fetching && state.status !== "pairing" && state.status !== "confirm" && (state.status !== "sending" || deliveryPending);
|
|
1239
|
+
if (!canApply()) return;
|
|
1240
|
+
const revision = countRevision;
|
|
1241
|
+
try {
|
|
1242
|
+
const status = await transport.status();
|
|
1243
|
+
if (!canApply()) return;
|
|
1244
|
+
if (revision === countRevision) count = status.count;
|
|
1245
|
+
selection = status.selection ?? "";
|
|
1246
|
+
apply(status);
|
|
1247
|
+
} catch (error) {
|
|
1248
|
+
if (!onError) throw error;
|
|
1249
|
+
if (canApply()) onError();
|
|
1250
|
+
}
|
|
1251
|
+
};
|
|
1252
|
+
const settle = (result, revision, paired) => {
|
|
1253
|
+
const updates = pushed;
|
|
1254
|
+
pushed = void 0;
|
|
1255
|
+
if (result.status === "changed") {
|
|
1256
|
+
count = result.count;
|
|
1257
|
+
selection = result.selection;
|
|
1258
|
+
if (!paired) return set({
|
|
1259
|
+
status: "failed",
|
|
1260
|
+
count,
|
|
1261
|
+
reason: "The waiting comments changed."
|
|
1262
|
+
});
|
|
1263
|
+
code = paired.code;
|
|
1264
|
+
return set({
|
|
1265
|
+
status: "confirm",
|
|
1266
|
+
count,
|
|
1267
|
+
agent: paired.agent
|
|
1268
|
+
});
|
|
1269
|
+
}
|
|
1270
|
+
if (result.status === "failed") return set({
|
|
1271
|
+
status: "failed",
|
|
1272
|
+
count,
|
|
1273
|
+
reason: result.reason
|
|
1274
|
+
});
|
|
1275
|
+
if (paired) agent = paired.agent;
|
|
1276
|
+
if (result.status === "limited") return limited(result.retryAt);
|
|
1277
|
+
if (result.status === "pending") return pending();
|
|
1278
|
+
sendId = result.id;
|
|
1279
|
+
const update = updates?.get(result.id);
|
|
1280
|
+
thread = update?.thread ?? result.thread;
|
|
1281
|
+
if (revision === countRevision) count = 0;
|
|
1282
|
+
set({
|
|
1283
|
+
status: "sent",
|
|
1284
|
+
count,
|
|
1285
|
+
inbox: inbox()
|
|
1286
|
+
});
|
|
1287
|
+
later(sentFor, update?.done ? finish : working);
|
|
1288
|
+
};
|
|
1289
|
+
const deliver = async (request, paired) => {
|
|
1290
|
+
const revision = countRevision;
|
|
1291
|
+
pushed = transport.subscribe ? /* @__PURE__ */ new Map() : void 0;
|
|
1292
|
+
set({
|
|
1293
|
+
status: "sending",
|
|
1294
|
+
count
|
|
1295
|
+
});
|
|
1296
|
+
const at = step;
|
|
1297
|
+
let result;
|
|
1298
|
+
try {
|
|
1299
|
+
result = await request();
|
|
1300
|
+
} catch (error) {
|
|
1301
|
+
result = {
|
|
1302
|
+
status: "failed",
|
|
1303
|
+
reason: reasonOf(error)
|
|
1304
|
+
};
|
|
1305
|
+
}
|
|
1306
|
+
if (at === step) settle(result, revision, paired);
|
|
1307
|
+
};
|
|
1308
|
+
const drop = (dropped) => void transport.cancel(dropped).catch(() => void 0);
|
|
1309
|
+
const poll = () => {
|
|
1310
|
+
later(pairingInterval, async () => {
|
|
1311
|
+
if (state.status !== "pairing" || !code) return;
|
|
1312
|
+
if (Date.now() >= state.expiresAt) return set({
|
|
1313
|
+
...state,
|
|
1314
|
+
reason: "expired"
|
|
1315
|
+
});
|
|
1316
|
+
const at = step;
|
|
1317
|
+
const check = await transport.pollPairing(code).catch(() => ({ status: "waiting" }));
|
|
1318
|
+
if (at !== step || state.status !== "pairing") return;
|
|
1319
|
+
if (check.status === "waiting") return poll();
|
|
1320
|
+
if (check.status === "paired") return set({
|
|
1321
|
+
status: "confirm",
|
|
1322
|
+
count,
|
|
1323
|
+
agent: check.agent
|
|
1324
|
+
});
|
|
1325
|
+
set({
|
|
1326
|
+
...state,
|
|
1327
|
+
reason: check.status
|
|
1328
|
+
});
|
|
1329
|
+
});
|
|
1330
|
+
};
|
|
1331
|
+
const pair = async (copy) => {
|
|
1332
|
+
if (fetching) return;
|
|
1333
|
+
fetching = true;
|
|
1334
|
+
const at = ++step;
|
|
1335
|
+
try {
|
|
1336
|
+
const pairing = transport.startPairing();
|
|
1337
|
+
const prompt = pairing.then((next) => next.prompt);
|
|
1338
|
+
prompt.catch(() => void 0);
|
|
1339
|
+
const copied = (async () => copy ? copy(prompt) : true)().then((ok) => ({ ok }), (error) => ({
|
|
1340
|
+
ok: false,
|
|
1341
|
+
error
|
|
1342
|
+
}));
|
|
1343
|
+
const next = await pairing;
|
|
1344
|
+
const outcome = await copied;
|
|
1345
|
+
if (at !== step) return drop(next.code);
|
|
1346
|
+
fetching = false;
|
|
1347
|
+
if (!outcome.ok) {
|
|
1348
|
+
drop(next.code);
|
|
1349
|
+
const reason = "error" in outcome ? reasonOf(outcome.error) : "The browser did not let the page copy the setup prompt.";
|
|
1350
|
+
return set({
|
|
1351
|
+
status: "failed",
|
|
1352
|
+
count,
|
|
1353
|
+
reason
|
|
1354
|
+
});
|
|
1355
|
+
}
|
|
1356
|
+
if (code) drop(code);
|
|
1357
|
+
code = next.code;
|
|
1358
|
+
count = next.count;
|
|
1359
|
+
selection = next.selection;
|
|
1360
|
+
set({
|
|
1361
|
+
status: "pairing",
|
|
1362
|
+
count,
|
|
1363
|
+
prompt: next.prompt,
|
|
1364
|
+
expiresAt: next.expiresAt,
|
|
1365
|
+
agent: next.agent
|
|
1366
|
+
});
|
|
1367
|
+
poll();
|
|
1368
|
+
} catch (error) {
|
|
1369
|
+
if (at === step) {
|
|
1370
|
+
fetching = false;
|
|
1371
|
+
set({
|
|
1372
|
+
status: "failed",
|
|
1373
|
+
count,
|
|
1374
|
+
reason: reasonOf(error)
|
|
1375
|
+
});
|
|
1376
|
+
}
|
|
1377
|
+
}
|
|
1378
|
+
};
|
|
1379
|
+
const reselect = async () => {
|
|
1380
|
+
const at = ++selections;
|
|
1381
|
+
const pairing = code;
|
|
1382
|
+
const status = await transport.status().catch(() => void 0);
|
|
1383
|
+
if (!status || at !== selections || !pairing || pairing !== code || state.status !== "pairing" && state.status !== "confirm") return;
|
|
1384
|
+
count = status.count;
|
|
1385
|
+
selection = status.selection ?? "";
|
|
1386
|
+
state = {
|
|
1387
|
+
...state,
|
|
1388
|
+
count
|
|
1389
|
+
};
|
|
1390
|
+
notify();
|
|
1391
|
+
};
|
|
1392
|
+
return {
|
|
1393
|
+
get state() {
|
|
1394
|
+
return state;
|
|
1395
|
+
},
|
|
1396
|
+
onChange(listener) {
|
|
1397
|
+
listeners.add(listener);
|
|
1398
|
+
return () => {
|
|
1399
|
+
listeners.delete(listener);
|
|
1400
|
+
};
|
|
1401
|
+
},
|
|
1402
|
+
start() {
|
|
1403
|
+
const current = ++run;
|
|
1404
|
+
const stop = transport.subscribe?.((event) => {
|
|
1405
|
+
if (current !== run) return;
|
|
1406
|
+
if (event.type === "count") {
|
|
1407
|
+
countRevision++;
|
|
1408
|
+
if (state.status === "pairing" || state.status === "confirm") return void reselect();
|
|
1409
|
+
count = event.count;
|
|
1410
|
+
if (state.status !== "sending") {
|
|
1411
|
+
state = state.status === "idle" ? idleState() : {
|
|
1412
|
+
...state,
|
|
1413
|
+
count
|
|
1414
|
+
};
|
|
1415
|
+
notify();
|
|
1416
|
+
}
|
|
1417
|
+
return;
|
|
1418
|
+
}
|
|
1419
|
+
if (state.status === "sending") {
|
|
1420
|
+
const update = pushed?.get(event.id);
|
|
1421
|
+
pushed?.set(event.id, event.type === "done" ? {
|
|
1422
|
+
...update,
|
|
1423
|
+
done: true
|
|
1424
|
+
} : {
|
|
1425
|
+
...update,
|
|
1426
|
+
thread: event.thread ?? update?.thread
|
|
1427
|
+
});
|
|
1428
|
+
return;
|
|
1429
|
+
}
|
|
1430
|
+
if (fetching || state.status === "pairing" || state.status === "confirm") return;
|
|
1431
|
+
if ((state.status === "working" || state.status === "sent") && sendId !== event.id) return;
|
|
1432
|
+
if (event.type === "working") {
|
|
1433
|
+
thread = event.thread ?? (event.id === sendId ? thread : void 0);
|
|
1434
|
+
sendId = event.id;
|
|
1435
|
+
if (state.status !== "sent") working();
|
|
1436
|
+
return;
|
|
1437
|
+
}
|
|
1438
|
+
if (event.id === sendId) finish();
|
|
1439
|
+
});
|
|
1440
|
+
refresh(() => void 0);
|
|
1441
|
+
return () => {
|
|
1442
|
+
stop?.();
|
|
1443
|
+
if (current !== run) return;
|
|
1444
|
+
run++;
|
|
1445
|
+
if (code) drop(code);
|
|
1446
|
+
code = void 0;
|
|
1447
|
+
fetching = false;
|
|
1448
|
+
pushed = void 0;
|
|
1449
|
+
agent = void 0;
|
|
1450
|
+
count = 0;
|
|
1451
|
+
finish();
|
|
1452
|
+
};
|
|
1453
|
+
},
|
|
1454
|
+
refresh: () => refresh(),
|
|
1455
|
+
async send(copy) {
|
|
1456
|
+
if (fetching || state.status !== "idle" && state.status !== "failed" && state.status !== "working") return;
|
|
1457
|
+
if (state.status !== "failed" && count === 0) return;
|
|
1458
|
+
if (!agent) return pair(copy);
|
|
1459
|
+
return deliver(() => transport.send());
|
|
1460
|
+
},
|
|
1461
|
+
async pairAnother(copy) {
|
|
1462
|
+
if (state.status !== "idle" || !state.canPairAnother) return;
|
|
1463
|
+
return pair(copy);
|
|
1464
|
+
},
|
|
1465
|
+
async allow() {
|
|
1466
|
+
if (state.status !== "confirm" || !code) return;
|
|
1467
|
+
const used = code;
|
|
1468
|
+
code = void 0;
|
|
1469
|
+
return deliver(() => transport.allow(used, selection), {
|
|
1470
|
+
code: used,
|
|
1471
|
+
agent: state.agent
|
|
1472
|
+
});
|
|
1473
|
+
},
|
|
1474
|
+
async copyAgain(copy) {
|
|
1475
|
+
if (state.status !== "pairing" || !state.reason) return;
|
|
1476
|
+
return pair(copy);
|
|
1477
|
+
},
|
|
1478
|
+
cancel() {
|
|
1479
|
+
if (state.status !== "pairing" && state.status !== "confirm") return;
|
|
1480
|
+
if (code) drop(code);
|
|
1481
|
+
code = void 0;
|
|
1482
|
+
fetching = false;
|
|
1483
|
+
idle();
|
|
1484
|
+
}
|
|
1485
|
+
};
|
|
1486
|
+
}
|
|
1487
|
+
//#endregion
|
|
409
1488
|
//#region src/rest-transport.ts
|
|
410
|
-
/** The URL of an attachment key below the Comments API, such as `/
|
|
1489
|
+
/** The URL of an attachment key below the Comments API, such as `/_vitehub/comments/attachments/comments/<id>/shot.png`. */
|
|
411
1490
|
function commentAttachmentUrl(key, base = "") {
|
|
412
1491
|
return `${base.replace(/\/+$/, "")}${commentsRoute}/attachments/${key.split("/").map((part) => encodeURIComponent(part)).join("/")}`;
|
|
413
1492
|
}
|
|
414
1493
|
/**
|
|
415
|
-
* Requests to the Comments API at `<base>/
|
|
1494
|
+
* Requests to the Comments API at `<base>/_vitehub/comments`, with the guest headers and the session cookie. A
|
|
416
1495
|
* failure throws a diagnostic with the `code`, `why`, and `fix` of the error object of the answer, and its `status`.
|
|
417
1496
|
*/
|
|
418
1497
|
function createCommentsRestRequest(options = {}) {
|
|
419
|
-
const route = options.route ?? `${(options.base ?? "").replace(/\/+$/, "")}/
|
|
1498
|
+
const route = options.route ?? `${(options.base ?? "").replace(/\/+$/, "")}/_vitehub/comments`;
|
|
420
1499
|
return async (path, init = {}) => {
|
|
421
1500
|
const send = options.fetch ?? globalThis.fetch;
|
|
422
1501
|
const headers = new Headers(typeof options.headers === "function" ? await options.headers() : options.headers);
|
|
@@ -439,13 +1518,15 @@ function createCommentsRestRequest(options = {}) {
|
|
|
439
1518
|
throw commentsUnansweredError(0, cause);
|
|
440
1519
|
}
|
|
441
1520
|
const value = await response.json().catch(() => void 0);
|
|
1521
|
+
const retryAfter = retryAfterOf(response);
|
|
442
1522
|
if (isRecordOrFunction(value) && isRecordOrFunction(value.error)) {
|
|
443
1523
|
const { code, why, fix } = value.error;
|
|
444
|
-
if (typeof code !== "string" || typeof why !== "string") throw commentsUnansweredError(response.status);
|
|
1524
|
+
if (typeof code !== "string" || typeof why !== "string") throw commentsUnansweredError(response.status, void 0, retryAfter);
|
|
445
1525
|
throw commentsAnswerError({
|
|
446
1526
|
code,
|
|
447
1527
|
why,
|
|
448
1528
|
...typeof fix === "string" ? { fix } : {},
|
|
1529
|
+
...retryAfter === void 0 ? {} : { retryAfter },
|
|
449
1530
|
status: response.status
|
|
450
1531
|
});
|
|
451
1532
|
}
|
|
@@ -458,7 +1539,7 @@ function createCommentsRestRequest(options = {}) {
|
|
|
458
1539
|
options.onAnswer?.(answer);
|
|
459
1540
|
return answer;
|
|
460
1541
|
}
|
|
461
|
-
throw commentsUnansweredError(response.status);
|
|
1542
|
+
throw commentsUnansweredError(response.status, void 0, retryAfter);
|
|
462
1543
|
};
|
|
463
1544
|
}
|
|
464
1545
|
function listSearch(query) {
|
|
@@ -480,12 +1561,13 @@ function listSearch(query) {
|
|
|
480
1561
|
return params;
|
|
481
1562
|
}
|
|
482
1563
|
const thread = (id) => `/threads/${encodeURIComponent(id)}`;
|
|
483
|
-
const post = (body) => ({
|
|
1564
|
+
const post = (body, account) => ({
|
|
484
1565
|
method: "POST",
|
|
485
|
-
...body === void 0 ? {} : { body: JSON.stringify(body) }
|
|
1566
|
+
...body === void 0 ? {} : { body: JSON.stringify(body) },
|
|
1567
|
+
...account === void 0 ? {} : { headers: { [outboxAccountHeader]: encodeURIComponent(account) } }
|
|
486
1568
|
});
|
|
487
1569
|
/**
|
|
488
|
-
* A transport over the Comments API at `<base>/
|
|
1570
|
+
* A transport over the Comments API at `<base>/_vitehub/comments`: the routes that the overlay uses. The server
|
|
489
1571
|
* runs the `authorize` policy of `server/comments.ts` for the user of the request: the app session, or a guest that
|
|
490
1572
|
* `guest` names. Guests and share-link readers can use it. It has no
|
|
491
1573
|
* `subscribe`: the client refreshes when the window gets focus.
|
|
@@ -493,19 +1575,28 @@ const post = (body) => ({
|
|
|
493
1575
|
function createRestTransport(options = {}) {
|
|
494
1576
|
const request = createCommentsRestRequest(options);
|
|
495
1577
|
const base = (options.base ?? "").replace(/\/+$/, "");
|
|
1578
|
+
const upload = async (input, account) => (await request("/attachments", post(input, account))).data;
|
|
496
1579
|
return {
|
|
497
1580
|
async list(query) {
|
|
498
1581
|
return (await request(`/threads?${listSearch(query)}`)).data;
|
|
499
1582
|
},
|
|
500
1583
|
async get(input) {
|
|
501
|
-
const path = "
|
|
1584
|
+
const path = "threadId" in input ? thread(input.threadId) : `/scopes/${encodeURIComponent(input.scope)}/threads/${input.n}`;
|
|
502
1585
|
return (await request(path)).data;
|
|
503
1586
|
},
|
|
504
|
-
async create(input) {
|
|
505
|
-
|
|
1587
|
+
async create({ files, outbox, ...input }) {
|
|
1588
|
+
const body = await withCommentFiles(input, {
|
|
1589
|
+
files,
|
|
1590
|
+
outbox
|
|
1591
|
+
}, input.subject, (data) => upload(data, outbox?.account));
|
|
1592
|
+
return (await request("/threads", post(body, outbox?.account))).data;
|
|
506
1593
|
},
|
|
507
|
-
async reply({ threadId, ...input }) {
|
|
508
|
-
const
|
|
1594
|
+
async reply({ threadId, subject, files, outbox, ...input }) {
|
|
1595
|
+
const body = await withCommentFiles(input, {
|
|
1596
|
+
files,
|
|
1597
|
+
outbox
|
|
1598
|
+
}, subject, (data) => upload(data, outbox?.account));
|
|
1599
|
+
const answer = await request(`${thread(threadId)}/comments`, post(body, outbox?.account));
|
|
509
1600
|
if (!answer.thread) throw commentsUnansweredError(200);
|
|
510
1601
|
return answer.thread;
|
|
511
1602
|
},
|
|
@@ -535,15 +1626,58 @@ function createRestTransport(options = {}) {
|
|
|
535
1626
|
}
|
|
536
1627
|
return (await request(`/people?${params}`)).data;
|
|
537
1628
|
},
|
|
538
|
-
|
|
539
|
-
return (await request("/attachments", post(input))).data;
|
|
540
|
-
},
|
|
1629
|
+
upload: (input) => upload(input),
|
|
541
1630
|
attachmentUrl: (key) => commentAttachmentUrl(key, base)
|
|
542
1631
|
};
|
|
543
1632
|
}
|
|
544
1633
|
//#endregion
|
|
1634
|
+
//#region src/send-transport.ts
|
|
1635
|
+
/**
|
|
1636
|
+
* The transport of `createCommentSend()` over the Send routes of Comments, `<base>/_vitehub/comments/send/**`. The
|
|
1637
|
+
* ViteHub pill uses it, and an app renders `CommentSend` of `@vite-hub/ui` with it in its own pill:
|
|
1638
|
+
*
|
|
1639
|
+
* ```ts
|
|
1640
|
+
* import { createCommentSend, createCommentSendTransport } from "@vite-hub/comments/client"
|
|
1641
|
+
*
|
|
1642
|
+
* const send = createCommentSend(createCommentSendTransport({ page: () => location.pathname }))
|
|
1643
|
+
* ```
|
|
1644
|
+
*/
|
|
1645
|
+
function createCommentSendTransport(options = {}) {
|
|
1646
|
+
const request = createCommentsRestRequest(options);
|
|
1647
|
+
const page = () => encodeURIComponent(typeof options.page === "function" ? options.page() : options.page ?? location.pathname);
|
|
1648
|
+
const post = (body) => ({
|
|
1649
|
+
method: "POST",
|
|
1650
|
+
...body === void 0 ? {} : { body: JSON.stringify(body) }
|
|
1651
|
+
});
|
|
1652
|
+
const code = (value) => encodeURIComponent(value);
|
|
1653
|
+
const pairingPages = /* @__PURE__ */ new Map();
|
|
1654
|
+
return {
|
|
1655
|
+
status: async () => (await request(`/send/status?page=${page()}`)).data,
|
|
1656
|
+
startPairing: async () => {
|
|
1657
|
+
const path = page();
|
|
1658
|
+
const pairing = (await request(`/send/start?page=${path}`, post())).data;
|
|
1659
|
+
pairingPages.set(pairing.code, path);
|
|
1660
|
+
return pairing;
|
|
1661
|
+
},
|
|
1662
|
+
pollPairing: async (value) => (await request(`/send/pair/${code(value)}`)).data,
|
|
1663
|
+
allow: async (value, selection) => {
|
|
1664
|
+
const path = pairingPages.get(value) ?? page();
|
|
1665
|
+
const result = (await request(`/send/allow?page=${path}`, post({
|
|
1666
|
+
code: value,
|
|
1667
|
+
selection
|
|
1668
|
+
}))).data;
|
|
1669
|
+
if (result.status !== "changed") pairingPages.delete(value);
|
|
1670
|
+
return result;
|
|
1671
|
+
},
|
|
1672
|
+
cancel: async (value) => {
|
|
1673
|
+
pairingPages.delete(value);
|
|
1674
|
+
await request(`/send/pair/${code(value)}`, { method: "DELETE" });
|
|
1675
|
+
},
|
|
1676
|
+
send: async () => (await request(`/send?page=${page()}`, post())).data
|
|
1677
|
+
};
|
|
1678
|
+
}
|
|
1679
|
+
//#endregion
|
|
545
1680
|
//#region src/client.ts
|
|
546
|
-
/** A cache and a change signal over a transport. It has no framework in it. */
|
|
547
1681
|
function createCommentsClient(options) {
|
|
548
1682
|
const { transport } = options;
|
|
549
1683
|
const listeners = /* @__PURE__ */ new Set();
|
|
@@ -560,7 +1694,7 @@ function createCommentsClient(options) {
|
|
|
560
1694
|
const notify = () => {
|
|
561
1695
|
for (const listener of listeners) listener();
|
|
562
1696
|
};
|
|
563
|
-
const matchesQuery = (thread) => {
|
|
1697
|
+
const matchesQuery = (thread, waiting = false) => {
|
|
564
1698
|
const user = options.user;
|
|
565
1699
|
if (query.subject && (thread.subject.kind !== query.subject.kind || thread.subject.id !== query.subject.id)) return false;
|
|
566
1700
|
if (query.subjectKind !== void 0 && thread.subject.kind !== query.subjectKind) return false;
|
|
@@ -569,11 +1703,17 @@ function createCommentsClient(options) {
|
|
|
569
1703
|
if (query.status && thread.status !== query.status) return false;
|
|
570
1704
|
if (user && query.mine && thread.author.id !== user.id) return false;
|
|
571
1705
|
if (user && query.mentions && !thread.messages.some((message) => message.mentions.includes(user.id))) return false;
|
|
1706
|
+
if (waiting) return !(query.mentions && !user);
|
|
572
1707
|
if (transportOwnsMembership() && !listed.has(thread.id)) return false;
|
|
573
1708
|
return true;
|
|
574
1709
|
};
|
|
575
1710
|
const publish = () => {
|
|
576
|
-
|
|
1711
|
+
const stored = [...byId.values()];
|
|
1712
|
+
const { server, local } = outbox ? outbox.overlay(stored) : {
|
|
1713
|
+
server: stored,
|
|
1714
|
+
local: []
|
|
1715
|
+
};
|
|
1716
|
+
threads = [...server.filter((thread) => matchesQuery(thread)).sort((a, b) => a.n - b.n), ...local.filter((thread) => matchesQuery(thread, true))];
|
|
577
1717
|
notify();
|
|
578
1718
|
};
|
|
579
1719
|
const upsert = (thread) => {
|
|
@@ -668,6 +1808,22 @@ function createCommentsClient(options) {
|
|
|
668
1808
|
}
|
|
669
1809
|
return threads;
|
|
670
1810
|
};
|
|
1811
|
+
/** The thread as the client shows it, with what waits in the outbox, also when the query leaves it out. */
|
|
1812
|
+
const shown = (threadId) => {
|
|
1813
|
+
const stored = byId.get(threadId);
|
|
1814
|
+
const { server, local } = outbox ? outbox.overlay(stored ? [stored] : []) : {
|
|
1815
|
+
server: stored ? [stored] : [],
|
|
1816
|
+
local: []
|
|
1817
|
+
};
|
|
1818
|
+
return server[0] ?? local.find((thread) => thread.id === threadId) ?? null;
|
|
1819
|
+
};
|
|
1820
|
+
const outbox = options.outbox && createOutbox(options.outbox, {
|
|
1821
|
+
transport,
|
|
1822
|
+
user: options.user,
|
|
1823
|
+
threads: () => byId.values(),
|
|
1824
|
+
upsert: (thread) => void upsert(thread),
|
|
1825
|
+
changed: publish
|
|
1826
|
+
});
|
|
671
1827
|
const refreshQuietly = () => {
|
|
672
1828
|
refresh().catch(() => void 0);
|
|
673
1829
|
};
|
|
@@ -701,19 +1857,37 @@ function createCommentsClient(options) {
|
|
|
701
1857
|
publish();
|
|
702
1858
|
},
|
|
703
1859
|
refresh,
|
|
704
|
-
get: (input) =>
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
1860
|
+
get: async (input) => {
|
|
1861
|
+
const thread = await transport.get(input);
|
|
1862
|
+
if (!thread || !outbox) return thread;
|
|
1863
|
+
upsert(thread);
|
|
1864
|
+
return shown(thread.id) ?? thread;
|
|
1865
|
+
},
|
|
1866
|
+
create: async (input) => outbox ? outbox.create(input) : upsert(await transport.create(input)),
|
|
1867
|
+
reply: (input) => outbox ? outbox.reply(input) : mutate(input.threadId, async () => upsert(await transport.reply(input))),
|
|
1868
|
+
edit: async (input) => {
|
|
1869
|
+
if (outbox?.owns(input.threadId, input.messageId)) {
|
|
1870
|
+
if (!await outbox.edit(input.messageId, input.body, input.mentions)) throw commentsClientDiagnostics.COMMENTS_OUTBOX_SENDING();
|
|
1871
|
+
return shown(input.threadId);
|
|
1872
|
+
}
|
|
1873
|
+
return mutate(input.threadId, async () => upsert(await transport.edit(input)));
|
|
1874
|
+
},
|
|
1875
|
+
delete: async (input) => {
|
|
1876
|
+
if (outbox?.owns(input.threadId, input.messageId)) {
|
|
1877
|
+
if (!await outbox.dismiss(input.messageId ?? input.threadId)) throw commentsClientDiagnostics.COMMENTS_OUTBOX_SENDING();
|
|
1878
|
+
return shown(input.threadId);
|
|
1879
|
+
}
|
|
1880
|
+
return mutate(input.threadId, async () => {
|
|
1881
|
+
const next = await transport.delete(input);
|
|
1882
|
+
if (next) upsert(next);
|
|
1883
|
+
else drop(input.threadId);
|
|
1884
|
+
return next;
|
|
1885
|
+
});
|
|
1886
|
+
},
|
|
714
1887
|
resolve: (input) => mutate(input.threadId, async () => upsert(await transport.resolve(input))),
|
|
715
1888
|
reopen: (input) => mutate(input.threadId, async () => upsert(await transport.reopen(input))),
|
|
716
1889
|
users: (input) => transport.users(input),
|
|
1890
|
+
outbox,
|
|
717
1891
|
onChange(listener) {
|
|
718
1892
|
listeners.add(listener);
|
|
719
1893
|
return () => {
|
|
@@ -727,6 +1901,7 @@ function createCommentsClient(options) {
|
|
|
727
1901
|
window.addEventListener("focus", refreshQuietly);
|
|
728
1902
|
stops.push(() => window.removeEventListener("focus", refreshQuietly));
|
|
729
1903
|
}
|
|
1904
|
+
if (outbox) stops.push(outbox.start());
|
|
730
1905
|
refreshQuietly();
|
|
731
1906
|
return () => {
|
|
732
1907
|
for (const stop of stops.splice(0)) stop();
|
|
@@ -735,4 +1910,4 @@ function createCommentsClient(options) {
|
|
|
735
1910
|
};
|
|
736
1911
|
}
|
|
737
1912
|
//#endregion
|
|
738
|
-
export { anchorRange, commentAttachmentUrl, commentsErrorDiagnostics, createAnchor, createCommentsClient, createCommentsRestRequest, createRestTransport, foldName, resolveAnchor, selectorFor };
|
|
1913
|
+
export { anchorRange, commentAttachmentUrl, commentsErrorDiagnostics, createAnchor, createCommentSend, createCommentSendTransport, createCommentsClient, createCommentsRestRequest, createIndexedDBOutboxStore, createMemoryOutboxStore, createRestTransport, foldName, replyMessageId, resolveAnchor, selectorFor };
|