Discordia icon indicating copy to clipboard operation
Discordia copied to clipboard

Static object data

Open SinisterRectus opened this issue 4 years ago • 1 comments

The current behavior of Discordia is to recycle and update container objects in place.

In an asynchronous event handler, this means that the object you receive may change over the course of the event callback's execution:

client:on("roleCreate", function(role)

  print(role.name) -- A
  timer.sleep(ms) -- role name can change during this sleep
  print(role.name) -- B

end)

To preserve the data, you would need to locally cache a value:

client:on("roleCreate", function(role)

  local name = role.name
  print(name) -- A
  timer.sleep(ms) -- obviously name cannot change here
  print(name) -- A

end)

With the introduction of intents and lack of guaranteed consistency (see #234), I'm considering changing Discordia behavior to only use static objects. In this example, the role object would never update inside of the event handler. If the current state is needed, you would fetch the new role:

client:on("roleCreate", function(original)

  print(original.name) -- A
  timer.sleep(ms) -- role updates but the current object does not
  print(original.name) -- A

  local updated = original.guild:getRole(original.id)
  print(updated.name) -- B

end)

One thing that might make this more complicated is how to handle HTTP updates performed by the current user. Does it make sense to apply the update in that case?

client:on("roleCreate", function(original)

  print(original.name) -- A
  original:setName("B")
  print(original.name) -- A or B?

end)

SinisterRectus avatar May 10 '20 13:05 SinisterRectus

I'm thinking of making this behavior a client option for toggling between static and dynamic container objects.

SinisterRectus avatar Sep 05 '20 14:09 SinisterRectus