aboutsummaryrefslogtreecommitdiff
path: root/src/server/websocket.ts
diff options
context:
space:
mode:
authorJames Hu <51237606+jameshu111@users.noreply.github.com>2023-05-06 16:01:38 -0400
committerJames Hu <51237606+jameshu111@users.noreply.github.com>2023-05-06 16:01:38 -0400
commit488a3af4ffd9cac870a33a1ae358cf90fd957117 (patch)
tree9206474b9bdd9611741e4c44c1d6f4cc5a9d8af8 /src/server/websocket.ts
parent0c25989104bef9785bf112490c73541bd4bc1764 (diff)
clean up and comment remFromList
Diffstat (limited to 'src/server/websocket.ts')
-rw-r--r--src/server/websocket.ts49
1 files changed, 31 insertions, 18 deletions
diff --git a/src/server/websocket.ts b/src/server/websocket.ts
index 74b5dacaa..e94b48f4f 100644
--- a/src/server/websocket.ts
+++ b/src/server/websocket.ts
@@ -317,6 +317,18 @@ export namespace WebSocket {
);
}
+ /**
+ * findClosestIndex() is a helper function that will try to find
+ * the closest index of a list that has the same value as
+ * a specified argument/index pair.
+ * @param list the list to search through
+ * @param indexesToDelete a list of indexes that are already marked for deletion
+ * so they will be ignored
+ * @param value the value of the item to remove
+ * @param hintIndex the index that the element was at on the client's copy of
+ * the data
+ * @returns the closest index with the same value or -1 if the element was not found.
+ */
function findClosestIndex(list: any, indexesToDelete: number[], value: any, hintIndex : number) {
let closestIndex = -1;
for(let i = 0; i < list.length; i++) {
@@ -329,28 +341,31 @@ export namespace WebSocket {
return closestIndex;
}
+ /**
+ * remFromListField() receives the items to remove and a hint
+ * from the client, and attempts to make the modification to the
+ * server's copy of the data. If server's copy does not match
+ * the client's after removal, the server will SEND BACk
+ * its version to the client.
+ * @param socket the socket that the client is connected on
+ * @param diff an object containing the items to remove and a hint
+ * (the hint contains start index and deleteCount, the number of
+ * items to delete)
+ * @param curListItems the server's current copy of the data
+ */
function remFromListField(socket: Socket, diff: Diff, curListItems?: Transferable): void {
- // console.log("##### %O", diff);
diff.diff.$set = diff.diff.$remFromSet;
delete diff.diff.$remFromSet;
- // console.log(JSON.stringify(diff));
const updatefield = Array.from(Object.keys(diff.diff.$set))[0];
- // console.log("websocket/remfromlist: UPDATE FIELD: " + updatefield);
const remListItems = diff.diff.$set[updatefield].fields;
const curList = (curListItems as any)?.fields?.[updatefield.replace('fields.', '')]?.fields.filter((f: any) => f !== null) || [];
const hint = diff.diff.$set.hint;
- // console.log(remListItems);
- // console.log(curList);
if(hint) {
- // console.log("websocket/remfromlist: %O", hint);
- // console.log(findClosestIndex([5, 5, 3], [], 5, 0));
- // console.log(findClosestIndex([5, 5, 3], [0], 5, 1));
+ // indexesToRemove stores the indexes that we mark for deletion, which is later used to filter the list (delete the elements)
let indexesToRemove: number[] = [];
for(let i = 0; i < hint.deleteCount; i++) {
- // console.log("VALUE: %d", remListItems[i]);
- // console.log("HINT INDEX: %d", i + hint.start);
if(curList[i + hint.start] == remListItems[i]) {
indexesToRemove.push(i + hint.start);
continue;
@@ -364,31 +379,29 @@ export namespace WebSocket {
}
}
- // console.log("INDEXES TO REMOVE: ", indexesToRemove);
diff.diff.$set[updatefield].fields = curList?.filter(
(curItem: any, index : number) => !(indexesToRemove.includes(index))
);
- // console.log("websocket/remfromlist: newdiff: %O", diff.diff.$set[updatefield].fields);
} else {
+ // go back to the original way to delete if we didn't receive
+ // a hint from the client
diff.diff.$set[updatefield].fields = curList?.filter(
(curItem: any) => !remListItems.some((remItem: any) => (remItem.fieldId ? remItem.fieldId === curItem.fieldId :
remItem.heading ? remItem.heading === curItem.heading : remItem === curItem))
);
- // console.log("websocket/remfromlist: newdiff: %O", diff.diff.$set[updatefield].fields);
}
-
+ // if the client and server have different versions of the data after
+ // deletion, they will have different lengths and the server will
+ // send its version of the data to the client
const sendBack = diff.diff.length !== diff.diff.$set[updatefield].fields.length;
- // console.log("websocket/remfromlist: DIFFY " + JSON.stringify(diff.diff.$set));
- // console.log("websocket/remfromlist: DIFF LENGTH " + diff.diff.length);
- // console.log("websocket/remfromlist: DIFF $SET " + diff.diff.$set[updatefield].fields);
delete diff.diff.length;
Database.Instance.update(
diff.id,
diff.diff,
() => {
- // console.log("websocket/remfromlist: lambda: %s", JSON.stringify(diff))
if (sendBack) {
+ // the two copies are different, so the server sends its copy.
console.log('SEND BACK');
const id = socket.id;
socket.id = '';