V-Sekai-fire / godot-jsondiffpatch

Diff & patch JavaScript objects
MIT License
0 stars 0 forks source link

Convert to gdscript. #1

Open fire opened 1 month ago

fire commented 1 month ago

Differential Synchronization Explained

Differential Synchronization (DS) is a method for synchronizing data across multiple devices or services in real-time. It is particularly useful in applications where multiple users interact with the same data simultaneously, such as collaborative editing tools.

Core Components of Differential Synchronization:

  1. Shadow Copy: Each client maintains a shadow copy of the document which represents the last known synchronized state with the server.
  2. Backup Shadow Copy: A backup of the shadow copy is also maintained to handle data loss during synchronization failures.
  3. Diff Algorithm: A mechanism to compute the differences between two versions of a document.
  4. Patch Mechanism: Applies changes from one version of the document to another using the diffs.

Workflow:

  1. Local Edits: Changes are made by the client to their local version of the document.
  2. Diff Calculation: The differences between the local document and the shadow copy are calculated.
  3. Send Diff to Server: The diff is sent from the client to the server.
  4. Server Reconciliation: The server applies the received diff to its version of the document, reconciles changes, and updates its shadow copy.
  5. Server Diff Calculation: The server calculates the diff between its updated document and the shadow copy for each client.
  6. Send Diff to Clients: The server sends the calculated diff back to the clients.
  7. Client Patching: Each client applies the diff to both their local document and their shadow copy.
fire commented 1 month ago
import jsondiffpatch from 'jsondiffpatch';

// Initialize the diff/patch engine
const diffPatch = jsondiffpatch.create();

// Simulate client and server documents as JSON objects
let clientDoc = {
    title: "Original Title",
    content: "Initial content",
    comments: [{ id: 1, text: "First comment" }]
};

let serverDoc = {
    title: "Original Title",
    content: "Initial content",
    comments: [{ id: 1, text: "First comment" }]
};

// Shadow copies on client and server
let clientShadow = JSON.parse(JSON.stringify(clientDoc));
let serverShadow = JSON.parse(JSON.stringify(serverDoc));

// Function to simulate client making an edit
function clientMakesEdit(newDoc) {
    clientDoc = newDoc;
    const diff = diffPatch.diff(clientShadow, clientDoc);
    clientShadow = JSON.parse(JSON.stringify(clientDoc)); // Update client shadow to latest doc
    return diff;
}

// Function to apply changes on the server
function applyChangesOnServer(diff) {
    serverDoc = diffPatch.patch(serverDoc, diff);
    serverShadow = JSON.parse(JSON.stringify(serverDoc)); // Update server shadow
    return diffPatch.diff(serverShadow, serverDoc); // Diff for client update
}

// Client edits the document
const clientDiff = clientMakesEdit({
    title: "Updated Title",
    content: "Revised content",
    comments: [{ id: 1, text: "Edited first comment" }, { id: 2, text: "Second comment" }]
});

// Server applies the changes and prepares the update for the client
const serverDiff = applyChangesOnServer(clientDiff);

// Apply server changes to client
clientDoc = diffPatch.patch(clientDoc, serverDiff);

console.log("Final Client Doc:", clientDoc);
console.log("Final Server Doc:", serverDoc);
fire commented 1 month ago

https://neil.fraser.name/writing/sync/

35605.pdf

https://research.google/pubs/differential-synchronization/

fire commented 1 month ago

Originally we start with a list of properties and we send the index of the properties to overwrite.

In the new design we start with a json array of properties and we generate a json diff patch.