71 lines
No EOL
1.7 KiB
Text
71 lines
No EOL
1.7 KiB
Text
import "math/util" for Util
|
|
|
|
//my own event class because Events does too much with its tags and too little with disposing
|
|
class Event{
|
|
construct new(){
|
|
_listeners = {}
|
|
_listenersOnce = {}
|
|
_index = -1
|
|
}
|
|
|
|
listen(fn){
|
|
_index = _index + 1
|
|
_listeners[_index] = fn
|
|
return ListenerToken.new(this, _index)
|
|
}
|
|
|
|
listenOnce(fn){
|
|
_index = _index + 1
|
|
_listenersOnce[_index] = fn
|
|
return ListenerToken.new(this, _index)
|
|
}
|
|
|
|
unlisten(id){
|
|
if(id is Num) {
|
|
var removed = false
|
|
removed = removed || _listeners.remove(id)
|
|
removed = removed || _listenersOnce.remove(id)
|
|
if(!removed){
|
|
System.print("[warn] tried to remove unknown listener id")
|
|
}
|
|
} else if(id is Fn){
|
|
var removed = false
|
|
System.print(_listeners.keys)
|
|
for(index in _listeners.keys){
|
|
if(_listeners[index] == id){
|
|
//! this might lead to problems because I'm modifying the thing I'm iterating over
|
|
//in that case we need to defer the removal
|
|
_listeners.remove(index)
|
|
removed = true
|
|
}
|
|
}
|
|
if(!removed){
|
|
System.print("[warn] tried to remove unknown listener fn")
|
|
}
|
|
} else if(id is ListenerToken) {
|
|
id.discard()
|
|
} else {
|
|
System.print("[warn] how is a %(id.type.name) related to the listener? pass the listener index or function.")
|
|
}
|
|
}
|
|
|
|
emit(arguments){
|
|
_listeners.each{|val|
|
|
Util.call_arg_list(val, arguments)
|
|
}
|
|
}
|
|
}
|
|
|
|
class ListenerToken{
|
|
event{_event}
|
|
index{_index}
|
|
|
|
construct new(event, index){
|
|
_event = event
|
|
_index = index
|
|
}
|
|
|
|
discard(){
|
|
event.unlisten(index)
|
|
}
|
|
} |