Jump to content

Distinguish between bjs & canvas2d classes and other classes


royibernthal
 Share

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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)) {}

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

@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.

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...