Skip to content

Instantly share code, notes, and snippets.

@back2dos
Last active July 9, 2020 03:20
Typesafe version of `flash.events.EventDispatcher` to make the Flash API suck a little less. Would also work for `js.html.EventTarget`.
package flash.events;
extern class EventDispatcher implements IEventDispatcher {
function new(?target:IEventDispatcher) : Void;
function addEventListener<T:Event>(type:EventType<T>, listener: T->Void, useCapture:Bool = false, priority:Int = 0, useWeakReference:Bool = false):Void;
function dispatchEvent(event:Event):Bool;
function hasEventListener(type:String):Bool;
function removeEventListener<T:Event>(type:EventType<T>, listener: T->Void, useCapture:Bool = false):Void;
function toString():String;
function willTrigger(type:String):Bool;
}
package flash.events;
abstract EventType<T:Event>(String) from String to String {}
package flash.events;
extern interface IEventDispatcher {
function addEventListener<T:Event>(type:EventType<T>, listener: T->Void, useCapture:Bool = false, priority:Int = 0, useWeakReference:Bool = false):Void;
function dispatchEvent(event:Event):Bool;
function hasEventListener(type:String):Bool;
function removeEventListener<T:Event>(type:EventType<T>, listener: T->Void, useCapture:Bool = false):Void;
function toString():String;
function willTrigger(type:String):Bool;
}
package flash.event.*;
extern class ProgressEvent extends Event {
var bytesLoaded:Float;
var bytesTotal:Float;
function new(type:EventType<ProgressEvent>, bubbles:Bool = false, cancelable:Bool = false, bytesLoaded:Float = 0, bytesTotal:Float = 0) : Void;
static var PROGRESS(default, null):EventType<ProgressEvent>;
static var SOCKET_DATA(default, null):EventType<ProgressEvent>;
}
package ;
import flash.events.*;
class Usage {
static function main() {
var source = new EventDispatcher();
source.addEventListener(ProgressEvent.PROGRESS, function (e) $type(e));//yields ProgressEvent
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment