Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: splice should shift remaining elements #181

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@colyseus/schema",
"version": "2.0.35",
"version": "2.0.36",
"description": "Binary state serializer with delta encoding for games",
"bin": {
"schema-codegen": "./bin/schema-codegen"
Expand Down
10 changes: 10 additions & 0 deletions src/types/ArraySchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,15 @@ export class ArraySchema<V = any> implements Array<V>, SchemaDecoderCallbacks {
this.$deleteAt(indexes[i]);
}

const shiftAmount = items.length - deleteCount;
if (shiftAmount > 0) {
for (let i = indexes.length - 1; i >= start + deleteCount; i--) {
const currentIndex = indexes[i];
const newIndex = currentIndex + shiftAmount;
this.setAt(newIndex, this.$items.get(currentIndex));
}
}

for (let i = 0; i < items.length; i++) {
this.setAt(start + i, items[i]);
}
Expand Down Expand Up @@ -395,6 +404,7 @@ export class ArraySchema<V = any> implements Array<V>, SchemaDecoderCallbacks {
* @param thisArg An object to which the this keyword can refer in the callbackfn function.
* If thisArg is omitted, undefined is used as the this value.
*/
// @ts-ignore
every(callbackfn: (value: V, index: number, array: V[]) => unknown, thisArg?: any): boolean {
return Array.from(this.$items.values()).every(callbackfn, thisArg);
}
Expand Down
18 changes: 18 additions & 0 deletions test/ArraySchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,24 @@ describe("ArraySchema Tests", () => {
assert.strictEqual(decodedState.arrayOfPlayers[1].name, "Katarina Lyons");
})

it("should correctly shift elements when inserting more items than removed", () => {
const state = new State();
const p1 = new Player("Jake");
const p2 = new Player("Snake");
const p3 = new Player("Cyberhawk");

state.arrayOfPlayers = new ArraySchema(p1, p2);
state.arrayOfPlayers.splice(1, 0, p3);

const decodedState = new State();
decodedState.decode(state.encode());

assert.strictEqual(decodedState.arrayOfPlayers.length, 3);
assert.strictEqual(decodedState.arrayOfPlayers[0].name, "Jake");
assert.strictEqual(decodedState.arrayOfPlayers[1].name, "Cyberhawk");
assert.strictEqual(decodedState.arrayOfPlayers[2].name, "Snake");
});

it("should adjust the indexes of the elements after a splice", () => {
const state = new State();
const p1 = new Player("Jake")
Expand Down