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

onAdd callback gets called multiple times #147

Open
tufcode opened this issue May 8, 2023 · 3 comments
Open

onAdd callback gets called multiple times #147

tufcode opened this issue May 8, 2023 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@tufcode
Copy link

tufcode commented May 8, 2023

SetSchema's onAdd callback gets called twice when it is inside a MapSchema.

export class MyRoomState extends Schema {
  @type({ map: Restaurant }) restaurants = new MapSchema<Restaurant>();
}
export class Restaurant extends Schema {
  @type({ set: Something }) objects: SetSchema<Something> = new SetSchema<Something>();
}
room.state.restaurants.onAdd((restaurant, restaurantKey) => {
   console.log("Restaurant added");
   restaurant.objects.onAdd(async (gameObject, objectKey) => {
      console.log("Oh hi there!")
   })
})

Now, if we look at browser console

Expectation

Restaurant added
Oh hi there!

Reality

Restaurant added
Oh hi there!
Oh hi there!

@tufcode
Copy link
Author

tufcode commented May 8, 2023

Also happens with ArraySchema

@emre-sahinn
Copy link

Yeah happens to me too, we need an urgent fix :(
This bug breaks the whole game

@endel
Copy link
Member

endel commented May 8, 2023

Thanks for reporting, and sorry for the trouble!

A workaround, for now, is to provide false as a second argument to the inner onAdd, although this doesn't look nice. I will add tests for this and a proper fix soon!

This is due to onAdd now immediately triggering for existing items (in case of registering a callback late)

Workaround:

room.state.restaurants.onAdd((restaurant, restaurantKey) => {
   console.log("Restaurant added");
   restaurant.objects.onAdd(async (gameObject, objectKey) => {
      console.log("Oh hi there!")
   }, false) // here
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants