royibernthal Posted March 5, 2017 Share Posted March 5, 2017 Given a class instance, is there a way to know in runtime if it belongs to bjs or canvas2d namespaces via ts/js? For instance, if I have an instance of BABYLON.AbstractMesh, the check will return true because it belongs to the BABYLON namespace. Is there perhaps some property/function that exists in all bjs classes? Quote Link to comment Share on other sites More sharing options...
fenomas Posted March 5, 2017 Share Posted March 5, 2017 I don't think there's a robust way to do this. You can check for specific cases, like: myMesh instanceof BABYLON.Node ..because meshes all inherit from Node, but there's no single root that every BJS class inherits from. Alternately, you could check for property names like foo._scene or foo.__serializableMembers because many Babylon objects have them, but there are many others that don't. If those aren't enough, this seems like a bad code smell - there's probably a better solution than introspection. Quote Link to comment Share on other sites More sharing options...
royibernthal Posted March 6, 2017 Author Share Posted March 6, 2017 I have a dispose function which recursively iterates over objects and calls their dispose function (if exists). The problem is, doing that on some bjs objects (e.g. meshes) results in an infinite recursion, on bjs objects I'd just like to call their dispose function without iterating over them recursively, the recourse is meant for my project's objects. I could do some ugly workarounds like adding to each of my project's objects some shared property in order to be able to identify it at runtime, but I'd like to avoid that. static dispose(value: any): void { if (value == null) return; //TODO: don't iterate over objects that belong to bjs as it'll result in an infinite recursion if (typeof value === 'object') { for (var key in value) { dispose( value[key] ); } } else if (value.dispose != null) value.dispose(); } Does that code smell bad? Quote Link to comment Share on other sites More sharing options...
AlbertTJames Posted March 6, 2017 Share Posted March 6, 2017 Hi! What would be the use case of this function ? Two ways to check the class of which the object is an instance of : if (object.constructor === BABYLON.Canvas2D) {} if (object instanceof BABYLON.Canvas2D) {} To check if there is a dispose function: if ((object.hasOwnProperty("dispose"))&&(object.dispose.constructor === Function)) {} Quote Link to comment Share on other sites More sharing options...
fenomas Posted March 6, 2017 Share Posted March 6, 2017 8 hours ago, royibernthal said: Does that code smell bad? I'm just guessing out of context, but my guess would be yes I mean, in the narrow sense: it only makes sense to recursively walk through a data structure if that data is a tree of some kind. If I understand you right, you have one set of objects where that's the case, which are being managed together with some BJS objects? However since BJS objects don't live in any kind of tree structure, probably they need to be managed separately from other stuff that is. But more generally, a given object's dispose function should internally dispose any other assets that that object is responsible for, right? I think (but haven't confirmed in any detail) that most Babylon objects behave this way - so calling mesh.dispose() should already dispose any assets that can be safely disposed with that mesh. So that's what I meant about recursively walking through a data structure disposing everything sounding like an iffy thing to do. Not meaning this as criticism of course - naturally you could have good reasons for doing it that I don't know about. Quote Link to comment Share on other sites More sharing options...
JCPalmer Posted March 6, 2017 Share Posted March 6, 2017 I have not used Canvas 2D, but you might get rewarded by checking if layermask can be found in a source text search. If Canvas 2D is implemented with its own camera's, then layermask are highly likely how canvas 2d identifies which meshes are its. Quote Link to comment Share on other sites More sharing options...
royibernthal Posted March 7, 2017 Author Share Posted March 7, 2017 @fenomas Something felt weird and I wasn't able to put my finger on it. This was some old undocumented code which I decided to "upgrade" from iterating over arrays to iterating over objects as well. The original intention was to allow passing multiple objects to this functions in an array, manually and intentionally, and not to recursively iterate over objects. Needless to say, I documented it so I won't forget it again static dispose(value: any | any[]): void { if (value == null) return; if (value instanceof Array) value.forEach(item => MemoryUtils.dispose(item)); else if (value.dispose != null) value.dispose(); } Thanks for the help everybody, your answers are helpful if I were to stick with the approach mentioned in the topic. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.