(v4) Move TresObject.__tres
to a WeakMap
?
#552
Replies: 3 comments 2 replies
-
Hi, @andretchen0, for v4 we merged #516 which refactored the use of But I'm highly interested in the use of |
Beta Was this translation helpful? Give feedback.
-
Sure thing. I think I'm just about ready to show a POC for |
Beta Was this translation helpful? Give feedback.
-
Hi @andretchen0 I have been reading more about the Should we create an issue for this aimed at |
Beta Was this translation helpful? Give feedback.
-
Currently, Tres stores data directly on the THREE objects it creates, in
Object.userData.__tres
.This approach means it's available to users and can be overwritten accidentally if a user does something like
Object.userData = myData
.It also means that all objects that Tres manages need to have and maintain
userData
over their lifetimes. But not all THREE objects haveuserData
, e.g.,BufferAttribute
.Proposal
WeakMap
for associatingTHREE.Object3D
, etc., with Tres data.WeakMap
intoroot
or elsewhere.A goal in the design of
WeakMap
s is to do exactly the sort of thing we're doing with Tres, associating dynamic state with an object:And
WeakMap
would allow us to more easily extend Tres to handle objects that don't have.userData
. We could associate any object with Tres data, simply by using the object as a key in theWeakMap
.A
WeakMap
key/value is garbage collected when there are no longer references to the key. That means that, like the current setup, we won't have to manage deleting values ourselves – edit: like currently, they'll be garbage collected when the key is freed, assuming no other references exist. (Note that this won't fix memory leaks. But it should not add new leaks or new needs for avoiding leaks.)Beta Was this translation helpful? Give feedback.
All reactions