EventManager Class Update

Hate keeping track of event listeners?

Wish you had removeAllListeners()?  Wish you had removeAllListeners(EventDispatcherWithABunchOfListeners)? Wish you could remove all listeners that targetted a specific function?

A while ago I posted a class I wrote to handle actionscript 3 listeners.

I updated EventManager.as today to by default set weakReference to true and to always add the listener (if you used it before it might break code that didn’t declare the last parameter).

The class has come in handy in so many projects and makes code much nicer. It’s especially useful for destroying all the listeners of an external SWF…

Find the latest version here.

Note that the class doesn’t have full support for listeners with useCapture specified… but who really uses those anyway?

AS3 Primitives

*Edit: I have found that I am wrong about the following (as shown from the comments). In AS3, numbers are in fact placed in their wrapper classes. My interpretation are wrong. You can keep read the following if you want to know how it DOESN’T work. Basically what I describe is how Java works. AVM2 doesn’t do this, it wraps everything as an object… Sort of strange as there are performence gains using primitives.

I read a post recently that asked whether AS3 Number objects are actually treated as objects in AS3.

Number, int, etc are primitives. Just like in Java, these types have their Object class associations in case you want to create instances and call various methods that come with the Number class. For example, the Number class has functions like toFixed for number precison and accuracy.

The concept of primitives are sort of confusing to newer programming as they go against object oriented programming. In fact, in Smalltalk, one of the first OOP languages, EVERYTHING is an object. The +, -, *, etc are treated as “messages”.

In Smalltalk, since everything is an object and has messages passed, you have to specify the function for “+” and “-“. Primitives in higher level languages don’t have that just for this reason. You don’t want to have to write the code to add two numbers together do you? The concatanation syntax for Strings is also “+”. Notice how “-” is not properly handled with Strings while it is with Numbers… 

Also, in terms of an ideological argument, Number can be said to be the lowest form to describe an object. Think of the composition property of object oriented design. Everything is made of objects that are made of objects that are made of objects… There are theoretically an infinite amount of layers in-between.  Object oriented design is the practice of selecting certain layers and abstracting the rest. But at the very core, every object has to be made up of primatives (more specifically, since Strings can be represented as numbers). Think of any class you’ve ever programmed. The properties will always have either primatives or other objects. If you recurisvely loop through other objects they will all also be made up of primatives (unless they have no properties at all… that’s sort of an anomoly and bad programming design dependant on “isA” relationships… so I’m going to avoid that tangent).

Primitive types are the building blocks. Just like materials are made from atoms… you can’t really go lower than Numbers in describing an object. Additionally, primitives are passed by VALUE rather by reference.

Here’s another post I wrote a while back that went a little deeper into this concept.

Google Chrome Doesn’t Always Seperate Each Tab to Different Threads

I’m using Google Chrome right now, and I have to say that it’s blazing fast. Based on Javascript benchmarks I’ve ran from around the web, I found it to be way faster than IE7, way faster than Firefox 2, much faster than Firefox 3, and even faster than Opera. Chrome’s new Javascript engine is awesome, but I just wanted to clarify that tabs do not always run as a seperate process.

Google mentions on their in one of their Chrome FAQs that it’s up to the web developer to decide if a new link opens as a seperate process or not.

I found this out first hand by going to W3 School’s html samples page and clicking on one of the links. Instead of opening on the same tab, the link opens on a new tab. So now I had http://www.w3schools.com/HTML/tryit.asp?filename=tryhtml_basic open in a tab. I edited the html source code on the left frame and put in a simple infinite loop with Javascript then hit run. As expected, the tab, instead of the entire browser, hung.

However, when I clicked on the W3 School’s html sample tab (which launched the now frozen tab), I noticed it was also frozen. My other tabs, such as Gmail, Google Calendar, etc were fine.

Google answered why: 

New tabs spawned from a web page, however, are usually opened in the same process, so that the original page can access the new tab using JavaScript.

Makes sense. In fact, that is one of the reasons why current web browsers run in a single thread. No big deal. Most of the time I have multiple “sessions” of browsing and I don’t mind if a few tabs share the same process thread.

Anyway, if you don’t have Google Chrome, get it. Finally multi-core machines will see benefits in browsing the web. I can have 10 tabs open without seeing a slowdown.