3) Events and chaos

The event DOM-based model, introduced with ActionScript 3.0, was a fantastic improvement for building complex applications. (Maybe some of you guys remember the good old Delegate class.) But if we pay attention to the memory leaks issues in ActionScript, arguably the persistence of object references through events is responsible in most of the cases. Because the event model is very flexible and easy-to-use, event listeners are often disseminated all over the code, without any rules or well defined structures. The fact is that if you implement a large number of unorganized lines of code within an application, you introduce "chaos in the computer"!

Now, just imagine a complete on line music editor, with tens of modules called, created, and displayed on the fly. Each module is composed of many different classes which are responsible for synchronous or asynchronous treatments (loading and saving data, playing back music, checking for music theory rules, etc.). Such an application cannot be developed if you do not take care of memory usage. Unfortunately, there is no way to easily fix event management issues. Each event listener must be removed before deleting unused modules and this could become really fastidious when you have to check hundreds of classes.

4) The EventCollector class

As we commonly say: "The simplest ideas work the best."
And the event collection concept is probably one of the most simple reusable piece of code ever written in ActionScript.

We know, that (fortunately) there are no mechanisms to automatically remove event listener references when setting an object to null. But if we consider the process of deleting an object, we would never have to know the number or type of event listeners associated to it and what they are doing. Our only concern should be to ensure their destruction.
That is precisely what the EventCollector class does! So, it has been designed with two main goals:

  • provide a convenient API to gain more time and write well-structured code,
  • ensure to delete event references in all cases.

Moreover, the EventCollector class  has been the SPAS 3.0 core event manager since 2006, which means that its robustness, efficiency and flexibility have been well-tested for many years.