question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ArraySchema incorrect removed index (onRemove on client receives incorrect index)

See original GitHub issue

When removes an item from a ArraySchema that’s not the last one (for example: array.shift(), that removes first one), The onRemove(instance, key) defined in the Client receives as key and instance always the last one element of the array.

Example

We have a Schema called World that contains an array of Points (schema too) called test, and an action that add elements when data is “a: 1”, or removes the first one element if data is “a: 2”

export class World extends Schema {  
  @type([Point])
  test = new ArraySchema<Point>();

  testAc(data) {
    if (data.a == 1) {
      this.test.push(new Point(0,1));
      this.test.push(new Point(0,2));
      this.test.push(new Point(0,3));
    } else (data.a == 2) {
      this.test.shift();
    }
  }
}

Then on Client:

room.send('testAc', {a: 1});
// result in the onChange of test in World
// [0] = Point(0,1)
// [1] = Point(0,2)
// [2] = Point(0,3)
room.send('testAc', {a: 2});
// result in the onChange of test in World
// [0] = Point(0,2)
// [1] = Point(0,3)
room.state.world.onRemove = (instance, key) => {
  console.log(instance, key);
 //result spected: Point(0,1), key: 0
 //BAD RESULT THAT WE RECEIVED INSTEAD: Point(0,3), key: 2
}

I’m using the Client in Angular 10 imported in this way: import * as BABYLON from '@babylonjs/core/Legacy/legacy';

Thanks, Cheers 😃

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
Steditorcommented, Nov 10, 2020

I think I now understand, where the problem came from: I assumed that the calls to onRemove were atomic in the sense, that if you repeatedly remove the first element of an array using splice(0, 1), you’ll always get the event “element at index 0 removed”. From the test suite I now learned, that this is not the case, but that the passed index seems to be an internal one and not the actual index in the current array. (I’m not sure if that’s expected behavior) In the error I described earlier, this internal index was higher than the number of elements still in the array.

Something else is fishy with the indices when removing and adding elements. Maybe it’s me using the framework in a wrong way, but: As soon as you mix unshift and splice, the array is all mixed up. See https://github.com/Steditor/schema/blob/master/test/ArraySchemaTest.ts#L516-L555 for a test case.

0reactions
endelcommented, Oct 23, 2020

Hi @Steditor, thanks for reporting, could you please provide more info about which operations you’ve done, and how is your structure represented so I can reproduce over here?

If you can provide a test scenario like this would be ideal: https://github.com/colyseus/schema/blob/master/test/ArraySchemaTest.ts#L9-L48

Cheers!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Schema - Colyseus & Arena Cloud Documentation
Changes the contents of an array by removing or replacing existing elements and/or adding new elements in place. // find the index of...
Read more >
Splice removing the wrong index - javascript - Stack Overflow
The function itself and splice is working just fine. Your issue is with the react state update. There are several issues with your...
Read more >
Falcon Sandbox v3.20 © Hybrid Analysis
Hybrid Analysis Report False-Positive. Tip: Click an analysed process below to view more details. Analysed 2 processes in total (System Resource Monitor).
Read more >
index error with latest version - Google Groups
Actually I did not define any index with that name in that table, I have just one primary key. Older doctrine version had...
Read more >
https://lab.las3.de/gitlab/eye-tracking-classroom/...
diff --git b/.eslintrc a/.eslintrc new file mode 100755 index 0000000..45054ee ... -0,0 +1,37 @@ +class Logger { + + constructor() { + this.enabled...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found