@pmate/chat-sdk 0.12.0-next.96 → 0.13.0-profile.1
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/framework.d.ts +2 -0
- package/lib/framework.js +2 -0
- package/lib/messageView.d.ts +5 -0
- package/lib/messageView.js +2 -2
- package/lib/profile.d.ts +14 -0
- package/lib/profile.js +10 -0
- package/lib/profileWorker.d.ts +1 -0
- package/lib/profileWorker.js +3 -0
- package/lib/runtime/profileChat.d.ts +138 -0
- package/lib/runtime/profileChat.js +324 -0
- package/lib/runtime/profileCommitQueue.d.ts +38 -0
- package/lib/runtime/profileCommitQueue.js +101 -0
- package/lib/runtime/profileCoordinator.d.ts +46 -0
- package/lib/runtime/profileCoordinator.js +135 -0
- package/lib/runtime/profileDatabase.d.ts +42 -0
- package/lib/runtime/profileDatabase.js +164 -0
- package/lib/runtime/profileGenerationBuilder.d.ts +18 -0
- package/lib/runtime/profileGenerationBuilder.js +112 -0
- package/lib/runtime/profileInboxFeed.d.ts +38 -0
- package/lib/runtime/profileInboxFeed.js +240 -0
- package/lib/runtime/profileMemoryStore.d.ts +115 -0
- package/lib/runtime/profileMemoryStore.js +265 -0
- package/lib/runtime/profileOutboxDispatcher.d.ts +31 -0
- package/lib/runtime/profileOutboxDispatcher.js +106 -0
- package/lib/runtime/profileOwner.d.ts +57 -0
- package/lib/runtime/profileOwner.js +202 -0
- package/lib/runtime/profileProjection.d.ts +77 -0
- package/lib/runtime/profileProjection.js +625 -0
- package/lib/runtime/profileReadingIndex.d.ts +17 -0
- package/lib/runtime/profileReadingIndex.js +99 -0
- package/lib/runtime/profileReplica.d.ts +59 -0
- package/lib/runtime/profileReplica.js +236 -0
- package/lib/runtime/profileStoreTypes.d.ts +163 -0
- package/lib/runtime/profileStoreTypes.js +1 -0
- package/lib/runtime/profileWindows.d.ts +38 -0
- package/lib/runtime/profileWindows.js +175 -0
- package/lib/runtime/profileWorkerHost.d.ts +33 -0
- package/lib/runtime/profileWorkerHost.js +156 -0
- package/package.json +11 -1
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
export function compareProfileOrder(a, b) {
|
|
2
|
+
for (let i = 0; i < a.length; i++) {
|
|
3
|
+
if (a[i] < b[i])
|
|
4
|
+
return -1;
|
|
5
|
+
if (a[i] > b[i])
|
|
6
|
+
return 1;
|
|
7
|
+
}
|
|
8
|
+
return 0;
|
|
9
|
+
}
|
|
10
|
+
function lowerBound(rows, compare) {
|
|
11
|
+
let low = 0, high = rows.length;
|
|
12
|
+
while (low < high) {
|
|
13
|
+
const middle = (low + high) >>> 1;
|
|
14
|
+
if (compare(rows[middle]) < 0)
|
|
15
|
+
low = middle + 1;
|
|
16
|
+
else
|
|
17
|
+
high = middle;
|
|
18
|
+
}
|
|
19
|
+
return low;
|
|
20
|
+
}
|
|
21
|
+
/** A directory contains references to immutable blocks. Blocks seal at 128
|
|
22
|
+
* ordinary appends; historical inserts and moves never split or merge them. */
|
|
23
|
+
export class ProfileWindowDirectory {
|
|
24
|
+
generation;
|
|
25
|
+
threadId;
|
|
26
|
+
windows;
|
|
27
|
+
byMessage = new Map();
|
|
28
|
+
baseLookup;
|
|
29
|
+
removed = new Set();
|
|
30
|
+
offsets = [];
|
|
31
|
+
size = 0;
|
|
32
|
+
constructor(generation, threadId, windows = []) {
|
|
33
|
+
this.generation = generation;
|
|
34
|
+
this.threadId = threadId;
|
|
35
|
+
this.windows = [...windows].sort((a, b) => a.sequence - b.sequence);
|
|
36
|
+
for (const window of this.windows) {
|
|
37
|
+
if (window.generation !== generation || window.threadId !== threadId || window.count !== window.refs.length)
|
|
38
|
+
throw Error("INVALID_PROFILE_WINDOW");
|
|
39
|
+
for (let i = 0; i < window.refs.length; i++) {
|
|
40
|
+
const ref = window.refs[i];
|
|
41
|
+
if (this.byMessage.has(ref.messageId) || i && compareProfileOrder(window.refs[i - 1].orderKey, ref.orderKey) >= 0)
|
|
42
|
+
throw Error("INVALID_PROFILE_WINDOW_ORDER");
|
|
43
|
+
this.byMessage.set(ref.messageId, ref);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
this.reindex();
|
|
47
|
+
}
|
|
48
|
+
get count() { return this.size; }
|
|
49
|
+
get blocks() { return this.windows; }
|
|
50
|
+
/** A transaction draft shares immutable blocks and the committed ID directory.
|
|
51
|
+
* Only IDs in replaced blocks enter the overlay; no whole-thread Map copy. */
|
|
52
|
+
fork() {
|
|
53
|
+
const draft = new ProfileWindowDirectory(this.generation, this.threadId);
|
|
54
|
+
draft.windows = [...this.windows];
|
|
55
|
+
draft.baseLookup = this;
|
|
56
|
+
draft.reindex();
|
|
57
|
+
return draft;
|
|
58
|
+
}
|
|
59
|
+
lookup(id) {
|
|
60
|
+
return this.removed.has(id) ? undefined : this.byMessage.get(id) ?? this.baseLookup?.lookup(id);
|
|
61
|
+
}
|
|
62
|
+
/** Returns changes only. Call accept after the corresponding durable commit. */
|
|
63
|
+
insert(ref, options) {
|
|
64
|
+
const previous = this.lookup(ref.messageId);
|
|
65
|
+
if (previous) {
|
|
66
|
+
if (compareProfileOrder(previous.orderKey, ref.orderKey) !== 0)
|
|
67
|
+
throw Error("PROFILE_MESSAGE_ORDER_CHANGED");
|
|
68
|
+
return this.result([]);
|
|
69
|
+
}
|
|
70
|
+
const nonempty = this.windows.filter(window => window.refs.length);
|
|
71
|
+
const last = nonempty.at(-1)?.refs.at(-1);
|
|
72
|
+
const append = !options.historical && (!last || compareProfileOrder(last.orderKey, ref.orderKey) < 0);
|
|
73
|
+
let target;
|
|
74
|
+
if (append)
|
|
75
|
+
target = this.windows.find(window => window.status === "pending");
|
|
76
|
+
else {
|
|
77
|
+
// Empty sealed windows retain their last boundary for historical routing.
|
|
78
|
+
const index = lowerBound(this.windows, window => window.lastOrderKey ? compareProfileOrder(window.lastOrderKey, ref.orderKey) : -1);
|
|
79
|
+
target = this.windows[Math.min(index, this.windows.length - 1)];
|
|
80
|
+
}
|
|
81
|
+
if (!target) {
|
|
82
|
+
const windowId = options.newWindowId();
|
|
83
|
+
if (this.windows.some(window => window.windowId === windowId))
|
|
84
|
+
throw Error("PROFILE_WINDOW_ID_COLLISION");
|
|
85
|
+
target = { generation: this.generation, threadId: this.threadId, windowId,
|
|
86
|
+
sequence: (this.windows.at(-1)?.sequence ?? -1) + 1, status: "pending", refs: [], count: 0, readingIndex: [], revision: options.revision };
|
|
87
|
+
}
|
|
88
|
+
const index = lowerBound(target.refs, row => compareProfileOrder(row.orderKey, ref.orderKey));
|
|
89
|
+
const refs = [...target.refs.slice(0, index), ref, ...target.refs.slice(index)];
|
|
90
|
+
const next = { ...target, refs, count: refs.length, firstOrderKey: refs[0].orderKey,
|
|
91
|
+
lastOrderKey: refs.at(-1).orderKey, revision: options.revision,
|
|
92
|
+
status: target.status === "sealed" || refs.length >= 128 ? "sealed" : "pending" };
|
|
93
|
+
return this.result([next]);
|
|
94
|
+
}
|
|
95
|
+
remove(messageId, revision) {
|
|
96
|
+
const existing = this.lookup(messageId);
|
|
97
|
+
if (!existing)
|
|
98
|
+
return this.result([]);
|
|
99
|
+
const block = lowerBound(this.windows, window => window.lastOrderKey ? compareProfileOrder(window.lastOrderKey, existing.orderKey) : -1);
|
|
100
|
+
const target = this.windows[block];
|
|
101
|
+
if (!target || !target.refs.some(ref => ref.messageId === messageId))
|
|
102
|
+
throw Error("PROFILE_ANCHOR_UNAVAILABLE");
|
|
103
|
+
const refs = target.refs.filter(ref => ref.messageId !== messageId);
|
|
104
|
+
return this.result([{ ...target, refs, count: refs.length,
|
|
105
|
+
firstOrderKey: refs[0]?.orderKey ?? target.firstOrderKey,
|
|
106
|
+
lastOrderKey: refs.at(-1)?.orderKey ?? target.lastOrderKey,
|
|
107
|
+
readingIndex: target.readingIndex.filter(ref => ref.messageId !== messageId), revision }]);
|
|
108
|
+
}
|
|
109
|
+
result(changes) {
|
|
110
|
+
const replacements = new Map(changes.map(window => [window.windowId, window]));
|
|
111
|
+
const pending = this.windows.map(window => replacements.get(window.windowId) ?? window);
|
|
112
|
+
for (const window of changes)
|
|
113
|
+
if (!this.windows.some(old => old.windowId === window.windowId))
|
|
114
|
+
pending.push(window);
|
|
115
|
+
return { windows: changes, windowCount: pending.length, pendingWindowId: pending.find(window => window.status === "pending")?.windowId };
|
|
116
|
+
}
|
|
117
|
+
accept(change) {
|
|
118
|
+
if (!change.windows.length)
|
|
119
|
+
return;
|
|
120
|
+
for (const next of change.windows) {
|
|
121
|
+
const index = this.windows.findIndex(window => window.windowId === next.windowId);
|
|
122
|
+
if (index < 0)
|
|
123
|
+
this.windows.push(next);
|
|
124
|
+
else {
|
|
125
|
+
for (const old of this.windows[index].refs) {
|
|
126
|
+
this.byMessage.delete(old.messageId);
|
|
127
|
+
this.removed.add(old.messageId);
|
|
128
|
+
}
|
|
129
|
+
this.windows[index] = next;
|
|
130
|
+
}
|
|
131
|
+
for (const ref of next.refs) {
|
|
132
|
+
this.byMessage.set(ref.messageId, ref);
|
|
133
|
+
this.removed.delete(ref.messageId);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
this.reindex();
|
|
137
|
+
}
|
|
138
|
+
reindex() {
|
|
139
|
+
this.size = 0;
|
|
140
|
+
this.offsets = [];
|
|
141
|
+
for (const window of this.windows) {
|
|
142
|
+
this.offsets.push(this.size);
|
|
143
|
+
this.size += window.refs.length;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
/** Exclusive message anchor; no storage, sort, or reading work on this path. */
|
|
147
|
+
readPage(anchor, limit, direction = "older") {
|
|
148
|
+
if (!Number.isSafeInteger(limit) || limit < 1 || limit > 1000)
|
|
149
|
+
throw Error("INVALID_MESSAGE_LIMIT");
|
|
150
|
+
let position = direction === "older" ? this.size : -1;
|
|
151
|
+
if (anchor) {
|
|
152
|
+
const ref = this.lookup(anchor);
|
|
153
|
+
if (!ref)
|
|
154
|
+
throw Error("PROFILE_ANCHOR_UNAVAILABLE");
|
|
155
|
+
const block = lowerBound(this.windows, window => window.lastOrderKey ? compareProfileOrder(window.lastOrderKey, ref.orderKey) : -1);
|
|
156
|
+
const window = this.windows[block];
|
|
157
|
+
if (!window)
|
|
158
|
+
throw Error("PROFILE_ANCHOR_UNAVAILABLE");
|
|
159
|
+
const index = lowerBound(window.refs, row => compareProfileOrder(row.orderKey, ref.orderKey));
|
|
160
|
+
position = this.offsets[block] + index;
|
|
161
|
+
}
|
|
162
|
+
const start = direction === "older" ? Math.max(0, position - limit) : position + 1;
|
|
163
|
+
const end = direction === "older" ? position : Math.min(this.size, start + limit);
|
|
164
|
+
if (start >= end)
|
|
165
|
+
return [];
|
|
166
|
+
let block = Math.max(0, lowerBound(this.offsets, offset => offset - (start + 1)) - 1);
|
|
167
|
+
const result = [];
|
|
168
|
+
while (block < this.windows.length && this.offsets[block] < end) {
|
|
169
|
+
const offset = this.offsets[block], refs = this.windows[block].refs;
|
|
170
|
+
result.push(...refs.slice(Math.max(0, start - offset), Math.min(refs.length, end - offset)));
|
|
171
|
+
block++;
|
|
172
|
+
}
|
|
173
|
+
return result;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { ProfileDatabase } from "./profileDatabase.js";
|
|
2
|
+
import { ProfileReplica, type ProfileMessagePort } from "./profileReplica.js";
|
|
3
|
+
import type { ProfileIdentity } from "./profileStoreTypes.js";
|
|
4
|
+
export type ProfileWorkerConfiguration = {
|
|
5
|
+
identity: ProfileIdentity;
|
|
6
|
+
epoch?: string;
|
|
7
|
+
windowStart: number;
|
|
8
|
+
network?: {
|
|
9
|
+
hubHttpUrl: string;
|
|
10
|
+
token: string;
|
|
11
|
+
uploadEndpoint?: string;
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
export declare const profileChannelName: (identity: ProfileIdentity) => string;
|
|
15
|
+
/** Installs the SDK host in a Dedicated Worker, not on the rendering thread.
|
|
16
|
+
* Identity cannot change within a host; a Profile switch creates another host. */
|
|
17
|
+
export declare function installProfileWorker(port: ProfileMessagePort, options?: {
|
|
18
|
+
database?: (identity: ProfileIdentity) => ProfileDatabase;
|
|
19
|
+
channel?: (name: string) => ProfileMessagePort & {
|
|
20
|
+
close(): void;
|
|
21
|
+
};
|
|
22
|
+
fetch?: typeof fetch;
|
|
23
|
+
}): () => Promise<void>;
|
|
24
|
+
/** The application passes its bundler-created Worker. Worker code and main-page
|
|
25
|
+
* code ship together, so protocol negotiation precedes any snapshot handoff. */
|
|
26
|
+
export declare function openProfileWorker(worker: ProfileMessagePort, configuration: ProfileWorkerConfiguration, timeoutMs?: number): Promise<{
|
|
27
|
+
close: () => Promise<void>;
|
|
28
|
+
update: (config: ProfileWorkerConfiguration) => Promise<void>;
|
|
29
|
+
}>;
|
|
30
|
+
export declare function connectProfileWorker(worker: ProfileMessagePort, configuration: ProfileWorkerConfiguration, timeoutMs?: number): Promise<{
|
|
31
|
+
replica: ProfileReplica;
|
|
32
|
+
close: () => Promise<void>;
|
|
33
|
+
}>;
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { ProfileDatabase } from "./profileDatabase.js";
|
|
2
|
+
import { ProfileOwner } from "./profileOwner.js";
|
|
3
|
+
import { ProfileReplica, serveProfileOwner } from "./profileReplica.js";
|
|
4
|
+
import { PROFILE_PROTOCOL_VERSION } from "./profileMemoryStore.js";
|
|
5
|
+
import { ProfileInboxFeed } from "./profileInboxFeed.js";
|
|
6
|
+
import { ProfileOutboxDispatcher } from "./profileOutboxDispatcher.js";
|
|
7
|
+
import { ChatClient } from "./chatClient.js";
|
|
8
|
+
export const profileChannelName = (identity) => `pmate-profile:${JSON.stringify([identity.accountId, identity.appId, identity.profileId])}`;
|
|
9
|
+
/** Installs the SDK host in a Dedicated Worker, not on the rendering thread.
|
|
10
|
+
* Identity cannot change within a host; a Profile switch creates another host. */
|
|
11
|
+
export function installProfileWorker(port, options = {}) {
|
|
12
|
+
let owner, database;
|
|
13
|
+
let channel;
|
|
14
|
+
let scope, opening;
|
|
15
|
+
let disposers = [];
|
|
16
|
+
let feed, dispatcher, connection;
|
|
17
|
+
let network;
|
|
18
|
+
const stopNetwork = async () => { await dispatcher?.close(); await feed?.close(); connection?.close(); dispatcher = undefined; feed = undefined; connection = undefined; };
|
|
19
|
+
const reply = (requestId, result, error) => port.postMessage({ profileControl: "result", requestId, result, error, protocolVersion: PROFILE_PROTOCOL_VERSION });
|
|
20
|
+
const listener = (event) => {
|
|
21
|
+
const request = event.data;
|
|
22
|
+
if (!request || !["open", "close"].includes(request.profileControl) || !request.requestId)
|
|
23
|
+
return;
|
|
24
|
+
void (async () => {
|
|
25
|
+
if (request.protocolVersion !== PROFILE_PROTOCOL_VERSION)
|
|
26
|
+
throw Error("PROFILE_PROTOCOL_INCOMPATIBLE");
|
|
27
|
+
if (request.profileControl === "close") {
|
|
28
|
+
await opening;
|
|
29
|
+
await stopNetwork();
|
|
30
|
+
await owner?.close();
|
|
31
|
+
for (const off of disposers.splice(0))
|
|
32
|
+
off();
|
|
33
|
+
owner = undefined;
|
|
34
|
+
opening = undefined;
|
|
35
|
+
channel?.close();
|
|
36
|
+
channel = undefined;
|
|
37
|
+
database?.close();
|
|
38
|
+
database = undefined;
|
|
39
|
+
reply(request.requestId, null);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
const config = request.configuration;
|
|
43
|
+
if (!config || ![config.identity?.accountId, config.identity?.appId, config.identity?.profileId].every(value => typeof value === "string" && value.length > 0) || !Number.isSafeInteger(config.windowStart))
|
|
44
|
+
throw Error("INVALID_PROFILE_WORKER_CONFIGURATION");
|
|
45
|
+
const nextScope = profileChannelName(config.identity);
|
|
46
|
+
if (scope && scope !== nextScope)
|
|
47
|
+
throw Error("PROFILE_WORKER_IDENTITY_MISMATCH");
|
|
48
|
+
scope = nextScope;
|
|
49
|
+
if (network && config.network && network.hubHttpUrl !== config.network.hubHttpUrl)
|
|
50
|
+
throw Error("PROFILE_WORKER_ENDPOINT_MISMATCH");
|
|
51
|
+
if (config.network)
|
|
52
|
+
network = config.network;
|
|
53
|
+
if (!opening) {
|
|
54
|
+
opening = (async () => {
|
|
55
|
+
database = options.database?.(config.identity) ?? new ProfileDatabase(config.identity);
|
|
56
|
+
const stored = await database.inboxState.get("current");
|
|
57
|
+
owner = await ProfileOwner.open({ database, epoch: stored?.epoch ?? config.epoch ?? "uninitialized", windowStart: config.windowStart,
|
|
58
|
+
onError: error => port.postMessage({ profileControl: "error", error: error instanceof Error ? error.message : "PROFILE_OWNER_FAILED" }) });
|
|
59
|
+
channel = options.channel?.(nextScope) ?? new BroadcastChannel(nextScope);
|
|
60
|
+
disposers.push(serveProfileOwner(owner, port, nextScope), serveProfileOwner(owner, channel, nextScope));
|
|
61
|
+
if (network) {
|
|
62
|
+
const report = (error) => port.postMessage({ profileControl: "error", error: error instanceof Error ? error.message : "PROFILE_NETWORK_FAILED" });
|
|
63
|
+
const endpoint = network.hubHttpUrl.replace(/\/$/, "").replace(/^ws/, "http");
|
|
64
|
+
const token = () => network.token;
|
|
65
|
+
connection = new ChatClient({ appId: config.identity.appId, profileId: config.identity.profileId,
|
|
66
|
+
hubUrl: endpoint.replace(/^http/, "ws"), fetchImpl: options.fetch, getToken: async () => token(),
|
|
67
|
+
session: { token: token(), identity: { accountId: config.identity.accountId, app: config.identity.appId, profileId: config.identity.profileId } } });
|
|
68
|
+
feed = new ProfileInboxFeed({ owner, hubHttpUrl: endpoint, getToken: token, fetch: options.fetch, onError: report,
|
|
69
|
+
onProgress: progress => { const message = { profileControl: "progress", progress }; port.postMessage(message); channel?.postMessage(message); } });
|
|
70
|
+
owner.setSynchronizer(() => feed.synchronize());
|
|
71
|
+
dispatcher = new ProfileOutboxDispatcher({ owner, hubHttpUrl: endpoint, getToken: token, fetch: options.fetch,
|
|
72
|
+
uploadEndpoint: network.uploadEndpoint,
|
|
73
|
+
sendMessage: async (message) => { await connection.connect(); return connection.sendPrepared(message); }, onError: report });
|
|
74
|
+
dispatcher.start();
|
|
75
|
+
void feed.start().catch(report);
|
|
76
|
+
}
|
|
77
|
+
})().catch(async (error) => {
|
|
78
|
+
await stopNetwork();
|
|
79
|
+
for (const off of disposers.splice(0))
|
|
80
|
+
off();
|
|
81
|
+
channel?.close();
|
|
82
|
+
channel = undefined;
|
|
83
|
+
await owner?.close();
|
|
84
|
+
owner = undefined;
|
|
85
|
+
database?.close();
|
|
86
|
+
database = undefined;
|
|
87
|
+
opening = undefined;
|
|
88
|
+
throw error;
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
await opening;
|
|
92
|
+
reply(request.requestId, { scope: nextScope, revision: owner.current.commitRevision });
|
|
93
|
+
})().catch(error => reply(request.requestId, undefined, error instanceof Error ? error.message : "PROFILE_WORKER_FAILED"));
|
|
94
|
+
};
|
|
95
|
+
port.addEventListener("message", listener);
|
|
96
|
+
port.start?.();
|
|
97
|
+
return async () => {
|
|
98
|
+
await opening;
|
|
99
|
+
await stopNetwork();
|
|
100
|
+
await owner?.close();
|
|
101
|
+
port.removeEventListener("message", listener);
|
|
102
|
+
for (const off of disposers.splice(0))
|
|
103
|
+
off();
|
|
104
|
+
channel?.close();
|
|
105
|
+
database?.close();
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
/** The application passes its bundler-created Worker. Worker code and main-page
|
|
109
|
+
* code ship together, so protocol negotiation precedes any snapshot handoff. */
|
|
110
|
+
export async function openProfileWorker(worker, configuration, timeoutMs = 30000) {
|
|
111
|
+
async function control(action, config = configuration) {
|
|
112
|
+
const requestId = crypto.randomUUID();
|
|
113
|
+
return new Promise((resolve, reject) => {
|
|
114
|
+
const timer = setTimeout(() => { worker.removeEventListener("message", receive); reject(Error("PROFILE_WORKER_TIMEOUT")); }, timeoutMs);
|
|
115
|
+
const receive = (event) => {
|
|
116
|
+
const response = event.data;
|
|
117
|
+
if (response?.profileControl !== "result" || response.requestId !== requestId)
|
|
118
|
+
return;
|
|
119
|
+
clearTimeout(timer);
|
|
120
|
+
worker.removeEventListener("message", receive);
|
|
121
|
+
if (response.protocolVersion !== PROFILE_PROTOCOL_VERSION)
|
|
122
|
+
reject(Error("PROFILE_PROTOCOL_INCOMPATIBLE"));
|
|
123
|
+
else if (response.error)
|
|
124
|
+
reject(Error(response.error));
|
|
125
|
+
else
|
|
126
|
+
resolve();
|
|
127
|
+
};
|
|
128
|
+
worker.addEventListener("message", receive);
|
|
129
|
+
worker.start?.();
|
|
130
|
+
try {
|
|
131
|
+
worker.postMessage({ profileControl: action, requestId, configuration: config, protocolVersion: PROFILE_PROTOCOL_VERSION });
|
|
132
|
+
}
|
|
133
|
+
catch (error) {
|
|
134
|
+
clearTimeout(timer);
|
|
135
|
+
worker.removeEventListener("message", receive);
|
|
136
|
+
reject(error);
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
await control("open");
|
|
141
|
+
return { close: () => control("close"), update: (config) => control("open", config) };
|
|
142
|
+
}
|
|
143
|
+
export async function connectProfileWorker(worker, configuration, timeoutMs = 30000) {
|
|
144
|
+
const scope = profileChannelName(configuration.identity);
|
|
145
|
+
const host = await openProfileWorker(worker, configuration, timeoutMs);
|
|
146
|
+
const replica = new ProfileReplica(worker, scope, undefined, timeoutMs, configuration.identity.profileId);
|
|
147
|
+
try {
|
|
148
|
+
await replica.start();
|
|
149
|
+
}
|
|
150
|
+
catch (error) {
|
|
151
|
+
replica.close();
|
|
152
|
+
await host.close();
|
|
153
|
+
throw error;
|
|
154
|
+
}
|
|
155
|
+
return { replica, close: async () => { replica.close(); await host.close(); } };
|
|
156
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pmate/chat-sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0-profile.1",
|
|
4
4
|
"description": "Local-first chat SDK, message subscriptions, reading state, and shared chat UI primitives",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./lib/index.js",
|
|
@@ -14,6 +14,16 @@
|
|
|
14
14
|
"lib"
|
|
15
15
|
],
|
|
16
16
|
"exports": {
|
|
17
|
+
"./profile": {
|
|
18
|
+
"types": "./lib/profile.d.ts",
|
|
19
|
+
"import": "./lib/profile.js",
|
|
20
|
+
"default": "./lib/profile.js"
|
|
21
|
+
},
|
|
22
|
+
"./profile-worker": {
|
|
23
|
+
"types": "./lib/profileWorker.d.ts",
|
|
24
|
+
"import": "./lib/profileWorker.js",
|
|
25
|
+
"default": "./lib/profileWorker.js"
|
|
26
|
+
},
|
|
17
27
|
".": {
|
|
18
28
|
"types": "./lib/index.d.ts",
|
|
19
29
|
"import": "./lib/index.js",
|