|
1 | | -import { Flock } from "@loro-dev/flock"; |
| 1 | +import { Flock, decodeVersionVector, encodeVersionVector } from "@loro-dev/flock"; |
2 | 2 | import type { |
3 | 3 | ExportBundle as FlockExportBundle, |
4 | 4 | VersionVector as FlockVersion, |
@@ -31,45 +31,6 @@ function deserializeBundle(bytes: Uint8Array): FlockExportBundle { |
31 | 31 | return { version: 0, entries: {} }; |
32 | 32 | } |
33 | 33 |
|
34 | | -function serializeVersion(version: FlockVersion | undefined): Uint8Array { |
35 | | - return encoder.encode(JSON.stringify(version ?? {})); |
36 | | -} |
37 | | - |
38 | | -function deserializeVersion(bytes: Uint8Array): FlockVersion { |
39 | | - if (!bytes.length) { |
40 | | - return {}; |
41 | | - } |
42 | | - try { |
43 | | - const parsed = JSON.parse(decoder.decode(bytes)); |
44 | | - if (!parsed || typeof parsed !== "object") { |
45 | | - return {}; |
46 | | - } |
47 | | - const next: FlockVersion = {}; |
48 | | - for (const [peer, value] of Object.entries( |
49 | | - parsed as Record<string, unknown>, |
50 | | - )) { |
51 | | - if (!value || typeof value !== "object") { |
52 | | - continue; |
53 | | - } |
54 | | - const entry = value as { logicalCounter?: unknown; physicalTime?: unknown }; |
55 | | - const logicalCounter = |
56 | | - typeof entry.logicalCounter === "number" && |
57 | | - Number.isFinite(entry.logicalCounter) |
58 | | - ? Math.trunc(entry.logicalCounter) |
59 | | - : 0; |
60 | | - const physicalTime = |
61 | | - typeof entry.physicalTime === "number" && |
62 | | - Number.isFinite(entry.physicalTime) |
63 | | - ? entry.physicalTime |
64 | | - : 0; |
65 | | - next[peer] = { logicalCounter, physicalTime }; |
66 | | - } |
67 | | - return next; |
68 | | - } catch { |
69 | | - return {}; |
70 | | - } |
71 | | -} |
72 | | - |
73 | 34 | function importSnapshot(flock: Flock, data: Uint8Array): void { |
74 | 35 | if (!data.length) return; |
75 | 36 | const bundle = deserializeBundle(data); |
@@ -112,11 +73,11 @@ export class FlockServerAdaptor implements CrdtServerAdaptor { |
112 | 73 | const flock = new Flock(); |
113 | 74 | importSnapshot(flock, documentData); |
114 | 75 |
|
115 | | - const serverVersion = serializeVersion(flock.version()); |
| 76 | + const serverVersion = encodeVersionVector(flock.version()); |
116 | 77 | let updates: Uint8Array[] | undefined; |
117 | 78 |
|
118 | 79 | if (clientVersion.length > 0) { |
119 | | - const clientVV = deserializeVersion(clientVersion); |
| 80 | + const clientVV = decodeVersionVector(clientVersion); |
120 | 81 | const delta = exportBundle(flock, clientVV); |
121 | 82 | updates = [serializeBundle(delta)]; |
122 | 83 | } else if (documentData.length > 0) { |
@@ -167,7 +128,7 @@ export class FlockServerAdaptor implements CrdtServerAdaptor { |
167 | 128 | getVersion(documentData: Uint8Array): Uint8Array { |
168 | 129 | const flock = new Flock(); |
169 | 130 | importSnapshot(flock, documentData); |
170 | | - return serializeVersion(flock.version()); |
| 131 | + return encodeVersionVector(flock.version()); |
171 | 132 | } |
172 | 133 |
|
173 | 134 | merge(documents: Uint8Array[]): Uint8Array { |
|
0 commit comments