Repository navigation
Expand file tree
/
Copy pathoffline-replica.ts
More file actions
80 lines (80 loc) · 2.66 KB
/
Copy pathoffline-replica.ts
File metadata and controls
80 lines (80 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { mergeUpdates } from "@smartdoca/slate/yjs";
import { openReplicaDatabase, replicaKey } from "./replica-database.js";
import { notifyOfflineDocumentsChanged } from "./offline-document-catalog.js";
export type PendingUpdate = { id: string; update: Uint8Array };
export type Replica = {
checkpoint: Uint8Array;
pending: PendingUpdate[];
epochId?: string;
baseline?: unknown;
};
/** User + deployment(origin-owned IndexedDB) + resource + codec partition. Never stores credentials.
* A single read-write transaction merges the checkpoint and outbox, including across browser tabs.
* Only an exact durable server ACK removes a pending operation. */
export async function openReplica(
userId: string,
resourceId: string,
codec = "slatetsx-yjs-v1",
) {
const key = replicaKey(userId, resourceId, codec);
const db = await openReplicaDatabase();
function transaction(change?: (value: Replica) => Replica) {
return new Promise<Replica>((resolve, reject) => {
const tx = db.transaction("replicas", change ? "readwrite" : "readonly"),
store = tx.objectStore("replicas");
const request = store.get(key);
let value: Replica;
let created = false;
request.onsuccess = () => {
try {
value = request.result ?? {
checkpoint: new Uint8Array([0, 0]),
pending: [],
};
if (change) {
value = change(value);
store.put(value, key);
created = request.result === undefined && !!value.epochId;
}
} catch {
tx.abort();
}
};
tx.oncomplete = () => {
resolve(value!);
if (created) notifyOfflineDocumentsChanged();
};
tx.onabort = tx.onerror = () =>
reject(tx.error ?? new Error("本地保存失败"));
});
}
return {
load: () => transaction(),
store: (
update: Uint8Array,
pending?: PendingUpdate,
epochId?: string,
baseline?: unknown,
) =>
transaction((value) => {
if (epochId && value.epochId && epochId !== value.epochId)
throw new Error("本地文档版本不匹配");
return {
...value,
...(epochId ? { epochId } : {}),
...(baseline ? { baseline } : {}),
checkpoint: mergeUpdates([value.checkpoint, update]),
pending:
pending && !value.pending.some((p) => p.id === pending.id)
? [...value.pending, pending]
: value.pending,
};
}),
acknowledge: (id: string) =>
transaction((value) => ({
...value,
pending: value.pending.filter((p) => p.id !== id),
})),
close: () => db.close(),
};
}