@pmate/chat-sdk 0.12.0-next.88 → 0.12.0-next.89
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/lib/runtime/chatSession.d.ts +2 -0
- package/lib/runtime/chatSession.js +17 -1
- package/lib/runtime/createChat.d.ts +2 -0
- package/lib/runtime/createChat.js +17 -4
- package/lib/runtime/groupClient.d.ts +1 -0
- package/lib/runtime/groupClient.js +3 -3
- package/lib/runtime/inboxClient.d.ts +63 -0
- package/lib/runtime/inboxClient.js +259 -0
- package/lib/runtime/inboxGroup.d.ts +4 -0
- package/lib/runtime/inboxGroup.js +51 -0
- package/lib/runtime/inboxStore.d.ts +178 -0
- package/lib/runtime/inboxStore.js +307 -0
- package/lib/runtime/inboxTransport.d.ts +6 -0
- package/lib/runtime/inboxTransport.js +67 -0
- package/lib/runtime/index.d.ts +2 -0
- package/lib/runtime/index.js +2 -0
- package/lib/runtime/messageClient.d.ts +2 -0
- package/lib/runtime/messageClient.js +3 -3
- package/lib/runtime/readSync.d.ts +6 -0
- package/lib/runtime/readSync.js +42 -1
- package/lib/runtime/readingClient.d.ts +3 -0
- package/lib/runtime/readingClient.js +11 -2
- package/package.json +1 -1
package/lib/runtime/readSync.js
CHANGED
|
@@ -5,10 +5,14 @@ export class ReadSyncClient {
|
|
|
5
5
|
controller = new AbortController();
|
|
6
6
|
inFlight;
|
|
7
7
|
readAfter = 0;
|
|
8
|
+
inboxApplying;
|
|
9
|
+
inboxDirty = false;
|
|
8
10
|
constructor(options) {
|
|
9
11
|
this.options = options;
|
|
10
12
|
}
|
|
11
13
|
async loadLocal() {
|
|
14
|
+
if (this.options.inbox)
|
|
15
|
+
return this.applyInboxLocal();
|
|
12
16
|
const { appId, profileId, persistence, metadataOnly } = this.options;
|
|
13
17
|
const state = await persistence.load(appId, profileId) ?? createReadIndex(appId, profileId);
|
|
14
18
|
const normalize = (current) => {
|
|
@@ -20,6 +24,37 @@ export class ReadSyncClient {
|
|
|
20
24
|
return persistence.update(appId, profileId, normalize);
|
|
21
25
|
}
|
|
22
26
|
stop() { this.controller.abort(); }
|
|
27
|
+
applyInboxLocal() {
|
|
28
|
+
this.inboxDirty = true;
|
|
29
|
+
if (this.inboxApplying)
|
|
30
|
+
return this.inboxApplying;
|
|
31
|
+
return this.inboxApplying = (async () => {
|
|
32
|
+
let state;
|
|
33
|
+
do {
|
|
34
|
+
this.inboxDirty = false;
|
|
35
|
+
state = await this.installInboxLocal();
|
|
36
|
+
} while (this.inboxDirty && !this.controller.signal.aborted);
|
|
37
|
+
return state;
|
|
38
|
+
})().finally(() => { this.inboxApplying = undefined; });
|
|
39
|
+
}
|
|
40
|
+
async installInboxLocal() {
|
|
41
|
+
const { inbox, persistence, appId, profileId } = this.options;
|
|
42
|
+
if (!inbox)
|
|
43
|
+
throw new Error("INBOX_NOT_CONFIGURED");
|
|
44
|
+
const snapshot = await inbox.store.readingSnapshot();
|
|
45
|
+
const versions = persistence.loadVersions ? await persistence.loadVersions(appId, profileId) : {};
|
|
46
|
+
const next = await persistence.update(appId, profileId, (current, currentVersions) => {
|
|
47
|
+
assertReadIndexVersions(versions, currentVersions);
|
|
48
|
+
if (this.controller.signal.aborted)
|
|
49
|
+
throw new Error("READ_SYNC_STOPPED");
|
|
50
|
+
// Inbox replaces old server-derived positions. Preserve pending local
|
|
51
|
+
// observations/operations, not stale state from the retired projection.
|
|
52
|
+
return installReadSnapshot({ ...current, threads: [] }, snapshot);
|
|
53
|
+
});
|
|
54
|
+
if (!this.controller.signal.aborted)
|
|
55
|
+
this.options.onChange?.(next);
|
|
56
|
+
return next;
|
|
57
|
+
}
|
|
23
58
|
synchronize() {
|
|
24
59
|
if (this.inFlight)
|
|
25
60
|
return this.inFlight;
|
|
@@ -60,7 +95,7 @@ export class ReadSyncClient {
|
|
|
60
95
|
return false;
|
|
61
96
|
const { appId, profileId, persistence } = this.options;
|
|
62
97
|
const query = new URLSearchParams({ appId, profileId, ...(this.options.metadataOnly ? { metadataOnly: "true" } : {}) });
|
|
63
|
-
const snapshot = await this.request(`/api/social/reading/snapshot?${query}`);
|
|
98
|
+
const snapshot = this.options.inbox ? await this.options.inbox.store.readingSnapshot() : await this.request(`/api/social/reading/snapshot?${query}`);
|
|
64
99
|
// Validate the complete response before changing any durable operation.
|
|
65
100
|
installReadSnapshot(createReadIndex(appId, profileId), snapshot);
|
|
66
101
|
const thread = snapshot.threads.find(thread => thread.id === operation.threadId);
|
|
@@ -91,6 +126,8 @@ export class ReadSyncClient {
|
|
|
91
126
|
return repaired;
|
|
92
127
|
}
|
|
93
128
|
async run(repairedLegacy = false) {
|
|
129
|
+
if (this.options.inbox?.offlineMode())
|
|
130
|
+
return this.applyInboxLocal();
|
|
94
131
|
const { appId, profileId, persistence } = this.options;
|
|
95
132
|
const loaded = persistence.loadVersioned ? await persistence.loadVersioned(appId, profileId) : { state: await persistence.load(appId, profileId), versions: {} };
|
|
96
133
|
const current = loaded.state ?? createReadIndex(appId, profileId);
|
|
@@ -129,6 +166,10 @@ export class ReadSyncClient {
|
|
|
129
166
|
throw new Error("INVALID_READING_RECEIPT");
|
|
130
167
|
this.readAfter = Math.max(this.readAfter, receipt.acceptedAt);
|
|
131
168
|
}
|
|
169
|
+
if (this.options.inbox) {
|
|
170
|
+
await this.options.inbox.synchronize();
|
|
171
|
+
return this.applyInboxLocal();
|
|
172
|
+
}
|
|
132
173
|
const query = new URLSearchParams({ appId, profileId });
|
|
133
174
|
if (this.options.metadataOnly)
|
|
134
175
|
query.set("metadataOnly", "true");
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { type ConversationReadMessage } from "./conversationRead.js";
|
|
2
2
|
import { type ReadIndexPersistence, type ReadIndexState } from "./readIndex.js";
|
|
3
|
+
import type { InboxClient } from "./inboxClient.js";
|
|
3
4
|
export type ReadingClientOptions = {
|
|
5
|
+
inbox?: InboxClient;
|
|
4
6
|
appId: string;
|
|
5
7
|
profileId: string;
|
|
6
8
|
sessionToken: string;
|
|
@@ -34,6 +36,7 @@ export declare class ReadingClient {
|
|
|
34
36
|
private retry?;
|
|
35
37
|
private retryDelay;
|
|
36
38
|
private offWake?;
|
|
39
|
+
private offInbox?;
|
|
37
40
|
private generation;
|
|
38
41
|
constructor(options: ReadingClientOptions);
|
|
39
42
|
getSnapshot: () => ReadIndexState | undefined;
|
|
@@ -25,6 +25,7 @@ export class ReadingClient {
|
|
|
25
25
|
retry;
|
|
26
26
|
retryDelay = 1000;
|
|
27
27
|
offWake;
|
|
28
|
+
offInbox;
|
|
28
29
|
generation = 0;
|
|
29
30
|
constructor(options) {
|
|
30
31
|
this.options = options;
|
|
@@ -35,6 +36,12 @@ export class ReadingClient {
|
|
|
35
36
|
this.persistence = new BatchedReadIndex(storage);
|
|
36
37
|
this.tasks = new WindowedTasks(1000, error => this.failed(error));
|
|
37
38
|
this.sync = new ReadSyncClient({ ...options, persistence: this.persistence, onChange: state => this.publish(state) });
|
|
39
|
+
if (options.inbox)
|
|
40
|
+
this.offInbox = options.inbox.subscribe(() => {
|
|
41
|
+
if (!this.closed)
|
|
42
|
+
void this.sync.applyInboxLocal().then(state => { if (state.pending.length)
|
|
43
|
+
this.refresh(); }).catch(error => this.failed(error));
|
|
44
|
+
});
|
|
38
45
|
}
|
|
39
46
|
getSnapshot = () => this.state;
|
|
40
47
|
subscribe = (listener) => { this.listeners.add(listener); return () => { this.listeners.delete(listener); }; };
|
|
@@ -65,10 +72,11 @@ export class ReadingClient {
|
|
|
65
72
|
window.addEventListener("focus", wake);
|
|
66
73
|
this.offWake = () => { window.removeEventListener("online", wake); window.removeEventListener("focus", wake); };
|
|
67
74
|
}
|
|
68
|
-
this.
|
|
75
|
+
if (!this.options.inbox || this.state?.pending.length)
|
|
76
|
+
this.refresh();
|
|
69
77
|
}
|
|
70
78
|
refresh = () => {
|
|
71
|
-
if (!this.closed && !isOffline())
|
|
79
|
+
if (!this.closed && !isOffline() && !this.options.inbox?.offlineMode())
|
|
72
80
|
this.tasks.schedule("reading", async () => { if (!isOffline())
|
|
73
81
|
await this.synchronize(); });
|
|
74
82
|
};
|
|
@@ -180,6 +188,7 @@ export class ReadingClient {
|
|
|
180
188
|
this.closed = true;
|
|
181
189
|
this.generation++;
|
|
182
190
|
this.offWake?.();
|
|
191
|
+
this.offInbox?.();
|
|
183
192
|
this.tasks.close();
|
|
184
193
|
clearTimeout(this.retry);
|
|
185
194
|
this.sync.stop();
|
package/package.json
CHANGED