assertTrue is the professional blog of Luke Bayes and Ali Mills

Where's haXe?

Posted by Ali Mills Wed, 02 Aug 2006 07:30:00 GMT

The code below may look like ActionScript, but it’s not. It’s haXe. Can you spot the differences?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package mediator.events;

import mediator.events.Event;
import mediator.events.EventListener;

class EventDispatcher {
    private var listeners:Array<EventListener>;
    private var eventTarget:Dynamic;

    public function new(?eventTarget:Dynamic) {
        listeners = new Array();
        if(eventTarget) this.eventTarget = eventTarget else this.eventTarget = this; 
    }

    public function addEventListener(type:EventType, handler:Dynamic, context:Dynamic):Void {
        removeEventListener(type, handler, context);
        listeners.push(new EventListener(type, handler, context));
    }

    public function removeEventListener(type:EventType, handler:Dynamic, context:Dynamic):EventListener {
        var listener:EventListener = new EventListener(type, handler, context);
        for(i in 0...listeners.length) {
            if(listeners[i].equals(listener)) {
                return listeners.splice(i, 1)[0];
            }
        }
        return null;
    }

    public function dispatchEvent(event:Event):Bool {
        var type:EventType = event.type;
        var listener:EventListener;
        untyped {event["target"] = eventTarget;}
        for(i in 0...listeners.length) {
            listener = listeners[i];
            if(listener.type == type) {
                Reflect.callMethod(listener.context, listener.handler, [event]);
            }
        }
        return true;
    }
}

Tags  | 2 comments

Comments

  1. iongion replied: Avatar Dont compare it to AS, compare it with ... : (?!?) :) Array -> Looks C's Template-ishness public function new(?eventTarget:Dynamic) -> Typo ? ... really looks like a typo i in 0...listeners.length -> Pascal-ish ? untyped {event["target"] = eventTarget;} -> tellTarget-ish :P ? Anyway, Nicolas really likes programming languages i guess :D !
    Posted: 45 minutes later.
  2. iongion commented: Avatar

    Dont compare it to AS, compare it with ... : (?!?) :)

    • Array<EventListener> -> Looks C's Template-ishness
    • public function new(?eventTarget:Dynamic) -> Typo ? ... really looks like a typo
    • i in 0...listeners.length -> Pascal-ish ?
    • untyped {event["target"] = eventTarget;} -> tellTarget-ish :P ?

    Anyway, Nicolas really likes programming languages i guess :D !

    Posted: about 1 hour later.

Your Reply

Comment Form.

Fields denoted with an "*" are required.