|
|
|
@ -75,31 +75,31 @@ fastify.register(async function (fastify: Fastify) {
|
|
|
|
|
const requestId = data._request;
|
|
|
|
|
|
|
|
|
|
if (data.type === "pullUpdates") {
|
|
|
|
|
send(socket, requestId, room.updates.slice(data.version));
|
|
|
|
|
send(socket, requestId, room.updates.slice(data.version));
|
|
|
|
|
} else if (data.type === "pushUpdates") {
|
|
|
|
|
let received = data.updates.map((json: any) => ({
|
|
|
|
|
clientID: json.clientID,
|
|
|
|
|
changes: ChangeSet.fromJSON(json.changes),
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
if (data.version != room.updates.length) {
|
|
|
|
|
received = rebaseUpdates(received, room.updates.slice(data.version));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (let update of received) {
|
|
|
|
|
room.updates.push(update);
|
|
|
|
|
room.doc = update.changes.apply(room.doc);
|
|
|
|
|
}
|
|
|
|
|
send(
|
|
|
|
|
socket,
|
|
|
|
|
requestId,
|
|
|
|
|
received.map((update: any) => ({
|
|
|
|
|
clientID: update.clientID,
|
|
|
|
|
changes: update.changes.toJSON(),
|
|
|
|
|
})),
|
|
|
|
|
);
|
|
|
|
|
} else if (data.type == "getDocument") {
|
|
|
|
|
send(socket, requestId, { version: room.updates.length, doc: room.doc.toString() });
|
|
|
|
|
let received = data.updates.map((json: any) => ({
|
|
|
|
|
clientID: json.clientID,
|
|
|
|
|
changes: ChangeSet.fromJSON(json.changes),
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
if (data.version != room.updates.length) {
|
|
|
|
|
received = rebaseUpdates(received, room.updates.slice(data.version));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (let update of received) {
|
|
|
|
|
room.updates.push(update);
|
|
|
|
|
room.doc = update.changes.apply(room.doc);
|
|
|
|
|
}
|
|
|
|
|
send(
|
|
|
|
|
socket,
|
|
|
|
|
requestId,
|
|
|
|
|
received.map((update: any) => ({
|
|
|
|
|
clientID: update.clientID,
|
|
|
|
|
changes: update.changes.toJSON(),
|
|
|
|
|
})),
|
|
|
|
|
);
|
|
|
|
|
} else if (data.type === "getDocument") {
|
|
|
|
|
send(socket, requestId, { version: room.updates.length, doc: room.doc.toString() });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|