Skip to content

SyncNodeData and SyncLinkData methods of DataSyncService don't delete elements correctly #63

@Smorgaz

Description

@Smorgaz

In the SyncNodeData method of the DataSyncService the following code is not correct:

// account for removed node data
if (changes.removedNodeKeys) {
    const removals = changes.removedNodeKeys.map(key => keyIdxMap.get(key)).sort();
    for (let i = removals.length - 1; i >= 0; i--) {
        draft.splice(removals[i], 1);
    }
}

Specifically the sort() function of javascript is sorting the array of indices as strings. This is the default behavior if no sort function is provided. If you try deleting multiple nodes with indices of different orders of magnitude (for example [10, 11, 7]) a semi-random node will be deleted instead. The correct version of the code would look like this:

// account for removed node data
if (changes.removedNodeKeys) {
    const removals = (changes.removedNodeKeys.map((key) => keyIdxMap.get(key)) as number[]).sort(
        (a, b) => a - b
    );
    for (let i = removals.length - 1; i >= 0; i--) {
        draft.splice(removals[i], 1);
    }
}

The SyncLinkData method behaves analogously.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions