PDA

View Full Version : Enforcing Event Types


ASWC
10-12-2010, 05:36 PM
The AS3 event system is not enforced for example this works just fine:
var sprite:Sprite = new Sprite();
sprite.addEventListener("An event I just made up", handleFakeEvent);
function handleFakeEvent(e:Event):void
{

}
I can register an event that doesn't exist and the compiler will never complain. For the same reason if you register Event.COMPLETE with a Loader object you won't get any error but you'll also never get your handler to fire. This type of errors are more common than we might think and in a complex system they might be really hard to spot since you will never get an error showing at compile time or runtime. Enforcing Event types is possible at least for Custom class or frameworks but I never seen it done. All my custom classes are now using a EventTypeEnforcer class so only the Event types my classes are meant to deal with can be registered.
What about you? Do you deal with that or just ignore it and think it's useless?

wvxvw
10-12-2010, 09:12 PM
That's why .NET uses enums for this purpose... You could do that in AS3 too, but you'd have to extend all base classes / have some wrapper for them to change addEventListener() to something like addEventHandler(event:Event, ...). Other than that, you could have extended EventDispatcher and make a method that will check the metadata every once the addEventListener is called, and if the even type isn't found in the metadata, then throw an error...

senocular
10-12-2010, 09:42 PM
Though AS3 doesn't support enums, static classes/class variables are used for this. Event.COMPLETE, MouseEvent.CLICK, etc. all help identify event errors when used since they only contain events supported by those event type, and any spelling error will be identified with a compiler error.

ASWC
10-12-2010, 10:05 PM
this is my take on this:
override public function addEventListener(type:String, listener:Function,
useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
{
enforcer.validType(type);
super.addEventListener(type, listener, useCapture, priority, useWeakReference);
}
enforcer is an instance of my EventTypeEnforcer class with which I register all event type I want my class to accept. If the type is not in the list this code throw an error. Pretty simple and efficient. Why would I need something more complicated?

bowljoman
10-13-2010, 10:56 PM
The AS3 event system is not enforced for example this works just fine:
I can register an event that doesn't exist and the compiler will never complain.


Because EventDispatcher is not a protected or private interface, the object can not know all event types it will encounter. Neither does the compiler check if the swf file will load an external swf at some point in the future that contains a new event definition.

I'd say you should use constants rather than 'some_type' if you have trouble with strings that point to the wrong event type constant;


For the same reason if you register Event.COMPLETE with a Loader object you won't get any error but you'll also never get your handler to fire.
What about you? Do you deal with that or just ignore it and think it's useless?

Youre mistake is adding the listener to the Loader , rather than the contentLoaderInfo object, which does always under all circumstances, when it occurs, dispatch the 'Event.Complete' event.

I dont use 'eventType' as a string, I always use ClassName.Constant_name, if not just to reduce the string table.

Name an event that doesn't fire as documented?

If you look, I do not believe that Loader is documented to dispatch Event.Complete.

ASWC
10-14-2010, 01:36 AM
bowljoman, I do not have any troubles at all with the event system. This is just a discussion as valid as any other discussion about the pros and cons of that pattern or this pattern or this way of doing things.

bowljoman
10-14-2010, 04:52 AM
Well, you complained that the loader never fired a complete event and that the event system is it buggy, when the truth is that the loader class is not expected to fire the complete event.

I think it is great how it is because you are allowed to create event definition at run time.

I would also advise to register the events you are concerned about in the meta data of the class in cases where you feel you need to keep your self on the grass. Then you get your events listed in the docs and auto-complete.

If you ask me it is bad practice to program

dispatchEvent('An event I just made up');

...unless you intend to create def's on the fly.

If your class only dispatches one or two types of events, put them in the meta-data.


[Event (etc.....)]


Maybe the event system would seem less buggy if you read the metadata/events listed for the framework classes such as the Loader.

ASWC
10-14-2010, 05:04 AM
Sorry but I don't think you understood what I was talking about here. I was talking about enforcing event type. I actually never said that the event system was buggy or that I had any problem working with it. please let's once and for all be clear.

There are no problem with the AS3 event system. I don't find it buggy. I don't have any problem with it.

This post is only about enforcing event type -> useful vs not useful

I personally find it useful I apply it in my code. Once again this has nothing to do with having problem with the event system or finding it buggy which I don't. :confused:

bowljoman
10-14-2010, 06:11 AM
Well, your not enforcing anything either, you are just dropping it.


Event.COMPLETE


As was pointed out above, constants are what you should be using, and since you are concerned about forgetting what events your own classes dispatch, you should use that class as the one with the const value.

Event type as string give you flexibility that you would not have otherwise.

You can simply ..

dispatchEvent(new Event('An event I just made up'));


... so my opinion of event enforcement is that it is

Event.Complete.Useless

bowljoman
10-14-2010, 06:20 AM
Look,

Instead of throwing an error and raining on someones parade at runtime, why do you not document your events with metadata as suggested by the API so the IDE provides the list of events while programming?